You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerializeHyphPattern.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.tools.anttasks;
  19. // Java
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.ObjectOutputStream;
  23. import java.util.List;
  24. // Ant
  25. import org.apache.tools.ant.Task;
  26. import org.apache.tools.ant.DirectoryScanner;
  27. import org.apache.tools.ant.types.FileSet;
  28. // FOP
  29. import org.apache.fop.hyphenation.HyphenationTree;
  30. import org.apache.fop.hyphenation.HyphenationException;
  31. /**
  32. * SerializeHyphPattern
  33. */
  34. public class SerializeHyphPattern extends Task {
  35. private List filesets = new java.util.ArrayList();
  36. private File targetDir;
  37. private boolean errorDump = false;
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public void execute() throws org.apache.tools.ant.BuildException {
  42. // deal with the filesets
  43. for (int i = 0; i < getFilesets().size(); i++) {
  44. FileSet fs = (FileSet) getFilesets().get(i);
  45. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  46. File basedir = ds.getBasedir();
  47. String[] files = ds.getIncludedFiles();
  48. for (int j = 0; j < files.length; j++) {
  49. processFile(basedir, files[j].substring(0, files[j].length() - 4));
  50. }
  51. }
  52. } // end execute
  53. /**
  54. * Adds a set of pattern files (nested fileset attribute).
  55. * @param set a fileset
  56. */
  57. public void addFileset(FileSet set) {
  58. filesets.add(set);
  59. }
  60. /**
  61. * Returns the current list of filesets.
  62. * @return the filesets
  63. */
  64. public List getFilesets() {
  65. return this.filesets;
  66. }
  67. /**
  68. * Sets the target directory
  69. * @param targetDir target directory
  70. */
  71. public void setTargetDir(String targetDir) {
  72. File dir = new File(targetDir);
  73. this.targetDir = dir;
  74. }
  75. /**
  76. * Controls the amount of error information dumped.
  77. * @param errorDump True if more error info should be provided
  78. */
  79. public void setErrorDump(boolean errorDump) {
  80. this.errorDump = errorDump;
  81. }
  82. /*
  83. * checks whether input or output files exists or the latter is older than input file
  84. * and start build if necessary
  85. */
  86. private void processFile(File basedir, String filename) {
  87. File infile = new File(basedir, filename + ".xml");
  88. File outfile = new File(targetDir, filename + ".hyp");
  89. //long outfileLastModified = outfile.lastModified();
  90. boolean startProcess = true;
  91. startProcess = rebuild(infile, outfile);
  92. if (startProcess) {
  93. buildPatternFile(infile, outfile);
  94. }
  95. }
  96. /*
  97. * serializes pattern files
  98. */
  99. private void buildPatternFile(File infile, File outfile) {
  100. System.out.println("Processing " + infile);
  101. HyphenationTree hTree = new HyphenationTree();
  102. try {
  103. hTree.loadPatterns(infile.toString());
  104. if (errorDump) {
  105. System.out.println("Stats: ");
  106. hTree.printStats();
  107. }
  108. } catch (HyphenationException ex) {
  109. System.err.println("Can't load patterns from xml file " + infile
  110. + " - Maybe hyphenation.dtd is missing?");
  111. if (errorDump) {
  112. System.err.println(ex.toString());
  113. }
  114. }
  115. // serialize class
  116. try {
  117. ObjectOutputStream out = new ObjectOutputStream(
  118. new java.io.BufferedOutputStream(
  119. new java.io.FileOutputStream(outfile)));
  120. out.writeObject(hTree);
  121. out.close();
  122. } catch (IOException ioe) {
  123. System.err.println("Can't write compiled pattern file: "
  124. + outfile);
  125. System.err.println(ioe);
  126. }
  127. }
  128. /**
  129. * Checks for existence of output file and compares
  130. * dates with input and stylesheet file
  131. */
  132. private boolean rebuild(File infile, File outfile) {
  133. if (outfile.exists()) {
  134. // checks whether output file is older than input file
  135. if (outfile.lastModified() < infile.lastModified()) {
  136. return true;
  137. }
  138. } else {
  139. // if output file does not exist, start process
  140. return true;
  141. }
  142. return false;
  143. } // end rebuild
  144. /*
  145. * //quick access for debugging
  146. * public static void main (String args[]) {
  147. * SerializeHyphPattern ser = new SerializeHyphPattern();
  148. * FileSet set = new FileSet();
  149. * set.setDir(new File("src/hyph"));
  150. * set.setIncludes("*.xml");
  151. * ser.addFileset(set);
  152. * ser.setTargetDir("build/hyph");
  153. * ser.execute();
  154. * }
  155. */
  156. }