transformerimpl.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,145 行 · 第 1/5 页
JAVA
2,145 行
/** * Set the value of a property. Recognized properties are: * * <p>"http://xml.apache.org/xslt/sourcebase" - the base URL for the * source, which is needed when pure SAX ContentHandler transformation * is to be done.</p> * * @param name The property name, which is a fully-qualified URI. * @param value The requested value for the property. * @throws IllegalArgumentException if the property name is not legal. */ public void setOutputProperty(String name, String value) throws IllegalArgumentException { synchronized (m_reentryGuard) { // Get the output format that was set by the user, otherwise get the // output format from the stylesheet. if (null == m_outputFormat) { m_outputFormat = (OutputProperties) getStylesheet().getOutputComposed().clone(); } if (!m_outputFormat.isLegalPropertyKey(name)) throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_OUTPUT_PROPERTY_NOT_RECOGNIZED, new Object[]{name})); //"output property not recognized: " //+ name); m_outputFormat.setProperty(name, value); } } /** * Set the output properties for the transformation. These * properties will override properties set in the templates * with xsl:output. * * <p>If argument to this function is null, any properties * previously set will be removed.</p> * * @param oformat A set of output properties that will be * used to override any of the same properties in effect * for the transformation. * * @see javax.xml.transform.OutputKeys * @see java.util.Properties * * @throws IllegalArgumentException if any of the argument keys are not * recognized and are not namespace qualified. */ public void setOutputProperties(Properties oformat) throws IllegalArgumentException { synchronized (m_reentryGuard) { if (null != oformat) { // See if an *explicit* method was set. String method = (String) oformat.get(OutputKeys.METHOD); if (null != method) m_outputFormat = new OutputProperties(method); else if(m_outputFormat==null) m_outputFormat = new OutputProperties(); } if (null != oformat) { m_outputFormat.copyFrom(oformat); } // copyFrom does not set properties that have been already set, so // this must be called after, which is a bit in the reverse from // what one might think. m_outputFormat.copyFrom(m_stylesheetRoot.getOutputProperties()); } } /** * Get a copy of the output properties for the transformation. These * properties will override properties set in the templates * with xsl:output. * * <p>Note that mutation of the Properties object returned will not * effect the properties that the transformation contains.</p> * * @returns A copy of the set of output properties in effect * for the next transformation. * * NEEDSDOC ($objectName$) @return */ public Properties getOutputProperties() { return (Properties) getOutputFormat().getProperties().clone(); } /** * Create a result ContentHandler from a Result object, based * on the current OutputProperties. * * @param outputTarget Where the transform result should go, * should not be null. * * @return A valid ContentHandler that will create the * result tree when it is fed SAX events. * * @throws TransformerException */ public ContentHandler createResultContentHandler(Result outputTarget) throws TransformerException { return createResultContentHandler(outputTarget, getOutputFormat()); } /** * Create a ContentHandler from a Result object and an OutputProperties. * * @param outputTarget Where the transform result should go, * should not be null. * @param format The OutputProperties object that will contain * instructions on how to serialize the output. * * @return A valid ContentHandler that will create the * result tree when it is fed SAX events. * * @throws TransformerException */ public ContentHandler createResultContentHandler( Result outputTarget, OutputProperties format) throws TransformerException { ContentHandler handler = null; // If the Result object contains a Node, then create // a ContentHandler that will add nodes to the input node. org.w3c.dom.Node outputNode = null; if (outputTarget instanceof DOMResult) { outputNode = ((DOMResult) outputTarget).getNode(); org.w3c.dom.Document doc; short type; if (null != outputNode) { type = outputNode.getNodeType(); doc = (org.w3c.dom.Node.DOCUMENT_NODE == type) ? (org.w3c.dom.Document) outputNode : outputNode.getOwnerDocument(); } else { doc = org.apache.xpath.DOMHelper.createDocument(); outputNode = doc; type = outputNode.getNodeType(); ((DOMResult) outputTarget).setNode(outputNode); } handler = (org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (org.w3c.dom.DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode); } else if (outputTarget instanceof SAXResult) { handler = ((SAXResult) outputTarget).getHandler(); if (null == handler) throw new IllegalArgumentException( "handler can not be null for a SAXResult"); } // Otherwise, create a ContentHandler that will serialize the // result tree to either a stream or a writer. else if (outputTarget instanceof StreamResult) { StreamResult sresult = (StreamResult) outputTarget; String method = format.getProperty(OutputKeys.METHOD); try { Serializer serializer = SerializerFactory.getSerializer(format.getProperties()); if (null != sresult.getWriter()) serializer.setWriter(sresult.getWriter()); else if (null != sresult.getOutputStream()) serializer.setOutputStream(sresult.getOutputStream()); else if (null != sresult.getSystemId()) { String fileURL = sresult.getSystemId(); if (fileURL.startsWith("file:///")) { if (fileURL.substring(8).indexOf(":") >0) fileURL = fileURL.substring(8); else fileURL = fileURL.substring(7); } m_outputStream = new java.io.FileOutputStream(fileURL); serializer.setOutputStream(m_outputStream); } else throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null)); //"No output specified!"); handler = serializer.asContentHandler(); this.setSerializer(serializer); } catch (UnsupportedEncodingException uee) { throw new TransformerException(uee); } catch (IOException ioe) { throw new TransformerException(ioe); } } else { throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[]{outputTarget.getClass().getName()})); //"Can't transform to a Result of type " //+ outputTarget.getClass().getName() //+ "!"); } return handler; } /** * Process the source tree to the output result. * @param xmlSource The input for the source tree. * @param outputTarget The output source target. * * @throws TransformerException */ public void transform(Source xmlSource, Result outputTarget) throws TransformerException { transform(xmlSource, outputTarget, true); } /** * Process the source tree to the output result. * @param xmlSource The input for the source tree. * @param outputTarget The output source target. * @param shouldRelease Flag indicating whether to release DTMManager. * * @throws TransformerException */ public void transform(Source xmlSource, Result outputTarget, boolean shouldRelease) throws TransformerException { synchronized (m_reentryGuard) { ContentHandler handler = createResultContentHandler(outputTarget); m_outputTarget = outputTarget; this.setContentHandler(handler); transform(xmlSource, shouldRelease); } } /** * Process the source node to the output result, if the * processor supports the "http://xml.org/trax/features/dom/input" * feature. * %REVIEW% Do we need a Node version of this? * @param node The input source node, which can be any valid DTM node. * @param outputTarget The output source target. * * @throws TransformerException */ public void transformNode(int node, Result outputTarget) throws TransformerException { ContentHandler handler = createResultContentHandler(outputTarget); m_outputTarget = outputTarget; this.setContentHandler(handler); transformNode(node); } /** * Process the source node to the output result, if the * processor supports the "http://xml.org/trax/features/dom/input" * feature. * %REVIEW% Do we need a Node version of this? * @param node The input source node, which can be any valid DTM node. * @param outputTarget The output source target. * * @throws TransformerException */ public void transformNode(int node) throws TransformerException { //dml setExtensionsTable(getStylesheet()); // Make sure we're not writing to the same output content handler. synchronized (m_outputContentHandler) { m_hasBeenReset = false; XPathContext xctxt = getXPathContext(); DTM dtm = xctxt.getDTM(node); try { pushGlobalVars(node); // ========== // Give the top-level templates a chance to pass information into // the context (this is mainly for setting up tables for extensions). StylesheetRoot stylesheet = this.getStylesheet(); int n = stylesheet.getGlobalImportCount(); for (int i = 0; i < n; i++) { StylesheetComposed imported = stylesheet.getGlobalImport(i); int includedCount = imported.getIncludeCountComposed(); for (int j = -1; j < includedCount; j++) { Stylesheet included = imported.getIncludeComposed(j); included.runtimeInit(this); for (ElemTemplateElement child = included.getFirstChildElem(); child != null; child = child.getNextSiblingElem()) { child.runtimeInit(this); } } } // =========== // System.out.println("Calling applyTemplateToNode - "+Thread.currentThread().getName()); DTMIterator dtmIter = new org.apache.xpath.axes.SelfIteratorNoPredicate(); dtmIter.setRoot(node, xctxt); xctxt.pushContextNodeList(dtmIter); try { this.applyTemplateToNode(null, null, node); } finally { xctxt.popContextNodeList(); } // m_stylesheetRoot.getStartRule().execute(this); // System.out.println("Done with applyTemplateToNode - "+Thread.currentThread().getName()); if (null != m_resultTreeHandler) { m_resultTreeHandler.endDocument(); } } catch (Exception se) { // System.out.println(Thread.currentThread().getName()+" threw an exception! " // +se.getMessage()); // If an exception was thrown, we need to make sure that any waiting // handlers can terminate, which I guess is best done by sending // an endDocument. // SAXSourceLocator while(se instanceof org.apache.xml.utils.WrappedRuntimeException) { Exception e = ((org.apache.xml.utils.WrappedRuntimeException)se).getException(); if(null != e) se = e; } if (null != m_resultTreeHandler) { try { if(se instanceof org.xml.sax.SAXParseException) m_resultTreeHandler.fatalError((org.xml.sax.SAXParseException)se); else if(se instanceof TransformerException) { TransformerException te = ((TransformerException)se); SAXSourceLocator sl = new SAXSourceLocator( te.getLocator() ); m_resultTreeHandler.fatalError(new org.xml.sax.SAXParseException(te.getMessage(), sl, te)); } else { m_resultTreeHandler.fatalError(new org.xml.sax.SAXParseException(se.getMessage(), new SAXSourceLocator(), se)); } } catch (Exception e){} } if(se instanceof TransformerException) { m_errorHandler.fatalError((TransformerException)se); } else if(se instanceof org.xml.sax.SAXParseException) { m_errorHandler.fatalError(new TransformerException(se.getMessage(), new SAXSourceLocator((org.xml.sax.SAXParseException)se), se)); } else { m_errorHandler.fatalError(new TransformerException(se)); } } finally { this.reset(); } } } /**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?