Source for javax.imageio.metadata.IIOMetadataNode

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