PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/fstmerge/examples/iText/rev2818-3786/left-trunk-3786/core/com/lowagie/tools/BuildTutorial.java

https://github.com/RoDaniel/featurehouse
Java | 130 lines | 100 code | 30 blank | 0 comment | 16 complexity | 766e1ae838880dd1c3f984d653753346 MD5 | raw file
  1. package com.lowagie.tools;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import javax.xml.transform.Result;
  8. import javax.xml.transform.Source;
  9. import javax.xml.transform.Templates;
  10. import javax.xml.transform.Transformer;
  11. import javax.xml.transform.TransformerFactory;
  12. import javax.xml.transform.stream.StreamResult;
  13. import javax.xml.transform.stream.StreamSource;
  14. public class BuildTutorial {
  15. static String root;
  16. static FileWriter build;
  17. public static void main(String[] args) {
  18. if (args.length == 4) {
  19. File srcdir = new File(args[0]);
  20. File destdir = new File(args[1]);
  21. File xsl_examples = new File(srcdir, args[2]);
  22. File xsl_site = new File(srcdir, args[3]);
  23. try {
  24. System.out.print("Building tutorial: ");
  25. root = new File(args[1], srcdir.getName()).getCanonicalPath();
  26. System.out.println(root);
  27. build = new FileWriter(new File(root, "build.xml"));
  28. build.write("<project name=\"tutorial\" default=\"all\" basedir=\".\">\n");
  29. build.write("<target name=\"all\">\n");
  30. action(srcdir, destdir, xsl_examples, xsl_site);
  31. build.write("</target>\n</project>");
  32. build.flush();
  33. build.close();
  34. }
  35. catch(IOException ioe) {
  36. ioe.printStackTrace();
  37. }
  38. } else {
  39. System.err
  40. .println("Wrong number of parameters.\nUsage: BuildSite srcdr destdir xsl_examples xsl_site");
  41. }
  42. }
  43. public static void action(File source, File destination, File xsl_examples, File xsl_site) throws IOException {
  44. if (".svn".equals(source.getName())) return;
  45. System.out.print(source.getName());
  46. if (source.isDirectory()) {
  47. System.out.print(" ");
  48. System.out.println(source.getCanonicalPath());
  49. File dest = new File(destination, source.getName());
  50. dest.mkdir();
  51. File current;
  52. File[] xmlFiles = source.listFiles();
  53. if (xmlFiles != null) {
  54. for (int i = 0; i < xmlFiles.length; i++) {
  55. current = xmlFiles[i];
  56. action(current, dest, xsl_examples, xsl_site);
  57. }
  58. }
  59. else {
  60. System.out.println("... skipped");
  61. }
  62. }
  63. else if (source.getName().equals("index.xml")) {
  64. System.out.println("... transformed");
  65. convert(source, xsl_site, new File(destination, "index.php"));
  66. File buildfile = new File(destination, "build.xml");
  67. String path = buildfile.getCanonicalPath().substring(root.length());
  68. path = path.replace(File.separatorChar, '/');
  69. if ("/build.xml".equals(path)) return;
  70. convert(source, xsl_examples, buildfile);
  71. build.write("\t<ant antfile=\"${basedir}");
  72. build.write(path);
  73. build.write("\" target=\"install\" inheritAll=\"false\" />\n");
  74. }
  75. else {
  76. System.out.println("... skipped");
  77. }
  78. }
  79. public static void convert(File infile, File xslfile, File outfile) {
  80. try {
  81. TransformerFactory factory = TransformerFactory.newInstance();
  82. Templates template = factory.newTemplates(new StreamSource(
  83. new FileInputStream(xslfile)));
  84. Transformer xformer = template.newTransformer();
  85. String branch = outfile.getParentFile().getCanonicalPath().substring(root.length());
  86. branch = branch.replace(File.separatorChar, '/');
  87. StringBuffer path = new StringBuffer();
  88. for (int i = 0; i < branch.length(); i++) {
  89. if (branch.charAt(i) == '/') path.append("/..");
  90. }
  91. xformer.setParameter("branch", branch);
  92. xformer.setParameter("root", path.toString());
  93. Source source = new StreamSource(new FileInputStream(infile));
  94. Result result = new StreamResult(new FileOutputStream(outfile));
  95. xformer.transform(source, result);
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. }