1 7 8 package javax.swing.text.html.parser; 9 10 import java.io.PrintStream ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.InputStream ; 14 import java.io.IOException ; 15 import java.io.FileNotFoundException ; 16 import java.io.BufferedInputStream ; 17 import java.io.DataInputStream ; 18 import java.util.Hashtable ; 19 import java.util.Vector ; 20 import java.util.BitSet ; 21 import java.util.StringTokenizer ; 22 import java.util.Enumeration ; 23 import java.util.Properties ; 24 import java.net.URL ; 25 26 39 public 40 class DTD implements DTDConstants { 41 public String name; 42 public Vector <Element > elements = new Vector <Element >(); 43 public Hashtable <String ,Element > elementHash 44 = new Hashtable <String ,Element >(); 45 public Hashtable <Object ,Entity > entityHash 46 = new Hashtable <Object ,Entity >(); 47 public final Element pcdata = getElement("#pcdata"); 48 public final Element html = getElement("html"); 49 public final Element meta = getElement("meta"); 50 public final Element base = getElement("base"); 51 public final Element isindex = getElement("isindex"); 52 public final Element head = getElement("head"); 53 public final Element body = getElement("body"); 54 public final Element applet = getElement("applet"); 55 public final Element param = getElement("param"); 56 public final Element p = getElement("p"); 57 public final Element title = getElement("title"); 58 final Element style = getElement("style"); 59 final Element link = getElement("link"); 60 final Element script = getElement("script"); 61 62 public static final int FILE_VERSION = 1; 63 64 68 protected DTD(String name) { 69 this.name = name; 70 defEntity("#RE", GENERAL, '\r'); 71 defEntity("#RS", GENERAL, '\n'); 72 defEntity("#SPACE", GENERAL, ' '); 73 defineElement("unknown", EMPTY, false, true, null, null, null, null); 74 } 75 76 80 public String getName() { 81 return name; 82 } 83 84 89 public Entity getEntity(String name) { 90 return (Entity )entityHash.get(name); 91 } 92 93 98 public Entity getEntity(int ch) { 99 return (Entity )entityHash.get(new Integer (ch)); 100 } 101 102 110 boolean elementExists(String name) { 111 Element e = (Element )elementHash.get(name); 112 return ((e == null) ? false : true); 113 } 114 115 123 public Element getElement(String name) { 124 Element e = (Element )elementHash.get(name); 125 if (e == null) { 126 e = new Element (name, elements.size()); 127 elements.addElement(e); 128 elementHash.put(name, e); 129 } 130 return e; 131 } 132 133 140 public Element getElement(int index) { 141 return (Element )elements.elementAt(index); 142 } 143 144 156 public Entity defineEntity(String name, int type, char data[]) { 157 Entity ent = (Entity )entityHash.get(name); 158 if (ent == null) { 159 ent = new Entity (name, type, data); 160 entityHash.put(name, ent); 161 if (((type & GENERAL) != 0) && (data.length == 1)) { 162 switch (type & ~GENERAL) { 163 case CDATA: 164 case SDATA: 165 entityHash.put(new Integer (data[0]), ent); 166 break; 167 } 168 } 169 } 170 return ent; 171 } 172 173 187 public Element defineElement(String name, int type, 188 boolean omitStart, boolean omitEnd, ContentModel content, 189 BitSet exclusions, BitSet inclusions, AttributeList atts) { 190 Element e = getElement(name); 191 e.type = type; 192 e.oStart = omitStart; 193 e.oEnd = omitEnd; 194 e.content = content; 195 e.exclusions = exclusions; 196 e.inclusions = inclusions; 197 e.atts = atts; 198 return e; 199 } 200 201 211 public void defineAttributes(String name, AttributeList atts) { 212 Element e = getElement(name); 213 e.atts = atts; 214 } 215 216 221 public Entity defEntity(String name, int type, int ch) { 222 char data[] = {(char)ch}; 223 return defineEntity(name, type, data); 224 } 225 226 231 protected Entity defEntity(String name, int type, String str) { 232 int len = str.length(); 233 char data[] = new char[len]; 234 str.getChars(0, len, data, 0); 235 return defineEntity(name, type, data); 236 } 237 238 243 protected Element defElement(String name, int type, 244 boolean omitStart, boolean omitEnd, ContentModel content, 245 String [] exclusions, String [] inclusions, AttributeList atts) { 246 BitSet excl = null; 247 if (exclusions != null && exclusions.length > 0) { 248 excl = new BitSet (); 249 for (int i = 0; i < exclusions.length; i++) { 250 String str = exclusions[i]; 251 if (str.length() > 0) { 252 excl.set(getElement(str).getIndex()); 253 } 254 } 255 } 256 BitSet incl = null; 257 if (inclusions != null && inclusions.length > 0) { 258 incl = new BitSet (); 259 for (int i = 0; i < inclusions.length; i++) { 260 String str = inclusions[i]; 261 if (str.length() > 0) { 262 incl.set(getElement(str).getIndex()); 263 } 264 } 265 } 266 return defineElement(name, type, omitStart, omitEnd, content, excl, incl, atts); 267 } 268 269 274 protected AttributeList defAttributeList(String name, int type, int modifier, String value, String values, AttributeList atts) { 275 Vector vals = null; 276 if (values != null) { 277 vals = new Vector (); 278 for (StringTokenizer s = new StringTokenizer (values, "|") ; s.hasMoreTokens() ;) { 279 String str = s.nextToken(); 280 if (str.length() > 0) { 281 vals.addElement(str); 282 } 283 } 284 } 285 return new AttributeList (name, type, modifier, value, vals, atts); 286 } 287 288 293 protected ContentModel defContentModel(int type, Object obj, ContentModel next) { 294 return new ContentModel (type, obj, next); 295 } 296 297 301 public String toString() { 302 return name; 303 } 304 305 308 static Hashtable dtdHash = new Hashtable (); 309 310 public static void putDTDHash(String name, DTD dtd) { 311 dtdHash.put(name, dtd); 312 } 313 322 public static DTD getDTD(String name) throws IOException { 323 name = name.toLowerCase(); 324 DTD dtd = (DTD )dtdHash.get(name); 325 if (dtd == null) 326 dtd = new DTD (name); 327 328 return dtd; 329 } 330 331 335 public void read(DataInputStream in) throws IOException { 336 if (in.readInt() != FILE_VERSION) { 337 } 338 339 String [] names = new String [in.readShort()]; 343 for (int i = 0; i < names.length; i++) { 344 names[i] = in.readUTF(); 345 } 346 347 348 int num = in.readShort(); 352 for (int i = 0; i < num; i++) { 353 short nameId = in.readShort(); 354 int type = in.readByte(); 355 String name = in.readUTF(); 356 defEntity(names[nameId], type | GENERAL, name); 357 } 358 359 num = in.readShort(); 362 for (int i = 0; i < num; i++) { 363 short nameId = in.readShort(); 364 int type = in.readByte(); 365 byte flags = in.readByte(); 366 ContentModel m = readContentModel(in, names); 367 String [] exclusions = readNameArray(in, names); 368 String [] inclusions = readNameArray(in, names); 369 AttributeList atts = readAttributeList(in, names); 370 defElement(names[nameId], type, 371 ((flags & 0x01) != 0), ((flags & 0x02) != 0), 372 m, exclusions, inclusions, atts); 373 } 374 } 375 376 private ContentModel readContentModel(DataInputStream in, String [] names) 377 throws IOException { 378 byte flag = in.readByte(); 379 switch(flag) { 380 case 0: return null; 382 case 1: { int type = in.readByte(); 384 ContentModel m = readContentModel(in, names); 385 ContentModel next = readContentModel(in, names); 386 return defContentModel(type, m, next); 387 } 388 case 2: { int type = in.readByte(); 390 Element el = getElement(names[in.readShort()]); 391 ContentModel next = readContentModel(in, names); 392 return defContentModel(type, el, next); 393 } 394 default: 395 throw new IOException ("bad bdtd"); 396 } 397 } 398 399 private String [] readNameArray(DataInputStream in, String [] names) 400 throws IOException { 401 int num = in.readShort(); 402 if (num == 0) { 403 return null; 404 } 405 String [] result = new String [num]; 406 for (int i = 0; i < num; i++) { 407 result[i] = names[in.readShort()]; 408 } 409 return result; 410 } 411 412 413 private AttributeList readAttributeList(DataInputStream in, String [] names) 414 throws IOException { 415 AttributeList result = null; 416 for (int num = in.readByte(); num > 0; --num) { 417 short nameId = in.readShort(); 418 int type = in.readByte(); 419 int modifier = in.readByte(); 420 short valueId = in.readShort(); 421 String value = (valueId == -1) ? null : names[valueId]; 422 Vector values = null; 423 short numValues = in.readShort(); 424 if (numValues > 0) { 425 values = new Vector (numValues); 426 for (int i = 0; i < numValues; i++) { 427 values.addElement(names[in.readShort()]); 428 } 429 } 430 result = new AttributeList (names[nameId], type, modifier, value, 431 values, result); 432 } 435 return result; 436 } 437 438 } 439 | Popular Tags |