home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 October / INTERNET108.ISO / pc / software / windows / building / xmlspy / xmlspyentcomplete5.exe / Data1.cab / _48C6029AFB21499CBA023F7846FCFBFD < prev    next >
Encoding:
Text File  |  2003-05-08  |  12.1 KB  |  370 lines

  1. /**
  2.  * Node.java
  3.  *
  4.  * This file was generated by XMLSPY 5 Enterprise Edition.
  5.  *
  6.  * YOU SHOULD NOT MODIFY THIS FILE, BECAUSE IT WILL BE
  7.  * OVERWRITTEN WHEN YOU RE-RUN CODE GENERATION.
  8.  *
  9.  * Refer to the XMLSPY Documentation for further details.
  10.  * http://www.altova.com/xmlspy
  11.  */
  12.  
  13.  
  14. package com.altova.xml;
  15.  
  16.  
  17. public abstract class Node implements java.io.Serializable {
  18.     final protected static short Attribute = 0;
  19.     final protected static short Element   = 1;
  20.  
  21.     protected static String getDomNodeValue(org.w3c.dom.Node node) {
  22.         if (node == null)
  23.             return null;
  24.         String value = node.getNodeValue();
  25.         if (value != null)
  26.             return value;
  27.         org.w3c.dom.Node child = node.getFirstChild();
  28.         if (child != null && child.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
  29.             return child.getNodeValue();
  30.         else
  31.             return null;
  32.     }
  33.  
  34.     protected static void setDomNodeValue(org.w3c.dom.Node node, String value) {
  35.         if (node == null)
  36.             return;
  37.         if (node.getNodeValue() != null) {
  38.             node.setNodeValue(value);
  39.             return;
  40.         }
  41.         org.w3c.dom.Node child = node.getFirstChild();
  42.         if (child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE) {
  43.             node.appendChild(node.getOwnerDocument().createTextNode(value));
  44.             return;
  45.         }
  46.     }
  47.  
  48.     protected org.w3c.dom.Node domNode = null;
  49.  
  50.     public Node() {
  51.         domNode = Document.createTemporaryDomNode();
  52.     }
  53.  
  54.     public Node(Node node) {
  55.         domNode = node.domNode;
  56.     }
  57.  
  58.     public Node(org.w3c.dom.Node domNode) {
  59.         this.domNode = domNode;
  60.     }
  61.  
  62.     public Node(org.w3c.dom.Document domDocument) {
  63.         this.domNode = domDocument.getDocumentElement();
  64.     }
  65.  
  66.     public org.w3c.dom.Node getDomNode() {
  67.         return domNode;
  68.     }
  69.  
  70.     public void mapPrefix(String prefix, String URI) {
  71.         if (URI == null || URI.equals(""))
  72.             return;
  73.  
  74.         org.w3c.dom.Element element = (org.w3c.dom.Element)domNode;
  75.         if (prefix == null || prefix.equals(""))
  76.             element.setAttribute("xmlns", URI);
  77.         else
  78.             element.setAttribute("xmlns:" + prefix, URI);
  79.     }
  80.  
  81.     protected void declareNamespace(String prefix, String URI) {
  82.         if (URI == null || URI.equals(""))
  83.             return;
  84.         org.w3c.dom.Element root = domNode.getOwnerDocument().getDocumentElement();
  85.  
  86.         org.w3c.dom.NamedNodeMap attrs = root.getAttributes();
  87.         if (attrs != null) {
  88.             for (int i = 0; i < attrs.getLength(); i++) {
  89.                 org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
  90.                 if (attr.getValue().equals(URI)) // namespace URI already mapped?
  91.                     return; // do not overwrite
  92.             }
  93.         }
  94.  
  95.         if (prefix == null || prefix.equals(""))
  96.             root.setAttribute("xmlns", URI);
  97.         else
  98.             root.setAttribute("xmlns:" + prefix, URI);
  99.     }
  100.  
  101.     protected org.w3c.dom.Node appendDomChild(int type, String namespaceURI, String name, String value) {
  102.         switch (type) {
  103.         case Attribute:
  104.             org.w3c.dom.Attr attribute = domNode.getOwnerDocument().createAttributeNS(namespaceURI, name);
  105.             attribute.setNodeValue(value);
  106.             domNode.getAttributes().setNamedItemNS(attribute);
  107.             return attribute;
  108.  
  109.         case Element:
  110.             org.w3c.dom.Element element = domNode.getOwnerDocument().createElementNS(namespaceURI, name);
  111.             if (value != null && !value.equals(""))
  112.                 element.appendChild(domNode.getOwnerDocument().createTextNode(value));
  113.             domNode.appendChild(element);
  114.             return element;
  115.  
  116.         default:
  117.             throw new XmlException("Unknown type");
  118.         }
  119.     }
  120.  
  121.     protected boolean domNodeNameEquals(org.w3c.dom.Node node, String namespaceURI, String localName) {
  122.         if (node == null)
  123.             return false;
  124.         String nodeURI = node.getNamespaceURI() == null ? "" : node.getNamespaceURI();
  125.         String nodeLocalName = node.getLocalName() == null ? "" : node.getLocalName();
  126.         if (namespaceURI == null)
  127.             namespaceURI = "";
  128.         if (localName == null)
  129.             localName = "";
  130.         return nodeURI.equals(namespaceURI) && nodeLocalName.equals(localName);
  131.     }
  132.  
  133.     protected int getDomChildCount(int type, String namespaceURI, String name) {
  134.         switch (type) {
  135.         case Attribute:
  136.             return ((org.w3c.dom.Element)domNode).hasAttributeNS(namespaceURI, name) ? 1 : 0;
  137.  
  138.         case Element:
  139.             org.w3c.dom.NodeList elements = domNode.getChildNodes();
  140.             int length = elements.getLength();
  141.             int count = 0;
  142.  
  143.             for (int i = 0; i < length; i++) {
  144.                 org.w3c.dom.Node child = elements.item(i);
  145.                 if (domNodeNameEquals(child, namespaceURI, name))
  146.                     count++;
  147.             }
  148.             return count;
  149.  
  150.         default:
  151.             throw new XmlException("Unknown type");
  152.         }
  153.     }
  154.  
  155.     protected boolean hasDomChild(int type, String namespaceURI, String name) {
  156.         switch (type) {
  157.         case Attribute:
  158.             return ((org.w3c.dom.Element)domNode).hasAttributeNS(namespaceURI, name) ? true : false;
  159.  
  160.         case Element:
  161.             org.w3c.dom.NodeList elements = domNode.getChildNodes();
  162.             int length = elements.getLength();
  163.             for (int i = 0; i < length; i++)
  164.                 if (domNodeNameEquals(elements.item(i), namespaceURI, name))
  165.                     return true;
  166.             return false;
  167.  
  168.         default:
  169.             throw new XmlException("Unknown type");
  170.         }
  171.     }
  172.  
  173.     protected org.w3c.dom.Node getDomChildAt(int type, String namespaceURI, String name, int index) {
  174.         int length, count = 0;
  175.  
  176.         switch (type) {
  177.         case Attribute:
  178.             return domNode.getAttributes().getNamedItemNS(namespaceURI, name);
  179.  
  180.         case Element:
  181.             org.w3c.dom.NodeList elements = domNode.getChildNodes();
  182.             length = elements.getLength();
  183.             for (int i = 0; i < length; i++) {
  184.                 org.w3c.dom.Node child = elements.item(i);
  185.                 if (domNodeNameEquals(child, namespaceURI, name) && count++ == index)
  186.                     return child;
  187.             }
  188.             throw new XmlException("Index out of range");
  189.  
  190.         default:
  191.             throw new XmlException("Unknown type");
  192.         }
  193.     }
  194.  
  195.     protected org.w3c.dom.Node getDomChild(int type, String namespaceURI, String name) {
  196.         return getDomChildAt(type, namespaceURI, name, 0);
  197.     }
  198.  
  199.     protected org.w3c.dom.Node insertDomChildAt(int type, String namespaceURI, String name, int index, String value) {
  200.         if (type == Attribute) {
  201.             return appendDomChild(type, namespaceURI, name, value);
  202.         } else {
  203.             org.w3c.dom.Element element = domNode.getOwnerDocument().createElementNS(namespaceURI, name);
  204.             element.appendChild(domNode.getOwnerDocument().createTextNode(value));
  205.             return domNode.insertBefore(element, getDomChildAt(Element, namespaceURI, name, index));
  206.         }
  207.     }
  208.  
  209.     protected org.w3c.dom.Node insertDomElementAt(String namespaceURI, String name, int index, com.altova.xml.Node srcNode) {
  210.         srcNode.domNode = domNode.insertBefore(
  211.                 cloneDomElementAs(namespaceURI, name, srcNode),
  212.                 getDomChildAt(Element, namespaceURI, name, index)
  213.                 );
  214.         return srcNode.domNode;
  215.     }
  216.  
  217.     protected org.w3c.dom.Node replaceDomChildAt(int type, String namespaceURI, String name, int index, String value) {
  218.         if (type == Attribute) {
  219.             return appendDomChild(type, namespaceURI, name, value);
  220.         } else {
  221.             org.w3c.dom.Element element = domNode.getOwnerDocument().createElementNS(namespaceURI, name);
  222.             element.appendChild(domNode.getOwnerDocument().createTextNode(value));
  223.             return domNode.replaceChild(element, getDomChildAt(Element, namespaceURI, name, index));
  224.         }
  225.     }
  226.  
  227.     protected org.w3c.dom.Node replaceDomElementAt(String namespaceURI, String name, int index, com.altova.xml.Node srcNode) {
  228.         srcNode.domNode = domNode.replaceChild(
  229.                 cloneDomElementAs(namespaceURI, name, srcNode),
  230.                 getDomChildAt(Element, namespaceURI, name, index)
  231.                 );
  232.         return srcNode.domNode;
  233.     }
  234.  
  235.     protected org.w3c.dom.Node setDomChildAt(int type, String namespaceURI, String name, String value, int index) {
  236.         int length, count = 0;
  237.  
  238.         switch (type) {
  239.         case Attribute:
  240.             org.w3c.dom.Attr oldAttr = ((org.w3c.dom.Element)domNode).getAttributeNodeNS(namespaceURI, name);
  241.             ((org.w3c.dom.Element)domNode).setAttributeNS(namespaceURI, name, value);
  242.             return oldAttr;
  243.  
  244.         case Element:
  245.             org.w3c.dom.NodeList elements = domNode.getChildNodes();
  246.             length = elements.getLength();
  247.             for (int i = 0; i < length; i++) {
  248.                 org.w3c.dom.Node child = elements.item(i);
  249.                 if (domNodeNameEquals(child, namespaceURI, name) && count++ == index)
  250.                     return child.replaceChild(child.getOwnerDocument().createTextNode(value), child.getFirstChild());
  251.             }
  252.             throw new XmlException("Index out of range");
  253.  
  254.         default:
  255.             throw new XmlException("Unknown type");
  256.         }
  257.     }
  258.  
  259.     protected org.w3c.dom.Node setDomChild(int type, String namespaceURI, String name, String value) {
  260.         if (type == Attribute || getDomChildCount(type, namespaceURI, name) > 0) {
  261.             return setDomChildAt(type, namespaceURI, name, value, 0);
  262.         } else {
  263.             appendDomChild(type, namespaceURI, name, value);
  264.             return null;
  265.         }
  266.     }
  267.  
  268.     protected org.w3c.dom.Node removeDomChildAt(int type, String namespaceURI, String name, int index) {
  269.         int length, count = 0;
  270.  
  271.         switch (type) {
  272.         case Attribute:
  273.             return domNode.getAttributes().removeNamedItemNS(namespaceURI, name);
  274.  
  275.         case Element:
  276.             org.w3c.dom.NodeList elements = domNode.getChildNodes();
  277.             length = elements.getLength();
  278.             for (int i = 0; i < length; i++) {
  279.                 org.w3c.dom.Node child = elements.item(i);
  280.                 if (domNodeNameEquals(child, namespaceURI, name) && count++ == index)
  281.                     return domNode.removeChild(child);
  282.             }
  283.             throw new XmlException("Index out of range");
  284.  
  285.         default:
  286.             throw new XmlException("Unknown type");
  287.         }
  288.     }
  289.  
  290.     protected org.w3c.dom.Node appendDomElement(String namespaceURI, String name, com.altova.xml.Node srcNode) {
  291.         srcNode.domNode = domNode.appendChild(cloneDomElementAs(namespaceURI, name, srcNode));
  292.         return srcNode.domNode;
  293.     }
  294.  
  295.     protected org.w3c.dom.Element cloneDomElementAs(String namespaceURI, String name, com.altova.xml.Node srcNode) {
  296.         org.w3c.dom.Element newDomNode = domNode.getOwnerDocument().createElementNS(namespaceURI, name);
  297.         org.w3c.dom.Element srcDomNode = (org.w3c.dom.Element)srcNode.domNode;
  298.         org.w3c.dom.Document doc = newDomNode.getOwnerDocument();
  299.  
  300.         org.w3c.dom.NodeList list = srcDomNode.getChildNodes();
  301.         int length = list.getLength();
  302.         for (int i = 0; i < length; i++)
  303.             newDomNode.appendChild(doc.importNode(list.item(i), true));
  304.  
  305.         org.w3c.dom.NamedNodeMap srcAttributes = srcDomNode.getAttributes();
  306.         org.w3c.dom.NamedNodeMap newAttributes = newDomNode.getAttributes();
  307.         length = srcAttributes.getLength();
  308.         for (int i = 0; i < length; i++)
  309.             newAttributes.setNamedItemNS((org.w3c.dom.Attr)doc.importNode(srcAttributes.item(i), false));
  310.  
  311.         return newDomNode;
  312.     }
  313.  
  314.     protected void cloneInto(org.w3c.dom.Element newDomNode) {
  315.         while (domNode.getFirstChild() != null) {
  316.             org.w3c.dom.Node n = newDomNode.getOwnerDocument().importNode(domNode.getFirstChild(), true);
  317.             newDomNode.appendChild(n);
  318.             domNode.removeChild(domNode.getFirstChild());
  319.         }
  320.  
  321.         org.w3c.dom.NamedNodeMap srcAttributes = ((org.w3c.dom.Element)domNode).getAttributes();
  322.         org.w3c.dom.NamedNodeMap newAttributes = newDomNode.getAttributes();
  323.         while (srcAttributes.getLength() > 0) {
  324.             org.w3c.dom.Node n = srcAttributes.item(0);
  325.             newAttributes.setNamedItem(newDomNode.getOwnerDocument().importNode(n, true));
  326.             srcAttributes.removeNamedItem(n.getNodeName());
  327.         }
  328.  
  329.         domNode = newDomNode;
  330.     }
  331.  
  332.     protected static String lookupPrefix(org.w3c.dom.Node node, String URI) {
  333.         if (node == null || URI == null || URI.equals(""))
  334.             return null;
  335.  
  336.         if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
  337.             org.w3c.dom.NamedNodeMap attrs = node.getAttributes();
  338.             if (attrs != null) {
  339.                 int len = attrs.getLength();
  340.                 for (int i = 0; i < len; i++) {
  341.                     org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
  342.                     String name = attr.getName();
  343.                     String value = attr.getValue();
  344.                     if (value != null && value.equals(URI)) {
  345.                         if (name.startsWith("xmlns:"))
  346.                             return name.substring(6);
  347.                         else
  348.                             return null;
  349.                     }
  350.                 }
  351.             }
  352.             return lookupPrefix(node.getParentNode(), URI);
  353.         } else if (node.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE) {
  354.             return lookupPrefix(((org.w3c.dom.Attr)node).getOwnerElement(), URI);
  355.         } else {
  356.             return null;
  357.         }
  358.     }
  359.  
  360.     protected static void internalAdjustPrefix(org.w3c.dom.Node node, boolean qualified) {
  361.         if (node != null && qualified) {
  362.             String prefix = lookupPrefix(node, node.getNamespaceURI());
  363.             if (prefix != null)
  364.                 node.setPrefix(prefix);
  365.         }
  366.     }
  367.  
  368.     public abstract void adjustPrefix();
  369. }
  370.