home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / parser.jar / com / sun / xml / tree / ParentNode.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-02-23  |  5.3 KB  |  332 lines

  1. package com.sun.xml.tree;
  2.  
  3. import java.io.IOException;
  4. import org.w3c.dom.DOMException;
  5. import org.w3c.dom.Document;
  6. import org.w3c.dom.Node;
  7. import org.w3c.dom.NodeList;
  8. import org.xml.sax.SAXException;
  9.  
  10. abstract class ParentNode extends NodeBase implements XmlReadable {
  11.    private NodeBase[] children;
  12.    private int length;
  13.  
  14.    public Node appendChild(Node var1) throws DOMException {
  15.       if (super.readonly) {
  16.          throw new DomEx((short)7);
  17.       } else {
  18.          NodeBase var2 = this.checkDocument(var1);
  19.          if (var1.getNodeType() == 11) {
  20.             this.consumeFragment(var1, (Node)null);
  21.             return var1;
  22.          } else {
  23.             this.checkNotAncestor(var1);
  24.             this.checkChildType(var2.getNodeType());
  25.             if (this.children == null) {
  26.                this.children = new NodeBase[3];
  27.             } else if (this.children.length == this.length) {
  28.                NodeBase[] var3 = new NodeBase[this.length * 2];
  29.                System.arraycopy(this.children, 0, var3, 0, this.length);
  30.                this.children = var3;
  31.             }
  32.  
  33.             var2.setParentNode(this, this.length);
  34.             this.children[this.length++] = var2;
  35.             this.mutated();
  36.             return var2;
  37.          }
  38.       }
  39.    }
  40.  
  41.    abstract void checkChildType(int var1) throws DOMException;
  42.  
  43.    private NodeBase checkDocument(Node var1) throws DOMException {
  44.       if (var1 == null) {
  45.          throw new DomEx((short)3);
  46.       } else if (!(var1 instanceof NodeBase)) {
  47.          throw new DomEx((short)4);
  48.       } else {
  49.          Document var2 = var1.getOwnerDocument();
  50.          XmlDocument var3 = super.ownerDocument;
  51.          NodeBase var4 = (NodeBase)var1;
  52.          if (var3 == null && this instanceof XmlDocument) {
  53.             var3 = (XmlDocument)this;
  54.          }
  55.  
  56.          if (var2 != null && var2 != var3) {
  57.             throw new DomEx((short)4);
  58.          } else {
  59.             if (var2 == null) {
  60.                var4.setOwnerDocument(var3);
  61.             }
  62.  
  63.             if (var4.hasChildNodes()) {
  64.                int var5 = 0;
  65.  
  66.                while(true) {
  67.                   Node var6 = var4.item(var5);
  68.                   if (var6 == null) {
  69.                      break;
  70.                   }
  71.  
  72.                   if (var6.getOwnerDocument() == null) {
  73.                      ((NodeBase)var6).setOwnerDocument(var3);
  74.                   } else if (var6.getOwnerDocument() != var3) {
  75.                      throw new DomEx((short)4);
  76.                   }
  77.  
  78.                   ++var5;
  79.                }
  80.             }
  81.  
  82.             return var4;
  83.          }
  84.       }
  85.    }
  86.  
  87.    private void checkNotAncestor(Node var1) throws DOMException {
  88.       if (var1.hasChildNodes()) {
  89.          for(Object var2 = this; var2 != null; var2 = ((Node)var2).getParentNode()) {
  90.             if (var1 == var2) {
  91.                throw new DomEx((short)3);
  92.             }
  93.          }
  94.  
  95.       }
  96.    }
  97.  
  98.    private void consumeFragment(Node var1, Node var2) throws DOMException {
  99.       ParentNode var3 = (ParentNode)var1;
  100.  
  101.       Node var4;
  102.       for(int var5 = 0; (var4 = var3.item(var5)) != null; ++var5) {
  103.          this.checkNotAncestor(var4);
  104.          this.checkChildType(var4.getNodeType());
  105.       }
  106.  
  107.       while((var4 = var3.item(0)) != null) {
  108.          this.insertBefore(var4, var2);
  109.       }
  110.  
  111.    }
  112.  
  113.    public void doneChild(NodeEx var1, ParseContext var2) throws SAXException {
  114.    }
  115.  
  116.    public void doneParse(ParseContext var1) throws SAXException {
  117.    }
  118.  
  119.    public NodeList getElementsByTagName(String var1) {
  120.       if ("*".equals(var1)) {
  121.          var1 = null;
  122.       }
  123.  
  124.       return new TagList(this, var1);
  125.    }
  126.  
  127.    public final Node getFirstChild() {
  128.       return this.length == 0 ? null : this.children[0];
  129.    }
  130.  
  131.    public final int getIndexOf(Node var1) {
  132.       for(int var2 = 0; var2 < this.length; ++var2) {
  133.          if (this.children[var2] == var1) {
  134.             return var2;
  135.          }
  136.       }
  137.  
  138.       return -1;
  139.    }
  140.  
  141.    public final Node getLastChild() {
  142.       return this.length == 0 ? null : this.children[this.length - 1];
  143.    }
  144.  
  145.    public final int getLength() {
  146.       return this.length;
  147.    }
  148.  
  149.    public final boolean hasChildNodes() {
  150.       return this.length > 0;
  151.    }
  152.  
  153.    public Node insertBefore(Node var1, Node var2) throws DOMException {
  154.       if (super.readonly) {
  155.          throw new DomEx((short)7);
  156.       } else if (var2 == null) {
  157.          return this.appendChild(var1);
  158.       } else if (this.length == 0) {
  159.          throw new DomEx((short)8);
  160.       } else {
  161.          NodeBase var3 = this.checkDocument(var1);
  162.          if (var1.getNodeType() == 11) {
  163.             this.consumeFragment(var1, var2);
  164.             return var1;
  165.          } else {
  166.             this.checkNotAncestor(var1);
  167.             this.checkChildType(var1.getNodeType());
  168.             if (this.children.length == this.length) {
  169.                NodeBase[] var4 = new NodeBase[this.length * 2];
  170.                System.arraycopy(this.children, 0, var4, 0, this.length);
  171.                this.children = var4;
  172.             }
  173.  
  174.             for(int var5 = 0; var5 < this.length; ++var5) {
  175.                if (this.children[var5] == var2) {
  176.                   var3.setParentNode(this, var5);
  177.                   System.arraycopy(this.children, var5, this.children, var5 + 1, this.length - var5);
  178.                   this.children[var5] = var3;
  179.                   ++this.length;
  180.                   this.mutated();
  181.                   return var1;
  182.                }
  183.             }
  184.  
  185.             throw new DomEx((short)8);
  186.          }
  187.       }
  188.    }
  189.  
  190.    public final Node item(int var1) {
  191.       if (this.length != 0 && var1 < this.length) {
  192.          try {
  193.             return this.children[var1];
  194.          } catch (ArrayIndexOutOfBoundsException var2) {
  195.             return null;
  196.          }
  197.       } else {
  198.          return null;
  199.       }
  200.    }
  201.  
  202.    private void mutated() {
  203.       XmlDocument var1 = super.ownerDocument;
  204.       if (var1 == null && this instanceof XmlDocument) {
  205.          var1 = (XmlDocument)this;
  206.       }
  207.  
  208.       if (var1 != null) {
  209.          ++var1.mutationCount;
  210.       }
  211.  
  212.    }
  213.  
  214.    void reduceWaste() {
  215.       if (this.children != null) {
  216.          if (this.children.length - this.length > 6) {
  217.             this.trimToSize();
  218.          }
  219.  
  220.       }
  221.    }
  222.  
  223.    public Node removeChild(Node var1) throws DOMException {
  224.       if (super.readonly) {
  225.          throw new DomEx((short)7);
  226.       } else if (!(var1 instanceof NodeBase)) {
  227.          throw new DomEx((short)8);
  228.       } else {
  229.          NodeBase var2 = (NodeBase)var1;
  230.  
  231.          for(int var3 = 0; var3 < this.length; ++var3) {
  232.             if (this.children[var3] == var2) {
  233.                if (var3 + 1 != this.length) {
  234.                   System.arraycopy(this.children, var3 + 1, this.children, var3, this.length - 1 - var3);
  235.                }
  236.  
  237.                --this.length;
  238.                this.children[this.length] = null;
  239.                var2.setParentNode((ParentNode)null, -1);
  240.                this.mutated();
  241.                return var1;
  242.             }
  243.          }
  244.  
  245.          throw new DomEx((short)8);
  246.       }
  247.    }
  248.  
  249.    public Node replaceChild(Node var1, Node var2) throws DOMException {
  250.       if (super.readonly) {
  251.          throw new DomEx((short)7);
  252.       } else if (var1 != null && var2 != null) {
  253.          if (this.children == null) {
  254.             throw new DomEx((short)8);
  255.          } else {
  256.             NodeBase var3 = this.checkDocument(var1);
  257.             if (var1.getNodeType() == 11) {
  258.                this.consumeFragment(var1, var2);
  259.                return this.removeChild(var2);
  260.             } else {
  261.                this.checkNotAncestor(var1);
  262.                this.checkChildType(var1.getNodeType());
  263.  
  264.                for(int var4 = 0; var4 < this.length; ++var4) {
  265.                   if (this.children[var4] == var2) {
  266.                      var3.setParentNode(this, var4);
  267.                      this.children[var4] = var3;
  268.                      ((NodeBase)var2).setParentNode((ParentNode)null, -1);
  269.                      this.mutated();
  270.                      return var2;
  271.                   }
  272.                }
  273.  
  274.                throw new DomEx((short)8);
  275.             }
  276.          }
  277.       } else {
  278.          throw new DomEx((short)3);
  279.       }
  280.    }
  281.  
  282.    public void startParse(ParseContext var1) throws SAXException {
  283.    }
  284.  
  285.    public void trimToSize() {
  286.       if (this.length == 0) {
  287.          this.children = null;
  288.       } else if (this.children.length != this.length) {
  289.          NodeBase[] var1 = new NodeBase[this.length];
  290.          System.arraycopy(this.children, 0, var1, 0, this.length);
  291.          this.children = var1;
  292.       }
  293.  
  294.    }
  295.  
  296.    public void writeChildrenXml(XmlWriteContext var1) throws IOException {
  297.       if (this.children != null) {
  298.          int var2 = 0;
  299.          boolean var3 = true;
  300.          boolean var4 = true;
  301.          if (((NodeBase)this).getNodeType() == 1) {
  302.             var3 = "preserve".equals(((NodeBase)this).getInheritedAttribute("xml:space"));
  303.             var2 = var1.getIndentLevel();
  304.          }
  305.  
  306.          try {
  307.             if (!var3) {
  308.                var1.setIndentLevel(var2 + 2);
  309.             }
  310.  
  311.             for(int var7 = 0; var7 < this.length; ++var7) {
  312.                if (!var3 && this.children[var7].getNodeType() != 3) {
  313.                   var1.printIndent();
  314.                   var4 = false;
  315.                }
  316.  
  317.                this.children[var7].writeXml(var1);
  318.             }
  319.          } finally {
  320.             if (!var3) {
  321.                var1.setIndentLevel(var2);
  322.                if (!var4) {
  323.                   var1.printIndent();
  324.                }
  325.             }
  326.  
  327.          }
  328.  
  329.       }
  330.    }
  331. }
  332.