📄 xsltprocess.java
字号:
**/ public void setDestdir(File dir) { destDir = dir; } /** * Set the desired file extension to be used for the target; * optional, default is html. * @param name the extension to use **/ public void setExtension(String name) { targetExtension = name; } /** * Name of the stylesheet to use - given either relative * to the project's basedir or as an absolute path; required. * * @param xslFile the stylesheet to use */ public void setStyle(String xslFile) { this.xslFile = xslFile; } /** * Set the optional classpath to the XSL processor * * @param classpath the classpath to use when loading the XSL processor */ public void setClasspath(Path classpath) { createClasspath().append(classpath); } /** * Set the optional classpath to the XSL processor * * @return a path instance to be configured by the Ant core. */ public Path createClasspath() { if (classpath == null) { classpath = new Path(getProject()); } return classpath.createPath(); } /** * Set the reference to an optional classpath to the XSL processor * * @param r the id of the Ant path instance to act as the classpath * for loading the XSL processor */ public void setClasspathRef(Reference r) { createClasspath().setRefid(r); } /** * Set the name of the XSL processor to use; optional, default trax. * Other values are "xalan" for Xalan1 * * @param processor the name of the XSL processor */ public void setProcessor(String processor) { this.processor = processor; } /** * Whether to use the implicit fileset. * * <p>Set this to false if you want explicit control with nested * resource collections.</p> * @param useimplicitfileset set to true if you want to use implicit fileset * @since Ant 1.7 */ public void setUseImplicitFileset(boolean useimplicitfileset) { useImplicitFileset = useimplicitfileset; } /** * Add the catalog to our internal catalog * * @param xmlCatalog the XMLCatalog instance to use to look up DTDs */ public void addConfiguredXMLCatalog(XMLCatalog xmlCatalog) { this.xmlCatalog.addConfiguredXMLCatalog(xmlCatalog); } /** * Pass the filename of the current processed file as a xsl parameter * to the transformation. This value sets the name of that xsl parameter. * * @param fileNameParameter name of the xsl parameter retrieving the * current file name */ public void setFileNameParameter(String fileNameParameter) { this.fileNameParameter = fileNameParameter; } /** * Pass the directory name of the current processed file as a xsl parameter * to the transformation. This value sets the name of that xsl parameter. * * @param fileDirParameter name of the xsl parameter retrieving the * current file directory */ public void setFileDirParameter(String fileDirParameter) { this.fileDirParameter = fileDirParameter; } /** * Load processor here instead of in setProcessor - this will be * called from within execute, so we have access to the latest * classpath. * * @param proc the name of the processor to load. * @exception Exception if the processor cannot be loaded. */ private void resolveProcessor(String proc) throws Exception { String classname; if (proc.equals(PROCESSOR_TRAX)) { classname = TRAX_LIAISON_CLASS; } else { //anything else is a classname classname = proc; } Class clazz = loadClass(classname); liaison = (XSLTLiaison) clazz.newInstance(); } /** * Load named class either via the system classloader or a given * custom classloader. * * As a side effect, the loader is set as the thread context classloader * @param classname the name of the class to load. * @return the requested class. * @exception Exception if the class could not be loaded. */ private Class loadClass(String classname) throws Exception { if (classpath == null) { return Class.forName(classname); } else { loader = getProject().createClassLoader(classpath); loader.setThreadContextLoader(); Class c = Class.forName(classname, true, loader); return c; } } /** * Specifies the output name for the styled result from the * <tt>in</tt> attribute; required if <tt>in</tt> is set * * @param outFile the output File instance. */ public void setOut(File outFile) { this.outFile = outFile; } /** * specifies a single XML document to be styled. Should be used * with the <tt>out</tt> attribute; ; required if <tt>out</tt> is set * * @param inFile the input file */ public void setIn(File inFile) { this.inFile = inFile; } /** * Throws a BuildException if the destination directory hasn't * been specified. * @since Ant 1.7 */ private void checkDest() { if (destDir == null) { String msg = "destdir attributes must be set!"; throw new BuildException(msg); } } /** * Styles all existing resources. * * @since Ant 1.7 */ private void processResources(Resource stylesheet) { Iterator iter = resources.iterator(); while (iter.hasNext()) { Resource r = (Resource) iter.next(); if (!r.isExists()) { continue; } File base = baseDir; String name = r.getName(); if (r instanceof FileResource) { FileResource f = (FileResource) r; base = f.getBaseDir(); if (base == null) { name = f.getFile().getAbsolutePath(); } } process(base, name, destDir, stylesheet); } } /** * Processes the given input XML file and stores the result * in the given resultFile. * * @param baseDir the base directory for resolving files. * @param xmlFile the input file * @param destDir the destination directory * @param stylesheet the stylesheet to use. * @exception BuildException if the processing fails. */ private void process(File baseDir, String xmlFile, File destDir, Resource stylesheet) throws BuildException { File outF = null; File inF = null; try { long styleSheetLastModified = stylesheet.getLastModified(); inF = new File(baseDir, xmlFile); if (inF.isDirectory()) { log("Skipping " + inF + " it is a directory.", Project.MSG_VERBOSE); return; } FileNameMapper mapper = null; if (mapperElement != null) { mapper = mapperElement.getImplementation(); } else { mapper = new StyleMapper(); } String[] outFileName = mapper.mapFileName(xmlFile); if (outFileName == null || outFileName.length == 0) { log("Skipping " + inFile + " it cannot get mapped to output.", Project.MSG_VERBOSE); return; } else if (outFileName == null || outFileName.length > 1) { log("Skipping " + inFile + " its mapping is ambiguos.", Project.MSG_VERBOSE); return; } outF = new File(destDir, outFileName[0]); if (force || inF.lastModified() > outF.lastModified() || styleSheetLastModified > outF.lastModified()) { ensureDirectoryFor(outF); log("Processing " + inF + " to " + outF); configureLiaison(stylesheet); setLiaisonDynamicFileParameters(liaison, inF); liaison.transform(inF, outF); } } catch (Exception ex) { // If failed to process document, must delete target document, // or it will not attempt to process it the second time log("Failed to process " + inFile, Project.MSG_INFO); if (outF != null) { outF.delete(); } throw new BuildException(ex); } } //-- processXML /** * Process the input file to the output file with the given stylesheet. * * @param inFile the input file to process. * @param outFile the destination file. * @param stylesheet the stylesheet to use. * @exception BuildException if the processing fails. */ private void process(File inFile, File outFile, Resource stylesheet) throws BuildException { try { long styleSheetLastModified = stylesheet.getLastModified(); log("In file " + inFile + " time: " + inFile.lastModified(), Project.MSG_DEBUG); log("Out file " + outFile + " time: " + outFile.lastModified(), Project.MSG_DEBUG); log("Style file " + xslFile + " time: " + styleSheetLastModified, Project.MSG_DEBUG); if (force || inFile.lastModified() >= outFile.lastModified() || styleSheetLastModified >= outFile.lastModified()) { ensureDirectoryFor(outFile); log("Processing " + inFile + " to " + outFile, Project.MSG_INFO); configureLiaison(stylesheet); setLiaisonDynamicFileParameters(liaison, inFile); liaison.transform(inFile, outFile); } else { log("Skipping input file " + inFile + " because it is older than output file " + outFile + " and so is the stylesheet " + stylesheet, Project.MSG_DEBUG); } } catch (Exception ex) { log("Failed to process " + inFile, Project.MSG_INFO); if (outFile != null) { outFile.delete(); } throw new BuildException(ex); } } /** * Ensure the directory exists for a given file * * @param targetFile the file for which the directories are required. * @exception BuildException if the directories cannot be created. */ private void ensureDirectoryFor(File targetFile) throws BuildException { File directory = targetFile.getParentFile(); if (!directory.exists()) { if (!directory.mkdirs()) { throw new BuildException("Unable to create directory: " + directory.getAbsolutePath()); } } } /** * Get the factory instance configured for this processor * * @return the factory instance in use */ public Factory getFactory() { return factory; } /** * Get the XML catalog containing entity definitions * * @return the XML catalog for the task. */ public XMLCatalog getXMLCatalog() { xmlCatalog.setProject(getProject()); return xmlCatalog; } /** * Get an enumeration on the outputproperties. * @return the outputproperties */ public Enumeration getOutputProperties() { return outputProperties.elements(); } /** * Get the Liason implementation to use in processing. * * @return an instance of the XSLTLiason interface. */ protected XSLTLiaison getLiaison() { // if processor wasn't specified, see if TraX is available. If not, // default it to xalan, depending on which is in the classpath if (liaison == null) { if (processor != null) { try { resolveProcessor(processor); } catch (Exception e) { throw new BuildException(e); } } else { try { resolveProcessor(PROCESSOR_TRAX); } catch (Throwable e1) { e1.printStackTrace(); throw new BuildException(e1); } } } return liaison; } /** * Create an instance of an XSL parameter for configuration by Ant. * * @return an instance of the Param class to be configured. */ public Param createParam() { Param p = new Param(); params.addElement(p); return p; } /** * The Param inner class used to store XSL parameters */ public static class Param { /** The parameter name */ private String name = null; /** The parameter's value */ private String expression = null; private String ifProperty;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -