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 / ElementNode.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-02-23  |  7.3 KB  |  399 lines

  1. package com.sun.xml.tree;
  2.  
  3. import java.io.CharArrayWriter;
  4. import java.io.IOException;
  5. import java.io.Writer;
  6. import org.w3c.dom.Attr;
  7. import org.w3c.dom.DOMException;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.NamedNodeMap;
  10. import org.w3c.dom.Node;
  11.  
  12. public class ElementNode extends ParentNode implements ElementEx {
  13.    private String tag;
  14.    private AttributeSet attributes;
  15.    private String idAttributeName;
  16.    private Object userObject;
  17.    private static final char[] tagStart = new char[]{'<', '/'};
  18.    private static final char[] tagEnd = new char[]{' ', '/', '>'};
  19.  
  20.    void checkChildType(int var1) throws DOMException {
  21.       switch (var1) {
  22.          case 1:
  23.          case 3:
  24.          case 4:
  25.          case 5:
  26.          case 7:
  27.          case 8:
  28.             return;
  29.          case 2:
  30.          case 6:
  31.          default:
  32.             throw new DomEx((short)3);
  33.       }
  34.    }
  35.  
  36.    public Node cloneNode(boolean var1) {
  37.       try {
  38.          ElementNode var2 = (ElementNode)((NodeBase)this).getOwnerDocument().createElement(this.tag);
  39.          if (this.attributes != null) {
  40.             var2.setAttributes(new AttributeSet(this.attributes, true));
  41.          }
  42.  
  43.          if (var1) {
  44.             int var3 = 0;
  45.  
  46.             while(true) {
  47.                Node var4 = ((ParentNode)this).item(var3);
  48.                if (var4 == null) {
  49.                   break;
  50.                }
  51.  
  52.                ((ParentNode)var2).appendChild(var4.cloneNode(true));
  53.                ++var3;
  54.             }
  55.          }
  56.  
  57.          return var2;
  58.       } catch (DOMException var5) {
  59.          throw new RuntimeException(((NodeBase)this).getMessage("EN-001"));
  60.       }
  61.    }
  62.  
  63.    public String getAttribute(String var1) {
  64.       return this.attributes == null ? "" : this.attributes.getValue(var1);
  65.    }
  66.  
  67.    public String getAttribute(String var1, String var2) {
  68.       if (this.attributes == null) {
  69.          return "";
  70.       } else {
  71.          Attr var3 = this.getAttributeNode(var1, var2);
  72.          return var3 == null ? "" : var3.getValue();
  73.       }
  74.    }
  75.  
  76.    public Attr getAttributeNode(String var1) {
  77.       return this.attributes != null ? (Attr)this.attributes.getNamedItem(var1) : null;
  78.    }
  79.  
  80.    public Attr getAttributeNode(String var1, String var2) {
  81.       if (var2 == null) {
  82.          return null;
  83.       } else if (this.attributes != null) {
  84.          int var3 = 0;
  85.  
  86.          while(true) {
  87.             AttributeNode var4 = (AttributeNode)this.attributes.item(var3);
  88.             if (var4 == null) {
  89.                return null;
  90.             }
  91.  
  92.             if (var2.equals(var4.getName())) {
  93.                String var5 = var4.getNamespace();
  94.                if (var5 != null && var5.equals(var1)) {
  95.                   return var4;
  96.                }
  97.             }
  98.  
  99.             ++var3;
  100.          }
  101.       } else {
  102.          return null;
  103.       }
  104.    }
  105.  
  106.    public NamedNodeMap getAttributes() {
  107.       if (this.attributes == null) {
  108.          this.attributes = new AttributeSet(this);
  109.       }
  110.  
  111.       return this.attributes;
  112.    }
  113.  
  114.    public String getIdAttributeName() {
  115.       return this.idAttributeName;
  116.    }
  117.  
  118.    public String getLocalName() {
  119.       int var1 = this.tag.indexOf(58);
  120.       return var1 < 0 ? this.tag : this.tag.substring(var1 + 1);
  121.    }
  122.  
  123.    public String getNamespace() {
  124.       String var1;
  125.       if ((var1 = this.getPrefix()) == null) {
  126.          return ((NodeBase)this).getInheritedAttribute("xmlns");
  127.       } else if (!"xml".equals(var1) && !"xmlns".equals(var1)) {
  128.          String var2 = ((NodeBase)this).getInheritedAttribute("xmlns:" + var1);
  129.          if (var2 == null) {
  130.             throw new IllegalStateException(((NodeBase)this).getMessage("EN-000", new Object[]{var1}));
  131.          } else {
  132.             return var2;
  133.          }
  134.       } else {
  135.          return null;
  136.       }
  137.    }
  138.  
  139.    public String getNodeName() {
  140.       return this.tag;
  141.    }
  142.  
  143.    public short getNodeType() {
  144.       return 1;
  145.    }
  146.  
  147.    public String getPrefix() {
  148.       int var1 = this.tag.indexOf(58);
  149.       return var1 < 0 ? null : this.tag.substring(0, var1);
  150.    }
  151.  
  152.    public String getTagName() {
  153.       return this.tag;
  154.    }
  155.  
  156.    public Object getUserObject() {
  157.       return this.userObject;
  158.    }
  159.  
  160.    public void normalize() {
  161.       boolean var2 = false;
  162.       boolean var3 = false;
  163.       if (super.readonly) {
  164.          throw new DomEx((short)7);
  165.       } else {
  166.          Node var1;
  167.          for(int var4 = 0; (var1 = ((ParentNode)this).item(var4)) != null; ++var4) {
  168.             switch (var1.getNodeType()) {
  169.                case 1:
  170.                   ((Element)var1).normalize();
  171.                case 2:
  172.                default:
  173.                   break;
  174.                case 3:
  175.                   Node var5 = ((ParentNode)this).item(var4 + 1);
  176.                   if (var5 != null && var5.getNodeType() == 3) {
  177.                      ((TextNode)var1).joinNextText();
  178.                      --var4;
  179.                   } else {
  180.                      if (!var3) {
  181.                         var2 = "preserve".equals(((NodeBase)this).getInheritedAttribute("xml:space"));
  182.                         var3 = true;
  183.                      }
  184.  
  185.                      if (!var2) {
  186.                         char[] var6 = ((TextNode)var1).data;
  187.                         if (var6 != null && var6.length != 0) {
  188.                            int var7 = this.removeWhiteSpaces(var6);
  189.                            if (var7 != var6.length) {
  190.                               char[] var8 = new char[var7];
  191.                               System.arraycopy(var6, 0, var8, 0, var7);
  192.                               ((TextNode)var1).data = var8;
  193.                            }
  194.                         } else {
  195.                            ((ParentNode)this).removeChild(var1);
  196.                            --var4;
  197.                         }
  198.                      }
  199.                   }
  200.             }
  201.          }
  202.  
  203.       }
  204.    }
  205.  
  206.    public void removeAttribute(String var1) throws DOMException {
  207.       if (super.readonly) {
  208.          throw new DomEx((short)7);
  209.       } else if (this.attributes == null) {
  210.          throw new DomEx((short)8);
  211.       } else {
  212.          this.attributes.removeNamedItem(var1);
  213.       }
  214.    }
  215.  
  216.    public Attr removeAttributeNode(Attr var1) throws DOMException {
  217.       if (((NodeBase)this).isReadonly()) {
  218.          throw new DomEx((short)7);
  219.       } else {
  220.          Attr var2 = this.getAttributeNode(var1.getNodeName());
  221.          if (var2 == null) {
  222.             throw new DomEx((short)8);
  223.          } else {
  224.             this.removeAttribute(var2.getNodeName());
  225.             return var2;
  226.          }
  227.       }
  228.    }
  229.  
  230.    public int removeWhiteSpaces(char[] var1) {
  231.       int var2 = 0;
  232.       int var3 = 0;
  233.  
  234.       while(var3 < var1.length) {
  235.          boolean var4 = false;
  236.          char var5 = var1[var3++];
  237.          if (var5 == ' ' || var5 == '\t' || var5 == '\n' || var5 == '\r') {
  238.             var5 = ' ';
  239.             var4 = true;
  240.          }
  241.  
  242.          var1[var2++] = var5;
  243.          if (var4) {
  244.             while(var3 < var1.length) {
  245.                var5 = var1[var3];
  246.                if (var5 != ' ' && var5 != '\t' && var5 != '\n' && var5 != '\r') {
  247.                   break;
  248.                }
  249.  
  250.                ++var3;
  251.             }
  252.          }
  253.       }
  254.  
  255.       return var2;
  256.    }
  257.  
  258.    public void setAttribute(String var1, String var2) throws DOMException {
  259.       if (super.readonly) {
  260.          throw new DomEx((short)7);
  261.       } else {
  262.          if (this.attributes == null) {
  263.             this.attributes = new AttributeSet(this);
  264.          }
  265.  
  266.          AttributeNode var3;
  267.          if ((var3 = (AttributeNode)this.attributes.getNamedItem(var1)) != null) {
  268.             var3.setNodeValue(var2);
  269.          } else {
  270.             var3 = new AttributeNode(var1, var2, true, (String)null);
  271.             ((NodeBase)var3).setOwnerDocument((XmlDocument)((NodeBase)this).getOwnerDocument());
  272.             this.attributes.setNamedItem(var3);
  273.          }
  274.  
  275.       }
  276.    }
  277.  
  278.    public Attr setAttributeNode(Attr var1) throws DOMException {
  279.       if (super.readonly) {
  280.          throw new DomEx((short)7);
  281.       } else if (!(var1 instanceof AttributeNode)) {
  282.          throw new DomEx((short)4);
  283.       } else {
  284.          if (this.attributes == null) {
  285.             this.attributes = new AttributeSet(this);
  286.          }
  287.  
  288.          return (Attr)this.attributes.setNamedItem(var1);
  289.       }
  290.    }
  291.  
  292.    void setAttributes(AttributeSet var1) {
  293.       AttributeSet var2 = this.attributes;
  294.       if (var2 != null && var2.isReadonly()) {
  295.          throw new DomEx((short)7);
  296.       } else {
  297.          if (var1 != null) {
  298.             var1.setNameScope(this);
  299.          }
  300.  
  301.          this.attributes = var1;
  302.          if (var2 != null) {
  303.             var2.setNameScope((ElementNode)null);
  304.          }
  305.  
  306.       }
  307.    }
  308.  
  309.    public void setIdAttributeName(String var1) {
  310.       if (super.readonly) {
  311.          throw new DomEx((short)7);
  312.       } else {
  313.          this.idAttributeName = var1;
  314.       }
  315.    }
  316.  
  317.    public void setPrefix(String var1) {
  318.       int var2 = this.tag.indexOf(58);
  319.       if (var1 == null) {
  320.          if (var2 >= 0) {
  321.             this.tag = this.tag.substring(var2 + 1);
  322.          }
  323.       } else {
  324.          StringBuffer var3 = new StringBuffer(var1);
  325.          var3.append(':');
  326.          if (var2 < 0) {
  327.             var3.append(this.tag);
  328.          } else {
  329.             var3.append(this.tag.substring(var2 + 1));
  330.          }
  331.  
  332.          this.tag = var3.toString();
  333.       }
  334.    }
  335.  
  336.    public void setReadonly(boolean var1) {
  337.       if (this.attributes != null) {
  338.          this.attributes.setReadonly();
  339.       }
  340.  
  341.       super.setReadonly(var1);
  342.    }
  343.  
  344.    protected void setTag(String var1) {
  345.       this.tag = var1;
  346.    }
  347.  
  348.    public void setUserObject(Object var1) {
  349.       this.userObject = var1;
  350.    }
  351.  
  352.    public String toString() {
  353.       try {
  354.          CharArrayWriter var1 = new CharArrayWriter();
  355.          XmlWriteContext var2 = new XmlWriteContext(var1);
  356.          this.writeXml(var2);
  357.          return var1.toString();
  358.       } catch (Exception var3) {
  359.          return super.toString();
  360.       }
  361.    }
  362.  
  363.    public void trimToSize() {
  364.       super.trimToSize();
  365.       if (this.attributes != null) {
  366.          this.attributes.trimToSize();
  367.       }
  368.  
  369.    }
  370.  
  371.    public void write(Writer var1) throws IOException {
  372.       this.writeXml(new XmlWriteContext(var1));
  373.    }
  374.  
  375.    public void writeXml(XmlWriteContext var1) throws IOException {
  376.       Writer var2 = var1.getWriter();
  377.       if (this.tag == null) {
  378.          throw new IllegalStateException(((NodeBase)this).getMessage("EN-002"));
  379.       } else {
  380.          var2.write(tagStart, 0, 1);
  381.          var2.write(this.tag);
  382.          if (this.attributes != null) {
  383.             this.attributes.writeXml(var1);
  384.          }
  385.  
  386.          if (!((ParentNode)this).hasChildNodes()) {
  387.             var2.write(tagEnd, 0, 3);
  388.          } else {
  389.             var2.write(tagEnd, 2, 1);
  390.             ((ParentNode)this).writeChildrenXml(var1);
  391.             var2.write(tagStart, 0, 2);
  392.             var2.write(this.tag);
  393.             var2.write(tagEnd, 2, 1);
  394.          }
  395.  
  396.       }
  397.    }
  398. }
  399.