FileDocCategorySizeDatePackage
DTD.javaAPI DocJava SE 5 API13180Fri Aug 26 14:58:20 BST 2005javax.swing.text.html.parser

DTD

public class DTD extends Object implements DTDConstants
The representation of an SGML DTD. DTD describes a document syntax and is used in parsing of HTML documents. It contains a list of elements and their attributes as well as a list of entities defined in the DTD.
see
Element
see
AttributeList
see
ContentModel
see
Parser
author
Arthur van Hoff
version
1.20 05/05/04

Fields Summary
public String
name
public Vector
elements
public Hashtable
elementHash
public Hashtable
entityHash
public final Element
pcdata
public final Element
html
public final Element
meta
public final Element
base
public final Element
isindex
public final Element
head
public final Element
body
public final Element
applet
public final Element
param
public final Element
p
public final Element
title
final Element
style
final Element
link
final Element
script
public static final int
FILE_VERSION
static Hashtable
dtdHash
The hashtable of DTDs.
Constructors Summary
protected DTD(String name)
Creates a new DTD with the specified name.

param
name the name, as a String of the new DTD


                            
       
	this.name = name;
	defEntity("#RE", GENERAL, '\r");
	defEntity("#RS", GENERAL, '\n");
	defEntity("#SPACE", GENERAL, ' ");
	defineElement("unknown", EMPTY, false, true, null, null, null, null);
    
Methods Summary
protected javax.swing.text.html.parser.AttributeListdefAttributeList(java.lang.String name, int type, int modifier, java.lang.String value, java.lang.String values, javax.swing.text.html.parser.AttributeList atts)
Creates and returns an AttributeList.

param
name the attribute list's name
return
the new AttributeList

	Vector vals = null;
	if (values != null) {
	    vals = new Vector();
	    for (StringTokenizer s = new StringTokenizer(values, "|") ; s.hasMoreTokens() ;) {
		String str = s.nextToken();
		if (str.length() > 0) {
		    vals.addElement(str);
		}
	    }
	}
	return new AttributeList(name, type, modifier, value, vals, atts);
    
protected javax.swing.text.html.parser.ContentModeldefContentModel(int type, java.lang.Object obj, javax.swing.text.html.parser.ContentModel next)
Creates and returns a new content model.

param
type the type of the new content model
return
the new ContentModel

	return new ContentModel(type, obj, next);
    
protected javax.swing.text.html.parser.ElementdefElement(java.lang.String name, int type, boolean omitStart, boolean omitEnd, javax.swing.text.html.parser.ContentModel content, java.lang.String[] exclusions, java.lang.String[] inclusions, javax.swing.text.html.parser.AttributeList atts)
Creates and returns an Element.

param
name the element's name
return
the new Element

	BitSet excl = null;
	if (exclusions != null && exclusions.length > 0) {
	    excl = new BitSet();
	    for (int i = 0; i < exclusions.length; i++) {
		String str = exclusions[i];
		if (str.length() > 0) {
		    excl.set(getElement(str).getIndex());
		}
	    }
	}
	BitSet incl = null;
	if (inclusions != null && inclusions.length > 0) {
	    incl = new BitSet();
	    for (int i = 0; i < inclusions.length; i++) {
		String str = inclusions[i];
		if (str.length() > 0) {
		    incl.set(getElement(str).getIndex());
		}
	    }
	}
	return defineElement(name, type, omitStart, omitEnd, content, excl, incl, atts);
    
public javax.swing.text.html.parser.EntitydefEntity(java.lang.String name, int type, int ch)
Creates and returns a character Entity.

param
name the entity's name
return
the new character Entity

	char data[] = {(char)ch};
	return defineEntity(name, type, data);
    
protected javax.swing.text.html.parser.EntitydefEntity(java.lang.String name, int type, java.lang.String str)
Creates and returns an Entity.

param
name the entity's name
return
the new Entity

	int len = str.length();
	char data[] = new char[len];
	str.getChars(0, len, data, 0);
	return defineEntity(name, type, data);
    
public voiddefineAttributes(java.lang.String name, javax.swing.text.html.parser.AttributeList atts)
Returns the Element which matches the specified AttributeList. If one doesn't exist, a new one is created and returned.

param
name the name of the Element
param
atts the AttributeList specifying the Element
return
the Element specified

	Element e = getElement(name);
	e.atts = atts;
    
public javax.swing.text.html.parser.ElementdefineElement(java.lang.String name, int type, boolean omitStart, boolean omitEnd, javax.swing.text.html.parser.ContentModel content, java.util.BitSet exclusions, java.util.BitSet inclusions, javax.swing.text.html.parser.AttributeList atts)
Returns the Element which matches the specified parameters. If one doesn't exist, a new one is created and returned.

param
name the name of the Element
param
type the type of the Element
param
omitStart true
param
omitEnd true if end should be omitted
param
content the ContentModel
param
atts the AttributeList specifying the Element
return
the Element specified

	Element e = getElement(name);
	e.type = type;
	e.oStart = omitStart;
	e.oEnd = omitEnd;
	e.content = content;
	e.exclusions = exclusions;
	e.inclusions = inclusions;
	e.atts = atts;
	return e;
    
public javax.swing.text.html.parser.EntitydefineEntity(java.lang.String name, int type, char[] data)
Defines an entity. If the Entity specified by name, type, and data exists, it is returned; otherwise a new Entity is created and is returned.

param
name the name of the Entity as a String
param
type the type of the Entity
param
data the Entity's data
return
the Entity requested or a new Entity if not found

	Entity ent = (Entity)entityHash.get(name);
	if (ent == null) {
	    ent = new Entity(name, type, data);
	    entityHash.put(name, ent);
	    if (((type & GENERAL) != 0) && (data.length == 1)) {
		switch (type & ~GENERAL) {
		  case CDATA:
		  case SDATA:
		    entityHash.put(new Integer(data[0]), ent);
		    break;
		}
	    }
	}
	return ent;
    
booleanelementExists(java.lang.String name)
Returns true if the element is part of the DTD, otherwise returns false.

param
name the requested String
return
true if name exists as part of the DTD, otherwise returns false

	Element e = (Element)elementHash.get(name);
	return ((e == null) ? false : true);
    
public static javax.swing.text.html.parser.DTDgetDTD(java.lang.String name)
Returns a DTD with the specified name. If a DTD with that name doesn't exist, one is created and returned. Any uppercase characters in the name are converted to lowercase.

param
name the name of the DTD
return
the DTD which corresponds to name

	name = name.toLowerCase();
	DTD dtd = (DTD)dtdHash.get(name);
	if (dtd == null)
	  dtd = new DTD(name);

	return dtd;
    
public javax.swing.text.html.parser.ElementgetElement(java.lang.String name)
Gets an element by name. A new element is created if the element doesn't exist.

param
name the requested String
return
the Element corresponding to name, which may be newly created

	Element e = (Element)elementHash.get(name);
	if (e == null) {
	    e = new Element(name, elements.size());
	    elements.addElement(e);
	    elementHash.put(name, e);
	}
	return e;
    
public javax.swing.text.html.parser.ElementgetElement(int index)
Gets an element by index.

param
index the requested index
return
the Element corresponding to index

	return (Element)elements.elementAt(index);
    
public javax.swing.text.html.parser.EntitygetEntity(java.lang.String name)
Gets an entity by name.

return
the Entity corresponding to the name String

	return (Entity)entityHash.get(name);
    
public javax.swing.text.html.parser.EntitygetEntity(int ch)
Gets a character entity.

return
the Entity corresponding to the ch character

	return (Entity)entityHash.get(new Integer(ch));
    
public java.lang.StringgetName()
Gets the name of the DTD.

return
the name of the DTD

	return name;
    
public static voidputDTDHash(java.lang.String name, javax.swing.text.html.parser.DTD dtd)


         
    dtdHash.put(name, dtd);
  
public voidread(java.io.DataInputStream in)
Recreates a DTD from an archived format.

param
in the DataInputStream to read from

	if (in.readInt() != FILE_VERSION) {
	}

	//
	// Read the list of names
	//
	String[] names = new String[in.readShort()];
	for (int i = 0; i < names.length; i++) {
	    names[i] = in.readUTF();
	}


	//
	// Read the entities
	//
	int num = in.readShort();
	for (int i = 0; i < num; i++) {
	    short nameId = in.readShort();
	    int type = in.readByte();
	    String name = in.readUTF();
	    defEntity(names[nameId], type | GENERAL, name);
	}

	// Read the elements
	//
	num = in.readShort();
	for (int i = 0; i < num; i++) {
	    short nameId = in.readShort();
	    int type = in.readByte();
	    byte flags = in.readByte();
	    ContentModel m = readContentModel(in, names);
	    String[] exclusions = readNameArray(in, names);
	    String[] inclusions = readNameArray(in, names);
	    AttributeList atts = readAttributeList(in, names);
	    defElement(names[nameId], type,
		       ((flags & 0x01) != 0), ((flags & 0x02) != 0),
		       m, exclusions, inclusions, atts);
	}
    
private javax.swing.text.html.parser.AttributeListreadAttributeList(java.io.DataInputStream in, java.lang.String[] names)

	AttributeList result = null;
	for (int num = in.readByte(); num > 0; --num) {
	    short nameId = in.readShort();
	    int type = in.readByte();
	    int modifier = in.readByte();
	    short valueId = in.readShort();
	    String value = (valueId == -1) ? null : names[valueId];
	    Vector values = null;
	    short numValues = in.readShort();
	    if (numValues > 0) {
		values = new Vector(numValues);
		for (int i = 0; i < numValues; i++) {
		    values.addElement(names[in.readShort()]);
		}
	    }
result = new AttributeList(names[nameId], type, modifier, value,
				       values, result);
	    // We reverse the order of the linked list by doing this, but
	    // that order isn't important.
	}
	return result;
    
private javax.swing.text.html.parser.ContentModelreadContentModel(java.io.DataInputStream in, java.lang.String[] names)

	byte flag = in.readByte();
	switch(flag) {
	    case 0:		// null
		return null;
	    case 1: {		// content_c
		int type = in.readByte();
		ContentModel m = readContentModel(in, names);
		ContentModel next = readContentModel(in, names);
		return defContentModel(type, m, next);
	    }
	    case 2: {		// content_e
		int type = in.readByte();
		Element el = getElement(names[in.readShort()]);
		ContentModel next = readContentModel(in, names);
		return defContentModel(type, el, next);
	    }
	default:
		throw new IOException("bad bdtd");
	}
    
private java.lang.String[]readNameArray(java.io.DataInputStream in, java.lang.String[] names)

	int num = in.readShort();
	if (num == 0) {
	    return null;
	}
	String[] result = new String[num];
	for (int i = 0; i < num; i++) {
	    result[i] = names[in.readShort()];
	}
	return result;
    
public java.lang.StringtoString()
Returns a string representation of this DTD.

return
the string representation of this DTD

	return name;