📄 transformerimpl.java
字号:
Vector uriAndLocalNames = null; while (e.hasMoreTokens()) { final String token = e.nextToken(); // look for the last colon, as the String may be // something like "http://abc.com:local" int lastcolon = token.lastIndexOf(':'); String uri; String localName; if (lastcolon > 0) { uri = token.substring(0, lastcolon); localName = token.substring(lastcolon+1); } else { // no colon at all, lets hope this is the // local name itself then uri = null; localName = token; } if (uriAndLocalNames == null) { uriAndLocalNames = new Vector(); } // add the uri/localName as a pair, in that order uriAndLocalNames.addElement(uri); uriAndLocalNames.addElement(localName); } handler.setCdataSectionElements(uriAndLocalNames); } } } // Call setDoctype() if needed if (doctypePublic != null || doctypeSystem != null) { handler.setDoctype(doctypeSystem, doctypePublic); } } /** * Internal method to create the initial set of properties. There * are two layers of properties: the default layer and the base layer. * The latter contains properties defined in the stylesheet or by * the user using this API. */ private Properties createOutputProperties(Properties outputProperties) { final Properties defaults = new Properties(); setDefaults(defaults, "xml"); // Copy propeties set in stylesheet to base final Properties base = new Properties(defaults); if (outputProperties != null) { final Enumeration names = outputProperties.propertyNames(); while (names.hasMoreElements()) { final String name = (String) names.nextElement(); base.setProperty(name, outputProperties.getProperty(name)); } } else { base.setProperty(OutputKeys.ENCODING, _translet._encoding); if (_translet._method != null) base.setProperty(OutputKeys.METHOD, _translet._method); } // Update defaults based on output method final String method = base.getProperty(OutputKeys.METHOD); if (method != null) { if (method.equals("html")) { setDefaults(defaults,"html"); } else if (method.equals("text")) { setDefaults(defaults,"text"); } } return base; } /** * Internal method to get the default properties from the * serializer factory and set them on the property object. * @param props a java.util.Property object on which the properties are set. * @param method The output method type, one of "xml", "text", "html" ... */ private void setDefaults(Properties props, String method) { final Properties method_props = OutputPropertiesFactory.getDefaultMethodProperties(method); { final Enumeration names = method_props.propertyNames(); while (names.hasMoreElements()) { final String name = (String)names.nextElement(); props.setProperty(name, method_props.getProperty(name)); } } } /** * Verifies if a given output property name is a property defined in * the JAXP 1.1 / TrAX spec */ private boolean validOutputProperty(String name) { return (name.equals(OutputKeys.ENCODING) || name.equals(OutputKeys.METHOD) || name.equals(OutputKeys.INDENT) || name.equals(OutputKeys.DOCTYPE_PUBLIC) || name.equals(OutputKeys.DOCTYPE_SYSTEM) || name.equals(OutputKeys.CDATA_SECTION_ELEMENTS) || name.equals(OutputKeys.MEDIA_TYPE) || name.equals(OutputKeys.OMIT_XML_DECLARATION) || name.equals(OutputKeys.STANDALONE) || name.equals(OutputKeys.VERSION) || name.charAt(0) == '{'); } /** * Checks if a given output property is default (2nd layer only) */ private boolean isDefaultProperty(String name, Properties properties) { return (properties.get(name) == null); } /** * Implements JAXP's Transformer.setParameter() * Add a parameter for the transformation. The parameter is simply passed * on to the translet - no validation is performed - so any unused * parameters are quitely ignored by the translet. * * @param name The name of the parameter * @param value The value to assign to the parameter */ public void setParameter(String name, Object value) { if (value == null) { ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_SET_PARAM_VALUE, name); throw new IllegalArgumentException(err.toString()); } if (_isIdentity) { if (_parameters == null) { _parameters = new Hashtable(); } _parameters.put(name, value); } else { _translet.addParameter(name, value); } } /** * Implements JAXP's Transformer.clearParameters() * Clear all parameters set with setParameter. Clears the translet's * parameter stack. */ public void clearParameters() { if (_isIdentity && _parameters != null) { _parameters.clear(); } else { _translet.clearParameters(); } } /** * Implements JAXP's Transformer.getParameter() * Returns the value of a given parameter. Note that the translet will not * keep values for parameters that were not defined in the stylesheet. * * @param name The name of the parameter * @return An object that contains the value assigned to the parameter */ public final Object getParameter(String name) { if (_isIdentity) { return (_parameters != null) ? _parameters.get(name) : null; } else { return _translet.getParameter(name); } } /** * Implements JAXP's Transformer.getURIResolver() * Set the object currently used to resolve URIs used in document(). * * @return The URLResolver object currently in use */ public URIResolver getURIResolver() { return _uriResolver; } /** * Implements JAXP's Transformer.setURIResolver() * Set an object that will be used to resolve URIs used in document(). * * @param resolver The URIResolver to use in document() */ public void setURIResolver(URIResolver resolver) { _uriResolver = resolver; } /** * This class should only be used as a DOMCache for the translet if the * URIResolver has been set. * * The method implements XSLTC's DOMCache interface, which is used to * plug in an external document loader into a translet. This method acts * as an adapter between TrAX's URIResolver interface and XSLTC's * DOMCache interface. This approach is simple, but removes the * possibility of using external document caches with XSLTC. * * @param baseURI The base URI used by the document call. * @param href The href argument passed to the document function. * @param translet A reference to the translet requesting the document */ public DOM retrieveDocument(String baseURI, String href, Translet translet) { try { // Argument to document function was: document(''); if (href.length() == 0) { href = new String(baseURI); } /* * Fix for bug 24188 * Incase the _uriResolver.resolve(href,base) is null * try to still retrieve the document before returning null * and throwing the FileNotFoundException in * com.sun.org.apache.xalan.internal.xsltc.dom.LoadDocument * */ Source resolvedSource = _uriResolver.resolve(href, baseURI); if (resolvedSource == null) { StreamSource streamSource = new StreamSource( SystemIDResolver.getAbsoluteURI(href, baseURI)); return getDOM(streamSource) ; } return getDOM(resolvedSource); } catch (TransformerException e) { if (_errorListener != null) postErrorToListener("File not found: " + e.getMessage()); return(null); } } /** * 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 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 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 resets the Transformer to its original configuration * Transformer code is reset to the same state it was when it was * created * @since 1.5 */ public void reset() { _method = null; _encoding = null; _sourceSystemId = null; _errorListener = this; _uriResolver = null; _dom = null; _parameters = null; _indentNumber = 0; setOutputProperties (null); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -