KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > tools > anttasks > SerializeHyphPattern


1 /*
2  * $Id: SerializeHyphPattern.java,v 1.2.2.1 2003/02/25 15:19:58 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

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 // FOP
58
import org.apache.fop.layout.hyphenation.HyphenationTree;
59 import org.apache.fop.layout.hyphenation.HyphenationException;
60
61 /**
62  * SerializeHyphPattern
63  */

64
65
66 public class SerializeHyphPattern extends MatchingTask {
67     private File sourceDir, targetDir;
68     private boolean errorDump = false;
69
70     /**
71      * Main method, which is called by ant.
72      */

73     public void execute() throws org.apache.tools.ant.BuildException {
74         DirectoryScanner ds = this.getDirectoryScanner(sourceDir);
75         String JavaDoc[] files = ds.getIncludedFiles();
76         for (int i = 0; i < files.length; i++) {
77             processFile(files[i].substring(0, files[i].length() - 4));
78         }
79     } // end execute
80

81
82     /**
83      * Sets the source directory
84      *
85      */

86     public void setSourceDir(String JavaDoc 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     /**
97      * Sets the target directory
98      *
99      */

100     public void setTargetDir(String JavaDoc targetDir) {
101         File dir = new File(targetDir);
102         this.targetDir = dir;
103     }
104
105     /**
106      * more error information
107      *
108      */

109     public void setErrorDump(boolean errorDump) {
110         this.errorDump = errorDump;
111     }
112
113
114     /*
115      * checks whether input or output files exists or the latter is older than input file
116      * and start build if necessary
117      */

118     private void processFile(String JavaDoc 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     /*
131      * serializes pattern files
132      */

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         // serialize class
150
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     /**
163      * Checks for existence of output file and compares
164      * dates with input and stylesheet file
165      */

166     private boolean rebuild(File infile, File outfile) {
167         if (outfile.exists()) {
168             // checks whether output file is older than input file
169
if (outfile.lastModified() < infile.lastModified()) {
170                 return true;
171             }
172         } else {
173             // if output file does not exist, start process
174
return true;
175         }
176         return false;
177     } // end rebuild
178

179     /*
180      * //quick access for debugging
181      * public static void main (String args[]) {
182      * SerializeHyphPattern ser = new SerializeHyphPattern();
183      * ser.setIncludes("*.xml");
184      * ser.setSourceDir("\\xml-fop\\hyph\\");
185      * ser.setTargetDir("\\xml-fop\\hyph\\");
186      * ser.execute();
187      * }
188      */

189
190
191 }
192
Popular Tags