📄 transformerfactoryimpl.java
字号:
return; } catch (NumberFormatException e) { // Falls through } } else if (value instanceof Integer) { _indentNumber = ((Integer) value).intValue(); return; } } // Throw an exception for all other attributes final ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name); throw new IllegalArgumentException(err.toString()); } /** * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s * or <code>Template</code>s created by this factory.</p> * * <p> * Feature names are fully qualified {@link java.net.URI}s. * Implementations may define their own features. * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature. * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state. * </p> * * <p>See {@link javax.xml.transform.TransformerFactory} for full documentation of specific features.</p> * * @param name Feature name. * @param value Is feature state <code>true</code> or <code>false</code>. * * @throws TransformerConfigurationException if this <code>TransformerFactory</code> * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature. * @throws NullPointerException If the <code>name</code> parameter is null. */ public void setFeature(String name, boolean value) throws TransformerConfigurationException { // feature name cannot be null if (name == null) { ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME); throw new NullPointerException(err.toString()); } // secure processing? else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { _isSecureProcessing = value; // all done processing feature return; } else { // unknown feature ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name); throw new TransformerConfigurationException(err.toString()); } } /** * javax.xml.transform.sax.TransformerFactory implementation. * Look up the value of a feature (to see if it is supported). * This method must be updated as the various methods and features of this * class are implemented. * * @param name The feature name * @return 'true' if feature is supported, 'false' if not */ public boolean getFeature(String name) { // All supported features should be listed here String[] features = { DOMSource.FEATURE, DOMResult.FEATURE, SAXSource.FEATURE, SAXResult.FEATURE, StreamSource.FEATURE, StreamResult.FEATURE, SAXTransformerFactory.FEATURE, SAXTransformerFactory.FEATURE_XMLFILTER }; // feature name cannot be null if (name == null) { ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME); throw new NullPointerException(err.toString()); } // Inefficient, but array is small for (int i =0; i < features.length; i++) { if (name.equals(features[i])) { return true; } } // secure processing? if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { return _isSecureProcessing; } // Feature not supported return false; } /** * javax.xml.transform.sax.TransformerFactory implementation. * Get the object that is used by default during the transformation to * resolve URIs used in document(), xsl:import, or xsl:include. * * @return The URLResolver used for this TransformerFactory and all * Templates and Transformer objects created using this factory */ public URIResolver getURIResolver() { return _uriResolver; } /** * javax.xml.transform.sax.TransformerFactory implementation. * Set the object that is used by default during the transformation to * resolve URIs used in document(), xsl:import, or xsl:include. Note that * this does not affect Templates and Transformers that are already * created with this factory. * * @param resolver The URLResolver used for this TransformerFactory and all * Templates and Transformer objects created using this factory */ public void setURIResolver(URIResolver resolver) { _uriResolver = resolver; } /** * javax.xml.transform.sax.TransformerFactory implementation. * Get the stylesheet specification(s) associated via the xml-stylesheet * processing instruction (see http://www.w3.org/TR/xml-stylesheet/) with * the document document specified in the source parameter, and that match * the given criteria. * * @param source The XML source document. * @param media The media attribute to be matched. May be null, in which * case the prefered templates will be used (i.e. alternate = no). * @param title The value of the title attribute to match. May be null. * @param charset The value of the charset attribute to match. May be null. * @return A Source object suitable for passing to the TransformerFactory. * @throws TransformerConfigurationException */ public Source getAssociatedStylesheet(Source source, String media, String title, String charset) throws TransformerConfigurationException { String baseId; XMLReader reader = null; InputSource isource = null; /** * Fix for bugzilla bug 24187 */ StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset); try { if (source instanceof DOMSource ) { final DOMSource domsrc = (DOMSource) source; baseId = domsrc.getSystemId(); final org.w3c.dom.Node node = domsrc.getNode(); final DOM2SAX dom2sax = new DOM2SAX(node); _stylesheetPIHandler.setBaseId(baseId); dom2sax.setContentHandler( _stylesheetPIHandler); dom2sax.parse(); } else { isource = SAXSource.sourceToInputSource(source); baseId = isource.getSystemId(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); if (_isSecureProcessing) { try { factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); } catch (org.xml.sax.SAXException e) {} } SAXParser jaxpParser = factory.newSAXParser(); reader = jaxpParser.getXMLReader(); if (reader == null) { reader = XMLReaderFactory.createXMLReader(); } _stylesheetPIHandler.setBaseId(baseId); reader.setContentHandler(_stylesheetPIHandler); reader.parse(isource); } if (_uriResolver != null ) { _stylesheetPIHandler.setURIResolver(_uriResolver); } } catch (StopParseException e ) { // startElement encountered so do not parse further } catch (javax.xml.parsers.ParserConfigurationException e) { throw new TransformerConfigurationException( "getAssociatedStylesheets failed", e); } catch (org.xml.sax.SAXException se) { throw new TransformerConfigurationException( "getAssociatedStylesheets failed", se); } catch (IOException ioe ) { throw new TransformerConfigurationException( "getAssociatedStylesheets failed", ioe); } return _stylesheetPIHandler.getAssociatedStylesheet(); } /** * javax.xml.transform.sax.TransformerFactory implementation. * Create a Transformer object that copies the input document to the result. * * @return A Transformer object that simply copies the source to the result. * @throws TransformerConfigurationException */ public Transformer newTransformer() throws TransformerConfigurationException { TransformerImpl result = new TransformerImpl(new Properties(), _indentNumber, this); if (_uriResolver != null) { result.setURIResolver(_uriResolver); } if (_isSecureProcessing) { result.setSecureProcessing(true); } return result; } /** * javax.xml.transform.sax.TransformerFactory implementation. * Process the Source into a Templates object, which is a a compiled * representation of the source. Note that this method should not be * used with XSLTC, as the time-consuming compilation is done for each * and every transformation. * * @return A Templates object that can be used to create Transformers. * @throws TransformerConfigurationException */ public Transformer newTransformer(Source source) throws TransformerConfigurationException { final Templates templates = newTemplates(source); final Transformer transformer = templates.newTransformer(); if (_uriResolver != null) { transformer.setURIResolver(_uriResolver); } return(transformer); } /** * Pass warning messages from the compiler to the error listener */ private void passWarningsToListener(Vector messages) throws TransformerException { if (_errorListener == null || messages == null) { return; } // Pass messages to listener, one by one final int count = messages.size(); for (int pos = 0; pos < count; pos++) { ErrorMsg msg = (ErrorMsg)messages.elementAt(pos); // Workaround for the TCK failure ErrorListener.errorTests.error001. if (msg.isWarningError()) _errorListener.error( new TransformerConfigurationException(msg.toString())); else _errorListener.warning( new TransformerConfigurationException(msg.toString())); } } /** * Pass error messages from the compiler to the error listener */ private void passErrorsToListener(Vector messages) { try { if (_errorListener == null || messages == null) { return; } // Pass messages to listener, one by one final int count = messages.size(); for (int pos = 0; pos < count; pos++) { String message = messages.elementAt(pos).toString(); _errorListener.error(new TransformerException(message)); } } catch (TransformerException e) { // nada } } /** * javax.xml.transform.sax.TransformerFactory implementation. * Process the Source into a Templates object, which is a a compiled * representation of the source. * * @param source The input stylesheet - DOMSource not supported!!! * @return A Templates object that can be used to create Transformers. * @throws TransformerConfigurationException */ public Templates newTemplates(Source source) throws TransformerConfigurationException { // If the _useClasspath attribute is true, try to load the translet from // the CLASSPATH and create a template object using the loaded // translet. if (_useClasspath) { String transletName = getTransletBaseName(source); if (_packageName != null) transletName = _packageName + "." + transletName; try { final Class clazz = ObjectFactory.findProviderClass( transletName, ObjectFactory.findClassLoader(), true); resetTransientAttributes(); return new TemplatesImpl(new Class[]{clazz}, transletName, null, _indentNumber, this); } catch (ClassNotFoundException cnfe) { ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, transletName); throw new TransformerConfigurationException(err.toString()); } catch (Exception e) { ErrorMsg err = new ErrorMsg( new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY) + e.getMessage()); throw new TransformerConfigurationException(err.toString()); } } // If _autoTranslet is true, we will try to load the bytecodes // from the translet classes without compiling the stylesheet.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -