📄 transformerimpl.java
字号:
_tohFactory = TransletOutputHandlerFactory.newInstance(); _tohFactory.setEncoding(_encoding); if (_method != null) { _tohFactory.setOutputMethod(_method); } // Set indentation number in the factory if (_indentNumber >= 0) { _tohFactory.setIndentNumber(_indentNumber); } // Return the content handler for this Result object try { // Result object could be SAXResult, DOMResult, or StreamResult if (result instanceof SAXResult) { final SAXResult target = (SAXResult)result; final ContentHandler handler = target.getHandler(); _tohFactory.setHandler(handler); /** * Fix for bug 24414 * If the lexicalHandler is set then we need to get that * for obtaining the lexical information */ LexicalHandler lexicalHandler = target.getLexicalHandler(); if (lexicalHandler != null ) { _tohFactory.setLexicalHandler(lexicalHandler); } _tohFactory.setOutputType(TransletOutputHandlerFactory.SAX); return _tohFactory.getSerializationHandler(); } else if (result instanceof StAXResult) { if (((StAXResult) result).getXMLEventWriter() != null) _tohFactory.setXMLEventWriter(((StAXResult) result).getXMLEventWriter()); else if (((StAXResult) result).getXMLStreamWriter() != null) _tohFactory.setXMLStreamWriter(((StAXResult) result).getXMLStreamWriter()); _tohFactory.setOutputType(TransletOutputHandlerFactory.STAX); return _tohFactory.getSerializationHandler(); } else if (result instanceof DOMResult) { _tohFactory.setNode(((DOMResult) result).getNode()); _tohFactory.setNextSibling(((DOMResult) result).getNextSibling()); _tohFactory.setOutputType(TransletOutputHandlerFactory.DOM); return _tohFactory.getSerializationHandler(); } else if (result instanceof StreamResult) { // Get StreamResult final StreamResult target = (StreamResult) result; // StreamResult may have been created with a java.io.File, // java.io.Writer, java.io.OutputStream or just a String // systemId. _tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM); // try to get a Writer from Result object final Writer writer = target.getWriter(); if (writer != null) { _tohFactory.setWriter(writer); return _tohFactory.getSerializationHandler(); } // or try to get an OutputStream from Result object final OutputStream ostream = target.getOutputStream(); if (ostream != null) { _tohFactory.setOutputStream(ostream); return _tohFactory.getSerializationHandler(); } // or try to get just a systemId string from Result object String systemId = result.getSystemId(); if (systemId == null) { ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_RESULT_ERR); throw new TransformerException(err.toString()); } // System Id may be in one of several forms, (1) a uri // that starts with 'file:', (2) uri that starts with 'http:' // or (3) just a filename on the local system. URL url = null; if (systemId.startsWith("file:")) { // if StreamResult(File) or setSystemID(File) was used, // the systemId will be URI encoded as a result of File.toURI(), // it must be decoded for use by URL try{ Class clazz = ObjectFactory.findProviderClass("java.net.URI", ObjectFactory.findClassLoader(), true); Constructor construct = clazz.getConstructor(new Class[] {java.lang.String.class} ); URI uri = (URI) construct.newInstance(new Object[]{systemId}) ; systemId = "file:"; String host = uri.getHost(); // decoded String String path = uri.getPath(); //decoded String if (path == null) { path = ""; } // if host (URI authority) then file:// + host + path // else just path (may be absolute or relative) if (host != null) { systemId += "//" + host + path; } else { systemId += "//" + path; } } catch(ClassNotFoundException e){ // running on J2SE 1.3 which doesn't have URI Class so OK to ignore //ClassNotFoundException. } catch (Exception exception) { // URI exception which means nothing can be done so OK to ignore } url = new URL(systemId); _ostream = new FileOutputStream(url.getFile()); _tohFactory.setOutputStream(_ostream); return _tohFactory.getSerializationHandler(); } else if (systemId.startsWith("http:")) { url = new URL(systemId); final URLConnection connection = url.openConnection(); _tohFactory.setOutputStream(_ostream = connection.getOutputStream()); return _tohFactory.getSerializationHandler(); } else { // system id is just a filename url = new File(systemId).toURL(); _tohFactory.setOutputStream( _ostream = new FileOutputStream(url.getFile())); return _tohFactory.getSerializationHandler(); } } } // If we cannot write to the location specified by the SystemId catch (UnknownServiceException e) { throw new TransformerException(e); } catch (ParserConfigurationException e) { throw new TransformerException(e); } // If we cannot create the file specified by the SystemId catch (IOException e) { throw new TransformerException(e); } return null; } /** * Set the internal DOM that will be used for the next transformation */ protected void setDOM(DOM dom) { _dom = dom; } /** * Builds an internal DOM from a TrAX Source object */ private DOM getDOM(Source source) throws TransformerException { try { DOM dom = null; 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; if (_dtmManager == null) { _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 (!_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; } catch (Exception e) { if (_errorListener != null) { postErrorToListener(e.getMessage()); } throw new TransformerException(e); } } /** * Returns the {@link com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl} * object that create this <code>Transformer</code>. */ protected TransformerFactoryImpl getTransformerFactory() { return _tfactory; } /** * Returns the {@link com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory} * object that create the <code>TransletOutputHandler</code>. */ protected TransletOutputHandlerFactory getTransletOutputHandlerFactory() { return _tohFactory; } 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 = _readerManager.getXMLReader(); try { // Hook up reader and output handler try { reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler); reader.setFeature(NAMESPACE_PREFIXES_FEATURE, true); } 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()); } // 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; } // Hook up reader and output handler try { reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler); reader.setFeature(NAMESPACE_PREFIXES_FEATURE, true); } 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 StAXSource) { final StAXSource staxSource = (StAXSource)source; StAXEvent2SAX staxevent2sax = null; StAXStream2SAX staxStream2SAX = null; if (staxSource.getXMLEventReader() != null) { final XMLEventReader xmlEventReader = staxSource.getXMLEventReader(); staxevent2sax = new StAXEvent2SAX(xmlEventReader); staxevent2sax.setContentHandler(handler); staxevent2sax.parse(); handler.flushPending(); } else if (staxSource.getXMLStreamReader() != null) { final XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader(); staxStream2SAX = new StAXStream2SAX(xmlStreamReader); staxStream2SAX.setContentHandler(handler); staxStream2SAX.parse(); handler.flushPending(); } } 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()); } } /** * Internal transformation method - uses the internal APIs of XSLTC */ private void transform(Source source, SerializationHandler handler, String encoding) throws TransformerException { try { /* * According to JAXP1.2, new SAXSource()/StreamSource() * should create an empty input tree, with a default root node. * new DOMSource()creates an empty document using DocumentBuilder. * newDocument(); Use DocumentBuilder.newDocument() for all 3 * situations, since there is no clear spec. how to create * an empty tree when both SAXSource() and StreamSource() are used. */ if ((source instanceof StreamSource && source.getSystemId()==null && ((StreamSource)source).getInputStream()==null && ((StreamSource)source).getReader()==null)|| (source instanceof SAXSource &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -