KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ccil > cowan > tagsoup > Schema


1 // This file is part of TagSoup.
2
//
3
// This program is free software; you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation; either version 2 of the License, or
6
// (at your option) any later version. You may also distribute
7
// and/or modify it under version 2.1 of the Academic Free License.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
//
13
//
14
// Model of document
15

16 package org.ccil.cowan.tagsoup;
17 import java.util.HashMap JavaDoc;
18
19 /**
20 Abstract class representing a TSSL schema.
21 Actual TSSL schemas are compiled into concrete subclasses of this class.
22 **/

23
24 public abstract class Schema {
25
26     public static final int M_ANY = 0xFFFFFFFF;
27     public static final int M_EMPTY = 0;
28     public static final int M_PCDATA = 1 << 30;
29     public static final int M_ROOT = 1 << 31;
30
31
32     public static final int F_RESTART = 1;
33     public static final int F_CDATA = 2;
34     public static final int F_NOFORCE = 4;
35
36     private HashMap JavaDoc theEntities =
37         new HashMap JavaDoc(); // String -> Character
38
private HashMap JavaDoc theElementTypes =
39         new HashMap JavaDoc(); // String -> ElementType
40

41     private String JavaDoc theURI = "";
42     private String JavaDoc thePrefix = "";
43
44     /**
45     Add or replace an element type for this schema.
46     @param name Name (Qname) of the element
47     @param model Models of the element's content as a vector of bits
48     @param memberOf Models the element is a member of as a vector of bits
49     @param flags Flags for the element
50     **/

51
52     public void elementType(String JavaDoc name, int model, int memberOf, int flags) {
53         ElementType e = new ElementType(name, model, memberOf, flags, this);
54         theElementTypes.put(name, e);
55         }
56
57     /**
58     Add or replace a default attribute for an element type in this schema.
59     @param elemName Name (Qname) of the element type
60     @param attrName Name (Qname) of the attribute
61     @param type Type of the attribute
62     @param value Default value of the attribute; null if no default
63     **/

64
65     public void attribute(String JavaDoc elemName, String JavaDoc attrName,
66                 String JavaDoc type, String JavaDoc value) {
67         ElementType e = getElementType(elemName);
68         if (e == null) {
69             throw new Error JavaDoc("Attribute " + attrName +
70                 " specified for unknown element type " +
71                 elemName);
72             }
73         e.setAttribute(attrName, type, value);
74         }
75
76     /**
77     Specify natural parent of an element in this schema.
78     @param name Name of the child element
79     @param parentName Name of the parent element
80     **/

81
82     public void parent(String JavaDoc name, String JavaDoc parentName) {
83         ElementType child = getElementType(name);
84         ElementType parent = getElementType(parentName);
85         if (child == null) {
86             throw new Error JavaDoc("No child " + name + " for parent " + parentName);
87             }
88         if (parent == null) {
89             throw new Error JavaDoc("No parent " + parentName + " for child " + name);
90             }
91         child.setParent(parent);
92         }
93
94     /**
95     Add to or replace a character entity in this schema.
96     @param name Name of the entity
97     @param value Value of the entity
98     **/

99
100     public void entity(String JavaDoc name, char value) {
101         theEntities.put(name, new Character JavaDoc(value));
102         }
103
104     /**
105     Get an ElementType by name.
106     @param name Name (Qname) of the element type
107     @return The corresponding ElementType
108     **/

109
110     public ElementType getElementType(String JavaDoc name) {
111         return (ElementType)(theElementTypes.get(name));
112         }
113
114     /**
115     Get an entity value by name.
116     @param name Name of the entity
117     @return The corresponding character, or 0 if none
118     **/

119
120     public char getEntity(String JavaDoc name) {
121 // System.err.println("%% Looking up entity " + name);
122
if (name.length() == 0) return 0;
123         if (name.charAt(0) == '#') {
124             if (name.length() > 1 && name.charAt(1) == 'x') {
125                 try {
126                     return (char)Integer.parseInt(name.substring(2), 16);
127                     }
128                 catch (NumberFormatException JavaDoc e) { return 0; }
129                 }
130             try {
131                 return (char)Integer.parseInt(name.substring(1));
132                 }
133             catch (NumberFormatException JavaDoc e) { return 0; }
134             }
135         Character JavaDoc c = (Character JavaDoc)theEntities.get(name);
136         if (c == null) {
137             return 0;
138             }
139         return c.charValue();
140         }
141
142     /**
143     Return the URI (namespace name) of this schema.
144     **/

145
146     public String JavaDoc getURI() {
147         return theURI;
148         }
149
150     /**
151     Return the prefix of this schema.
152     **/

153
154     public String JavaDoc getPrefix() {
155         return thePrefix;
156         }
157
158     /**
159     Change the URI (namespace name) of this schema.
160     **/

161
162     public void setURI(String JavaDoc uri) {
163         theURI = uri;
164         }
165
166     /**
167     Change the prefix of this schema.
168     **/

169
170     public void setPrefix(String JavaDoc prefix) {
171         thePrefix = prefix;
172         }
173
174     }
175
Popular Tags