📄 xsltprocess.java
字号:
private String unlessProperty; private Project project; /** * Set the current project * * @param project the current project */ public void setProject(Project project) { this.project = project; } /** * Set the parameter name. * * @param name the name of the parameter. */ public void setName(String name) { this.name = name; } /** * The parameter value * NOTE : was intended to be an XSL expression. * @param expression the parameter's value. */ public void setExpression(String expression) { this.expression = expression; } /** * Get the parameter name * * @return the parameter name * @exception BuildException if the name is not set. */ public String getName() throws BuildException { if (name == null) { throw new BuildException("Name attribute is missing."); } return name; } /** * Get the parameter's value * * @return the parameter value * @exception BuildException if the value is not set. */ public String getExpression() throws BuildException { if (expression == null) { throw new BuildException("Expression attribute is missing."); } return expression; } /** * Set whether this param should be used. It will be * used if the property has been set, otherwise it won't. * @param ifProperty name of property */ public void setIf(String ifProperty) { this.ifProperty = ifProperty; } /** * Set whether this param should NOT be used. It * will not be used if the property has been set, otherwise it * will be used. * @param unlessProperty name of property */ public void setUnless(String unlessProperty) { this.unlessProperty = unlessProperty; } /** * Ensures that the param passes the conditions placed * on it with <code>if</code> and <code>unless</code> properties. * @return true if the task passes the "if" and "unless" parameters */ public boolean shouldUse() { if (ifProperty != null && project.getProperty(ifProperty) == null) { return false; } else if (unlessProperty != null && project.getProperty(unlessProperty) != null) { return false; } return true; } } // Param /** * Create an instance of an output property to be configured. * @return the newly created output property. * @since Ant 1.5 */ public OutputProperty createOutputProperty() { OutputProperty p = new OutputProperty(); outputProperties.addElement(p); return p; } /** * Specify how the result tree should be output as specified * in the <a href="http://www.w3.org/TR/xslt#output"> * specification</a>. * @since Ant 1.5 */ public static class OutputProperty { /** output property name */ private String name; /** output property value */ private String value; /** * @return the output property name. */ public String getName() { return name; } /** * set the name for this property * @param name A non-null String that specifies an * output property name, which may be namespace qualified. */ public void setName(String name) { this.name = name; } /** * @return the output property value. */ public String getValue() { return value; } /** * set the value for this property * @param value The non-null string value of the output property. */ public void setValue(String value) { this.value = value; } } /** * Initialize internal instance of XMLCatalog * @throws BuildException on error */ public void init() throws BuildException { super.init(); xmlCatalog.setProject(getProject()); } /** * Loads the stylesheet and set xsl:param parameters. * * @param stylesheet the file from which to load the stylesheet. * @exception BuildException if the stylesheet cannot be loaded. * @deprecated since Ant 1.7 */ protected void configureLiaison(File stylesheet) throws BuildException { FileResource fr = new FileResource(); fr.setProject(getProject()); fr.setFile(stylesheet); configureLiaison(fr); } /** * Loads the stylesheet and set xsl:param parameters. * * @param stylesheet the resource from which to load the stylesheet. * @exception BuildException if the stylesheet cannot be loaded. * @since Ant 1.7 */ protected void configureLiaison(Resource stylesheet) throws BuildException { if (stylesheetLoaded && reuseLoadedStylesheet) { return; } stylesheetLoaded = true; try { log("Loading stylesheet " + stylesheet, Project.MSG_INFO); // We call liason.configure() and then liaison.setStylesheet() // so that the internal variables of liaison can be set up if (liaison instanceof XSLTLiaison2) { ((XSLTLiaison2) liaison).configure(this); } if (liaison instanceof XSLTLiaison3) { // If we are here we can set the stylesheet as a // resource ((XSLTLiaison3) liaison).setStylesheet(stylesheet); } else { // If we are here we cannot set the stylesheet as // a resource, but we can set it as a file. So, // we make an attempt to get it as a file if (stylesheet instanceof FileResource) { liaison.setStylesheet( ((FileResource) stylesheet).getFile()); } else { throw new BuildException(liaison.getClass().toString() + " accepts the stylesheet only as a file", getLocation()); } } for (Enumeration e = params.elements(); e.hasMoreElements();) { Param p = (Param) e.nextElement(); if (p.shouldUse()) { liaison.addParam(p.getName(), p.getExpression()); } } } catch (Exception ex) { log("Failed to transform using stylesheet " + stylesheet, Project.MSG_INFO); throw new BuildException(ex); } } /** * Sets file parameter(s) for directory and filename if the attribute * 'filenameparameter' or 'filedirparameter' are set in the task. * * @param liaison to change parameters for * @param inFile to get the additional file information from * @throws Exception if an exception occurs on filename lookup * * @since Ant 1.7 */ private void setLiaisonDynamicFileParameters( XSLTLiaison liaison, File inFile ) throws Exception { if (fileNameParameter != null) { liaison.addParam(fileNameParameter, inFile.getName()); } if (fileDirParameter != null) { String fileName = FileUtils.getRelativePath(baseDir, inFile); File file = new File(fileName); // Give always a slash as file separator, so the stylesheet could be sure about that // Use '.' so a dir+"/"+name would not result in an absolute path liaison.addParam( fileDirParameter, (file.getParent() != null) ? file.getParent().replace('\\', '/') : "."); } } /** * Create the factory element to configure a trax liaison. * @return the newly created factory element. * @throws BuildException if the element is created more than one time. */ public Factory createFactory() throws BuildException { if (factory != null) { throw new BuildException("'factory' element must be unique"); } factory = new Factory(); return factory; } /** * The factory element to configure a transformer factory * @since Ant 1.6 */ public static class Factory { /** the factory class name to use for TraXLiaison */ private String name; /** * the list of factory attributes to use for TraXLiaison */ private Vector attributes = new Vector(); /** * @return the name of the factory. */ public String getName() { return name; } /** * Set the name of the factory * @param name the name of the factory. */ public void setName(String name) { this.name = name; } /** * Create an instance of a factory attribute. * @param attr the newly created factory attribute */ public void addAttribute(Attribute attr) { attributes.addElement(attr); } /** * return the attribute elements. * @return the enumeration of attributes */ public Enumeration getAttributes() { return attributes.elements(); } /** * A JAXP factory attribute. This is mostly processor specific, for * example for Xalan 2.3+, the following attributes could be set: * <ul> * <li>http://xml.apache.org/xalan/features/optimize (true|false) </li> * <li>http://xml.apache.org/xalan/features/incremental (true|false) </li> * </ul> */ public static class Attribute implements DynamicConfigurator { /** attribute name, mostly processor specific */ private String name; /** attribute value, often a boolean string */ private Object value; /** * @return the attribute name. */ public String getName() { return name; } /** * @return the output property value. */ public Object getValue() { return value; } /** * Not used. * @param name not used * @return null * @throws BuildException never */ public Object createDynamicElement(String name) throws BuildException { return null; } /** * Set an attribute. * Only "name" and "value" are supported as names. * @param name the name of the attribute * @param value the value of the attribute * @throws BuildException on error */ public void setDynamicAttribute(String name, String value) throws BuildException { // only 'name' and 'value' exist. if ("name".equalsIgnoreCase(name)) { this.name = value; } else if ("value".equalsIgnoreCase(name)) { // a value must be of a given type // say boolean|integer|string that are mostly used. if ("true".equalsIgnoreCase(value)) { this.value = Boolean.TRUE; } else if ("false".equalsIgnoreCase(value)) { this.value = Boolean.FALSE; } else { try { this.value = new Integer(value); } catch (NumberFormatException e) { this.value = value; } } } else { throw new BuildException("Unsupported attribute: " + name); } } } // -- class Attribute } // -- class Factory /** * Mapper implementation of the "traditional" way <xslt> * mapped filenames. * * <p>If the file has an extension, chop it off. Append whatever * the user has specified as extension or ".html".</p> * * @since Ant 1.6.2 */ private class StyleMapper implements FileNameMapper { public void setFrom(String from) { } public void setTo(String to) { } public String[] mapFileName(String xmlFile) { int dotPos = xmlFile.lastIndexOf('.'); if (dotPos > 0) { xmlFile = xmlFile.substring(0, dotPos); } return new String[] {xmlFile + targetExtension}; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -