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

  1. /**
  2.  * Document.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. import java.util.*;
  17.  
  18.  
  19. public abstract class Document implements java.io.Serializable {
  20.     protected static javax.xml.parsers.DocumentBuilderFactory    factory        = null;
  21.     protected static javax.xml.parsers.DocumentBuilder            builder        = null;
  22.     protected static org.w3c.dom.Document                        tmpDocument    = null;
  23.     protected static org.w3c.dom.DocumentFragment                tmpFragment    = null;
  24.     protected static int                                        tmpNameCounter = 0;
  25.  
  26.     protected static synchronized javax.xml.parsers.DocumentBuilder getDomBuilder() {
  27.         try {
  28.             if (builder == null) {
  29.                 if (factory == null) {
  30.                     factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
  31.                     factory.setIgnoringElementContentWhitespace(true);
  32.                     factory.setNamespaceAware(true);
  33.                     //factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
  34.                     //factory.setValidating(true);
  35.                 }
  36.                 builder = factory.newDocumentBuilder();
  37.  
  38.                 builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
  39.                     public void warning(org.xml.sax.SAXParseException e) {
  40.                     }
  41.                     public void error(org.xml.sax.SAXParseException e) throws XmlException {
  42.                         throw new XmlException(e);
  43.                     }
  44.                     public void fatalError(org.xml.sax.SAXParseException e) throws XmlException {
  45.                         throw new XmlException(e);
  46.                     }
  47.                 });
  48.             }
  49.             return builder;
  50.         } catch (javax.xml.parsers.ParserConfigurationException e) {
  51.             throw new XmlException(e);
  52.         }
  53.     }
  54.  
  55.     protected static synchronized org.w3c.dom.Document getTemporaryDocument() {
  56.         if (tmpDocument == null)
  57.             tmpDocument = getDomBuilder().newDocument();
  58.         return tmpDocument;
  59.     }
  60.  
  61.     protected static synchronized org.w3c.dom.Node createTemporaryDomNode() {
  62.         String tmpName = "_" + tmpNameCounter++;
  63.         if (tmpFragment == null) {
  64.             tmpFragment = getTemporaryDocument().createDocumentFragment();
  65.             tmpDocument.appendChild(tmpFragment);
  66.         }
  67.         org.w3c.dom.Node node = getTemporaryDocument().createElement(tmpName);
  68.         tmpFragment.appendChild(node);
  69.         return node;
  70.     }
  71.  
  72.     protected String rootElementName    = null;
  73.     protected String namespaceURI        = null;
  74.     protected String schemaLocation        = null;
  75.  
  76.     public Document() {
  77.     }
  78.  
  79.     public void setRootElementName(String namespaceURI, String rootElementName) {
  80.         this.namespaceURI        = namespaceURI;
  81.         this.rootElementName    = rootElementName;
  82.     }
  83.  
  84.     public void setSchemaLocation(String schemaLocation) {
  85.         this.schemaLocation = schemaLocation;
  86.     }
  87.  
  88.     public org.w3c.dom.Node load(String filename) {
  89.         try {
  90.             return getDomBuilder().parse(new java.io.File(filename)).getDocumentElement();
  91.         } catch (org.xml.sax.SAXException e) {
  92.             throw new XmlException(e);
  93.         } catch (java.io.IOException e) {
  94.             throw new XmlException(e);
  95.         }
  96.     }
  97.  
  98.     public org.w3c.dom.Node load(java.io.InputStream istream) {
  99.         try {
  100.             return getDomBuilder().parse(istream).getDocumentElement();
  101.         } catch (org.xml.sax.SAXException e) {
  102.             throw new XmlException(e);
  103.         } catch (java.io.IOException e) {
  104.             throw new XmlException(e);
  105.         }
  106.     }
  107.  
  108.     public void save(String filename, Node node) {
  109.         finalizeRootElement(node);
  110.  
  111.         Node.internalAdjustPrefix(node.domNode, true);
  112.         node.adjustPrefix();
  113.  
  114.         internalSave(
  115.                 new javax.xml.transform.stream.StreamResult(
  116.                         new java.io.File(filename)
  117.                         ),
  118.                 node.domNode.getOwnerDocument()
  119.                 );
  120.     }
  121.  
  122.     public void save(java.io.OutputStream ostream, Node node) {
  123.         finalizeRootElement(node);
  124.  
  125.         Node.internalAdjustPrefix(node.domNode, true);
  126.         node.adjustPrefix();
  127.  
  128.         internalSave(
  129.                 new javax.xml.transform.stream.StreamResult(ostream),
  130.                 node.domNode.getOwnerDocument()
  131.                 );
  132.     }
  133.  
  134.     protected static void internalSave(javax.xml.transform.Result result, org.w3c.dom.Document doc) {
  135.         try {
  136.             javax.xml.transform.Source source
  137.                     = new javax.xml.transform.dom.DOMSource(doc);
  138.             javax.xml.transform.Transformer transformer
  139.                     = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
  140.             transformer.transform(source, result);
  141.         } catch (javax.xml.transform.TransformerConfigurationException e) {
  142.             throw new XmlException(e);
  143.         } catch (javax.xml.transform.TransformerException e) {
  144.             throw new XmlException(e);
  145.         }
  146.     }
  147.  
  148.     public org.w3c.dom.Node transform(Node node, String xslFilename) {
  149.         try {
  150.             javax.xml.transform.TransformerFactory factory = javax.xml.transform.TransformerFactory.newInstance();
  151.             javax.xml.transform.Transformer transformer = factory.newTransformer(
  152.                     new javax.xml.transform.stream.StreamSource(xslFilename)
  153.                     );
  154.  
  155.             javax.xml.transform.dom.DOMResult result = new javax.xml.transform.dom.DOMResult();
  156.             transformer.transform(
  157.                     new javax.xml.transform.dom.DOMSource(node.domNode),
  158.                     result
  159.                     );
  160.  
  161.             return result.getNode();
  162.         } catch (javax.xml.transform.TransformerException e) {
  163.             throw new XmlException(e);
  164.         }
  165.     }
  166.  
  167.     protected void finalizeRootElement(Node root) {
  168.         if (root.domNode.getParentNode().getNodeType() != org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE)
  169.             return;
  170.  
  171.         if (rootElementName == null || rootElementName.equals(""))
  172.             throw new XmlException("Call setRootElementName first");
  173.  
  174.         org.w3c.dom.Document doc = getDomBuilder().newDocument();
  175.         org.w3c.dom.Element newRootElement = doc.createElementNS(namespaceURI, rootElementName);
  176.         root.cloneInto(newRootElement);
  177.         doc.appendChild(newRootElement);
  178.  
  179.         newRootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
  180.         if (namespaceURI == null || namespaceURI.equals("")) {
  181.             if (schemaLocation != null && schemaLocation != "")
  182.                 newRootElement.setAttribute("xsi:noNamespaceSchemaLocation", schemaLocation);
  183.         } else {
  184.             if (schemaLocation != null && schemaLocation != "")
  185.                 newRootElement.setAttribute("xsi:schemaLocation", namespaceURI + " " + schemaLocation);
  186.         }
  187.  
  188.         root.domNode = newRootElement;
  189.         declareNamespaces(root);
  190.     }
  191.  
  192.     public abstract void declareNamespaces(Node node);
  193.  
  194.     protected void declareNamespace(Node node, String prefix, String URI) {
  195.         node.declareNamespace(prefix, URI);
  196.     }
  197. }
  198.