home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / xml4j.jar / com / ibm / xml / dom / NamedNodeMapImpl.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-08-30  |  3.3 KB  |  222 lines

  1. package com.ibm.xml.dom;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Enumeration;
  5. import java.util.Vector;
  6. import org.w3c.dom.Attr;
  7. import org.w3c.dom.DOMException;
  8. import org.w3c.dom.Document;
  9. import org.w3c.dom.NamedNodeMap;
  10. import org.w3c.dom.Node;
  11.  
  12. public class NamedNodeMapImpl implements NamedNodeMap, Serializable {
  13.    static final long serialVersionUID = -7039242451046758020L;
  14.    protected Vector nodes;
  15.    protected Document ownerDocument;
  16.    protected ElementImpl element;
  17.    protected NamedNodeMapImpl defaults;
  18.    protected int changes;
  19.    protected int lastDefaultsChanges;
  20.    protected boolean readOnly;
  21.  
  22.    protected NamedNodeMapImpl(Document var1, NamedNodeMapImpl var2) {
  23.       this.nodes = new Vector();
  24.       this.lastDefaultsChanges = -1;
  25.       this.readOnly = false;
  26.       this.ownerDocument = var1;
  27.       this.defaults = var2;
  28.    }
  29.  
  30.    protected NamedNodeMapImpl(ElementImpl var1, NamedNodeMapImpl var2) {
  31.       this(((NodeImpl)var1).getOwnerDocument(), var2);
  32.       this.element = var1;
  33.    }
  34.  
  35.    public int getLength() {
  36.       this.reconcileDefaults();
  37.       return this.nodes != null ? this.nodes.size() : 0;
  38.    }
  39.  
  40.    public Node item(int var1) {
  41.       this.reconcileDefaults();
  42.       return this.nodes != null && var1 < this.nodes.size() ? (Node)this.nodes.elementAt(var1) : null;
  43.    }
  44.  
  45.    public Node getNamedItem(String var1) {
  46.       int var2 = this.findNamePoint(var1);
  47.       return var2 < 0 ? null : (Node)this.nodes.elementAt(var2);
  48.    }
  49.  
  50.    public Node setNamedItem(Node var1) throws DOMException {
  51.       if (var1.getOwnerDocument() != this.ownerDocument) {
  52.          throw new DOMExceptionImpl((short)4, (String)null);
  53.       } else if (var1 instanceof AttrImpl && ((AttrImpl)var1).owned) {
  54.          throw new DOMExceptionImpl((short)10, (String)null);
  55.       } else {
  56.          int var2 = this.findNamePoint(var1.getNodeName());
  57.          Node var3 = null;
  58.          if (var2 >= 0) {
  59.             var3 = (Node)this.nodes.elementAt(var2);
  60.             if (this.element != null) {
  61.                ((NodeImpl)var1).parentNode = this.element;
  62.             }
  63.  
  64.             this.nodes.setElementAt(var1, var2);
  65.          } else {
  66.             var2 = -1 - var2;
  67.             if (this.nodes == null) {
  68.                this.nodes = new Vector();
  69.             }
  70.  
  71.             if (this.element != null) {
  72.                ((NodeImpl)var1).parentNode = this.element;
  73.             }
  74.  
  75.             this.nodes.insertElementAt(var1, var2);
  76.          }
  77.  
  78.          if (this.element != null) {
  79.             ((NodeImpl)var1).parentNode = this.element;
  80.          }
  81.  
  82.          ++this.changes;
  83.          return var3;
  84.       }
  85.    }
  86.  
  87.    public Node removeNamedItem(String var1) throws DOMException {
  88.       int var2 = this.findNamePoint(var1);
  89.       if (var2 < 0) {
  90.          throw new DOMExceptionImpl((short)8, (String)null);
  91.       } else {
  92.          Node var3 = (Node)this.nodes.elementAt(var2);
  93.          Node var4;
  94.          if (this.defaults != null && (var4 = this.defaults.getNamedItem(var1)) != null) {
  95.             NodeImpl var5 = (NodeImpl)var4.cloneNode(true);
  96.             var5.parentNode = this.element;
  97.             this.nodes.setElementAt(var5, var2);
  98.          } else {
  99.             this.nodes.removeElementAt(var2);
  100.          }
  101.  
  102.          if (this.element != null) {
  103.             ((NodeImpl)var3).parentNode = null;
  104.          }
  105.  
  106.          ++this.changes;
  107.          return var3;
  108.       }
  109.    }
  110.  
  111.    public NamedNodeMapImpl cloneMap() {
  112.       NamedNodeMapImpl var1 = new NamedNodeMapImpl(this.ownerDocument, this.defaults);
  113.       if (this.nodes != null) {
  114.          var1.nodes = new Vector(this.nodes.size());
  115.  
  116.          for(int var2 = 0; var2 < this.nodes.size(); ++var2) {
  117.             NodeImpl var3 = (NodeImpl)((Node)this.nodes.elementAt(var2)).cloneNode(true);
  118.             var3.parentNode = this.element;
  119.             var1.nodes.addElement(var3);
  120.          }
  121.       }
  122.  
  123.       var1.defaults = this.defaults;
  124.       var1.lastDefaultsChanges = this.lastDefaultsChanges;
  125.       return var1;
  126.    }
  127.  
  128.    void setReadOnly(boolean var1, boolean var2) {
  129.       this.readOnly = var1;
  130.       if (var2 && this.nodes != null) {
  131.          Enumeration var3 = this.nodes.elements();
  132.  
  133.          while(var3.hasMoreElements()) {
  134.             ((NodeImpl)var3.nextElement()).setReadOnly(var1, var2);
  135.          }
  136.       }
  137.  
  138.    }
  139.  
  140.    protected void reconcileDefaults() {
  141.       if (this.defaults != null && this.lastDefaultsChanges != this.defaults.changes) {
  142.          int var1 = 0;
  143.          int var2 = 0;
  144.          int var3 = this.nodes.size();
  145.          int var4 = this.defaults.nodes.size();
  146.          if (var3 == 0) {
  147.             Object var10000 = null;
  148.          } else {
  149.             Attr var12 = (Attr)this.nodes.elementAt(0);
  150.          }
  151.  
  152.          if (var4 == 0) {
  153.             Object var13 = null;
  154.          } else {
  155.             Attr var14 = (Attr)this.defaults.nodes.elementAt(0);
  156.          }
  157.  
  158.          while(var1 < var3 && var2 < var4) {
  159.             Attr var5 = (Attr)this.nodes.elementAt(var1);
  160.             Attr var6 = (Attr)this.defaults.nodes.elementAt(var2);
  161.             int var7 = var5.getNodeName().compareTo(var6.getNodeName());
  162.             if (var7 == 0 && !var5.getSpecified()) {
  163.                NodeImpl var11 = (NodeImpl)var6.cloneNode(true);
  164.                var11.parentNode = this.element;
  165.                this.nodes.setElementAt(var11, var1);
  166.                ++var1;
  167.                ++var2;
  168.             } else if (var7 > 0) {
  169.                NodeImpl var8 = (NodeImpl)var6.cloneNode(true);
  170.                var8.parentNode = this.element;
  171.                this.nodes.insertElementAt(var8, var1);
  172.                ++var1;
  173.                ++var2;
  174.             } else if (!var5.getSpecified()) {
  175.                this.nodes.removeElementAt(var1);
  176.             } else {
  177.                ++var1;
  178.             }
  179.          }
  180.  
  181.          while(var2 < var4) {
  182.             Attr var9 = (Attr)this.defaults.nodes.elementAt(var2++);
  183.             NodeImpl var10 = (NodeImpl)var9.cloneNode(true);
  184.             var10.parentNode = this.element;
  185.             this.nodes.addElement(var10);
  186.          }
  187.  
  188.          this.lastDefaultsChanges = this.defaults.changes;
  189.       }
  190.  
  191.    }
  192.  
  193.    private int findNamePoint(String var1) {
  194.       this.reconcileDefaults();
  195.       int var2 = 0;
  196.       if (this.nodes != null) {
  197.          int var3 = 0;
  198.          int var4 = this.nodes.size() - 1;
  199.  
  200.          while(var3 <= var4) {
  201.             var2 = (var3 + var4) / 2;
  202.             int var5 = var1.compareTo(((Node)this.nodes.elementAt(var2)).getNodeName());
  203.             if (var5 == 0) {
  204.                return var2;
  205.             }
  206.  
  207.             if (var5 < 0) {
  208.                var4 = var2 - 1;
  209.             } else {
  210.                var3 = var2 + 1;
  211.             }
  212.          }
  213.  
  214.          if (var3 > var2) {
  215.             var2 = var3;
  216.          }
  217.       }
  218.  
  219.       return -1 - var2;
  220.    }
  221. }
  222.