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 / parser / TXElement.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-08-30  |  10.7 KB  |  582 lines

  1. package com.ibm.xml.parser;
  2.  
  3. import java.util.Enumeration;
  4. import java.util.Vector;
  5. import org.w3c.dom.Attr;
  6. import org.w3c.dom.CDATASection;
  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. import org.w3c.dom.NodeList;
  12. import org.w3c.dom.Text;
  13.  
  14. public class TXElement extends Parent implements Element, Namespace {
  15.    static final long serialVersionUID = 1851699621748517673L;
  16.    public static final String S_XMLNS = "xmlns";
  17.    public static final String S_XMLNAMESPACEURI = "http://www.w3.org/XML/1998/namespace";
  18.    String name;
  19.    TXAttributeList attributes;
  20.    boolean isPreserveSpace = false;
  21.  
  22.    public TXElement(String var1) {
  23.       this.setTagName(var1);
  24.    }
  25.  
  26.    public synchronized Object clone() {
  27.       TXElement var1 = this.cloneWithoutChildren();
  28.       var1.children.ensureCapacity(super.children.getLength());
  29.  
  30.       for(int var2 = 0; var2 < super.children.getLength(); ++var2) {
  31.          ((Parent)var1).insertBefore(super.children.item(var2).cloneNode(true), (Node)null);
  32.       }
  33.  
  34.       return var1;
  35.    }
  36.  
  37.    public synchronized TXElement cloneWithoutChildren() {
  38.       ((Child)this).checkFactory();
  39.       TXElement var1 = (TXElement)super.factory.createElement(this.getTagName());
  40.       ((Child)var1).setFactory(((Child)this).getFactory());
  41.       var1.setPreserveSpace(this.isPreserveSpace);
  42.       if (this.attributes != null) {
  43.          for(int var2 = 0; var2 < this.attributes.getLength(); ++var2) {
  44.             var1.setAttributeNode((TXAttribute)this.attributes.item(var2).cloneNode(true));
  45.          }
  46.       }
  47.  
  48.       return var1;
  49.    }
  50.  
  51.    public Node cloneNode(boolean var1) {
  52.       return (Node)(var1 ? (Node)this.clone() : this.cloneWithoutChildren());
  53.    }
  54.  
  55.    public boolean equals(Node var1, boolean var2) {
  56.       if (var1 == null) {
  57.          return false;
  58.       } else if (!(var1 instanceof TXElement)) {
  59.          return false;
  60.       } else {
  61.          TXElement var3 = (TXElement)var1;
  62.          if (!var3.getTagName().equals(this.getTagName())) {
  63.             return false;
  64.          } else {
  65.             NamedNodeMap var4 = var3.getAttributes();
  66.             NamedNodeMap var5 = this.getAttributes();
  67.             if (var4.getLength() != var5.getLength()) {
  68.                return false;
  69.             } else {
  70.                for(int var6 = 0; var6 < var4.getLength(); ++var6) {
  71.                   Attr var7 = (Attr)var4.item(var6);
  72.                   Attr var8 = (Attr)var5.getNamedItem(var7.getName());
  73.                   if (var8 == null || !((Child)var7).equals(var8, true)) {
  74.                      return false;
  75.                   }
  76.                }
  77.  
  78.                if (var2 && !var3.children.equals(super.children, var2)) {
  79.                   return false;
  80.                } else {
  81.                   return true;
  82.                }
  83.             }
  84.          }
  85.       }
  86.    }
  87.  
  88.    public short getNodeType() {
  89.       return 1;
  90.    }
  91.  
  92.    public String getNodeName() {
  93.       return this.getTagName();
  94.    }
  95.  
  96.    public String getTagName() {
  97.       return this.name;
  98.    }
  99.  
  100.    /** @deprecated */
  101.    public String getName() {
  102.       return this.getTagName();
  103.    }
  104.  
  105.    public void setTagName(String var1) {
  106.       this.name = var1;
  107.       ((Child)this).clearDigest();
  108.    }
  109.  
  110.    public String getAttribute(String var1) {
  111.       if (this.attributes != null) {
  112.          TXAttribute var2 = (TXAttribute)this.attributes.getNamedItem(var1);
  113.          return var2 == null ? "" : var2.getValue();
  114.       } else {
  115.          return "";
  116.       }
  117.    }
  118.  
  119.    String getAttributeOrNull(String var1) {
  120.       if (this.attributes != null) {
  121.          TXAttribute var2 = (TXAttribute)this.attributes.getNamedItem(var1);
  122.          return var2 == null ? null : var2.getValue();
  123.       } else {
  124.          return null;
  125.       }
  126.    }
  127.  
  128.    public Attr getAttributeNode(String var1) {
  129.       return this.attributes != null ? (Attr)this.attributes.getNamedItem(var1) : null;
  130.    }
  131.  
  132.    public void setAttribute(String var1, String var2) throws DOMException {
  133.       ((Child)this).checkFactory();
  134.       Attr var3 = this.getAttributeNode(var1);
  135.       if (var3 == null) {
  136.          var3 = super.factory.createAttribute(var1);
  137.          this.setAttributeNode(var3);
  138.          var3.setValue(var2);
  139.       } else {
  140.          var3.setValue(var2);
  141.          ((Child)this).clearDigest();
  142.       }
  143.    }
  144.  
  145.    /** @deprecated */
  146.    public void setAttribute(TXAttribute var1) {
  147.       this.makeAttributeList();
  148.       this.attributes.setNamedItem(var1);
  149.    }
  150.  
  151.    public Attr setAttributeNode(Attr var1) {
  152.       this.makeAttributeList();
  153.       return (Attr)this.attributes.setNamedItem(var1);
  154.    }
  155.  
  156.    public void removeAttribute(String var1) {
  157.       if (this.attributes != null) {
  158.          this.attributes.removeNamedItem(var1);
  159.       }
  160.  
  161.       this.resetDefaultAttribute(var1);
  162.    }
  163.  
  164.    public Attr removeAttributeNode(Attr var1) {
  165.       Attr var2 = null;
  166.       if (this.attributes != null) {
  167.          String var3 = this.getAttributeOrNull(var1.getName());
  168.          if (var3 != null && var3.equals(var1.getValue())) {
  169.             var2 = (Attr)this.attributes.removeNamedItem(var1.getName());
  170.          }
  171.       }
  172.  
  173.       this.resetDefaultAttribute(var1.getName());
  174.       return var2;
  175.    }
  176.  
  177.    protected void resetDefaultAttribute(String var1) {
  178.       TXDocument var2 = (TXDocument)((Child)this).getOwnerDocument();
  179.       DTD var3 = (DTD)var2.getDoctype();
  180.       if (var3 != null) {
  181.          AttDef var4 = var3.getAttributeDeclaration(this.getNodeName(), var1);
  182.          if (var4 != null) {
  183.             switch (var4.getDefaultType()) {
  184.                case 0:
  185.                case 2:
  186.                case 3:
  187.                default:
  188.                   return;
  189.                case 1:
  190.                   if (!var2.isAddFixedAttributes()) {
  191.                      return;
  192.                   }
  193.                case -1:
  194.                   Attr var5 = var2.createAttribute(var1);
  195.                   var5.setNodeValue(var4.getDefaultStringValue());
  196.                   ((TXAttribute)var5).setSpecified(false);
  197.                   this.setAttributeNode(var5);
  198.             }
  199.          }
  200.       }
  201.    }
  202.  
  203.    public NamedNodeMap getAttributes() {
  204.       this.makeAttributeList();
  205.       return this.attributes;
  206.    }
  207.  
  208.    public TXAttribute[] getAttributeArray() {
  209.       return this.attributes == null ? new TXAttribute[0] : this.attributes.makeArray();
  210.    }
  211.  
  212.    public Enumeration attributeElements() {
  213.       this.makeAttributeList();
  214.       return this.attributes.elements();
  215.    }
  216.  
  217.    public String getNSLocalName() {
  218.       return getLocalNameForQName(this.getNodeName());
  219.    }
  220.  
  221.    public String getNSName() {
  222.       return this.getNamespaceForQName(this.getNodeName());
  223.    }
  224.  
  225.    /** @deprecated */
  226.    public String getUniversalName() {
  227.       String var1 = this.getNSName();
  228.       return var1 == null ? this.getNSLocalName() : var1 + ":" + this.getNSLocalName();
  229.    }
  230.  
  231.    public String createExpandedName() {
  232.       String var1 = this.getNSName();
  233.       return var1 != null && var1.length() != 0 ? var1 + ((Child)this).getFactory().expandedNameSeparator + this.getNSLocalName() : this.getNSLocalName();
  234.    }
  235.  
  236.    /** @deprecated */
  237.    public boolean isEmpty() {
  238.       return super.children.getLength() == 0;
  239.    }
  240.  
  241.    public boolean isPreserveSpace() {
  242.       return this.isPreserveSpace;
  243.    }
  244.  
  245.    public void setPreserveSpace(boolean var1) {
  246.       this.isPreserveSpace = var1;
  247.    }
  248.  
  249.    public String getLanguage() {
  250.       String var1 = this.getAttributeOrNull("xml:lang");
  251.       if (var1 != null) {
  252.          return var1;
  253.       } else {
  254.          if (super.parent != null) {
  255.             if (super.parent instanceof TXElement) {
  256.                return ((TXElement)super.parent).getLanguage();
  257.             }
  258.  
  259.             if (super.parent instanceof GeneralReference) {
  260.                return ((GeneralReference)super.parent).getLanguage();
  261.             }
  262.          }
  263.  
  264.          return null;
  265.       }
  266.    }
  267.  
  268.    public void normalize() {
  269.       normalize(this, this.isPreserveSpace());
  270.    }
  271.  
  272.    private static void normalize(Node var0, boolean var1) {
  273.       Node var2 = var0.getFirstChild();
  274.       if (var2 != null) {
  275.          if (var2.getNodeType() == 1) {
  276.             normalize(var2, ((TXElement)var2).isPreserveSpace());
  277.          } else if (var2.getNodeType() == 5) {
  278.             normalize(var2, var1);
  279.          }
  280.  
  281.          Node var3;
  282.          for(; (var3 = var2.getNextSibling()) != null; var2 = var3) {
  283.             short var4 = var3.getNodeType();
  284.             if (var4 == 3 && var2.getNodeType() == 3) {
  285.                ((Text)var3).setData(var2.getNodeValue() + var3.getNodeValue());
  286.                var0.removeChild(var2);
  287.                if (!var1 && Util.checkAllSpace(var3.getNodeValue())) {
  288.                   Node var5 = var3.getPreviousSibling();
  289.                   Node var6 = var3.getNextSibling();
  290.                   if ((var5 == null || var5.getNodeType() != 3) && (var6 == null || var6.getNodeType() != 3)) {
  291.                      ((TXText)var3).setIsIgnorableWhitespace(true);
  292.                   }
  293.                }
  294.             } else if (var4 == 1) {
  295.                normalize(var3, ((TXElement)var3).isPreserveSpace());
  296.             } else if (var4 == 5) {
  297.                normalize(var3, var1);
  298.             }
  299.          }
  300.  
  301.       }
  302.    }
  303.  
  304.    public void addTextElement(TXText var1) {
  305.       if (var1 != null) {
  306.          if (var1 instanceof CDATASection) {
  307.             ((Parent)this).appendChild(var1);
  308.          } else {
  309.             Node var2 = ((Parent)this).getLastChild();
  310.             if (var2 == null) {
  311.                ((Parent)this).appendChild(var1);
  312.             } else if (var2.getNodeType() != 3) {
  313.                ((Parent)this).appendChild(var1);
  314.             } else {
  315.                ((Text)var2).appendData(((TXCharacterData)var1).getData());
  316.                if (!var1.getIsIgnorableWhitespace() || !((TXText)var2).getIsIgnorableWhitespace()) {
  317.                   ((TXText)var2).setIsIgnorableWhitespace(false);
  318.                }
  319.  
  320.             }
  321.          }
  322.       }
  323.    }
  324.  
  325.    public TXElement searchDescendants(String var1) {
  326.       return this.searchDescendants(0, (String)null, var1);
  327.    }
  328.  
  329.    public TXElement searchDescendants(int var1, String var2, String var3) {
  330.       for(Child var4 = (Child)((Parent)this).getFirstChild(); var4 != null; var4 = (Child)var4.getNextSibling()) {
  331.          if (var4 instanceof TXElement) {
  332.             if (Match.matchName((TXElement)var4, var1, var2, var3)) {
  333.                return (TXElement)var4;
  334.             }
  335.  
  336.             TXElement var5 = ((TXElement)var4).searchDescendants(var1, var2, var3);
  337.             if (var5 != null) {
  338.                return var5;
  339.             }
  340.          }
  341.       }
  342.  
  343.       return null;
  344.    }
  345.  
  346.    public TXElement getElementNamed(String var1) {
  347.       return this.getNthElementNamed(0, 0, (String)null, var1);
  348.    }
  349.  
  350.    public TXElement getElementNamed(String var1, String var2) {
  351.       return this.getNthElementNamed(0, 2, var1, var2);
  352.    }
  353.  
  354.    public TXElement getElementNamed(int var1, String var2, String var3) {
  355.       return this.getNthElementNamed(0, var1, var2, var3);
  356.    }
  357.  
  358.    public TXElement getNthElementNamed(int var1, String var2) {
  359.       return this.getNthElementNamed(var1, 0, (String)null, var2);
  360.    }
  361.  
  362.    public TXElement getNthElementNamed(int var1, String var2, String var3) {
  363.       return this.getNthElementNamed(var1, 2, var2, var3);
  364.    }
  365.  
  366.    public TXElement getNthElementNamed(int var1, int var2, String var3, String var4) {
  367.       for(Node var5 = ((Parent)this).getFirstChild(); var5 != null; var5 = var5.getNextSibling()) {
  368.          if (var5 instanceof TXElement) {
  369.             TXElement var6 = (TXElement)var5;
  370.             if (Match.matchName(var6, var2, var3, var4)) {
  371.                if (var1 == 0) {
  372.                   return var6;
  373.                }
  374.  
  375.                --var1;
  376.             }
  377.          }
  378.       }
  379.  
  380.       return null;
  381.    }
  382.  
  383.    public TXElement[] searchChildrenAll(String var1) {
  384.       Vector var2 = new Vector();
  385.  
  386.       for(Node var3 = ((Parent)this).getFirstChild(); var3 != null; var3 = var3.getNextSibling()) {
  387.          if (var3 instanceof Element && Match.matchName((Namespace)var3, 0, (String)null, var1)) {
  388.             var2.addElement(var3);
  389.          }
  390.       }
  391.  
  392.       TXElement[] var4 = new TXElement[var2.size()];
  393.       var2.copyInto(var4);
  394.       return var4;
  395.    }
  396.  
  397.    public NodeList getElementsByTagName(String var1) {
  398.       return new TXNodeList.VectorNodeList(this.realSearchDescendantsAll(0, (String)null, var1));
  399.    }
  400.  
  401.    public TXElement[] searchDescendantsAll(String var1) {
  402.       return this.searchDescendantsAll(0, (String)null, var1);
  403.    }
  404.  
  405.    public TXElement[] searchDescendantsAll(int var1, String var2, String var3) {
  406.       Vector var4 = this.realSearchDescendantsAll(var1, var2, var3);
  407.       TXElement[] var5 = new TXElement[var4.size()];
  408.       var4.copyInto(var5);
  409.       return var5;
  410.    }
  411.  
  412.    public NodeList getElementsNamed(String var1) {
  413.       return this.getElementsNamed(0, (String)null, var1);
  414.    }
  415.  
  416.    public NodeList getElementsNamed(String var1, String var2) {
  417.       return this.getElementsNamed(2, var1, var2);
  418.    }
  419.  
  420.    public NodeList getElementsNamed(int var1, String var2, String var3) {
  421.       Vector var4 = new Vector();
  422.  
  423.       for(Node var5 = ((Parent)this).getFirstChild(); var5 != null; var5 = var5.getNextSibling()) {
  424.          if (var5 instanceof Element && Match.matchName((Namespace)var5, var1, var2, var3)) {
  425.             var4.addElement(var5);
  426.          }
  427.       }
  428.  
  429.       return new TXNodeList.VectorNodeList(var4);
  430.    }
  431.  
  432.    public void acceptPre(Visitor var1) throws Exception {
  433.       var1.visitElementPre(this);
  434.    }
  435.  
  436.    public void acceptPost(Visitor var1) throws Exception {
  437.       var1.visitElementPost(this);
  438.    }
  439.  
  440.    private Vector realSearchDescendantsAll(int var1, String var2, String var3) {
  441.       Vector var4 = new Vector();
  442.       SearchElementVisitor var5 = new SearchElementVisitor(var4, var1, var2, var3);
  443.  
  444.       try {
  445.          (new NonRecursivePreorderTreeTraversal(var5)).traverse(this);
  446.       } catch (Exception var6) {
  447.       }
  448.  
  449.       return var4;
  450.    }
  451.  
  452.    private void makeAttributeList() {
  453.       if (this.attributes == null) {
  454.          ((Child)this).checkFactory();
  455.          this.attributes = new TXAttributeList();
  456.          this.attributes.setParent(this);
  457.       }
  458.  
  459.    }
  460.  
  461.    protected void checkChildType(Node var1) throws DOMException {
  462.       switch (var1.getNodeType()) {
  463.          case 1:
  464.          case 3:
  465.          case 4:
  466.          case 5:
  467.          case 7:
  468.          case 8:
  469.          case 23:
  470.             return;
  471.          default:
  472.             throw new TXDOMException((short)3, "Specified node type (" + var1.getNodeType() + ") can't be a child of Element.");
  473.       }
  474.    }
  475.  
  476.    public String getNamespaceForPrefix(String var1) {
  477.       if (var1.equals("xml")) {
  478.          return "http://www.w3.org/XML/1998/namespace";
  479.       } else if (var1.equals("xmlns")) {
  480.          return var1;
  481.       } else {
  482.          String var2 = var1.length() == 0 ? "xmlns" : "xmlns:" + var1;
  483.          Object var3 = this;
  484.  
  485.          do {
  486.             String var4 = ((TXElement)var3).getAttributeOrNull(var2);
  487.             if (var4 != null) {
  488.                return var4;
  489.             }
  490.  
  491.             short var5;
  492.             do {
  493.                var3 = ((Node)var3).getParentNode();
  494.                if (var3 == null) {
  495.                   if (var2 == "xmlns") {
  496.                      return "";
  497.                   }
  498.  
  499.                   return null;
  500.                }
  501.  
  502.                var5 = ((Node)var3).getNodeType();
  503.             } while(var5 == 5);
  504.  
  505.             if (var5 != 1) {
  506.                if (var2 == "xmlns") {
  507.                   return "";
  508.                }
  509.  
  510.                return null;
  511.             }
  512.          } while(var3 != null);
  513.  
  514.          return null;
  515.       }
  516.    }
  517.  
  518.    public String getNamespaceForQName(String var1) {
  519.       int var2 = var1.indexOf(58);
  520.       String var3 = var2 < 0 ? "" : var1.substring(0, var2).intern();
  521.       return this.getNamespaceForPrefix(var3);
  522.    }
  523.  
  524.    public static String getLocalNameForQName(String var0) {
  525.       int var1 = var0.indexOf(58);
  526.       return var1 < 0 ? var0 : var0.substring(var1 + 1).intern();
  527.    }
  528.  
  529.    public void collectNamespaceAttributes() {
  530.       this.collectNamespaceAttributes(((Child)this).getParentNode());
  531.    }
  532.  
  533.    public void collectNamespaceAttributes(Node var1) {
  534.       if (((Child)this).getFactory().isProcessNamespace()) {
  535.          short var2;
  536.          for(; var1 != null && ((var2 = var1.getNodeType()) == 1 || var2 == 5); var1 = var1.getParentNode()) {
  537.             if (var2 == 1) {
  538.                NamedNodeMap var3 = var1.getAttributes();
  539.  
  540.                for(int var4 = 0; var4 < var3.getLength(); ++var4) {
  541.                   Node var5 = var3.item(var4);
  542.                   String var6 = var5.getNodeName();
  543.                   if ((var6.equals("xmlns") || var6.startsWith("xmlns:")) && this.getAttributeNode(var6) == null) {
  544.                      this.setAttribute(var6, var5.getNodeValue());
  545.                   }
  546.                }
  547.             }
  548.          }
  549.  
  550.       }
  551.    }
  552.  
  553.    public void removeOverlappedNamespaceAttributes() {
  554.       if (((Child)this).getFactory().isProcessNamespace()) {
  555.          Vector var1 = new Vector();
  556.          NamedNodeMap var2 = this.getAttributes();
  557.  
  558.          for(int var3 = 0; var3 < var2.getLength(); ++var3) {
  559.             Node var4 = var2.item(var3);
  560.             String var5 = var4.getNodeName();
  561.             short var7;
  562.             if (var5.equals("xmlns") || var5.startsWith("xmlns:")) {
  563.                for(Node var6 = ((Child)this).getParentNode(); var6 != null && ((var7 = var6.getNodeType()) == 1 || var7 == 5); var6 = var6.getParentNode()) {
  564.                   if (var7 == 1) {
  565.                      TXElement var8 = (TXElement)var6;
  566.                      String var9 = var8.getAttributeOrNull(var5);
  567.                      if (var9 != null && var9.equals(var4.getNodeValue())) {
  568.                         var1.addElement(var5);
  569.                      }
  570.                   }
  571.                }
  572.             }
  573.          }
  574.  
  575.          for(int var10 = 0; var10 < var1.size(); ++var10) {
  576.             this.removeAttribute((String)var1.elementAt(var10));
  577.          }
  578.  
  579.       }
  580.    }
  581. }
  582.