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

  1. using System;
  2. using System.Xml;
  3.  
  4. namespace Altova
  5. {
  6.     /// <summary>
  7.     /// class Node
  8.     /// </summary>
  9.     public abstract class Node
  10.     {
  11.         protected enum NodeType { Attribute, Element };
  12.         protected internal XmlNode domNode;
  13.  
  14.         #region Constructors
  15.  
  16.         public Node()
  17.         {
  18.             domNode = Document.CreateTemporaryDomNode();
  19.         }
  20.  
  21.         public Node(XmlDocument doc)
  22.         {
  23.             domNode = doc.DocumentElement;
  24.         }
  25.  
  26.         public Node(XmlNode node)
  27.         {
  28.             domNode = node;
  29.         }
  30.  
  31.         public Node(Node node)
  32.         {
  33.             domNode = node.domNode;
  34.         }
  35.  
  36.         #endregion
  37.  
  38.         #region Utility methods
  39.  
  40.         public void MapPrefix(string prefix, string URI) 
  41.         {
  42.             XmlElement element = (XmlElement)domNode;
  43.             if (prefix == null || prefix == "")
  44.                 element.SetAttribute("xmlns", URI);
  45.             else
  46.                 element.SetAttribute("xmlns:" + prefix, URI);
  47.         }
  48.  
  49.         protected internal void DeclareNamespace(string prefix, string URI)
  50.         {
  51.             XmlElement root = domNode.OwnerDocument.DocumentElement;
  52.             XmlAttributeCollection attrs = root.Attributes;
  53.             if (attrs != null) 
  54.             {
  55.                 for (int i = 0; i < attrs.Count; i++) 
  56.                 {
  57.                     XmlAttribute attr = attrs\[i\];
  58.                     if (attr.Value == URI) // namespace URI already mapped?
  59.                         return; // do not overwrite
  60.                 }
  61.             }
  62.             if (prefix == null || prefix == "")
  63.                 root.SetAttribute("xmlns", URI);
  64.             else
  65.                 root.SetAttribute("xmlns:" + prefix, URI);
  66.         }
  67.  
  68.         protected static string GetDomNodeValue(XmlNode node)
  69.         {
  70.             return node.InnerText;
  71.         }
  72.  
  73.         protected static void SetDomNodeValue(XmlNode node, string Value)
  74.         {
  75.             node.InnerText = Value;
  76.         }
  77.  
  78.         protected XmlElement CloneDomElementAs(string URI, string name, Node node)
  79.         {
  80.             XmlElement srcElement = (XmlElement)node.domNode;
  81.             XmlElement dstElement = domNode.OwnerDocument.CreateElement(name, URI);
  82.             XmlDocument doc = domNode.OwnerDocument;
  83.  
  84.             foreach (XmlAttribute attribute in srcElement.Attributes)
  85.                 dstElement.Attributes.Append((XmlAttribute)doc.ImportNode(attribute, false));
  86.             foreach (XmlNode childNode in srcElement.ChildNodes)
  87.                 dstElement.AppendChild(doc.ImportNode(childNode, true));
  88.             return dstElement;
  89.         }
  90.  
  91.         protected internal void MakeRoot(string namespaceURI, string rootElementName, string schemaLocation)
  92.         {
  93.             XmlDocument doc = new XmlDocument();
  94.             XmlElement root = doc.CreateElement(rootElementName, namespaceURI);
  95.  
  96.             doc.AppendChild(doc.CreateProcessingInstruction("xml", "version=\\"1.0\\" encoding=\\"UTF-8\\""));
  97.             doc.AppendChild(root);
  98.  
  99.             root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
  100.             if (namespaceURI == null || namespaceURI == "") 
  101.             {
  102.                 if (schemaLocation != null && schemaLocation != "")
  103.                 {
  104.                     XmlAttribute a = doc.CreateAttribute("xsi:noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
  105.                     a.Value = schemaLocation;
  106.                     root.SetAttributeNode(a);
  107.                 }
  108.             } 
  109.             else 
  110.             {
  111.                 if (schemaLocation != null && schemaLocation != "") 
  112.                 {
  113.                     XmlAttribute a = doc.CreateAttribute("xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
  114.                     a.Value = namespaceURI + " " + schemaLocation;
  115.                     root.SetAttributeNode(a);
  116.                 }
  117.             }
  118.  
  119.             foreach (XmlAttribute attribute in domNode.Attributes)
  120.                 root.Attributes.Append((XmlAttribute)doc.ImportNode(attribute, true));
  121.             foreach (XmlNode childNode in domNode.ChildNodes)
  122.                 root.AppendChild(doc.ImportNode(childNode, true));
  123.  
  124.             domNode = root;
  125.         }
  126.  
  127.         #endregion
  128.  
  129.         #region Child node manipulation methods
  130.  
  131.         public XmlNode getDOMNode()
  132.         {
  133.             return domNode;
  134.         }
  135.  
  136.         protected bool HasDomChild(NodeType type, string URI, string name)
  137.         {
  138.             if (type == NodeType.Attribute)
  139.             {
  140.                 foreach (XmlAttribute attribute in domNode.Attributes)
  141.                     if (attribute.NamespaceURI == URI && attribute.LocalName == name)
  142.                         return true;
  143.                 return false;
  144.             }
  145.             else
  146.             {
  147.                 foreach (XmlNode node in domNode.ChildNodes)
  148.                     if (node.NamespaceURI == URI && node.LocalName == name)
  149.                         return true;
  150.                 return false;
  151.             }
  152.         }
  153.  
  154.         protected int DomChildCount(NodeType type, string URI, string name)
  155.         {
  156.             if (type == NodeType.Attribute)
  157.             {
  158.                 foreach (XmlAttribute attribute in domNode.Attributes)
  159.                     if (attribute.NamespaceURI == URI && attribute.LocalName == name)
  160.                         return 1;
  161.                 return 0;
  162.             }
  163.             else
  164.             {
  165.                 int count = 0;
  166.                 foreach (XmlNode node in domNode.ChildNodes)
  167.                     if (node.NamespaceURI == URI && node.LocalName == name)
  168.                         count++;
  169.                 return count;
  170.             }
  171.         }
  172.  
  173.         protected XmlNode AppendDomChild(NodeType type, string URI, string name, string Value)
  174.         {
  175.             if (type == NodeType.Attribute)
  176.             {
  177.                 XmlAttribute attribute = domNode.OwnerDocument.CreateAttribute(name, URI);
  178.                 attribute.Value = Value;
  179.                 domNode.Attributes.Append(attribute);
  180.                 return attribute;
  181.             }
  182.             else
  183.             {
  184.                 XmlElement element = domNode.OwnerDocument.CreateElement(name, URI);
  185.                 element.InnerText = Value;
  186.                 domNode.AppendChild(element);
  187.                 return element;
  188.             }
  189.         }
  190.  
  191.         protected XmlNode AppendDomElement(string URI, string name, Node node)
  192.         {
  193.             node.domNode = CloneDomElementAs(URI, name, node);
  194.             return domNode.AppendChild(node.domNode);
  195.         }
  196.  
  197.         protected XmlNode InsertDomChildAt(NodeType type, string URI, string name, int index, string Value)
  198.         {
  199.             if (type == NodeType.Attribute)
  200.             {
  201.                 return AppendDomChild(type, URI, name, Value);
  202.             }
  203.             else
  204.             {
  205.                 XmlElement element = domNode.OwnerDocument.CreateElement(name, URI);
  206.                 element.InnerText = Value;
  207.                 domNode.InsertBefore(element, GetDomChildAt(type, URI, name, index));
  208.                 return element;
  209.             }
  210.         }
  211.  
  212.         protected XmlNode InsertDomElementAt(string URI, string name, int index, Node node)
  213.         {
  214.             node.domNode = CloneDomElementAs(URI, name, node);
  215.             return domNode.InsertBefore(node.domNode, GetDomChildAt(NodeType.Element, URI, name, index));
  216.         }
  217.  
  218.         protected XmlNode GetDomChildAt(NodeType type, string URI, string name, int index)
  219.         {
  220.             if (type == NodeType.Attribute)
  221.             {
  222.                 if (index > 0)
  223.                     throw new Exception("Index out of range");
  224.                 foreach (XmlAttribute a in domNode.Attributes)
  225.                     if (a.NamespaceURI == URI && a.LocalName == name)
  226.                         return a;
  227.                 throw new Exception("Not found");
  228.             }
  229.             else
  230.             {
  231.                 int count = 0;
  232.                 foreach (XmlNode n in domNode.ChildNodes)
  233.                     if (n.NamespaceURI == URI && n.LocalName == name)
  234.                         if (count++ == index)
  235.                             return n;
  236.                 if (count > 0)
  237.                     throw new Exception("Index out of range");
  238.                 else
  239.                     throw new Exception("Not found");
  240.             }
  241.         }
  242.  
  243.         protected XmlNode ReplaceDomChildAt(NodeType type, string URI, string name, int index, string Value)
  244.         {
  245.             if (type == NodeType.Attribute)
  246.             {
  247.                 XmlAttribute attr = domNode.OwnerDocument.CreateAttribute(name, URI);
  248.                 attr.Value = Value;
  249.                 return domNode.Attributes.Append(attr);
  250.             }
  251.             else
  252.             {
  253.                 XmlElement elem = domNode.OwnerDocument.CreateElement(name, URI);
  254.                 elem.InnerText = Value;
  255.                 return domNode.ReplaceChild(elem, GetDomChildAt(type, URI, name, index));
  256.             }
  257.         }
  258.  
  259.         protected XmlNode ReplaceDomElementAt(string URI, string name, int index, Node node)
  260.         {
  261.             node.domNode = CloneDomElementAs(URI, name, node);
  262.             return domNode.ReplaceChild(node.domNode, GetDomChildAt(NodeType.Element, URI, name, index));
  263.         }
  264.  
  265.         protected XmlNode RemoveDomChildAt(NodeType type, string URI, string name, int index)
  266.         {
  267.             if (type == NodeType.Attribute)
  268.             {
  269.                 return domNode.Attributes.RemoveNamedItem(name, URI);
  270.             }
  271.             else
  272.             {
  273.                 return domNode.RemoveChild(GetDomChildAt(type, URI, name, index));
  274.             }
  275.         }
  276.  
  277.         protected static string LookupPrefix(XmlNode node, string URI) 
  278.         {
  279.             if (node == null || URI == null || URI == "")
  280.                 return null;
  281.  
  282.             if (node.NodeType == XmlNodeType.Element) 
  283.             {
  284.                 XmlAttributeCollection attrs = node.Attributes;
  285.                 if (attrs != null) 
  286.                 {
  287.                     int len = attrs.Count;
  288.                     for (int i = 0; i < len; i++) 
  289.                     {
  290.                         XmlAttribute attr = attrs\[i\];
  291.                         String name = attr.Name;
  292.                         String value = attr.Value;
  293.                         if (value != null && value == URI) 
  294.                         {
  295.                             if (name.StartsWith("xmlns:"))
  296.                                 return name.Substring(6);
  297.                             else
  298.                                 return null;
  299.                         }
  300.                     }
  301.                 }
  302.                 return LookupPrefix(node.ParentNode, URI);
  303.             } 
  304.             else if (node.NodeType == XmlNodeType.Attribute) 
  305.             {
  306.                 return LookupPrefix(node.ParentNode, URI);
  307.             } 
  308.             else 
  309.             {
  310.                 return null;
  311.             }
  312.         }
  313.  
  314.         protected internal static void InternalAdjustPrefix(XmlNode node, bool qualified) 
  315.         {
  316.             if (node != null && qualified) 
  317.             {
  318.                 string prefix = LookupPrefix(node, node.NamespaceURI);
  319.                 if (prefix != null)
  320.                     node.Prefix = prefix;
  321.             }
  322.         }
  323.  
  324.         public abstract void AdjustPrefix();
  325.  
  326.         #endregion
  327.     }
  328. }
  329.