001: /*
002: * $Id: BuildTutorial.java 2865 2007-07-05 13:44:12Z blowagie $
003: * $Name$
004: *
005: * Copyright 2005 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050: package com.lowagie.tools;
051:
052: import java.io.File;
053: import java.io.FileInputStream;
054: import java.io.FileOutputStream;
055: import java.io.FileWriter;
056: import java.io.IOException;
057:
058: import javax.xml.transform.Result;
059: import javax.xml.transform.Source;
060: import javax.xml.transform.Templates;
061: import javax.xml.transform.Transformer;
062: import javax.xml.transform.TransformerFactory;
063: import javax.xml.transform.stream.StreamResult;
064: import javax.xml.transform.stream.StreamSource;
065:
066: /**
067: * This class can be used to build the iText website.
068: *
069: * @author Bruno Lowagie
070: */
071: public class BuildTutorial {
072:
073: static String root;
074: static FileWriter build;
075:
076: //~ Methods
077: // ----------------------------------------------------------------
078:
079: /**
080: * Main method so you can call the convert method from the command line.
081: * @param args 4 arguments are expected:
082: * <ul><li>a sourcedirectory (root of the tutorial xml-files),
083: * <li>a destination directory (where the html and build.xml files will be generated),
084: * <li>an xsl to transform the index.xml into a build.xml
085: * <li>an xsl to transform the index.xml into am index.html</ul>
086: */
087:
088: public static void main(String[] args) {
089: if (args.length == 4) {
090: File srcdir = new File(args[0]);
091: File destdir = new File(args[1]);
092: File xsl_examples = new File(srcdir, args[2]);
093: File xsl_site = new File(srcdir, args[3]);
094: try {
095: System.out.print("Building tutorial: ");
096: root = new File(args[1], srcdir.getName())
097: .getCanonicalPath();
098: System.out.println(root);
099: build = new FileWriter(new File(root, "build.xml"));
100: build
101: .write("<project name=\"tutorial\" default=\"all\" basedir=\".\">\n");
102: build.write("<target name=\"all\">\n");
103: action(srcdir, destdir, xsl_examples, xsl_site);
104: build.write("</target>\n</project>");
105: build.flush();
106: build.close();
107: } catch (IOException ioe) {
108: ioe.printStackTrace();
109: }
110: } else {
111: System.err
112: .println("Wrong number of parameters.\nUsage: BuildSite srcdr destdir xsl_examples xsl_site");
113: }
114: }
115:
116: /**
117: * Inspects a file or directory that is given and performs the necessary actions on it (transformation or recursion).
118: * @param source a sourcedirectory (possibly with a tutorial xml-file)
119: * @param destination a destination directory (where the html and build.xml file will be generated, if necessary)
120: * @param xsl_examples an xsl to transform the index.xml into a build.xml
121: * @param xsl_site an xsl to transform the index.xml into am index.html
122: * @throws IOException when something goes wrong while reading or creating a file or directory
123: */
124: public static void action(File source, File destination,
125: File xsl_examples, File xsl_site) throws IOException {
126: if (".svn".equals(source.getName()))
127: return;
128: System.out.print(source.getName());
129: if (source.isDirectory()) {
130: System.out.print(" ");
131: System.out.println(source.getCanonicalPath());
132: File dest = new File(destination, source.getName());
133: dest.mkdir();
134: File current;
135: File[] xmlFiles = source.listFiles();
136: if (xmlFiles != null) {
137: for (int i = 0; i < xmlFiles.length; i++) {
138: current = xmlFiles[i];
139: action(current, dest, xsl_examples, xsl_site);
140: }
141: } else {
142: System.out.println("... skipped");
143: }
144: } else if (source.getName().equals("index.xml")) {
145: System.out.println("... transformed");
146: convert(source, xsl_site, new File(destination,
147: "index.html"));
148: File buildfile = new File(destination, "build.xml");
149: String path = buildfile.getCanonicalPath().substring(
150: root.length());
151: path = path.replace(File.separatorChar, '/');
152: if ("/build.xml".equals(path))
153: return;
154: convert(source, xsl_examples, buildfile);
155: build.write("\t<ant antfile=\"${basedir}");
156: build.write(path);
157: build
158: .write("\" target=\"install\" inheritAll=\"false\" />\n");
159: } else {
160: System.out.println("... skipped");
161: }
162: }
163:
164: /**
165: * Converts an <code>infile</code>, using an <code>xslfile</code> to an
166: * <code>outfile</code>.
167: *
168: * @param infile
169: * the path to an XML file
170: * @param xslfile
171: * the path to the XSL file
172: * @param outfile
173: * the path for the output file
174: */
175: public static void convert(File infile, File xslfile, File outfile) {
176: try {
177: // Create transformer factory
178: TransformerFactory factory = TransformerFactory
179: .newInstance();
180:
181: // Use the factory to create a template containing the xsl file
182: Templates template = factory.newTemplates(new StreamSource(
183: new FileInputStream(xslfile)));
184:
185: // Use the template to create a transformer
186: Transformer xformer = template.newTransformer();
187:
188: // passing 2 parameters
189: String branch = outfile.getParentFile().getCanonicalPath()
190: .substring(root.length());
191: branch = branch.replace(File.separatorChar, '/');
192: StringBuffer path = new StringBuffer();
193: for (int i = 0; i < branch.length(); i++) {
194: if (branch.charAt(i) == '/')
195: path.append("/..");
196: }
197:
198: xformer.setParameter("branch", branch);
199: xformer.setParameter("root", path.toString());
200:
201: // Prepare the input and output files
202: Source source = new StreamSource(
203: new FileInputStream(infile));
204: Result result = new StreamResult(new FileOutputStream(
205: outfile));
206:
207: // Apply the xsl file to the source file and write the result to the
208: // output file
209: xformer.transform(source, result);
210: } catch (Exception e) {
211: e.printStackTrace();
212: }
213: }
214: }
215: //The End
|