1 17 18 19 20 package org.apache.fop.hyphenation; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 28 public class TernaryTreeAnalysis { 29 30 33 protected TernaryTree tt; 34 35 38 public TernaryTreeAnalysis(TernaryTree tt) { 39 this.tt = tt; 40 } 41 42 45 public static class NodeString { 46 47 50 public StringBuffer string = new StringBuffer (); 51 52 55 public int indent; 56 57 60 public List high = new ArrayList (); 61 62 65 public List low = new ArrayList (); 66 67 70 public NodeString(int indent) { 71 this.indent = indent; 72 string.append("+"); 73 } 74 75 } 76 77 80 protected class Node { 81 82 85 protected int index = 0; 86 87 90 protected int high = 0; 91 92 95 protected int low = 0; 96 97 100 protected int equal = 0; 101 102 105 protected String key = null; 106 107 110 protected boolean isLeafNode = false; 111 112 115 protected boolean isPacked = false; 116 117 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 (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 readKey() { 137 StringBuffer s = new StringBuffer (); 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 150 public String toNodeString() { 151 StringBuffer s = new StringBuffer (); 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 168 public String toCompactString() { 169 StringBuffer s = new StringBuffer (); 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 191 public String toString() { 192 StringBuffer s = new StringBuffer (); 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 214 public String toCompactNodes() { 215 StringBuffer s = new StringBuffer (); 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 229 public String toNodes() { 230 StringBuffer s = new StringBuffer (); 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 toString(char[] c) { 241 StringBuffer s = new StringBuffer (); 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 252 public String toString() { 253 StringBuffer s = new StringBuffer (); 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 |