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 17:44:59 UTC

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

zongaro     2003/12/04 08:44:59

  Modified:    java/src/org/apache/xalan/xsltc/trax TransformerImpl.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
  1.77      +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.76
  retrieving revision 1.77
  diff -u -r1.76 -r1.77
  --- TransformerImpl.java	5 Nov 2003 13:26:27 -0000	1.76
  +++ TransformerImpl.java	4 Dec 2003 16:44:59 -0000	1.77
  @@ -100,6 +100,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;
  @@ -112,7 +113,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;
  @@ -188,12 +191,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.
  @@ -446,229 +448,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());
  +        }
       }
   
       /**
  @@ -712,7 +621,6 @@
   	    else {
   		_translet.transform(getDOM(source), handler);
   	    }
  -	    _dtmManager = null;
   	}
   	catch (TransletException e) {
   	    if (_errorListener != null)	postErrorToListener(e.getMessage());
  @@ -1245,7 +1153,7 @@
   	    return(null);
   	}
       }
  -    
  +
       /**
        * Receive notification of a recoverable error. 
        * The transformer must continue to provide normal parsing events after
  
  
  

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