You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by zo...@apache.org on 2003/12/04 21:58:38 UTC

cvs commit: xml-xalan/java/src/org/apache/xalan/xsltc/trax TransformerImpl.java TransformerFactoryImpl.java

zongaro     2003/12/04 12:58:38

  Modified:    java/src/org/apache/xalan/xsltc/trax Tag: xslt20-compiled
                        TransformerImpl.java TransformerFactoryImpl.java
  Log:
  Changes to permit different DTM implementations to be supplied to XSLTC.
  
  XSLTC will now use ObjectFactory to look up a new XSLTC DTM Manager service
  provider (org.apache.xalan.xsltc.dom.XSLTCDTMManager).  The provider is looked
  up once when a TransformerFactory is created, and used by any Transformer
  objects created by that TransformerFactory.
  
  Also, moved code for caching XMLReader objects from XSLTC's
  TransformerFactoryImpl to a new org.apache.xml.utils.XMLReaderManager class.
  
  It is now the responsibility of the DTMManagerDefault class to request one of
  these cached XMLReader objects, so the benefit of reusing an XMLReader is now
  conferred upon both XSLTC and Xalan-J Interpretive, as well as upon references
  to the document() function.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.71.2.4  +123 -215  xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerImpl.java
  
  Index: TransformerImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerImpl.java,v
  retrieving revision 1.71.2.3
  retrieving revision 1.71.2.4
  diff -u -r1.71.2.3 -r1.71.2.4
  --- TransformerImpl.java	22 Oct 2003 18:50:44 -0000	1.71.2.3
  +++ TransformerImpl.java	4 Dec 2003 20:58:37 -0000	1.71.2.4
  @@ -98,6 +98,7 @@
   
   import org.apache.xalan.xsltc.DOM;
   import org.apache.xalan.xsltc.DOMCache;
  +import org.apache.xalan.xsltc.DOMEnhancedForDTM;
   import org.apache.xalan.xsltc.StripFilter;
   import org.apache.xalan.xsltc.Translet;
   import org.apache.xalan.xsltc.TransletException;
  @@ -110,7 +111,9 @@
   import org.apache.xalan.xsltc.runtime.AbstractTranslet;
   import org.apache.xalan.xsltc.runtime.Hashtable;
   import org.apache.xalan.xsltc.runtime.output.TransletOutputHandlerFactory;
  +
   import org.apache.xml.dtm.DTMWSFilter;
  +import org.apache.xml.utils.XMLReaderManager;
   
   import org.xml.sax.ContentHandler;
   import org.xml.sax.InputSource;
  @@ -186,12 +189,11 @@
        * object belongs to.
        */
       private TransformerFactoryImpl _tfactory = null;
  -    
  +
       /**
  -     * A reference to the XSLTCDTMManager which is used to build the DOM/DTM
  -     * for this transformer.
  +     * A reference to an object that creates and caches XMLReader objects.
        */
  -    private XSLTCDTMManager _dtmManager = null;
  +    private XMLReaderManager _readerManager = XMLReaderManager.getInstance();
       
       /**
        * A flag indicating whether we use incremental building of the DTM.
  @@ -435,229 +437,136 @@
       /**
        * Builds an internal DOM from a TrAX Source object
        */
  -    private DOM getDOM(Source source)
  -	throws TransformerException {
  -	try {
  -	    DOM dom = null;
  -	    SAXImpl saxImpl = null;
  -	    DTMWSFilter wsfilter;
  -	    if (_translet != null && _translet instanceof StripFilter) {
  -	        wsfilter = new DOMWSFilter(_translet);
  -            } else {
  -	        wsfilter = null;
  -            }
  -            
  -            boolean hasIdCall = (_translet != null) ? _translet.hasIdCall() 
  -                                                      : false;
  -	    // Get systemId from source
  -	    if (source != null) {
  -                _sourceSystemId = source.getSystemId();
  -	    }
  -
  -	    if (source instanceof SAXSource) {
  -		// Get all info from the input SAXSource object
  -		final SAXSource sax = (SAXSource)source;
  -		XMLReader reader = sax.getXMLReader();
  -		final InputSource input = sax.getInputSource();
  -                final boolean hasUserReader = reader != null;
  -
  -		// Create a reader if not set by user
  -		if (reader == null) {
  -		    reader = _tfactory.getXMLReader();
  -		}
  -
  -		// Create a new internal DOM and set up its builder to trap
  -		// all content/lexical events
  -		if (_dtmManager == null) {
  -		    _dtmManager = XSLTCDTMManager.newInstance();
  -		}
  +    private DOM getDOM(Source source) throws TransformerException {
  +        try {
  +            DOM dom = null;
   
  -                //dtmManager.setIncremental(_isIncremental);
  -		saxImpl = (SAXImpl)_dtmManager.getDTM(sax, false, wsfilter, true, false,
  -                                                 hasUserReader, 0, hasIdCall);
  -		//final DOMBuilder builder = ((SAXImpl)dom).getBuilder();
  -		try {
  -		    reader.setProperty(LEXICAL_HANDLER_PROPERTY, saxImpl);
  -		}
  -		catch (SAXException e) {
  -		    // quitely ignored
  -		}
  -		reader.setContentHandler(saxImpl);
  -		reader.setDTDHandler(saxImpl);
  -		saxImpl.setDocumentURI(_sourceSystemId);
  -	    }
  -	    else if (source instanceof DOMSource) {
  -		// Create a new internal DTM and build it directly from DOM
  -		if (_dtmManager == null) {
  -		    _dtmManager = XSLTCDTMManager.newInstance();
  -		}
  -    
  -                //dtmManager.setIncremental(_isIncremental);
  -		saxImpl = (SAXImpl)_dtmManager.getDTM(source, false, wsfilter, true,
  -                                                 false, hasIdCall);
  -		saxImpl.setDocumentURI(_sourceSystemId);
  -	    }
  -	    // Handle StreamSource input
  -	    else if (source instanceof StreamSource) {
  -		// Get all info from the input StreamSource object
  -		final StreamSource stream = (StreamSource)source;
  -		final InputStream streamInput = stream.getInputStream();
  -		final Reader streamReader = stream.getReader();
  -		final XMLReader reader = _tfactory.getXMLReader();
  -
  -		// Create a new internal DOM and set up its builder to trap
  -		// all content/lexical events
  -		if (_dtmManager == null) {
  -		    _dtmManager = XSLTCDTMManager.newInstance();
  -		}
  +            if (source != null) {
  +                DTMWSFilter wsfilter;
  +                if (_translet != null && _translet instanceof StripFilter) {
  +                    wsfilter = new DOMWSFilter(_translet);
  +                 } else {
  +                    wsfilter = null;
  +                 }
  +            
  +                 boolean hasIdCall = (_translet != null) ? _translet.hasIdCall()
  +                                                         : false;
   
  -		//dtmManager.setIncremental(_isIncremental);
  -		
  -		InputSource input;
  -		if (streamInput != null) {
  -		    input = new InputSource(streamInput);
  -		    input.setSystemId(_sourceSystemId);
  -		}
  -		else if (streamReader != null) {
  -		    input = new InputSource(streamReader);
  -		    input.setSystemId(_sourceSystemId);
  -		}
  -		else if (_sourceSystemId != null)
  -		    input = new InputSource(_sourceSystemId);
  -		else {
  -		    ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR);
  -		    throw new TransformerException(err.toString());
  -		}
  -		saxImpl = (SAXImpl)_dtmManager.getDTM(new SAXSource(reader, input),
  -                                                 false, wsfilter, true,
  -                                                 false, hasIdCall);
  -		saxImpl.setDocumentURI(_sourceSystemId);
  -	    }
  -	    else if (source instanceof XSLTCSource) {
  -		final XSLTCSource xsltcsrc = (XSLTCSource)source;
  -		if (_dtmManager == null) {
  -		    _dtmManager = XSLTCDTMManager.newInstance();
  -		}
  -		
  -		dom = xsltcsrc.getDOM(_dtmManager, _translet);
  -	    }
  -	    // DOM already set via a call to setDOM()
  -	    else if (_dom != null) {
  -		dom = _dom; _dom = null;   // use only once, so reset to 'null'
  -	    }
  -	    else {
  -		return null;
  -	    }
  +                 XSLTCDTMManager dtmManager =
  +                         (XSLTCDTMManager)_tfactory.getDTMManagerClass()
  +                                                   .newInstance();
  +                 dom = (DOM)dtmManager.getDTM(source, false, wsfilter, true,
  +                                              false, false, 0, hasIdCall);
  +            } else if (_dom != null) {
  +                 dom = _dom;
  +                 _dom = null;  // use only once, so reset to 'null'
  +            } else {
  +                 return null;
  +            }
   
  -	    if (saxImpl != null) {
  -	        dom = saxImpl;
  -	    }
  -	    
  -	    if (!_isIdentity) {
  +            if (!_isIdentity) {
                   // Give the translet the opportunity to make a prepass of
                   // the document, in case it can extract useful information early
  -		_translet.prepassDocument(dom);
  -	    }
  -	    	    
  -	    return dom;
  +                _translet.prepassDocument(dom);
  +            }
   
  -	}
  -	//catch (FileNotFoundException e) {
  -//	    if (_errorListener != null)	postErrorToListener(e.getMessage());
  -//	    throw new TransformerException(e);
  -//	}
  -	//catch (MalformedURLException e) {
  -//	    if (_errorListener != null)	postErrorToListener(e.getMessage());
  -//	    throw new TransformerException(e);
  -//	}
  -//	catch (UnknownHostException e) {
  -//	    if (_errorListener != null)	postErrorToListener(e.getMessage());
  -//	    throw new TransformerException(e);
  -//	}
  -	catch (Exception e) {
  -	    if (_errorListener != null)	postErrorToListener(e.getMessage());
  -	    throw new TransformerException(e);
  -	}
  +            return dom;
  +
  +        }
  +        catch (Exception e) {
  +            if (_errorListener != null) {
  +                postErrorToListener(e.getMessage());
  +            }
  +            throw new TransformerException(e);
  +        }
       }
    
       private void transformIdentity(Source source, SerializationHandler handler)
   	throws Exception 
       {
  -	// Get systemId from source
  -	if (source != null) {
  -	    _sourceSystemId = source.getSystemId();
  -	}
  -
  -	if (source instanceof StreamSource) {
  -	    final StreamSource stream = (StreamSource) source;
  -	    final InputStream streamInput = stream.getInputStream();
  -	    final Reader streamReader = stream.getReader();
  -	    final XMLReader reader = _tfactory.getXMLReader();
  -
  -	    // Hook up reader and output handler 
  -	    try {
  -		reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler);
  -	    }
  -	    catch (SAXException e) {
  -		// Falls through
  -	    }
  -	    reader.setContentHandler(handler);
  -
  -	    // Create input source from source
  -	    InputSource input;
  -	    if (streamInput != null) {
  -		input = new InputSource(streamInput);
  -		input.setSystemId(_sourceSystemId); 
  -	    } 
  -	    else if (streamReader != null) {
  -		input = new InputSource(streamReader);
  -		input.setSystemId(_sourceSystemId); 
  -	    } 
  -	    else if (_sourceSystemId != null) {
  -		input = new InputSource(_sourceSystemId);
  -	    } 
  -	    else {
  -		ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR);
  -		throw new TransformerException(err.toString());
  -	    }
  +        // Get systemId from source
  +        if (source != null) {
  +            _sourceSystemId = source.getSystemId();
  +        }
   
  -	    // Start pushing SAX events
  -	    reader.parse(input);
  -	}
  -	else if (source instanceof SAXSource) {
  -	    final SAXSource sax = (SAXSource) source;
  -	    XMLReader reader = sax.getXMLReader();
  -	    final InputSource input = sax.getInputSource();
  +        if (source instanceof StreamSource) {
  +            final StreamSource stream = (StreamSource) source;
  +            final InputStream streamInput = stream.getInputStream();
  +            final Reader streamReader = stream.getReader();
  +            final XMLReader reader = _readerManager.getXMLReader();
  +
  +            try {
  +                // Hook up reader and output handler 
  +                try {
  +                    reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler);
  +                }
  +                catch (SAXException e) {
  +                    // Falls through
  +                }
  +                reader.setContentHandler(handler);
   
  -	    // Create a reader if not set by user
  -	    if (reader == null) {
  -		reader = _tfactory.getXMLReader();
  -	    }
  +                // Create input source from source
  +                InputSource input;
  +                if (streamInput != null) {
  +                    input = new InputSource(streamInput);
  +                    input.setSystemId(_sourceSystemId); 
  +                } 
  +                else if (streamReader != null) {
  +                    input = new InputSource(streamReader);
  +                    input.setSystemId(_sourceSystemId); 
  +                } 
  +                else if (_sourceSystemId != null) {
  +                    input = new InputSource(_sourceSystemId);
  +                } 
  +                else {
  +                    ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR);
  +                    throw new TransformerException(err.toString());
  +                }
   
  -	    // Hook up reader and output handler 
  -	    try {
  -		reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler);
  -	    }
  -	    catch (SAXException e) {
  -		// Falls through
  -	    }
  -	    reader.setContentHandler(handler);
  +                // Start pushing SAX events
  +                reader.parse(input);
  +            } finally {
  +                _readerManager.releaseXMLReader(reader);
  +            }
  +        } else if (source instanceof SAXSource) {
  +            final SAXSource sax = (SAXSource) source;
  +            XMLReader reader = sax.getXMLReader();
  +            final InputSource input = sax.getInputSource();
  +            boolean userReader = true;
  +
  +            try {
  +                // Create a reader if not set by user
  +                if (reader == null) {
  +                    reader = _readerManager.getXMLReader();
  +                    userReader = false;
  +                }
   
  -	    // Start pushing SAX events
  -	    reader.parse(input);
  -	}
  -	else if (source instanceof DOMSource) {
  -	    final DOMSource domsrc = (DOMSource) source;
  -	    new DOM2TO(domsrc.getNode(), handler).parse();
  -	}
  -	else if (source instanceof XSLTCSource) {
  -	    final DOM dom = ((XSLTCSource) source).getDOM(null, _translet);
  -	    ((SAXImpl)dom).copy(handler);
  -	}
  -	else {
  -	    ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR);
  -	    throw new TransformerException(err.toString());
  -	}
  +                // Hook up reader and output handler 
  +                try {
  +                    reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler);
  +                }
  +                catch (SAXException e) {
  +                    // Falls through
  +                }
  +                reader.setContentHandler(handler);
  +
  +                // Start pushing SAX events
  +                reader.parse(input);
  +            } finally {
  +                if (!userReader) {
  +                    _readerManager.releaseXMLReader(reader);
  +                }
  +            }
  +        } else if (source instanceof DOMSource) {
  +            final DOMSource domsrc = (DOMSource) source;
  +            new DOM2TO(domsrc.getNode(), handler).parse();
  +        } else if (source instanceof XSLTCSource) {
  +            final DOM dom = ((XSLTCSource) source).getDOM(null, _translet);
  +            ((SAXImpl)dom).copy(handler);
  +        } else {
  +            ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR);
  +            throw new TransformerException(err.toString());
  +        }
       }
   
       /**
  @@ -701,7 +610,6 @@
   	    else {
   		_translet.transform(getDOM(source), handler);
   	    }
  -	    _dtmManager = null;
   	}
   	catch (TransletException e) {
   	    if (_errorListener != null)	postErrorToListener(e.getMessage());
  @@ -1219,7 +1127,7 @@
   	    return(null);
   	}
       }
  -    
  +
       /**
        * Receive notification of a recoverable error. 
        * The transformer must continue to provide normal parsing events after
  
  
  
  1.64.2.1  +16 -24    xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java
  
  Index: TransformerFactoryImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java,v
  retrieving revision 1.64
  retrieving revision 1.64.2.1
  diff -u -r1.64 -r1.64.2.1
  --- TransformerFactoryImpl.java	14 Aug 2003 16:27:43 -0000	1.64
  +++ TransformerFactoryImpl.java	4 Dec 2003 20:58:38 -0000	1.64.2.1
  @@ -100,6 +100,8 @@
   import org.apache.xalan.xsltc.compiler.SourceLoader;
   import org.apache.xalan.xsltc.compiler.XSLTC;
   import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
  +import org.apache.xalan.xsltc.dom.XSLTCDTMManager;
  +
   import org.apache.xml.utils.ObjectFactory;
   
   import org.xml.sax.InputSource;
  @@ -230,15 +232,18 @@
       private int _indentNumber = -1;
   
       /**
  -     * A reference to a SAXParserFactory.
  +     * The provider of the XSLTC DTM Manager service.  This is fixed for any
  +     * instance of this class.  In order to change service providers, a new
  +     * XSLTC <code>TransformerFactory</code> must be instantiated.
  +     * @see XSLTCDTMManager#getDTMManagerClass()
        */
  -    private SAXParserFactory _parserFactory = null;
  +    private Class m_DTMManagerClass;
   
       /**
        * javax.xml.transform.sax.TransformerFactory implementation.
  -     * Contains nothing yet
        */
       public TransformerFactoryImpl() {
  +        m_DTMManagerClass = XSLTCDTMManager.getDTMManagerClass();
       }
   
       /**
  @@ -974,26 +979,6 @@
       }
   
       /**
  -     * This method is synchronized to allow instances of this class to 
  -     * be shared among threads. A tranformer object will call this 
  -     * method to get an XMLReader. A different instance of an XMLReader
  -     * is returned/cached for each thread.
  -     */
  -    public synchronized XMLReader getXMLReader() throws Exception {
  -	// First check if factory is instantiated
  -	if (_parserFactory == null) {
  -	    _parserFactory = SAXParserFactory.newInstance();
  -	    _parserFactory.setNamespaceAware(true);
  -	}
  -	XMLReader result = (XMLReader) _xmlReader.get();
  -	if (result == null) {
  -	    _xmlReader.set(
  -		result = _parserFactory.newSAXParser().getXMLReader());
  -	}
  -	return result;
  -    }
  -    
  -    /**
        * Reset the per-session attributes to their default values
        */
       private void resetTransientAttributes() {
  @@ -1315,5 +1300,12 @@
         	}
         	else
               return null;
  +    }
  +
  +    /**
  +     * Returns the Class object the provides the XSLTC DTM Manager service.
  +     */
  +    protected Class getDTMManagerClass() {
  +        return m_DTMManagerClass;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org