📄 transformerfactoryimpl.java
字号:
if (_autoTranslet) { byte[][] bytecodes = null; String transletClassName = getTransletBaseName(source); if (_packageName != null) transletClassName = _packageName + "." + transletClassName; if (_jarFileName != null) bytecodes = getBytecodesFromJar(source, transletClassName); else bytecodes = getBytecodesFromClasses(source, transletClassName); if (bytecodes != null) { if (_debug) { if (_jarFileName != null) System.err.println(new ErrorMsg( ErrorMsg.TRANSFORM_WITH_JAR_STR, transletClassName, _jarFileName)); else System.err.println(new ErrorMsg( ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName)); } // Reset the per-session attributes to their default values // after each newTemplates() call. resetTransientAttributes(); return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this); } } // Create and initialize a stylesheet compiler final XSLTC xsltc = new XSLTC(); if (_debug) xsltc.setDebug(true); if (_enableInlining) xsltc.setTemplateInlining(true); if (_isSecureProcessing) xsltc.setSecureProcessing(true); xsltc.init(); // Set a document loader (for xsl:include/import) if defined if (_uriResolver != null) { xsltc.setSourceLoader(this); } // Pass parameters to the Parser to make sure it locates the correct // <?xml-stylesheet ...?> PI in an XML input document if ((_piParams != null) && (_piParams.get(source) != null)) { // Get the parameters for this Source object PIParamWrapper p = (PIParamWrapper)_piParams.get(source); // Pass them on to the compiler (which will pass then to the parser) if (p != null) { xsltc.setPIParameters(p._media, p._title, p._charset); } } // Set the attributes for translet generation int outputType = XSLTC.BYTEARRAY_OUTPUT; if (_generateTranslet || _autoTranslet) { // Set the translet name xsltc.setClassName(getTransletBaseName(source)); if (_destinationDirectory != null) xsltc.setDestDirectory(_destinationDirectory); else { String xslName = getStylesheetFileName(source); if (xslName != null) { File xslFile = new File(xslName); String xslDir = xslFile.getParent(); if (xslDir != null) xsltc.setDestDirectory(xslDir); } } if (_packageName != null) xsltc.setPackageName(_packageName); if (_jarFileName != null) { xsltc.setJarFileName(_jarFileName); outputType = XSLTC.BYTEARRAY_AND_JAR_OUTPUT; } else outputType = XSLTC.BYTEARRAY_AND_FILE_OUTPUT; } // Compile the stylesheet final InputSource input = Util.getInputSource(xsltc, source); byte[][] bytecodes = xsltc.compile(null, input, outputType); final String transletName = xsltc.getClassName(); // Output to the jar file if the jar file name is set. if ((_generateTranslet || _autoTranslet) && bytecodes != null && _jarFileName != null) { try { xsltc.outputToJar(); } catch (java.io.IOException e) { } } // Reset the per-session attributes to their default values // after each newTemplates() call. resetTransientAttributes(); // Pass compiler warnings to the error listener if (_errorListener != this) { try { passWarningsToListener(xsltc.getWarnings()); } catch (TransformerException e) { throw new TransformerConfigurationException(e); } } else { xsltc.printWarnings(); } // Check that the transformation went well before returning if (bytecodes == null) { ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR); TransformerConfigurationException exc = new TransformerConfigurationException(err.toString()); // Pass compiler errors to the error listener if (_errorListener != null) { passErrorsToListener(xsltc.getErrors()); // As required by TCK 1.2, send a fatalError to the // error listener because compilation of the stylesheet // failed and no further processing will be possible. try { _errorListener.fatalError(exc); } catch (TransformerException te) { // well, we tried. } } else { xsltc.printErrors(); } throw exc; } return new TemplatesImpl(bytecodes, transletName, xsltc.getOutputProperties(), _indentNumber, this); } /** * javax.xml.transform.sax.SAXTransformerFactory implementation. * Get a TemplatesHandler object that can process SAX ContentHandler * events into a Templates object. * * @return A TemplatesHandler object that can handle SAX events * @throws TransformerConfigurationException */ public TemplatesHandler newTemplatesHandler() throws TransformerConfigurationException { final TemplatesHandlerImpl handler = new TemplatesHandlerImpl(_indentNumber, this); if (_uriResolver != null) { handler.setURIResolver(_uriResolver); } return handler; } /** * javax.xml.transform.sax.SAXTransformerFactory implementation. * Get a TransformerHandler object that can process SAX ContentHandler * events into a Result. This method will return a pure copy transformer. * * @return A TransformerHandler object that can handle SAX events * @throws TransformerConfigurationException */ public TransformerHandler newTransformerHandler() throws TransformerConfigurationException { final Transformer transformer = newTransformer(); if (_uriResolver != null) { transformer.setURIResolver(_uriResolver); } return new TransformerHandlerImpl((TransformerImpl) transformer); } /** * javax.xml.transform.sax.SAXTransformerFactory implementation. * Get a TransformerHandler object that can process SAX ContentHandler * events into a Result, based on the transformation instructions * specified by the argument. * * @param src The source of the transformation instructions. * @return A TransformerHandler object that can handle SAX events * @throws TransformerConfigurationException */ public TransformerHandler newTransformerHandler(Source src) throws TransformerConfigurationException { final Transformer transformer = newTransformer(src); if (_uriResolver != null) { transformer.setURIResolver(_uriResolver); } return new TransformerHandlerImpl((TransformerImpl) transformer); } /** * javax.xml.transform.sax.SAXTransformerFactory implementation. * Get a TransformerHandler object that can process SAX ContentHandler * events into a Result, based on the transformation instructions * specified by the argument. * * @param templates Represents a pre-processed stylesheet * @return A TransformerHandler object that can handle SAX events * @throws TransformerConfigurationException */ public TransformerHandler newTransformerHandler(Templates templates) throws TransformerConfigurationException { final Transformer transformer = templates.newTransformer(); final TransformerImpl internal = (TransformerImpl)transformer; return new TransformerHandlerImpl(internal); } /** * javax.xml.transform.sax.SAXTransformerFactory implementation. * Create an XMLFilter that uses the given source as the * transformation instructions. * * @param src The source of the transformation instructions. * @return An XMLFilter object, or null if this feature is not supported. * @throws TransformerConfigurationException */ public XMLFilter newXMLFilter(Source src) throws TransformerConfigurationException { Templates templates = newTemplates(src); if (templates == null) return null; return newXMLFilter(templates); } /** * javax.xml.transform.sax.SAXTransformerFactory implementation. * Create an XMLFilter that uses the given source as the * transformation instructions. * * @param templates The source of the transformation instructions. * @return An XMLFilter object, or null if this feature is not supported. * @throws TransformerConfigurationException */ public XMLFilter newXMLFilter(Templates templates) throws TransformerConfigurationException { try { return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates); } catch (TransformerConfigurationException e1) { if (_errorListener != null) { try { _errorListener.fatalError(e1); return null; } catch (TransformerException e2) { new TransformerConfigurationException(e2); } } throw e1; } } /** * Receive notification of a recoverable error. * The transformer must continue to provide normal parsing events after * invoking this method. It should still be possible for the application * to process the document through to the end. * * @param e The warning information encapsulated in a transformer * exception. * @throws TransformerException if the application chooses to discontinue * the transformation (always does in our case). */ public void error(TransformerException e) throws TransformerException { Throwable wrapped = e.getException(); if (wrapped != null) { System.err.println(new ErrorMsg(ErrorMsg.ERROR_PLUS_WRAPPED_MSG, e.getMessageAndLocation(), wrapped.getMessage())); } else { System.err.println(new ErrorMsg(ErrorMsg.ERROR_MSG, e.getMessageAndLocation())); } throw e; } /** * Receive notification of a non-recoverable error. * The application must assume that the transformation cannot continue * after the Transformer has invoked this method, and should continue * (if at all) only to collect addition error messages. In fact, * Transformers are free to stop reporting events once this method has * been invoked. * * @param e warning information encapsulated in a transformer * exception. * @throws TransformerException if the application chooses to discontinue * the transformation (always does in our case). */ public void fatalError(TransformerException e) throws TransformerException { Throwable wrapped = e.getException(); if (wrapped != null) { System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_PLUS_WRAPPED_MSG, e.getMessageAndLocation(), wrapped.getMessage())); } else { System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_MSG, e.getMessageAndLocation())); } throw e; } /** * Receive notification of a warning. * Transformers can use this method to report conditions that are not * errors or fatal errors. The default behaviour is to take no action. * After invoking this method, the Transformer must continue with the * transformation. It should still be possible for the application to * process the document through to the end. * * @param e The warning information encapsulated in a transformer * exception. * @throws TransformerException if the application chooses to discontinue * the transformation (never does in our case). */ public void warning(TransformerException e) throws TransformerException { Throwable wrapped = e.getException(); if (wrapped != null) { System.err.println(new ErrorMsg(ErrorMsg.WARNING_PLUS_WRAPPED_MSG, e.getMessageAndLocation(), wrapped.getMessage())); } else { System.err.println(new ErrorMsg(ErrorMsg.WARNING_MSG, e.getMessageAndLocation())); } } /** * This method implements XSLTC's SourceLoader interface. It is used to * glue a TrAX URIResolver to the XSLTC compiler's Input and Import classes. * * @param href The URI of the document to load * @param context The URI of the currently loaded document * @param xsltc The compiler that resuests the document * @return An InputSource with the loaded document
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -