1 50 package com.lowagie.tools; 51 52 import java.io.File ; 53 import java.io.FileInputStream ; 54 import java.io.FileOutputStream ; 55 import java.io.FileWriter ; 56 import java.io.IOException ; 57 58 import javax.xml.transform.Result ; 59 import javax.xml.transform.Source ; 60 import javax.xml.transform.Templates ; 61 import javax.xml.transform.Transformer ; 62 import javax.xml.transform.TransformerFactory ; 63 import javax.xml.transform.stream.StreamResult ; 64 import javax.xml.transform.stream.StreamSource ; 65 66 71 public class BuildTutorial { 72 73 static String root; 74 static FileWriter build; 75 76 79 87 88 public static void main(String [] args) { 89 if (args.length == 4) { 90 File srcdir = new File (args[0]); 91 File destdir = new File (args[1]); 92 File xsl_examples = new File (srcdir, args[2]); 93 File xsl_site = new File (srcdir, args[3]); 94 try { 95 System.out.print("Building tutorial: "); 96 root = new File (args[1], srcdir.getName()).getCanonicalPath(); 97 System.out.println(root); 98 build = new FileWriter (new File (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 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 123 public static void action(File source, File destination, File xsl_examples, File xsl_site) throws IOException { 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 dest = new File (destination, source.getName()); 130 dest.mkdir(); 131 File current; 132 File [] 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 (destination, "index.html")); 146 File buildfile = new File (destination, "build.xml"); 147 String 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 171 public static void convert(File infile, File xslfile, File outfile) { 172 try { 173 TransformerFactory factory = TransformerFactory.newInstance(); 175 176 Templates template = factory.newTemplates(new StreamSource ( 178 new FileInputStream (xslfile))); 179 180 Transformer xformer = template.newTransformer(); 182 183 String branch = outfile.getParentFile().getCanonicalPath().substring(root.length()); 185 branch = branch.replace(File.separatorChar, '/'); 186 StringBuffer path = new StringBuffer (); 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 Source source = new StreamSource (new FileInputStream (infile)); 196 Result result = new StreamResult (new FileOutputStream (outfile)); 197 198 xformer.transform(source, result); 201 } catch (Exception e) { 202 e.printStackTrace(); 203 } 204 } 205 } 206 | Popular Tags |