1 51 package org.apache.fop.tools.anttasks; 52 53 import java.io.*; 54 import org.apache.tools.ant.taskdefs.MatchingTask; 55 import org.apache.tools.ant.DirectoryScanner; 56 57 import org.apache.fop.layout.hyphenation.HyphenationTree; 59 import org.apache.fop.layout.hyphenation.HyphenationException; 60 61 64 65 66 public class SerializeHyphPattern extends MatchingTask { 67 private File sourceDir, targetDir; 68 private boolean errorDump = false; 69 70 73 public void execute() throws org.apache.tools.ant.BuildException { 74 DirectoryScanner ds = this.getDirectoryScanner(sourceDir); 75 String [] files = ds.getIncludedFiles(); 76 for (int i = 0; i < files.length; i++) { 77 processFile(files[i].substring(0, files[i].length() - 4)); 78 } 79 } 81 82 86 public void setSourceDir(String sourceDir) { 87 File dir = new File(sourceDir); 88 if (!dir.exists()) { 89 System.err.println("Fatal Error: source directory " + sourceDir 90 + " for hyphenation files doesn't exist."); 91 System.exit(1); 92 } 93 this.sourceDir = dir; 94 } 95 96 100 public void setTargetDir(String targetDir) { 101 File dir = new File(targetDir); 102 this.targetDir = dir; 103 } 104 105 109 public void setErrorDump(boolean errorDump) { 110 this.errorDump = errorDump; 111 } 112 113 114 118 private void processFile(String filename) { 119 File infile = new File(sourceDir, filename + ".xml"); 120 File outfile = new File(targetDir, filename + ".hyp"); 121 long outfileLastModified = outfile.lastModified(); 122 boolean startProcess = true; 123 124 startProcess = rebuild(infile, outfile); 125 if (startProcess) { 126 buildPatternFile(infile, outfile); 127 } 128 } 129 130 133 private void buildPatternFile(File infile, File outfile) { 134 System.out.println("Processing " + infile); 135 HyphenationTree hTree = new HyphenationTree(); 136 try { 137 hTree.loadPatterns(infile.toString()); 138 if (errorDump) { 139 System.out.println("Stats: "); 140 hTree.printStats(); 141 } 142 } catch (HyphenationException ex) { 143 System.err.println("Can't load patterns from xml file " + infile 144 + " - Maybe hyphenation.dtd is missing?"); 145 if (errorDump) { 146 System.err.println(ex.toString()); 147 } 148 } 149 try { 151 ObjectOutputStream out = 152 new ObjectOutputStream(new FileOutputStream(outfile)); 153 out.writeObject(hTree); 154 out.close(); 155 } catch (IOException ioe) { 156 System.err.println("Can't write compiled pattern file: " 157 + outfile); 158 System.err.println(ioe); 159 } 160 } 161 162 166 private boolean rebuild(File infile, File outfile) { 167 if (outfile.exists()) { 168 if (outfile.lastModified() < infile.lastModified()) { 170 return true; 171 } 172 } else { 173 return true; 175 } 176 return false; 177 } 179 189 190 191 } 192 | Popular Tags |