1 17 18 19 20 package org.apache.fop.hyphenation; 21 22 import java.io.BufferedReader ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 import java.util.zip.ZipFile ; 33 import java.util.zip.ZipEntry ; 34 35 38 public class HyphenationTreeAnalysis extends TernaryTreeAnalysis { 39 40 43 protected HyphenationTree ht; 44 45 48 public HyphenationTreeAnalysis(HyphenationTree ht) { 49 super(ht); 50 this.ht = ht; 51 } 52 53 56 protected class Node extends TernaryTreeAnalysis.Node { 57 private String value = null; 58 59 62 protected Node(int index) { 63 super(index); 64 if (isLeafNode) { 65 value = readValue().toString(); 66 } 67 } 68 69 private StringBuffer readValue() { 70 StringBuffer s = new StringBuffer (); 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 89 public String toNodeString() { 90 if (isLeafNode) { 91 StringBuffer s = new StringBuffer (); 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 107 public String toCompactString() { 108 if (isLeafNode) { 109 StringBuffer s = new StringBuffer (); 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 125 public String toString() { 126 StringBuffer s = new StringBuffer (); 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 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 (pos)); 142 NodeString highNs = new NodeString(pos); 143 highNs.low.add(new Integer (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 (pos)); 150 NodeString lowNs = new NodeString(pos); 151 lowNs.high.add(new Integer (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 167 public String toTree(List strings) { 168 StringBuffer indentString = new StringBuffer (); 169 for (int j = indentString.length(); j < ((NodeString) strings.get(0)).indent; ++j) { 170 indentString.append(' '); 171 } 172 StringBuffer tree = new StringBuffer (); 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 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 ) 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 210 public String toTree() { 211 List strings = new ArrayList (); 212 NodeString ns = new NodeString(0); 213 strings.add(ns); 214 addNode(1, strings, ns); 215 return toTree(strings); 216 } 217 218 222 public String toCompactNodes() { 223 StringBuffer s = new StringBuffer (); 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 237 public String toNodes() { 238 StringBuffer s = new StringBuffer (); 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 252 public String toString() { 253 StringBuffer s = new StringBuffer (); 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 286 public static void main(String [] args) { 287 HyphenationTree ht = null; 288 HyphenationTreeAnalysis hta = null; 289 int minCharCount = 2; 290 BufferedReader in = new BufferedReader (new java.io.InputStreamReader (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 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 ois = null; 327 System.out.print("Object file name: "); 328 token = in.readLine().trim(); 329 try { 330 String [] parts = token.split(":"); 331 InputStream is = null; 332 if (parts.length == 1) { 333 is = new FileInputStream (token); 334 } else if (parts.length == 2) { 335 ZipFile jar = new ZipFile (parts[0]); 336 ZipEntry entry = new ZipEntry (jar.getEntry(parts[1])); 337 is = jar.getInputStream(entry); 338 } 339 ois = new ObjectInputStream (is); 340 ht = (HyphenationTree) ois.readObject(); 341 hta = new HyphenationTreeAnalysis(ht); 342 } catch (Exception e) { 343 e.printStackTrace(); 344 } finally { 345 if (ois != null) { 346 try { 347 ois.close(); 348 } catch (IOException e) { 349 } 351 } 352 } 353 } else if (token.equals("w")) { 354 System.out.print("Object file name: "); 355 token = in.readLine().trim(); 356 ObjectOutputStream oos = null; 357 try { 358 oos = new ObjectOutputStream (new FileOutputStream (token)); 359 oos.writeObject(ht); 360 } catch (Exception e) { 361 e.printStackTrace(); 362 } finally { 363 if (oos != null) { 364 try { 365 oos.flush(); 366 } catch (IOException e) { 367 } 369 try { 370 oos.close(); 371 } catch (IOException e) { 372 } 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 reader = new BufferedReader (new FileReader (token)); 401 String line; 402 403 starttime = System.currentTimeMillis(); 404 while ((line = reader.readLine()) != null) { 405 Hyphenation hyp = ht.hyphenate(line, minCharCount, 407 minCharCount); 408 if (hyp != null) { 409 String hword = hyp.toString(); 410 } else { 413 } 415 counter++; 416 } 417 } catch (Exception 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 e) { 430 e.printStackTrace(); 431 } 432 } 433 434 } 435 436 } 437 | Popular Tags |