< prev index next >

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/dom/XSLTCDTMManager.java

Print this page
rev 1025 : 8023653: [JAXP] xalan inconsistently parses DOMSource and StreamSource

@@ -1,93 +1,133 @@
 /*
- * reserved comment block
- * DO NOT REMOVE OR ALTER!
+ * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
  */
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * $Id: XSLTCDTMManager.java,v 1.2 2005/08/16 22:32:54 jeffsuttor Exp $
- */
 package com.sun.org.apache.xalan.internal.xsltc.dom;
 
-import javax.xml.stream.XMLEventReader;
-import javax.xml.stream.XMLStreamReader;
-import javax.xml.transform.Source;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.sax.SAXSource;
-import javax.xml.transform.stream.StreamSource;
-import javax.xml.transform.stax.StAXSource;
-
 import com.sun.org.apache.xml.internal.dtm.DTM;
 import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase;
 import com.sun.org.apache.xml.internal.dtm.DTMException;
 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
 import com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault;
 import com.sun.org.apache.xml.internal.res.XMLErrorResources;
 import com.sun.org.apache.xml.internal.res.XMLMessages;
 import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
+import com.sun.org.apache.xml.internal.utils.XMLReaderManager;
+import com.sun.org.apache.xalan.internal.xsltc.runtime.Constants;
 import com.sun.org.apache.xalan.internal.xsltc.trax.DOM2SAX;
 import com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX;
 import com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX;
-
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.stax.StAXSource;
 import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
 import org.xml.sax.SAXNotRecognizedException;
 import org.xml.sax.SAXNotSupportedException;
 import org.xml.sax.XMLReader;
 
 /**
- * The default implementation for the DTMManager.
+ * The XSLTC DTMManager implementation.
  */
