KickJava   Java API By Example, From Geeks To Geeks.

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


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: TernaryTreeAnalysis.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.hyphenation;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * This class provides some useful methods to print the structure of a TernaryTree object
27  */

28 public class TernaryTreeAnalysis {
29
30     /**
31      * The TernaryTree object to analyse
32      */

33     protected TernaryTree tt;
34     
35     /**
36      * @param tt the TernaryTree object
37      */

38     public TernaryTreeAnalysis(TernaryTree tt) {
39         this.tt = tt;
40     }
41
42     /**
43      * Class representing a string of nodes in the tree representation of a TernaryTree
44      */

45     public static class NodeString {
46         
47         /**
48          * The node string being constructed
49          */

50         public StringBuffer JavaDoc string = new StringBuffer JavaDoc();
51         
52         /**
53          * The indent of the node string
54          */

55         public int indent;
56         
57         /**
58          * The list of branchpoints into the high direction
59          */

60         public List JavaDoc high = new ArrayList JavaDoc();
61         
62         /**
63          * The list of branchpoints into the low direction
64          */

65         public List JavaDoc low = new ArrayList JavaDoc();
66         
67         /**
68          * @param indent the indent of the nodestring
69          */

70         public NodeString(int indent) {
71             this.indent = indent;
72             string.append("+");
73         }
74
75     }
76
77     /**
78      * Class representing a node of the TernaryTree object
79      */

80     protected class Node {
81         
82         /**
83          * The index of the node
84          */

85         protected int index = 0;
86         
87         /**
88          * The index of the high node
89          */

90         protected int high = 0;
91         
92         /**
93          * The index of the high node
94          */

95         protected int low = 0;
96         
97         /**
98          * The index of the equal node
99          */

100         protected int equal = 0;
101         
102         /**
103          * The key following the node
104          */

105         protected String JavaDoc key = null;
106         
107         /**
108          * True if this is a leaf node
109          */

110         protected boolean isLeafNode = false;
111
112         /**
113          * True if this is a packed node
114          */

115         protected boolean isPacked = false;
116
117         /**
118          * @param index the index of the node
119          */

120         protected Node(int index) {
121             this.index = index;
122             if (tt.sc[index] == 0) {
123                 isLeafNode = true;
124             } else if (tt.sc[index] == 0xFFFF) {
125                 isLeafNode = true;
126                 isPacked = true;
127                 key = readKey().toString();
128             } else {
129                 key = new String JavaDoc(tt.sc, index, 1);
130                 high = tt.hi[index];
131                 low = tt.lo[index];
132                 equal = tt.eq[index];
133             }
134         }
135         
136         private StringBuffer JavaDoc readKey() {
137             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
138             int i = (int) tt.lo[index];
139             char c = tt.kv.get(i);
140             for (; c != 0; c = tt.kv.get(++i)) {
141                 s.append(c);
142             }
143             return s;
144         }
145
146         /**
147          * Construct the string representation of the node
148          * @return the string representing the node
149          */

150         public String JavaDoc toNodeString() {
151             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
152             if (isLeafNode) {
153                 s.append("-" + index);
154                 if (isPacked) {
155                     s.append(",=>'" + key + "'");
156                 }
157                 s.append(",leaf");
158             } else {
159                 s.append("-" + index + "--" + key + "-");
160             }
161             return s.toString();
162         }
163         
164         /**
165          * Construct the compact string representation of the node
166          * @return the string representing the node
167          */

168         public String JavaDoc toCompactString() {
169             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
170             if (isLeafNode) {
171                 s.append("-" + index);
172                 if (isPacked) {
173                     s.append(",=>'" + key + "'");
174                 }
175                 s.append(",leaf\n");
176             } else {
177                 if (high != 0) {
178                     s.append("(+-" + high + ")\n |\n");
179                 }
180                 s.append("-" + index + "- " + key + " (-" + equal + ")\n");
181                 if (low != 0) {
182                     s.append(" |\n(+-" + low + ")\n");
183                 }
184             }
185             return s.toString();
186         }
187         
188         /* (non-Javadoc)
189          * @see java.lang.Object#toString()
190          */

191         public String JavaDoc toString() {
192             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
193             s.append("Node " + index + ":\n");
194             if (isLeafNode) {
195                 if (isPacked) {
196                     s.append("key: " + key + "\n");
197                 }
198             } else {
199                 s.append("high: " + (high == 0 ? "-" : String.valueOf(high))
200                          + ", equal: " + equal
201                          + ", low: " + (low == 0 ? "-" : String.valueOf(low))
202                          + "\n");
203                 s.append("key: " + key + "\n");
204             }
205             return s.toString();
206         }
207         
208     }
209     
210     /**
211      * Construct the compact node representation of the TernaryTree object
212      * @return the string representing the tree
213      */

214     public String JavaDoc toCompactNodes() {
215         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
216         for (int i = 1; i < tt.sc.length; ++i) {
217             if (i != 1) {
218                 s.append("\n");
219             }
220             s.append((new Node(i)).toCompactString());
221         }
222         return s.toString();
223     }
224     
225     /**
226      * Construct the node representation of the TernaryTree object
227      * @return the string representing the tree
228      */

229     public String JavaDoc toNodes() {
230         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
231         for (int i = 1; i < tt.sc.length; ++i) {
232             if (i != 1) {
233                 s.append("\n");
234             }
235             s.append((new Node(i)).toString());
236         }
237         return s.toString();
238     }
239     
240     private static StringBuffer JavaDoc toString(char[] c) {
241         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
242         for (int i = 0; i < c.length; ++i) {
243             s.append((int) c[i]);
244             s.append(",");
245         }
246         return s;
247     }
248     
249     /* (non-Javadoc)
250      * @see java.lang.Object#toString()
251      */

252     public String JavaDoc toString() {
253         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
254
255         s.append("hi: ");
256         s.append(toString(tt.hi));
257         s.append("\n");
258         
259         s.append("eq: ");
260         s.append(toString(tt.eq));
261         s.append("\n");
262         
263         s.append("lo: ");
264         s.append(toString(tt.lo));
265         s.append("\n");
266         
267         s.append("sc: ");
268         for (int i = 0; i < tt.sc.length; ++i) {
269             if (tt.sc[i] == 0) {
270                 s.append("-");
271             } else if (tt.sc[i] == 0xFFFF) {
272                 s.append("^");
273             } else {
274                 s.append(tt.sc[i]);
275             }
276         }
277         s.append("\n");
278         
279         s.append("kv: ");
280         for (int i = 0; i < tt.kv.length(); ++i) {
281             if (tt.kv.get(i) == 0) {
282                 s.append("-");
283             } else {
284                 s.append(tt.kv.get(i));
285             }
286         }
287         s.append("\n");
288         
289         s.append("freenode: ");
290         s.append((int) tt.freenode);
291         s.append("\n");
292         
293         s.append("root: ");
294         s.append((int) tt.root);
295         s.append("\n");
296         
297         return s.toString();
298     }
299     
300
301 }
302
Popular Tags