IIOMetadataNode.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /* IIOMetadataNode.java --
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.imageio.metadata;
  32. import java.util.ArrayList;
  33. import java.util.HashMap;
  34. import java.util.Iterator;
  35. import java.util.List;
  36. import org.w3c.dom.Attr;
  37. import org.w3c.dom.DOMException;
  38. import org.w3c.dom.Document;
  39. import org.w3c.dom.Element;
  40. import org.w3c.dom.NamedNodeMap;
  41. import org.w3c.dom.Node;
  42. import org.w3c.dom.NodeList;
  43. import org.w3c.dom.TypeInfo;
  44. import org.w3c.dom.UserDataHandler;
  45. import javax.imageio.metadata.IIOMetadataFormatImpl.IIOMetadataNodeAttr;
  46. public class IIOMetadataNode
  47. implements Element, NodeList
  48. {
  49. private String name;
  50. private HashMap attrs = new HashMap();
  51. private List children = new ArrayList();
  52. private IIOMetadataNode parent;
  53. private Object obj;
  54. /**
  55. * Simple NamedNodeMap class for IIOMetadataNode.
  56. *
  57. * @author jlquinn
  58. */
  59. private class IIONamedNodeMap implements NamedNodeMap
  60. {
  61. HashMap attrs;
  62. /**
  63. * @param attrs
  64. * @param node
  65. */
  66. public IIONamedNodeMap(HashMap attrs)
  67. {
  68. this.attrs = attrs;
  69. }
  70. /* (non-Javadoc)
  71. * @see org.w3c.dom.NamedNodeMap#getNamedItem(java.lang.String)
  72. */
  73. public Node getNamedItem(String name)
  74. {
  75. return (Node)attrs.get(name);
  76. }
  77. /* (non-Javadoc)
  78. * @see org.w3c.dom.NamedNodeMap#setNamedItem(org.w3c.dom.Node)
  79. */
  80. public Node setNamedItem(Node arg) throws DOMException
  81. {
  82. if (arg instanceof IIOMetadataNodeAttr)
  83. {
  84. IIOMetadataNodeAttr attr = (IIOMetadataNodeAttr) arg;
  85. // The only code that can successfully do this is in this package.
  86. if (attr.owner != null)
  87. throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR, "");
  88. return (Node)attrs.put(attr.name, attr);
  89. }
  90. // Anything else gets treated as an invalid op.
  91. throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "");
  92. }
  93. /* (non-Javadoc)
  94. * @see org.w3c.dom.NamedNodeMap#removeNamedItem(java.lang.String)
  95. */
  96. public Node removeNamedItem(String name) throws DOMException
  97. {
  98. return (Node)attrs.remove(name);
  99. }
  100. /* (non-Javadoc)
  101. * @see org.w3c.dom.NamedNodeMap#item(int)
  102. */
  103. public Node item(int index)
  104. {
  105. return (Node)attrs.values().toArray()[index];
  106. }
  107. /* (non-Javadoc)
  108. * @see org.w3c.dom.NamedNodeMap#getLength()
  109. */
  110. public int getLength()
  111. {
  112. return attrs.size();
  113. }
  114. /* (non-Javadoc)
  115. * @see org.w3c.dom.NamedNodeMap#getNamedItemNS(java.lang.String, java.lang.String)
  116. */
  117. public Node getNamedItemNS(String namespaceURI, String localName)
  118. {
  119. return getNamedItem(localName);
  120. }
  121. /* (non-Javadoc)
  122. * @see org.w3c.dom.NamedNodeMap#setNamedItemNS(org.w3c.dom.Node)
  123. */
  124. public Node setNamedItemNS(Node arg) throws DOMException
  125. {
  126. return setNamedItem(arg);
  127. }
  128. /* (non-Javadoc)
  129. * @see org.w3c.dom.NamedNodeMap#removeNamedItemNS(java.lang.String, java.lang.String)
  130. */
  131. public Node removeNamedItemNS(String namespaceURI, String localName)
  132. throws DOMException
  133. {
  134. return removeNamedItem(localName);
  135. }
  136. }
  137. /**
  138. * Simple NodeList implementation for IIOMetadataNode.
  139. *
  140. * @author jlquinn
  141. *
  142. */
  143. private class IIONodeList implements NodeList
  144. {
  145. List children = new ArrayList();
  146. /* (non-Javadoc)
  147. * @see org.w3c.dom.NodeList#item(int)
  148. */
  149. public Node item(int index)
  150. {
  151. return (index < children.size()) ? (Node)children.get(index) : null;
  152. }
  153. /* (non-Javadoc)
  154. * @see org.w3c.dom.NodeList#getLength()
  155. */
  156. public int getLength()
  157. {
  158. return children.size();
  159. }
  160. }
  161. public IIOMetadataNode()
  162. {
  163. // Do nothing here.
  164. }
  165. public IIOMetadataNode(String nodename)
  166. {
  167. name = nodename;
  168. }
  169. public Object getUserObject()
  170. {
  171. return obj;
  172. }
  173. public void setUserObject(Object o)
  174. {
  175. obj = o;
  176. }
  177. public short compareDocumentPosition(Node other)
  178. throws DOMException
  179. {
  180. return Element.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC;
  181. }
  182. /* (non-Javadoc)
  183. * @see org.w3c.dom.Element#getAttribute(java.lang.String)
  184. */
  185. public String getAttribute(String name)
  186. {
  187. Attr anode = (Attr) attrs.get(name);
  188. return anode != null ? anode.getValue() : null;
  189. }
  190. /* (non-Javadoc)
  191. * @see org.w3c.dom.Element#getAttributeNode(java.lang.String)
  192. */
  193. public Attr getAttributeNode(String name)
  194. {
  195. String val = getAttribute(name);
  196. if (val != null)
  197. return new IIOMetadataNodeAttr(this, name, val);
  198. return null;
  199. }
  200. /* (non-Javadoc)
  201. * @see org.w3c.dom.Element#getAttributeNodeNS(java.lang.String, java.lang.String)
  202. */
  203. public Attr getAttributeNodeNS(String namespaceURI, String localName)
  204. {
  205. return getAttributeNode(localName);
  206. }
  207. /* (non-Javadoc)
  208. * @see org.w3c.dom.Element#getAttributeNS(java.lang.String, java.lang.String)
  209. */
  210. public String getAttributeNS(String namespaceURI, String localName)
  211. {
  212. return getAttribute(localName);
  213. }
  214. public String getBaseURI()
  215. {
  216. return null;
  217. }
  218. // Recursive function for assembling a node list.
  219. private void getElementsRecurse(IIONodeList list, String name)
  220. {
  221. for (int i=0; i < children.size(); i++)
  222. {
  223. if (((Node)children.get(i)).getNodeName().equals(name))
  224. list.children.add(children.get(i));
  225. getElementsRecurse(list, name);
  226. }
  227. }
  228. /* (non-Javadoc)
  229. * @see org.w3c.dom.Element#getElementsByTagName(java.lang.String)
  230. */
  231. public NodeList getElementsByTagName(String name)
  232. {
  233. IIONodeList list = new IIONodeList();
  234. getElementsRecurse(list, name);
  235. return list;
  236. }
  237. /* (non-Javadoc)
  238. * @see org.w3c.dom.Element#getElementsByTagNameNS(java.lang.String, java.lang.String)
  239. */
  240. public NodeList getElementsByTagNameNS(String namespaceURI, String localName)
  241. {
  242. IIONodeList list = new IIONodeList();
  243. getElementsRecurse(list, name);
  244. return list;
  245. }
  246. /* (non-Javadoc)
  247. * @see org.w3c.dom.Element#getTagName()
  248. */
  249. public String getTagName()
  250. {
  251. return name;
  252. }
  253. /* (non-Javadoc)
  254. * @see org.w3c.dom.Element#hasAttribute(java.lang.String)
  255. */
  256. public boolean hasAttribute(String name)
  257. {
  258. return attrs.containsKey(name);
  259. }
  260. /* (non-Javadoc)
  261. * @see org.w3c.dom.Element#hasAttributeNS(java.lang.String, java.lang.String)
  262. */
  263. public boolean hasAttributeNS(String namespaceURI, String localName)
  264. {
  265. return attrs.containsKey(localName);
  266. }
  267. /* (non-Javadoc)
  268. * @see org.w3c.dom.Element#removeAttribute(java.lang.String)
  269. */
  270. public void removeAttribute(String name)
  271. {
  272. attrs.remove(name);
  273. }
  274. /* (non-Javadoc)
  275. * @see org.w3c.dom.Element#removeAttributeNode(org.w3c.dom.Attr)
  276. */
  277. public Attr removeAttributeNode(Attr oldAttr)
  278. {
  279. return (Attr)attrs.remove(oldAttr.getName());
  280. }
  281. /* (non-Javadoc)
  282. * @see org.w3c.dom.Element#removeAttributeNS(java.lang.String, java.lang.String)
  283. */
  284. public void removeAttributeNS(String namespaceURI, String localName)
  285. {
  286. removeAttribute(localName);
  287. }
  288. /* (non-Javadoc)
  289. * @see org.w3c.dom.Element#setAttribute(java.lang.String, java.lang.String)
  290. */
  291. public void setAttribute(String name, String value)
  292. {
  293. Attr attr = getAttributeNode(name);
  294. if (attr != null)
  295. attr.setValue(value);
  296. else
  297. attrs.put(name, new IIOMetadataNodeAttr(this, name, value));
  298. }
  299. /* (non-Javadoc)
  300. * @see org.w3c.dom.Element#setAttributeNode(org.w3c.dom.Attr)
  301. */
  302. public Attr setAttributeNode(Attr newAttr)
  303. {
  304. return (Attr)attrs.put(newAttr.getName(), newAttr);
  305. }
  306. /* (non-Javadoc)
  307. * @see org.w3c.dom.Element#setAttributeNodeNS(org.w3c.dom.Attr)
  308. */
  309. public Attr setAttributeNodeNS(Attr newAttr)
  310. {
  311. return (Attr)attrs.put(newAttr.getName(), newAttr);
  312. }
  313. /* (non-Javadoc)
  314. * @see org.w3c.dom.Element#setAttributeNS(java.lang.String, java.lang.String, java.lang.String)
  315. */
  316. public void setAttributeNS(String namespaceURI, String qualifiedName, String value)
  317. {
  318. setAttribute(qualifiedName, value);
  319. }
  320. /* (non-Javadoc)
  321. * @see org.w3c.dom.NodeList#getLength()
  322. */
  323. public int getLength()
  324. {
  325. return children.size();
  326. }
  327. /* (non-Javadoc)
  328. * @see org.w3c.dom.NodeList#item(int)
  329. */
  330. public Node item(int index)
  331. {
  332. if (index < children.size())
  333. return (Node)children.get(index);
  334. else
  335. return null;
  336. }
  337. /* (non-Javadoc)
  338. * @see org.w3c.dom.Node#appendChild(org.w3c.dom.Node)
  339. */
  340. public Node appendChild(Node newChild)
  341. {
  342. if (newChild == null)
  343. throw new IllegalArgumentException("Child node is null");
  344. IIOMetadataNode child = (IIOMetadataNode) newChild;
  345. children.add(child);
  346. child.parent = this;
  347. return this;
  348. }
  349. /* (non-Javadoc)
  350. * @see org.w3c.dom.Node#cloneNode(boolean)
  351. */
  352. public Node cloneNode(boolean deep)
  353. {
  354. IIOMetadataNode newnode = new IIOMetadataNode(name);
  355. newnode.parent = null;
  356. newnode.obj = obj;
  357. if (deep)
  358. {
  359. for (int i=0; i < children.size(); i++)
  360. newnode.children.add(((Node)children.get(i)).cloneNode(deep));
  361. }
  362. // clone attrs
  363. for (Iterator it = attrs.values().iterator(); it.hasNext();)
  364. {
  365. IIOMetadataNodeAttr attr = (IIOMetadataNodeAttr)it.next();
  366. newnode.attrs.put(attr.name, attr.cloneNode(deep));
  367. attr.owner = newnode;
  368. }
  369. return newnode;
  370. }
  371. /* (non-Javadoc)
  372. * @see org.w3c.dom.Node#getAttributes()
  373. */
  374. public NamedNodeMap getAttributes()
  375. {
  376. return new IIONamedNodeMap(attrs);
  377. }
  378. /* (non-Javadoc)
  379. * @see org.w3c.dom.Node#getChildNodes()
  380. */
  381. public NodeList getChildNodes()
  382. {
  383. return this;
  384. }
  385. public Object getFeature(String feature, String version)
  386. {
  387. return null;
  388. }
  389. /* (non-Javadoc)
  390. * @see org.w3c.dom.Node#getFirstChild()
  391. */
  392. public Node getFirstChild()
  393. {
  394. return (children.size() > 0) ? (Node)children.get(0) : null;
  395. }
  396. /* (non-Javadoc)
  397. * @see org.w3c.dom.Node#getLastChild()
  398. */
  399. public Node getLastChild()
  400. {
  401. return (children.size() > 0) ? (Node)children.get(children.size() - 1)
  402. : null;
  403. }
  404. /* (non-Javadoc)
  405. * @see org.w3c.dom.Node#getLocalName()
  406. */
  407. public String getLocalName()
  408. {
  409. return name;
  410. }
  411. /* (non-Javadoc)
  412. * @see org.w3c.dom.Node#getNamespaceURI()
  413. */
  414. public String getNamespaceURI()
  415. {
  416. return null;
  417. }
  418. /* (non-Javadoc)
  419. * @see org.w3c.dom.Node#getNextSibling()
  420. */
  421. public Node getNextSibling()
  422. {
  423. // If this op needs to be faster, add links to prev and next nodes.
  424. if (parent == null) return null;
  425. int idx = parent.children.indexOf(this);
  426. return (idx == parent.children.size() - 1) ? null
  427. : (Node)parent.children.get(idx + 1);
  428. }
  429. /* (non-Javadoc)
  430. * @see org.w3c.dom.Node#getNodeName()
  431. */
  432. public String getNodeName()
  433. {
  434. return name;
  435. }
  436. /* (non-Javadoc)
  437. * @see org.w3c.dom.Node#getNodeType()
  438. */
  439. public short getNodeType()
  440. {
  441. return ELEMENT_NODE;
  442. }
  443. /* (non-Javadoc)
  444. * @see org.w3c.dom.Node#getNodeValue()
  445. */
  446. public String getNodeValue()
  447. {
  448. return null;
  449. }
  450. /* (non-Javadoc)
  451. * @see org.w3c.dom.Node#getOwnerDocument()
  452. */
  453. public Document getOwnerDocument()
  454. {
  455. // IOMetadataNodes have no owner
  456. return null;
  457. }
  458. /* (non-Javadoc)
  459. * @see org.w3c.dom.Node#getParentNode()
  460. */
  461. public Node getParentNode()
  462. {
  463. return parent;
  464. }
  465. /* (non-Javadoc)
  466. * @see org.w3c.dom.Node#getPrefix()
  467. */
  468. public String getPrefix()
  469. {
  470. return null;
  471. }
  472. /* (non-Javadoc)
  473. * @see org.w3c.dom.Node#getPreviousSibling()
  474. */
  475. public Node getPreviousSibling()
  476. {
  477. // If this op needs to be faster, add links to prev and next nodes.
  478. if (parent == null) return null;
  479. int idx = parent.children.indexOf(this);
  480. return (idx == 0) ? null
  481. : (Node)parent.children.get(idx - 1);
  482. }
  483. public TypeInfo getSchemaTypeInfo()
  484. {
  485. return null;
  486. }
  487. public String getTextContent()
  488. throws DOMException
  489. {
  490. return null;
  491. }
  492. public Object getUserData(String key)
  493. {
  494. return null;
  495. }
  496. /* (non-Javadoc)
  497. * @see org.w3c.dom.Node#hasAttributes()
  498. */
  499. public boolean hasAttributes()
  500. {
  501. return !attrs.isEmpty();
  502. }
  503. /* (non-Javadoc)
  504. * @see org.w3c.dom.Node#hasChildNodes()
  505. */
  506. public boolean hasChildNodes()
  507. {
  508. return !children.isEmpty();
  509. }
  510. /* (non-Javadoc)
  511. * @see org.w3c.dom.Node#insertBefore(org.w3c.dom.Node, org.w3c.dom.Node)
  512. */
  513. public Node insertBefore(Node newChild, Node refChild)
  514. {
  515. if (newChild == null)
  516. throw new IllegalArgumentException();
  517. int idx = children.indexOf(refChild);
  518. if (idx == -1)
  519. children.add(newChild);
  520. else
  521. children.add(idx, newChild);
  522. ((IIOMetadataNode)newChild).parent = this;
  523. return newChild;
  524. }
  525. public boolean isDefaultNamespace(String namespaceURI)
  526. {
  527. return true;
  528. }
  529. public boolean isEqualNode(Node arg)
  530. {
  531. return true;
  532. }
  533. public boolean isSameNode(Node other)
  534. {
  535. return this == other;
  536. }
  537. /* (non-Javadoc)
  538. * @see org.w3c.dom.Node#isSupported(java.lang.String, java.lang.String)
  539. */
  540. public boolean isSupported(String feature, String version)
  541. {
  542. // No DOM features are supported
  543. return false;
  544. }
  545. public String lookupNamespaceURI(String prefix)
  546. {
  547. return null;
  548. }
  549. public String lookupPrefix(String namespaceURI)
  550. {
  551. return null;
  552. }
  553. /* (non-Javadoc)
  554. * @see org.w3c.dom.Node#normalize()
  555. */
  556. public void normalize()
  557. {
  558. // No text nodes so no action
  559. }
  560. /* (non-Javadoc)
  561. * @see org.w3c.dom.Node#removeChild(org.w3c.dom.Node)
  562. */
  563. public Node removeChild(Node oldChild)
  564. {
  565. if (oldChild == null)
  566. throw new IllegalArgumentException();
  567. children.remove(oldChild);
  568. ((IIOMetadataNode)oldChild).parent = null;
  569. return oldChild;
  570. }
  571. /* (non-Javadoc)
  572. * @see org.w3c.dom.Node#replaceChild(org.w3c.dom.Node, org.w3c.dom.Node)
  573. */
  574. public Node replaceChild(Node newChild, Node oldChild)
  575. {
  576. if (newChild == null)
  577. throw new IllegalArgumentException();
  578. children.set(children.indexOf(oldChild), newChild);
  579. ((IIOMetadataNode)oldChild).parent = null;
  580. return oldChild;
  581. }
  582. public void setIdAttribute(String name, boolean isId)
  583. throws DOMException
  584. {
  585. }
  586. public void setIdAttributeNode(Attr idAttr, boolean isId)
  587. throws DOMException
  588. {
  589. }
  590. public void setIdAttributeNS(String namespaceURI, String localName, boolean isId)
  591. throws DOMException
  592. {
  593. }
  594. /* (non-Javadoc)
  595. * @see org.w3c.dom.Node#setNodeValue(java.lang.String)
  596. */
  597. public void setNodeValue(String nodeValue) throws DOMException
  598. {
  599. }
  600. /* (non-Javadoc)
  601. * @see org.w3c.dom.Node#setPrefix(java.lang.String)
  602. */
  603. public void setPrefix(String prefix)
  604. {
  605. }
  606. public void setTextContent(String textContent)
  607. throws DOMException
  608. {
  609. }
  610. public Object setUserData(String key, Object data, UserDataHandler handler)
  611. {
  612. return null;
  613. }
  614. }