KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.ccil.cowan.tagsoup;
14
15 /**
16 This class represents an element type in the schema.
17 An element type has a name, a content model vector, a member-of vector,
18 a flags vector, default attributes, and a schema to which it belongs.
19 @see Schema
20 */

21
22 public class ElementType {
23
24     private String JavaDoc theName; // element type name (Qname)
25
private String JavaDoc theNamespace; // element type namespace name
26
private String JavaDoc theLocalName; // element type local name
27
private int theModel; // bitmap: what the element contains
28
private int theMemberOf; // bitmap: what element is contained in
29
private int theFlags; // bitmap: element flags
30
private AttributesImpl theAtts; // default attributes
31
private ElementType theParent; // parent of this element type
32
private Schema theSchema; // schema to which this belongs
33

34     /**
35     Construct an ElementType:
36     but it's better to use Schema.element() instead.
37     The content model, member-of, and flags vectors are specified as ints.
38     @param name The element type name
39     @param model ORed-together bits representing the content models
40        allowed in the content of this element type
41     @param memberOf ORed-together bits representing the content models
42        to which this element type belongs
43     @param flags ORed-together bits representing the flags associated
44        with this element type
45     @param schema The schema with which this element type will be
46     associated
47     */

48
49     public ElementType(String JavaDoc name, int model, int memberOf, int flags, Schema schema) {
50         theName = name;
51         theModel = model;
52         theMemberOf = memberOf;
53         theFlags = flags;
54         theAtts = new AttributesImpl();
55         theSchema = schema;
56         theNamespace = namespace(name, false);
57         theLocalName = localName(name);
58         }
59
60     /**
61     Return a namespace name from a Qname.
62     The attribute flag tells us whether to return an empty namespace
63     name if there is no prefix, or use the schema default instead.
64     @param name The Qname
65     @param attribute True if name is an attribute name
66     @return The namespace name
67     **/

68     public String JavaDoc namespace(String JavaDoc name, boolean attribute) {
69         int colon = name.indexOf(':');
70         if (colon == -1) {
71             return attribute ? "" : theSchema.getURI();
72             }
73         String JavaDoc prefix = name.substring(0, colon);
74         if (prefix.equals("xml")) {
75             return "http://www.w3.org/XML/1998/namespace";
76             }
77         else {
78             return ("urn:x-prefix:" + prefix).intern();
79             }
80         }
81
82     /**
83     Return a local name from a Qname.
84     @param name The Qname
85     @return The local name
86     **/

87     public String JavaDoc localName(String JavaDoc name) {
88         int colon = name.indexOf(':');
89         if (colon == -1) {
90             return name;
91             }
92         else {
93             return name.substring(colon+1).intern();
94             }
95         }
96
97     /**
98     Returns the name of this element type.
99     @return The name of the element type
100     */

101
102     public String JavaDoc name() { return theName; }
103
104     /**
105     Returns the namespace name of this element type.
106     @return The namespace name of the element type
107     */

108
109     public String JavaDoc namespace() { return theNamespace; }
110
111     /**
112     Returns the local name of this element type.
113     @return The local name of the element type
114     */

115
116     public String JavaDoc localName() { return theLocalName; }
117
118     /**
119     Returns the content models of this element type.
120     @return The content models of this element type as a vector of bits
121     */

122
123     public int model() { return theModel; }
124
125     /**
126     Returns the content models to which this element type belongs.
127     @return The content models to which this element type belongs as a
128        vector of bits
129     */

130
131     public int memberOf() { return theMemberOf; }
132
133     /**
134     Returns the flags associated with this element type.
135     @return The flags associated with this element type as a vector of bits
136     */

137
138     public int flags() { return theFlags; }
139
140     /**
141     Returns the default attributes associated with this element type.
142     Attributes of type CDATA that don't have default values are
143     typically not included. Other attributes without default values
144     have an internal value of <tt>null</tt>.
145     The return value is an AttributesImpl to allow the caller to mutate
146     the attributes.
147     */

148
149     public AttributesImpl atts() {return theAtts;}
150
151     /**
152     Returns the parent element type of this element type.
153     @return The parent element type
154     */

155
156     public ElementType parent() {return theParent;}
157
158     /**
159     Returns the schema which this element type is associated with.
160     @return The schema
161     */

162
163     public Schema schema() {return theSchema;}
164
165
166     /**
167     Returns true if this element type can contain another element type.
168     That is, if any of the models in this element's model vector
169     match any of the models in the other element type's member-of
170     vector.
171     @param other The other element type
172     */

173
174     public boolean canContain(ElementType other) {
175         return (theModel & other.theMemberOf) != 0;
176         }
177
178
179     /**
180     Sets an attribute and its value into an AttributesImpl object.
181     Attempts to set a namespace declaration are ignored.
182     @param atts The AttributesImpl object
183     @param name The name (Qname) of the attribute
184     @param type The type of the attribute
185     @param value The value of the attribute
186     */

187
188     public void setAttribute(AttributesImpl atts, String JavaDoc name, String JavaDoc type, String JavaDoc value) {
189         if (name.equals("xmlns") || name.startsWith("xmlns:")) {
190             return;
191             }
192 ;
193         String JavaDoc namespace = namespace(name, true);
194         String JavaDoc localName = localName(name);
195         int i = atts.getIndex(name);
196         if (i == -1) {
197             name = name.intern();
198             if (type == null) type = "CDATA";
199             if (!type.equals("CDATA")) value = normalize(value);
200             atts.addAttribute(namespace, localName, name, type, value);
201             }
202         else {
203             if (type == null) type = atts.getType(i);
204             if (!type.equals("CDATA")) value=normalize(value);
205             atts.setAttribute(i, namespace, localName, name, type, value);
206             }
207         }
208
209     /**
210     Normalize an attribute value (ID-style).
211     CDATA-style attribute normalization is already done.
212     @param value The value to normalize
213     @return The normalized value
214     **/

215     public static String JavaDoc normalize(String JavaDoc value) {
216         if (value == null) return value;
217         value = value.trim();
218         if (value.indexOf(" ") == -1) return value;
219         boolean space = false;
220         int len = value.length();
221         StringBuffer JavaDoc b = new StringBuffer JavaDoc(len);
222         for (int i = 0; i < len; i++) {
223             char v = value.charAt(i);
224             if (v == ' ') {
225                 if (!space) b.append(v);
226                 space = true;
227                 }
228             else {
229                 b.append(v);
230                 space = false;
231                 }
232             }
233         return b.toString();
234         }
235
236     /**
237     Sets an attribute and its value into this element type.
238     @param name The name of the attribute
239     @param type The type of the attribute
240     @param value The value of the attribute
241     */

242
243     public void setAttribute(String JavaDoc name, String JavaDoc type, String JavaDoc value) {
244         setAttribute(theAtts, name, type, value);
245         }
246
247     /**
248     Sets the models of this element type.
249     @param model The content models of this element type as a vector of bits
250     */

251
252     public void setModel(int model) { theModel = model; }
253
254     /**
255     Sets the content models to which this element type belongs.
256     @param memberOf The content models to which this element type belongs as a vector of bits
257     */

258
259     public void setMemberOf(int memberOf) { theMemberOf = memberOf; }
260
261     /**
262     Sets the flags of this element type.
263     @param flags associated with this element type The flags as a vector of bits
264     */

265
266     public void setFlags(int flags) { theFlags = flags; }
267
268     /**
269     Sets the parent element type of this element type.
270     @param parent The parent element type
271     */

272
273     public void setParent(ElementType parent) { theParent = parent; }
274
275     }
276
Popular Tags