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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.hyphenation;
  19. import java.io.File;
  20. import java.io.FilenameFilter;
  21. import java.io.IOException;
  22. import java.io.ObjectOutputStream;
  23. /**
  24. * Serialize hyphenation patterns
  25. * For all xml files in the source directory a pattern file is built in the target directory
  26. * This class may be called from the ant build file in a java task
  27. */
  28. public class SerializeHyphPattern {
  29. private boolean errorDump = false;
  30. /**
  31. * Controls the amount of error information dumped.
  32. * @param errorDump True if more error info should be provided
  33. */
  34. public void setErrorDump(boolean errorDump) {
  35. this.errorDump = errorDump;
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public void serializeDir(File sourceDir, File targetDir) {
  41. final String extension = ".xml";
  42. String[] sourceFiles = sourceDir.list(new FilenameFilter() {
  43. public boolean accept(File dir, String name) {
  44. return(name.endsWith(extension));
  45. }
  46. });
  47. for (int j = 0; j < sourceFiles.length; j++) {
  48. File infile = new File(sourceDir, sourceFiles[j]);
  49. String outfilename =
  50. sourceFiles[j].substring(0, sourceFiles[j].length() - extension.length()) + ".hyp";
  51. File outfile = new File(targetDir, outfilename);
  52. serializeFile(infile, outfile);
  53. }
  54. }
  55. /*
  56. * checks whether input or output files exists or the latter is older than input file
  57. * and start build if necessary
  58. */
  59. private void serializeFile(File infile, File outfile) {
  60. boolean startProcess;
  61. startProcess = rebuild(infile, outfile);
  62. if (startProcess) {
  63. HyphenationTree hTree = buildPatternFile(infile);
  64. // serialize class
  65. try {
  66. ObjectOutputStream out = new ObjectOutputStream(
  67. new java.io.BufferedOutputStream(
  68. new java.io.FileOutputStream(outfile)));
  69. out.writeObject(hTree);
  70. out.close();
  71. } catch (IOException ioe) {
  72. System.err.println("Can't write compiled pattern file: "
  73. + outfile);
  74. System.err.println(ioe);
  75. }
  76. }
  77. }
  78. /*
  79. * serializes pattern files
  80. */
  81. private HyphenationTree buildPatternFile(File infile) {
  82. System.out.println("Processing " + infile);
  83. HyphenationTree hTree = new HyphenationTree();
  84. try {
  85. hTree.loadPatterns(infile.toString());
  86. if (errorDump) {
  87. System.out.println("Stats: ");
  88. hTree.printStats();
  89. }
  90. } catch (HyphenationException ex) {
  91. System.err.println("Can't load patterns from xml file " + infile
  92. + " - Maybe hyphenation.dtd is missing?");
  93. if (errorDump) {
  94. System.err.println(ex.toString());
  95. }
  96. }
  97. return hTree;
  98. }
  99. /**
  100. * Checks for existence of output file and compares
  101. * dates with input and stylesheet file
  102. */
  103. private boolean rebuild(File infile, File outfile) {
  104. if (outfile.exists()) {
  105. // checks whether output file is older than input file
  106. if (outfile.lastModified() < infile.lastModified()) {
  107. return true;
  108. }
  109. } else {
  110. // if output file does not exist, start process
  111. return true;
  112. }
  113. return false;
  114. } // end rebuild
  115. public static void main (String args[]) {
  116. SerializeHyphPattern ser = new SerializeHyphPattern();
  117. ser.serializeDir(new File(args[0]), new File(args[1]));
  118. }
  119. }