KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > hyphenation > HyphenationTreeAnalysis


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
18 /* $Id: HyphenationTreeAnalysis.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.hyphenation;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.zip.ZipFile JavaDoc;
33 import java.util.zip.ZipEntry JavaDoc;
34
35 /**
36  * This class provides some useful methods to print the structure of a HyphenationTree object
37  */

38 public class HyphenationTreeAnalysis extends TernaryTreeAnalysis {
39
40     /**
41      * The HyphenationTree object to analyse
42      */

43     protected HyphenationTree ht;
44     
45     /**
46      * @param ht the HyphenationTree object
47      */

48     public HyphenationTreeAnalysis(HyphenationTree ht) {
49         super(ht);
50         this.ht = ht;
51     }
52     
53     /**
54      * Class representing a node of the HyphenationTree object
55      */

56     protected class Node extends TernaryTreeAnalysis.Node {
57         private String JavaDoc value = null;
58
59         /**
60          * @param index the index of the node
61          */

62         protected Node(int index) {
63             super(index);
64             if (isLeafNode) {
65                 value = readValue().toString();
66             }
67         }
68         
69         private StringBuffer JavaDoc readValue() {
70             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
71             int i = (int) ht.eq[index];
72             byte v = ht.vspace.get(i);
73             for (; v != 0; v = ht.vspace.get(++i)) {
74                 int c = (int) ((v >>> 4) - 1);
75                 s.append(c);
76                 c = (int) (v & 0x0f);
77                 if (c == 0) {
78                     break;
79                 }
80                 c = (c - 1);
81                 s.append(c);
82             }
83             return s;
84         }
85
86         /* (non-Javadoc)
87          * @see org.apache.fop.hyphenation.TernaryTreeAnalysis.Node#toNodeString()
88          */

89         public String JavaDoc toNodeString() {
90             if (isLeafNode) {
91                 StringBuffer JavaDoc s = new StringBuffer JavaDoc();
92                 s.append("-" + index);
93                 if (isPacked) {
94                     s.append(",=>'" + key + "'");
95                 }
96                 s.append("," + value);
97                 s.append(",leaf");
98                 return s.toString();
99             } else {
100                 return super.toNodeString();
101             }
102         }
103         
104         /* (non-Javadoc)
105          * @see org.apache.fop.hyphenation.TernaryTreeAnalysis.Node#toCompactString()
106          */

107         public String JavaDoc toCompactString() {
108             if (isLeafNode) {
109                 StringBuffer JavaDoc s = new StringBuffer JavaDoc();
110                 s.append("-" + index);
111                 if (isPacked) {
112                     s.append(",=>'" + key + "'");
113                 }
114                 s.append("," + value);
115                 s.append(",leaf\n");
116                 return s.toString();
117             } else {
118                 return super.toCompactString();
119             }
120         }
121         
122         /* (non-Javadoc)
123          * @see java.lang.Object#toString()
124          */

125         public String JavaDoc toString() {
126             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
127             s.append(super.toString());
128             if (isLeafNode) {
129                 s.append("value: " + value + "\n");
130             }
131             return s.toString();
132         }
133         
134     }
135
136     private void addNode(int nodeIndex, List JavaDoc strings, NodeString ns) {
137         int pos = ns.indent + ns.string.length() + 1;
138         Node n = new Node(nodeIndex);
139         ns.string.append(n.toNodeString());
140         if (n.high != 0) {
141             ns.high.add(new Integer JavaDoc(pos));
142             NodeString highNs = new NodeString(pos);
143             highNs.low.add(new Integer JavaDoc(pos));
144             int index = strings.indexOf(ns);
145             strings.add(index, highNs);
146             addNode(n.high, strings, highNs);
147         }
148         if (n.low != 0) {
149             ns.low.add(new Integer JavaDoc(pos));
150             NodeString lowNs = new NodeString(pos);
151             lowNs.high.add(new Integer JavaDoc(pos));
152             int index = strings.indexOf(ns);
153             strings.add(index + 1, lowNs);
154             addNode(n.low, strings, lowNs);
155         }
156         if (!n.isLeafNode) {
157             addNode(n.equal, strings, ns);
158         }
159
160     }
161
162     /**
163      * Construct the tree representation of a list of node strings
164      * @param strings the list of node strings
165      * @return the string representing the tree
166      */

167     public String JavaDoc toTree(List JavaDoc strings) {
168         StringBuffer JavaDoc indentString = new StringBuffer JavaDoc();
169         for (int j = indentString.length(); j < ((NodeString) strings.get(0)).indent; ++j) {
170             indentString.append(' ');
171         }
172         StringBuffer JavaDoc tree = new StringBuffer JavaDoc();
173         for (int i = 0; i < strings.size(); ++i) {
174             NodeString ns = (NodeString) strings.get(i);
175             if (indentString.length() > ns.indent) {
176                 indentString.setLength(ns.indent);
177             } else {
178                 // should not happen
179
for (int j = indentString.length(); j < ns.indent; ++j) {
180                     indentString.append(' ');
181                 }
182             }
183             tree.append(indentString);
184             tree.append(ns.string + "\n");
185             
186             if (i + 1 == strings.size()) {
187                 continue;
188             }
189             for (int j = 0; j < ns.low.size(); ++j) {
190                 int pos = ((Integer JavaDoc) ns.low.get(j)).intValue();
191                 if (pos < indentString.length()) {
192                     indentString.setCharAt(pos, '|');
193                 } else {
194                     for (int k = indentString.length(); k < pos; ++k) {
195                         indentString.append(' ');
196                     }
197                     indentString.append('|');
198                 }
199             }
200             tree.append(indentString + "\n");
201         }
202         
203         return tree.toString();
204     }
205     
206     /**
207      * Construct the tree representation of the HyphenationTree object
208      * @return the string representing the tree
209      */

210     public String JavaDoc toTree() {
211         List JavaDoc strings = new ArrayList JavaDoc();
212         NodeString ns = new NodeString(0);
213         strings.add(ns);
214         addNode(1, strings, ns);
215         return toTree(strings);
216     }
217     
218     /**
219      * Construct the compact node representation of the HyphenationTree object
220      * @return the string representing the tree
221      */

222     public String JavaDoc toCompactNodes() {
223         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
224         for (int i = 1; i < ht.sc.length; ++i) {
225             if (i != 1) {
226                 s.append("\n");
227             }
228             s.append((new Node(i)).toCompactString());
229         }
230         return s.toString();
231     }
232     
233     /**
234      * Construct the node representation of the HyphenationTree object
235      * @return the string representing the tree
236      */

237     public String JavaDoc toNodes() {
238         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
239         for (int i = 1; i < ht.sc.length; ++i) {
240             if (i != 1) {
241                 s.append("\n");
242             }
243             s.append((new Node(i)).toString());
244         }
245         return s.toString();
246     }
247     
248     /**
249      * Construct the printed representation of the HyphenationTree object
250      * @return the string representing the tree
251      */

252     public String JavaDoc toString() {
253         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
254         
255         s.append("classes: \n");
256         s.append((new TernaryTreeAnalysis(ht.classmap)).toString());
257         
258         s.append("\npatterns: \n");
259         s.append(super.toString());
260         s.append("vspace: ");
261         for (int i = 0; i < ht.vspace.length(); ++i) {
262             byte v = ht.vspace.get(i);
263             if (v == 0) {
264                 s.append("--");
265             } else {
266                 int c = (int) ((v >>> 4) - 1);
267                 s.append(c);
268                 c = (int) (v & 0x0f);
269                 if (c == 0) {
270                     s.append("-");
271                 } else {
272                     c = (c - 1);
273                     s.append(c);
274                 }
275             }
276         }
277         s.append("\n");
278         
279         return s.toString();
280     }
281
282     /**
283      * Provide interactive access to a HyphenationTree object and its representation methods
284      * @param args the arguments
285      */

286     public static void main(String JavaDoc[] args) {
287         HyphenationTree ht = null;
288         HyphenationTreeAnalysis hta = null;
289         int minCharCount = 2;
290         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
291         while (true) {
292             System.out.print("l:\tload patterns from XML\n"
293                              + "L:\tload patterns from serialized object\n"
294                              + "s:\tset minimun character count\n"
295                              + "w:\twrite hyphenation tree to object file\n"
296                              + "p:\tprint hyphenation tree to stdout\n"
297                              + "n:\tprint hyphenation tree nodes to stdout\n"
298                              + "c:\tprint compact hyphenation tree nodes to stdout\n"
299                              + "t:\tprint tree representation of hyphenation tree to stdout\n"
300                              + "h:\thyphenate\n"
301                              + "f:\tfind pattern\n"
302                              + "b:\tbenchmark\n"
303                              + "q:\tquit\n\n"
304                              + "Command:");
305             try {
306                 String JavaDoc token = in.readLine().trim();
307                 if (token.equals("f")) {
308                     System.out.print("Pattern: ");
309                     token = in.readLine().trim();
310                     System.out.println("Values: " + ht.findPattern(token));
311                 } else if (token.equals("s")) {
312                     System.out.print("Minimum value: ");
313                     token = in.readLine().trim();
314                     minCharCount = Integer.parseInt(token);
315                 } else if (token.equals("l")) {
316                     ht = new HyphenationTree();
317                     hta = new HyphenationTreeAnalysis(ht);
318                     System.out.print("XML file name: ");
319                     token = in.readLine().trim();
320                     try {
321                         ht.loadPatterns(token);
322                     } catch (HyphenationException e) {
323                         e.printStackTrace();
324                     }
325                 } else if (token.equals("L")) {
326                     ObjectInputStream JavaDoc ois = null;
327                     System.out.print("Object file name: ");
328                     token = in.readLine().trim();
329                     try {
330                         String JavaDoc[] parts = token.split(":");
331                         InputStream JavaDoc is = null;
332                         if (parts.length == 1) {
333                             is = new FileInputStream JavaDoc(token);
334                         } else if (parts.length == 2) {
335                             ZipFile JavaDoc jar = new ZipFile JavaDoc(parts[0]);
336                             ZipEntry JavaDoc entry = new ZipEntry JavaDoc(jar.getEntry(parts[1]));
337                             is = jar.getInputStream(entry);
338                         }
339                         ois = new ObjectInputStream JavaDoc(is);
340                         ht = (HyphenationTree) ois.readObject();
341                         hta = new HyphenationTreeAnalysis(ht);
342                     } catch (Exception JavaDoc e) {
343                         e.printStackTrace();
344                     } finally {
345                         if (ois != null) {
346                             try {
347                                 ois.close();
348                             } catch (IOException JavaDoc e) {
349                                 //ignore
350
}
351                         }
352                     }
353                 } else if (token.equals("w")) {
354                     System.out.print("Object file name: ");
355                     token = in.readLine().trim();
356                     ObjectOutputStream JavaDoc oos = null;
357                     try {
358                         oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(token));
359                         oos.writeObject(ht);
360                     } catch (Exception JavaDoc e) {
361                         e.printStackTrace();
362                     } finally {
363                         if (oos != null) {
364                             try {
365                                 oos.flush();
366                             } catch (IOException JavaDoc e) {
367                                 //ignore
368
}
369                             try {
370                                 oos.close();
371                             } catch (IOException JavaDoc e) {
372                                 //ignore
373
}
374                         }
375                     }
376                 } else if (token.equals("p")) {
377                     System.out.print(hta);
378                 } else if (token.equals("n")) {
379                     System.out.print(hta.toNodes());
380                 } else if (token.equals("c")) {
381                     System.out.print(hta.toCompactNodes());
382                 } else if (token.equals("t")) {
383                     System.out.print(hta.toTree());
384                 } else if (token.equals("h")) {
385                     System.out.print("Word: ");
386                     token = in.readLine().trim();
387                     System.out.print("Hyphenation points: ");
388                     System.out.println(ht.hyphenate(token, minCharCount,
389                                                     minCharCount));
390                 } else if (token.equals("b")) {
391                     if (ht == null) {
392                         System.out.println("No patterns have been loaded.");
393                         break;
394                     }
395                     System.out.print("Word list filename: ");
396                     token = in.readLine().trim();
397                     long starttime = 0;
398                     int counter = 0;
399                     try {
400                         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(token));
401                         String JavaDoc line;
402                         
403                         starttime = System.currentTimeMillis();
404                         while ((line = reader.readLine()) != null) {
405                             // System.out.print("\nline: ");
406
Hyphenation hyp = ht.hyphenate(line, minCharCount,
407                                                            minCharCount);
408                             if (hyp != null) {
409                                 String JavaDoc hword = hyp.toString();
410                                 // System.out.println(line);
411
// System.out.println(hword);
412
} else {
413                                 // System.out.println("No hyphenation");
414
}
415                             counter++;
416                         }
417                     } catch (Exception JavaDoc ioe) {
418                         System.out.println("Exception " + ioe);
419                         ioe.printStackTrace();
420                     }
421                     long endtime = System.currentTimeMillis();
422                     long result = endtime - starttime;
423                     System.out.println(counter + " words in " + result
424                                        + " Milliseconds hyphenated");
425                     
426                 } else if (token.equals("q")) {
427                     break;
428                 }
429             } catch (IOException JavaDoc e) {
430                 e.printStackTrace();
431             }
432         }
433
434     }
435
436 }
437
Popular Tags