jaxp/src/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2DOM.java
changeset 12458 d601e4bba306
parent 12457 c348e06f0e82
child 21469 976a949a4b16
equal deleted inserted replaced
12457:c348e06f0e82 12458:d601e4bba306
    47 import org.xml.sax.ext.Locator2;
    47 import org.xml.sax.ext.Locator2;
    48 
    48 
    49 /**
    49 /**
    50  * @author G. Todd Miller
    50  * @author G. Todd Miller
    51  * @author Sunitha Reddy
    51  * @author Sunitha Reddy
       
    52  * @author Huizhe Wang
    52  */
    53  */
    53 public class SAX2DOM implements ContentHandler, LexicalHandler, Constants {
    54 public class SAX2DOM implements ContentHandler, LexicalHandler, Constants {
    54 
    55 
    55     private Node _root = null;
    56     private Node _root = null;
    56     private Document _document = null;
    57     private Document _document = null;
    67     /**
    68     /**
    68      * JAXP document builder factory. Create a single instance and use
    69      * JAXP document builder factory. Create a single instance and use
    69      * synchronization because the Javadoc is not explicit about
    70      * synchronization because the Javadoc is not explicit about
    70      * thread safety.
    71      * thread safety.
    71      */
    72      */
    72     static final DocumentBuilderFactory _factory =
    73     private DocumentBuilderFactory _factory =
    73             DocumentBuilderFactory.newInstance();
    74             DocumentBuilderFactory.newInstance();
    74     static final DocumentBuilder _internalBuilder;
    75     private boolean _internal = true;
    75     static {
    76 
    76         DocumentBuilder tmpBuilder = null;
    77     public SAX2DOM(boolean useServicesMachnism) throws ParserConfigurationException {
    77         try {
    78         _document = createDocument(useServicesMachnism);
    78             if (_factory instanceof com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl) {
       
    79                 tmpBuilder = _factory.newDocumentBuilder();
       
    80             }
       
    81         } catch(Exception e) {
       
    82             // It's OK. Will create DocumentBuilder every time
       
    83         }
       
    84         _internalBuilder = tmpBuilder;
       
    85     }
       
    86 
       
    87     public SAX2DOM() throws ParserConfigurationException {
       
    88         _document = createDocument();
       
    89         _root = _document;
    79         _root = _document;
    90     }
    80     }
    91 
    81 
    92     public SAX2DOM(Node root, Node nextSibling) throws ParserConfigurationException {
    82     public SAX2DOM(Node root, Node nextSibling, boolean useServicesMachnism) throws ParserConfigurationException {
    93         _root = root;
    83         _root = root;
    94         if (root instanceof Document) {
    84         if (root instanceof Document) {
    95           _document = (Document)root;
    85           _document = (Document)root;
    96         }
    86         }
    97         else if (root != null) {
    87         else if (root != null) {
    98           _document = root.getOwnerDocument();
    88           _document = root.getOwnerDocument();
    99         }
    89         }
   100         else {
    90         else {
   101           _document = createDocument();
    91           _document = createDocument(useServicesMachnism);
   102           _root = _document;
    92           _root = _document;
   103         }
    93         }
   104 
    94 
   105         _nextSibling = nextSibling;
    95         _nextSibling = nextSibling;
   106     }
    96     }
   107 
    97 
   108     public SAX2DOM(Node root) throws ParserConfigurationException {
    98     public SAX2DOM(Node root, boolean useServicesMachnism) throws ParserConfigurationException {
   109         this(root, null);
    99         this(root, null, useServicesMachnism);
   110     }
   100     }
   111 
   101 
   112     public Node getDOM() {
   102     public Node getDOM() {
   113         return _root;
   103         return _root;
   114     }
   104     }
   316     public void endDTD() { }
   306     public void endDTD() { }
   317     public void endEntity(String name) { }
   307     public void endEntity(String name) { }
   318     public void startDTD(String name, String publicId, String systemId)
   308     public void startDTD(String name, String publicId, String systemId)
   319         throws SAXException {}
   309         throws SAXException {}
   320 
   310 
   321     private static Document createDocument() throws ParserConfigurationException {
   311     private Document createDocument(boolean useServicesMachnism) throws ParserConfigurationException {
       
   312         if (_factory == null) {
       
   313             if (useServicesMachnism)
       
   314                 _factory = DocumentBuilderFactory.newInstance();
       
   315                 if (!(_factory instanceof com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl)) {
       
   316                     _internal = false;
       
   317                 }
       
   318             else
       
   319                 _factory = DocumentBuilderFactory.newInstance(
       
   320                   "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl",
       
   321                   SAX2DOM.class.getClassLoader()
       
   322                   );
       
   323         }
   322         Document doc;
   324         Document doc;
   323         if (_internalBuilder != null) {
   325         if (_internal) {
   324             //default implementation is thread safe
   326             //default implementation is thread safe
   325             doc = _internalBuilder.newDocument();
   327             doc = _factory.newDocumentBuilder().newDocument();
   326         } else {
   328         } else {
   327             synchronized(SAX2DOM.class) {
   329             synchronized(SAX2DOM.class) {
   328                 doc = _factory.newDocumentBuilder().newDocument();
   330                 doc = _factory.newDocumentBuilder().newDocument();
   329             }
   331             }
   330         }
   332         }