KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > tools > BuildTutorial


1 /*
2  * $Id: BuildTutorial.java 2366 2006-09-14 23:10:58Z xlv $
3  * $Name$
4  *
5  * Copyright 2005 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50 package com.lowagie.tools;
51
52 import java.io.File JavaDoc;
53 import java.io.FileInputStream JavaDoc;
54 import java.io.FileOutputStream JavaDoc;
55 import java.io.FileWriter JavaDoc;
56 import java.io.IOException JavaDoc;
57
58 import javax.xml.transform.Result JavaDoc;
59 import javax.xml.transform.Source JavaDoc;
60 import javax.xml.transform.Templates JavaDoc;
61 import javax.xml.transform.Transformer JavaDoc;
62 import javax.xml.transform.TransformerFactory JavaDoc;
63 import javax.xml.transform.stream.StreamResult JavaDoc;
64 import javax.xml.transform.stream.StreamSource JavaDoc;
65
66 /**
67  * This class can be used to build the iText website.
68  *
69  * @author Bruno Lowagie
70  */

71 public class BuildTutorial {
72
73     static String JavaDoc root;
74     static FileWriter JavaDoc build;
75     
76     //~ Methods
77
// ----------------------------------------------------------------
78

79     /**
80      * Main method so you can call the convert method from the command line.
81      * @param args 4 arguments are expected:
82      * <ul><li>a sourcedirectory (root of the tutorial xml-files),
83      * <li>a destination directory (where the html and build.xml files will be generated),
84      * <li>an xsl to transform the index.xml into a build.xml
85      * <li>an xsl to transform the index.xml into am index.html</ul>
86      */

87
88     public static void main(String JavaDoc[] args) {
89         if (args.length == 4) {
90             File JavaDoc srcdir = new File JavaDoc(args[0]);
91             File JavaDoc destdir = new File JavaDoc(args[1]);
92             File JavaDoc xsl_examples = new File JavaDoc(srcdir, args[2]);
93             File JavaDoc xsl_site = new File JavaDoc(srcdir, args[3]);
94             try {
95                 System.out.print("Building tutorial: ");
96                 root = new File JavaDoc(args[1], srcdir.getName()).getCanonicalPath();
97                 System.out.println(root);
98                 build = new FileWriter JavaDoc(new File JavaDoc(root, "build.xml"));
99                 build.write("<project name=\"tutorial\" default=\"all\" basedir=\".\">\n");
100                 build.write("<target name=\"all\">\n");
101                 action(srcdir, destdir, xsl_examples, xsl_site);
102                 build.write("</target>\n</project>");
103                 build.flush();
104                 build.close();
105             }
106             catch(IOException JavaDoc ioe) {
107                 ioe.printStackTrace();
108             }
109         } else {
110             System.err
111                     .println("Wrong number of parameters.\nUsage: BuildSite srcdr destdir xsl_examples xsl_site");
112         }
113     }
114
115     /**
116      * Inspects a file or directory that is given and performs the necessary actions on it (transformation or recursion).
117      * @param source a sourcedirectory (possibly with a tutorial xml-file)
118      * @param destination a destination directory (where the html and build.xml file will be generated, if necessary)
119      * @param xsl_examples an xsl to transform the index.xml into a build.xml
120      * @param xsl_site an xsl to transform the index.xml into am index.html
121      * @throws IOException when something goes wrong while reading or creating a file or directory
122      */

123     public static void action(File JavaDoc source, File JavaDoc destination, File JavaDoc xsl_examples, File JavaDoc xsl_site) throws IOException JavaDoc {
124         if ("CVS".equals(source.getName())) return;
125         System.out.print(source.getName());
126         if (source.isDirectory()) {
127             System.out.print(" ");
128             System.out.println(source.getCanonicalPath());
129             File JavaDoc dest = new File JavaDoc(destination, source.getName());
130             dest.mkdir();
131             File JavaDoc current;
132             File JavaDoc[] xmlFiles = source.listFiles();
133             if (xmlFiles != null) {
134                 for (int i = 0; i < xmlFiles.length; i++) {
135                     current = xmlFiles[i];
136                     action(current, dest, xsl_examples, xsl_site);
137                 }
138             }
139             else {
140                 System.out.println("... skipped");
141             }
142         }
143         else if (source.getName().equals("index.xml")) {
144             System.out.println("... transformed");
145             convert(source, xsl_site, new File JavaDoc(destination, "index.html"));
146             File JavaDoc buildfile = new File JavaDoc(destination, "build.xml");
147             String JavaDoc path = buildfile.getCanonicalPath().substring(root.length());
148             path = path.replace(File.separatorChar, '/');
149             if ("/build.xml".equals(path)) return;
150             convert(source, xsl_examples, buildfile);
151             build.write("\t<ant antfile=\"${basedir}");
152             build.write(path);
153             build.write("\" target=\"install\" inheritAll=\"false\" />\n");
154         }
155         else {
156             System.out.println("... skipped");
157         }
158     }
159     
160     /**
161      * Converts an <code>infile</code>, using an <code>xslfile</code> to an
162      * <code>outfile</code>.
163      *
164      * @param infile
165      * the path to an XML file
166      * @param xslfile
167      * the path to the XSL file
168      * @param outfile
169      * the path for the output file
170      */

171     public static void convert(File JavaDoc infile, File JavaDoc xslfile, File JavaDoc outfile) {
172         try {
173             // Create transformer factory
174
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
175
176             // Use the factory to create a template containing the xsl file
177
Templates JavaDoc template = factory.newTemplates(new StreamSource JavaDoc(
178                     new FileInputStream JavaDoc(xslfile)));
179
180             // Use the template to create a transformer
181
Transformer JavaDoc xformer = template.newTransformer();
182             
183             // passing 2 parameters
184
String JavaDoc branch = outfile.getParentFile().getCanonicalPath().substring(root.length());
185             branch = branch.replace(File.separatorChar, '/');
186             StringBuffer JavaDoc path = new StringBuffer JavaDoc();
187             for (int i = 0; i < branch.length(); i++) {
188                 if (branch.charAt(i) == '/') path.append("/..");
189             }
190             
191             xformer.setParameter("branch", branch);
192             xformer.setParameter("root", path.toString());
193
194             // Prepare the input and output files
195
Source JavaDoc source = new StreamSource JavaDoc(new FileInputStream JavaDoc(infile));
196             Result JavaDoc result = new StreamResult JavaDoc(new FileOutputStream JavaDoc(outfile));
197
198             // Apply the xsl file to the source file and write the result to the
199
// output file
200
xformer.transform(source, result);
201         } catch (Exception JavaDoc e) {
202             e.printStackTrace();
203         }
204     }
205 }
206 //The End
207
Popular Tags