transformerimpl.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,145 行 · 第 1/5 页
JAVA
2,145 行
*/ void setExtensionsTable(StylesheetRoot sroot) throws javax.xml.transform.TransformerException { try { if (sroot.getExtensions() != null) m_extensionsTable = new ExtensionsTable(sroot); } catch (javax.xml.transform.TransformerException te) {te.printStackTrace();} } //== Implementation of the XPath ExtensionsProvider interface. public boolean functionAvailable(String ns, String funcName) throws javax.xml.transform.TransformerException { return getExtensionsTable().functionAvailable(ns, funcName); } public boolean elementAvailable(String ns, String elemName) throws javax.xml.transform.TransformerException { return getExtensionsTable().elementAvailable(ns, elemName); } public Object extFunction(String ns, String funcName, Vector argVec, Object methodKey) throws javax.xml.transform.TransformerException {//System.out.println("TransImpl.extFunction() " + ns + " " + funcName +" " + getExtensionsTable()); return getExtensionsTable().extFunction(ns, funcName, argVec, methodKey, getXPathContext().getExpressionContext()); } //========================= /** * Reset the state. This needs to be called after a process() call * is invoked, if the processor is to be used again. */ public void reset() { if (!m_hasBeenReset && m_shouldReset) { m_hasBeenReset = true; if (this.m_outputStream != null) { try { m_outputStream.close(); } catch (java.io.IOException ioe){} } m_outputStream = null; // I need to look more carefully at which of these really // needs to be reset. m_countersTable = null; m_xcontext.reset(); m_xcontext.getVarStack().reset(); resetUserParameters(); m_currentTemplateElements.removeAllElements(); m_currentMatchTemplates.removeAllElements(); m_currentMatchedNodes.removeAllElements(); m_resultTreeHandler = null; m_outputTarget = null; m_keyManager = new KeyManager(); m_attrSetStack = null; m_countersTable = null; m_currentTemplateRuleIsNull = new BoolStack(); m_xmlSource = null; m_doc = DTM.NULL; m_isTransformDone = false; m_transformThread = null; // m_inputContentHandler = null; // For now, reset the document cache each time. m_xcontext.getSourceTreeManager().reset(); } // m_reportInPostExceptionFromThread = false; } /** * <code>getProperty</code> returns the current setting of the * property described by the <code>property</code> argument. * * %REVIEW% Obsolete now that source_location is handled in the TransformerFactory? * * @param property a <code>String</code> value * @return a <code>boolean</code> value */ public boolean getProperty(String property) { return false; } /** * Set a runtime property for this <code>TransformerImpl</code>. * * %REVIEW% Obsolete now that source_location is handled in the TransformerFactory? * * @param property a <code>String</code> value * @param value an <code>Object</code> value */ public void setProperty(String property, Object value) { } // ========= Transformer Interface Implementation ========== /** * <meta name="usage" content="experimental"/> * Get true if the parser events should be on the main thread, * false if not. Experimental. Can not be set right now. * * @return true if the parser events should be on the main thread, * false if not. */ public boolean isParserEventsOnMain() { return m_parserEventsOnMain; } /** * <meta name="usage" content="internal"/> * Get the thread that the transform process is on. * * @return The thread that the transform process is on, or null. */ public Thread getTransformThread() { return m_transformThread; } /** * <meta name="usage" content="internal"/> * Get the thread that the transform process is on. * * @param t The transform thread, may be null. */ public void setTransformThread(Thread t) { m_transformThread = t; } /** NEEDSDOC Field m_hasTransformThreadErrorCatcher */ private boolean m_hasTransformThreadErrorCatcher = false; /** * Return true if the transform was initiated from the transform method, * otherwise it was probably done from a pure parse events. * * NEEDSDOC ($objectName$) @return */ public boolean hasTransformThreadErrorCatcher() { return m_hasTransformThreadErrorCatcher; } /** * Process the source tree to SAX parse events. * @param source The input for the source tree. * * @throws TransformerException */ public void transform(Source source) throws TransformerException { transform(source, true); } /** * Process the source tree to SAX parse events. * @param source The input for the source tree. * @param shouldRelease Flag indicating whether to release DTMManager. * * @throws TransformerException */ public void transform(Source source, boolean shouldRelease) throws TransformerException { try { // Patch for bugzilla #13863. If we don't reset the namespaceContext // then we will get a NullPointerException if transformer is reused // (for stylesheets that use xsl:key). Not sure if this should go // here or in reset(). -is if(getXPathContext().getNamespaceContext() == null){ getXPathContext().setNamespaceContext(getStylesheet()); } String base = source.getSystemId(); // If no systemID of the source, use the base of the stylesheet. if(null == base) { base = m_stylesheetRoot.getBaseIdentifier(); } // As a last resort, use the current user dir. if(null == base) { String currentDir = ""; try { currentDir = System.getProperty("user.dir"); } catch (SecurityException se) {}// user.dir not accessible from applet if (currentDir.startsWith(java.io.File.separator)) base = "file://" + currentDir; else base = "file:///" + currentDir; base = base + java.io.File.separatorChar + source.getClass().getName(); } setBaseURLOfSource(base); DTMManager mgr = m_xcontext.getDTMManager(); DTM dtm = mgr.getDTM(source, false, this, true, true); dtm.setDocumentBaseURI(base); boolean hardDelete = true; // %REVIEW% I have to think about this. -sb try { // NOTE: This will work because this is _NOT_ a shared DTM, and thus has // only a single Document node. If it could ever be an RTF or other // shared DTM, look at dtm.getDocumentRoot(nodeHandle). this.transformNode(dtm.getDocument()); } finally { if (shouldRelease) mgr.release(dtm, hardDelete); } // Kick off the parse. When the ContentHandler gets // the startDocument event, it will call transformNode( node ). // reader.parse( xmlSource ); // This has to be done to catch exceptions thrown from // the transform thread spawned by the STree handler. Exception e = getExceptionThrown(); if (null != e) { if (e instanceof javax.xml.transform.TransformerException) { throw (javax.xml.transform.TransformerException) e; } else if (e instanceof org.apache.xml.utils.WrappedRuntimeException) { fatalError( ((org.apache.xml.utils.WrappedRuntimeException) e).getException()); } else { throw new javax.xml.transform.TransformerException(e); } } else if (null != m_resultTreeHandler) { m_resultTreeHandler.endDocument(); } } catch (org.apache.xml.utils.WrappedRuntimeException wre) { Throwable throwable = wre.getException(); while (throwable instanceof org.apache.xml.utils.WrappedRuntimeException) { throwable = ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException(); } fatalError(throwable); } // Patch attributed to David Eisenberg <david@catcode.com> catch (org.xml.sax.SAXParseException spe) { fatalError(spe); } catch (org.xml.sax.SAXException se) { m_errorHandler.fatalError(new TransformerException(se)); } finally { m_hasTransformThreadErrorCatcher = false; // This looks to be redundent to the one done in TransformNode. reset(); } } private void fatalError(Throwable throwable) throws TransformerException { if (throwable instanceof org.xml.sax.SAXParseException) m_errorHandler.fatalError(new TransformerException(throwable.getMessage(),new SAXSourceLocator((org.xml.sax.SAXParseException)throwable))); else m_errorHandler.fatalError(new TransformerException(throwable)); } /** * Get the base URL of the source. * * @return The base URL of the source tree, or null. */ public String getBaseURLOfSource() { return m_urlOfSource; } /** * Get the base URL of the source. * * * NEEDSDOC @param base * @return The base URL of the source tree, or null. */ public void setBaseURLOfSource(String base) { m_urlOfSource = base; } /** * Get the original output target. * * @return The Result object used to kick of the transform or null. */ public Result getOutputTarget() { return m_outputTarget; } /** * Set the original output target. This is useful when using a SAX transform and * supplying a ContentHandler or when the URI of the output target should * not be the same as the systemID of the original output target. * * * NEEDSDOC @param outputTarget */ public void setOutputTarget(Result outputTarget) { m_outputTarget = outputTarget; } /** * Get an output property that is in effect for the * transformation. The property specified may be a property * that was set with setOutputProperty, or it may be a * property specified in the stylesheet. * * @param name A non-null String that specifies an output * property name, which may be namespace qualified. * * NEEDSDOC @param qnameString * * @return The string value of the output property, or null * if no property was found. * * @throws IllegalArgumentException If the property is not supported. * * @see javax.xml.transform.OutputKeys */ public String getOutputProperty(String qnameString) throws IllegalArgumentException { String value = null; OutputProperties props = getOutputFormat(); value = props.getProperty(qnameString); if (null == value) { if (!props.isLegalPropertyKey(qnameString)) throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_OUTPUT_PROPERTY_NOT_RECOGNIZED, new Object[]{qnameString})); //"output property not recognized: " //+ qnameString); } return value; } /** * Get the value of a property, without using the default properties. This * can be used to test if a property has been explicitly set by the stylesheet * or user. * * @param name The property name, which is a fully-qualified URI. * * NEEDSDOC @param qnameString * * @return The value of the property, or null if not found. * * @throws IllegalArgumentException If the property is not supported, * and is not namespaced. */ public String getOutputPropertyNoDefault(String qnameString) throws IllegalArgumentException { String value = null; OutputProperties props = getOutputFormat(); value = (String) props.getProperties().get(qnameString); if (null == value) { if (!props.isLegalPropertyKey(qnameString)) throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_OUTPUT_PROPERTY_NOT_RECOGNIZED, new Object[]{qnameString})); //"output property not recognized: " // + qnameString); } return value; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?