📄 traxliaison.java
字号:
/** * Read in templates from the stylesheet */ private void readTemplates() throws IOException, TransformerConfigurationException, ParserConfigurationException, SAXException { // Use a stream so that you can close it yourself quickly // and avoid keeping the handle until the object is garbaged. // (always keep control), otherwise you won't be able to delete // the file quickly on windows. InputStream xslStream = null; try { xslStream = new BufferedInputStream(stylesheet.getInputStream()); templatesModTime = stylesheet.getLastModified(); Source src = getSource(xslStream, stylesheet); templates = getFactory().newTemplates(src); } finally { if (xslStream != null) { xslStream.close(); } } } /** * Create a new transformer based on the liaison settings * @throws Exception thrown if there is an error during creation. * @see #setStylesheet(java.io.File) * @see #addParam(java.lang.String, java.lang.String) * @see #setOutputProperty(java.lang.String, java.lang.String) */ private void createTransformer() throws Exception { if (templates == null) { readTemplates(); } transformer = templates.newTransformer(); // configure the transformer... transformer.setErrorListener(this); if (uriResolver != null) { transformer.setURIResolver(uriResolver); } for (int i = 0; i < outputProperties.size(); i++) { final String[] pair = (String[]) outputProperties.elementAt(i); transformer.setOutputProperty(pair[0], pair[1]); } } /** * Sets the paramters for the transformer. */ private void setTransformationParameters() { for (final Enumeration enumeration = params.keys(); enumeration.hasMoreElements();) { final String name = (String) enumeration.nextElement(); final String value = (String) params.get(name); transformer.setParameter(name, value); } } /** * return the Transformer factory associated to this liaison. * @return the Transformer factory associated to this liaison. * @throws BuildException thrown if there is a problem creating * the factory. * @see #setFactory(String) * @since Ant 1.5.2 */ private TransformerFactory getFactory() throws BuildException { if (tfactory != null) { return tfactory; } // not initialized yet, so create the factory if (factoryName == null) { tfactory = TransformerFactory.newInstance(); } else { try { Class clazz = Class.forName(factoryName); tfactory = (TransformerFactory) clazz.newInstance(); } catch (Exception e) { throw new BuildException(e); } } tfactory.setErrorListener(this); // specific attributes for the transformer for (int i = 0; i < attributes.size(); i++) { final Object[] pair = (Object[]) attributes.elementAt(i); tfactory.setAttribute((String) pair[0], pair[1]); } if (uriResolver != null) { tfactory.setURIResolver(uriResolver); } return tfactory; } /** * Set the factory name to use instead of JAXP default lookup. * @param name the fully qualified class name of the factory to use * or null for the default JAXP look up mechanism. * @since Ant 1.6 */ public void setFactory(String name) { factoryName = name; } /** * Set a custom attribute for the JAXP factory implementation. * @param name the attribute name. * @param value the value of the attribute, usually a boolean * string or object. * @since Ant 1.6 */ public void setAttribute(String name, Object value) { final Object[] pair = new Object[]{name, value}; attributes.addElement(pair); } /** * Set the output property for the current transformer. * Note that the stylesheet must be set prior to calling * this method. * @param name the output property name. * @param value the output property value. * @since Ant 1.5 * @since Ant 1.5 */ public void setOutputProperty(String name, String value) { final String[] pair = new String[]{name, value}; outputProperties.addElement(pair); } /** * Set the class to resolve entities during the transformation. * @param aResolver the resolver class. */ public void setEntityResolver(EntityResolver aResolver) { entityResolver = aResolver; } /** * Set the class to resolve URIs during the transformation * @param aResolver a <code>EntityResolver</code> value */ public void setURIResolver(URIResolver aResolver) { uriResolver = aResolver; } /** * Add a parameter. * @param name the name of the parameter * @param value the value of the parameter */ public void addParam(String name, String value) { params.put(name, value); } /** * Set a logger. * @param l a logger. */ public void setLogger(XSLTLogger l) { logger = l; } /** * Log an error. * @param e the exception to log. */ public void error(TransformerException e) { logError(e, "Error"); } /** * Log a fatal error. * @param e the exception to log. */ public void fatalError(TransformerException e) { logError(e, "Fatal Error"); throw new BuildException("Fatal error during transformation", e); } /** * Log a warning. * @param e the exception to log. */ public void warning(TransformerException e) { logError(e, "Warning"); } private void logError(TransformerException e, String type) { if (logger == null) { return; } StringBuffer msg = new StringBuffer(); SourceLocator locator = e.getLocator(); if (locator != null) { String systemid = locator.getSystemId(); if (systemid != null) { String url = systemid; if (url.startsWith("file:")) { url = FileUtils.getFileUtils().fromURI(url); } msg.append(url); } else { msg.append("Unknown file"); } int line = locator.getLineNumber(); if (line != -1) { msg.append(":"); msg.append(line); int column = locator.getColumnNumber(); if (column != -1) { msg.append(":"); msg.append(column); } } } msg.append(": "); msg.append(type); msg.append("! "); msg.append(e.getMessage()); if (e.getCause() != null) { msg.append(" Cause: "); msg.append(e.getCause()); } logger.log(msg.toString()); } // kept for backwards compatibility /** * @param file the filename to use for the systemid * @return the systemid * @deprecated since 1.5.x. * Use org.apache.tools.ant.util.JAXPUtils#getSystemId instead. */ protected String getSystemId(File file) { return JAXPUtils.getSystemId(file); } /** * Specific configuration for the TRaX liaison. * @param xsltTask the XSLTProcess task instance from which this liasion * is to be configured. */ public void configure(XSLTProcess xsltTask) { project = xsltTask.getProject(); XSLTProcess.Factory factory = xsltTask.getFactory(); if (factory != null) { setFactory(factory.getName()); // configure factory attributes for (Enumeration attrs = factory.getAttributes(); attrs.hasMoreElements();) { XSLTProcess.Factory.Attribute attr = (XSLTProcess.Factory.Attribute) attrs.nextElement(); setAttribute(attr.getName(), attr.getValue()); } } XMLCatalog xmlCatalog = xsltTask.getXMLCatalog(); // use XMLCatalog as the entity resolver and URI resolver if (xmlCatalog != null) { setEntityResolver(xmlCatalog); setURIResolver(xmlCatalog); } // configure output properties for (Enumeration props = xsltTask.getOutputProperties(); props.hasMoreElements();) { XSLTProcess.OutputProperty prop = (XSLTProcess.OutputProperty) props.nextElement(); setOutputProperty(prop.getName(), prop.getValue()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -