/* * $Id: BuildTutorial.java,v 1.8 2004/12/10 14:21:01 blowagie Exp $ * $Name: $ * * This code is free software. It may only be copied or modified * if you include the following copyright notice: * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * itext@lowagie.com */ package com.lowagie.tools; import java.io.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; /** * This class can be used to build the iText website. * * @author Bruno Lowagie (based on an example found in the Developer's Almanac) */ public class BuildTutorial { static String root; static FileWriter build; //~ Methods // ---------------------------------------------------------------- /** * Main method so you can call the convert method from the command line. * @param args 4 arguments are expected: *
infile
, using an xslfile
to an
* outfile
.
*
* @param infile
* the path to an XML file
* @param xslfile
* the path to the XSL file
* @param outfile
* the path for the output file
*/
public static void convert(File infile, File xslfile, File outfile) {
try {
// Create transformer factory
TransformerFactory factory = TransformerFactory.newInstance();
// Use the factory to create a template containing the xsl file
Templates template = factory.newTemplates(new StreamSource(
new FileInputStream(xslfile)));
// Use the template to create a transformer
Transformer xformer = template.newTransformer();
// passing 2 parameters
String branch = outfile.getParentFile().getCanonicalPath().substring(root.length());
branch = branch.replace(File.separatorChar, '/');
StringBuffer path = new StringBuffer();
for (int i = 0; i < branch.length(); i++) {
if (branch.charAt(i) == '/') path.append("/..");
}
xformer.setParameter("branch", branch);
xformer.setParameter("root", path.toString());
// Prepare the input and output files
Source source = new StreamSource(new FileInputStream(infile));
Result result = new StreamResult(new FileOutputStream(outfile));
// Apply the xsl file to the source file and write the result to the
// output file
xformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//The End