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

  1. using System;
  2. using System.Xml;
  3.  
  4. namespace Altova
  5. {
  6.     public abstract class Document
  7.     {
  8.         protected static XmlDocument            tmpDocument        = null;
  9.         protected static XmlDocumentFragment    tmpFragment        = null;
  10.         protected static int                    tmpNameCounter    = 0;
  11.  
  12.         protected static XmlDocument GetTemporaryDocument()
  13.         {
  14.             if (tmpDocument == null)
  15.                 tmpDocument = new XmlDocument();
  16.             return tmpDocument;
  17.         }
  18.  
  19.         public static XmlNode CreateTemporaryDomNode()
  20.         {
  21.             string tmpName = "_" + tmpNameCounter++;
  22.             if (tmpFragment == null)
  23.             {
  24.                 tmpFragment = GetTemporaryDocument().CreateDocumentFragment();
  25.                 tmpDocument.AppendChild(tmpFragment);
  26.             }
  27.  
  28.             XmlNode node = GetTemporaryDocument().CreateElement(tmpName);
  29.             tmpFragment.AppendChild(node);
  30.             return node;
  31.         }
  32.  
  33.         protected string rootElementName    = null;
  34.         protected string namespaceURI        = null;
  35.         protected string schemaLocation        = null;
  36.  
  37.         public Document()
  38.         {
  39.         }
  40.  
  41.         public void SetRootElementName(string namespaceURI, string rootElementName)
  42.         {
  43.             this.namespaceURI = namespaceURI;
  44.             this.rootElementName = rootElementName;
  45.         }
  46.  
  47.         public void SetSchemaLocation(string schemaLocation)
  48.         {
  49.             this.schemaLocation = schemaLocation;
  50.         }
  51.  
  52.         public XmlNode Load(XmlReader reader)
  53.         {
  54.             XmlDocument doc = new XmlDocument();
  55.             doc.Load(reader);
  56.             return doc.DocumentElement;
  57.         }
  58.  
  59.         public XmlNode Load(string filename)
  60.         {
  61.             XmlDocument doc = new XmlDocument();
  62.             doc.Load(filename);
  63.             return doc.DocumentElement;
  64.         }
  65.  
  66.         public void Save(string filename, Node node)
  67.         {
  68.             FinalizeRootElement(node);
  69.             Node.InternalAdjustPrefix(node.domNode, true);
  70.             node.AdjustPrefix();
  71.             node.domNode.OwnerDocument.Save(filename);
  72.         }
  73.  
  74.         abstract protected void DeclareNamespaces(Node node);
  75.  
  76.         protected void DeclareNamespace(Node node, string prefix, string URI)
  77.         {
  78.             node.DeclareNamespace(prefix, URI);
  79.         }
  80.  
  81.         protected void FinalizeRootElement(Node node)
  82.         {
  83.             if (node.domNode.ParentNode.NodeType != XmlNodeType.DocumentFragment)
  84.                 return;
  85.  
  86.             if (rootElementName == null || rootElementName == "")
  87.                 throw new Exception("Call SetRootElementName first");
  88.  
  89.             node.MakeRoot(namespaceURI, rootElementName, schemaLocation);
  90.             DeclareNamespaces(node);
  91.         }
  92.     }
  93. }
  94.