-public class XSLTCDTMManager extends DTMManagerDefault
-{
-
-    /** Set this to true if you want a dump of the DTM after creation */
-    private static final boolean DUMPTREE = false;
+public class XSLTCDTMManager extends DTMManagerDefault {
 
-    /** Set this to true if you want basic diagnostics */
-    private static final boolean DEBUG = false;
+    /**
+     * To enable switching namespace awareness of SAX Reader for stream sources.
+     */
+    protected boolean m_namespaceAware = true;
 
     /**
-     * Constructor DTMManagerDefault
+     * Constructor XSLTCDTMManager
      *
      */
-    public XSLTCDTMManager()
-    {
+    public XSLTCDTMManager() {
         super();
     }
 
     /**
      * Obtain a new instance of a <code>DTMManager</code>.
      * This static method creates a new factory instance.
      * The current implementation just returns a new XSLTCDTMManager instance.
      */
-    public static XSLTCDTMManager newInstance()
-    {
+    public static XSLTCDTMManager newInstance() {
         return new XSLTCDTMManager();
     }
 
     /**
      * Creates a new instance of the XSLTC DTM Manager service.
      * Creates a new instance of the default class
      * <code>com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager</code>.
      */
-      public static XSLTCDTMManager createNewDTMManagerInstance() {
-         return newInstance();
-      }
+    public static XSLTCDTMManager createNewDTMManagerInstance() {
+        return newInstance();
+    }
+
+    /**
+     * Configure namespace awareness
+     * This is used when creating the default SAX XMLReader.
+     */
+    public void setNamespaceAware(boolean namespaceAware) {
+        m_namespaceAware = namespaceAware;
+    }
+
+    /**
+     * This method returns the SAX2 parser to use with the InputSource
+     * obtained from this URI.
+     * It may return null if any SAX2-conformant XML parser can be used,
+     * or if getInputSource() will also return null. The parser must
+     * be free for use (i.e., not currently in use for another parse().
+     * After use of the parser is completed, the releaseXMLReader(XMLReader)
+     * must be called.
+     *
+     * @param inputSource The value returned from the URIResolver.
+     * @return  a SAX2 XMLReader to use to resolve the inputSource argument.
+     *
+     * @return non-null XMLReader reference ready to parse.
+     */
+    @Override
+    synchronized public XMLReader getXMLReader(Source inputSource) {
+        try {
+            XMLReader reader = (inputSource instanceof SAXSource) ?
+                ((SAXSource)inputSource).getXMLReader() : null;
+
+            // if user did not supply a reader, ask for one from the reader manager
+            if (null == reader) {
+                if (m_readerManager == null) {
+                    m_readerManager = XMLReaderManager.getInstance(super.useServicesMechnism());
+              }
+
+              if (!m_namespaceAware)
+                  m_readerManager.setFeature(Constants.NAMESPACE_FEATURE, false);
+
+              reader = m_readerManager.getXMLReader();
+            }
+
+            return reader;
+        } catch (SAXException se) {
+            throw new DTMException(se.getMessage(), se);
+        }
+    }
 
     /**
      * Get an instance of a DTM, loaded with the content from the
      * specified source.  If the unique flag is true, a new instance will
      * always be returned.  Otherwise it is up to the DTMManager to return a

@@ -169,20 +209,20 @@
      * @param newNameTable true if we want to use a separate ExpandedNameTable
      *                     for this DTM.
      *
      * @return a non-null DTM reference.
      */
-  public DTM getDTM(Source source, boolean unique,
-                    DTMWSFilter whiteSpaceFilter, boolean incremental,
-                    boolean doIndexing, boolean buildIdIndex,
-                    boolean newNameTable)
-  {
-    return getDTM(source, unique, whiteSpaceFilter, incremental,
-                  doIndexing, false, 0, buildIdIndex, newNameTable);
-  }
+    public DTM getDTM(Source source, boolean unique,
+                      DTMWSFilter whiteSpaceFilter, boolean incremental,
+                      boolean doIndexing, boolean buildIdIndex,
+                      boolean newNameTable)
+    {
+        return getDTM(source, unique, whiteSpaceFilter, incremental,
+                      doIndexing, false, 0, buildIdIndex, newNameTable);
+    }
 
-  /**
+    /**
      * Get an instance of a DTM, loaded with the content from the
      * specified source.  If the unique flag is true, a new instance will
      * always be returned.  Otherwise it is up to the DTMManager to return a
      * new instance or an instance that it already created and may be being used
      * by someone else.

@@ -210,16 +250,16 @@
     public DTM getDTM(Source source, boolean unique,
                       DTMWSFilter whiteSpaceFilter, boolean incremental,
                       boolean doIndexing, boolean hasUserReader, int size,
                       boolean buildIdIndex)
     {
-      return getDTM(source, unique, whiteSpaceFilter, incremental,
-                    doIndexing, hasUserReader, size,
-                    buildIdIndex, false);
-  }
+        return getDTM(source, unique, whiteSpaceFilter, incremental,
+                      doIndexing, hasUserReader, size,
+                      buildIdIndex, false);
+    }
 
-  /**
+    /**
      * Get an instance of a DTM, loaded with the content from the
      * specified source.  If the unique flag is true, a new instance will
      * always be returned.  Otherwise it is up to the DTMManager to return a
      * new instance or an instance that it already created and may be being used
      * by someone else.

@@ -244,20 +284,22 @@
      * @param newNameTable true if we want to use a separate ExpandedNameTable
      *                     for this DTM.
      *
      * @return a non-null DTM reference.
      */
-  public DTM getDTM(Source source, boolean unique,
-                    DTMWSFilter whiteSpaceFilter, boolean incremental,
-                    boolean doIndexing, boolean hasUserReader, int size,
-                    boolean buildIdIndex, boolean newNameTable)
-  {
-        if(DEBUG && null != source) {
-            System.out.println("Starting "+
-                         (unique ? "UNIQUE" : "shared")+
-                         " source: "+source.getSystemId());
+    public DTM getDTM(Source source, boolean unique,
+                      DTMWSFilter whiteSpaceFilter, boolean incremental,
+                      boolean doIndexing, boolean hasUserReader, int size,
+                      boolean buildIdIndex, boolean newNameTable)
+    {
+        /* Uncomment for debugging
+        if (null != source) {
+            System.out.println("Starting " +
+                (unique ? "UNIQUE" : "shared") +
+                " source: " + source.getSystemId());
         }
+        */
 
         int dtmPos = getFirstFreeDTMID();
         int documentID = dtmPos << IDENT_DTM_NODE_BITS;
 
         if ((null != source) && source instanceof StAXSource) {

@@ -296,21 +338,18 @@
                 }
                 else if (staxStream2SAX != null) {
                     staxStream2SAX.setContentHandler(dtm);
                     staxStream2SAX.parse();
                 }
-
-            }
-            catch (RuntimeException re) {
+            } catch (RuntimeException re) {
                 throw re;
-            }
-            catch (Exception e) {
+            } catch (Exception e) {
                 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
             }
 
             return dtm;
-        }else if ((null != source) && source instanceof DOMSource) {
+        } else if ((null != source) && source instanceof DOMSource) {
             final DOMSource domsrc = (DOMSource) source;
             final org.w3c.dom.Node node = domsrc.getNode();
             final DOM2SAX dom2sax = new DOM2SAX(node);
 
             SAXImpl dtm;

@@ -332,22 +371,18 @@
 
             dom2sax.setContentHandler(dtm);
 
             try {
                 dom2sax.parse();
-            }
-            catch (RuntimeException re) {
+            } catch (RuntimeException re) {
                 throw re;
-            }
-            catch (Exception e) {
+            } catch (Exception e) {
                 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
             }
 
             return dtm;
-        }
-        else
-        {
+        } else {
             boolean isSAXSource = (null != source)
                                   ? (source instanceof SAXSource) : true;
             boolean isStreamSource = (null != source)
                                   ? (source instanceof StreamSource) : false;
 

@@ -429,14 +464,14 @@
                     if (!hasUserReader) {
                         releaseXMLReader(reader);
                     }
                 }
 
-                if (DUMPTREE) {
-                    System.out.println("Dumping SAX2DOM");
-                    dtm.dumpDTM(System.err);
-                }
+                /* Uncomment for debugging
+                System.out.println("Dumping SAX2DOM");
+                dtm.dumpDTM(System.err);
+                */
 
                 return dtm;
             }
             else {
                 // It should have been handled by a derived class or the caller
< prev index next >