📄 dtd.java
字号:
{ Element e = newElement(name); e.type = type; e.oStart = headless; e.oEnd = tailless; e.content = content; e.exclusions = exclusions; e.inclusions = inclusions; e.atts = attributes; return e; } /** * Creates, intializes and adds to the entity table the new * entity. * @param name the name of the entity * @param type the type of the entity * @param data the data section of the entity * @return the created entity */ public Entity defineEntity(String name, int type, char[] data) { Entity e = newEntity(name, type); e.data = data; return e; } /** Place this DTD into the DTD table. */ public static void putDTDHash(String name, DTD dtd) { dtdHash.put(name, dtd); } /** * <p>Reads DTD from an archived format. This format is not standardized * and differs between implementations.</p><p> This implementation * reads and defines all entities and elements using * ObjectInputStream. The elements and entities can be written into the * stream in any order. The objects other than elements and entities * are ignored.</p> * @param stream A data stream to read from. * @throws java.io.IOException If one is thrown by the input stream */ public void read(DataInputStream stream) throws java.io.IOException { ObjectInputStream oi = new ObjectInputStream(stream); Object def; try { while (true) { def = oi.readObject(); if (def instanceof Element) { Element e = (Element) def; elementHash.put(e.name.toLowerCase(), e); assignField(e); } else if (def instanceof Entity) { Entity e = (Entity) def; entityHash.put(e.name, e); } } } catch (ClassNotFoundException ex) { throw new IOException(ex.getMessage()); } catch (EOFException ex) { // ok EOF } } /** * Returns the name of this instance of DTD. */ public String toString() { return name; } /** * Creates and returns new attribute (not an attribute list). * @param name the name of this attribute * @param type the type of this attribute (FIXED, IMPLIED or * REQUIRED from <code>DTDConstants</code>). * @param modifier the modifier of this attribute * @param default_value the default value of this attribute * @param allowed_values the allowed values of this attribute. The multiple * possible values in this parameter are supposed to be separated by * '|', same as in SGML DTD <code><!ATTLIST </code>tag. This parameter * can be null if no list of allowed values is specified. * @param atts the previous attribute of this element. This is * placed to the field * {@link javax.swing.text.html.parser.AttributeList#next }, * creating a linked list. * @return The attributes. */ protected AttributeList defAttributeList(String name, int type, int modifier, String default_value, String allowed_values, AttributeList atts ) { AttributeList al = new AttributeList(name); al.modifier = modifier; al.value = default_value; al.next = atts; if (allowed_values != null) { StringTokenizer st = new StringTokenizer(allowed_values, " \t|"); Vector v = new Vector(st.countTokens()); while (st.hasMoreTokens()) v.add(st.nextToken()); al.values = v; } return al; } /** * Creates a new content model. * @param type specifies the BNF operation for this content model. * The valid operations are documented in the * {@link javax.swing.text.html.parser.ContentModel#type }. * @param content the content of this content model * @param next if the content model is specified by BNF-like * expression, contains the rest of this expression. * @return The newly created content model. */ protected ContentModel defContentModel(int type, Object content, ContentModel next ) { ContentModel model = new ContentModel(); model.type = type; model.next = next; model.content = content; return model; } /** * Defines a new element and adds it to the element table. * If the element alredy exists, * overrides it settings with the specified values. * @param name the name of the new element * @param type the type of the element * @param headless true if the element needs no starting tag * @param tailless true if the element needs no closing tag * @param content the element content. * @param exclusions the elements that must be excluded from the * content of this element, in all levels of the hierarchy. * @param inclusions the elements that can be included as the * content of this element. * @param attributes the element attributes. * @return the created or updated element. */ protected Element defElement(String name, int type, boolean headless, boolean tailless, ContentModel content, String[] exclusions, String[] inclusions, AttributeList attributes ) { // compute the bit sets BitSet exclude = bitSet(exclusions); BitSet include = bitSet(inclusions); Element e = defineElement(name, type, headless, tailless, content, exclude, include, attributes ); return e; } /** * Creates, intializes and adds to the entity table the new * entity. * @param name the name of the entity * @param type the type of the entity * @param data the data section of the entity * @return the created entity */ protected Entity defEntity(String name, int type, String data) { Entity e = newEntity(name, type); e.data = data.toCharArray(); return e; } private void assignField(Element e) { String element_name = e.name; try { // Assign the field via reflection. Field f = getClass().getField(element_name.toLowerCase()); if ((f.getModifiers() & Modifier.PUBLIC) != 0) if ((f.getModifiers() & Modifier.STATIC) == 0) if (f.getType().isAssignableFrom(e.getClass())) f.set(this, e); } catch (IllegalAccessException ex) { unexpected(ex); } catch (NoSuchFieldException ex) { // This is ok. } // Some virtual machines may still lack the proper // implementation of reflection. As the tag fields // are not used anywhere in this implementation, // (and this class is also rarely used by the end user), // it may be better not to crash everything by throwing an error // for each case when the HTML parsing is required. catch (Throwable t) { // This VM has no reflection mechanism implemented! if (t instanceof OutOfMemoryError) throw (Error) t; } } /** * Create the bit set for this array of elements. * The unknown elements are automatically defined and added * to the element table. * @param elements * @return The bit set. */ private BitSet bitSet(String[] elements) { BitSet b = new BitSet(); for (int i = 0; i < elements.length; i++) { Element e = getElement(elements [ i ]); if (e == null) e = newElement(elements [ i ]); b.set(e.index); } return b; } /** * Find the element with the given name in the element table. * If not find, create a new element with this name and add to the * table. * @param name the name of the element * @return the found or created element. */ private Element newElement(String name) { Element e = (Element) elementHash.get(name.toLowerCase()); if (e == null) { e = new Element(); e.name = name; e.index = elements.size(); elements.add(e); elementHash.put(e.name.toLowerCase(), e); assignField(e); } return e; } /** * Creates and adds to the element table the entity with an * unitialized data section. Used internally. * @param name the name of the entity * @param type the type of the entity, a bitwise combination * of GENERAL, PARAMETER, SYSTEM and PUBLIC. * * @return the created entity */ private Entity newEntity(String name, int type) { Entity e = new Entity(name, type, null); entityHash.put(e.name, e); return e; } private void unexpected(Exception ex) { throw new Error("This should never happen, report a bug", ex); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -