001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 2000 (C) Intalio Inc. All Rights Reserved.
042: *
043: * $Id: Element.java 5951 2006-05-30 22:18:48Z bsnyder $
044: */package org.exolab.castor.xml.dtd;
045:
046: import java.util.HashSet;
047: import java.util.Hashtable;
048: import java.util.Iterator;
049: import java.util.Enumeration;
050:
051: /**
052: * Implementation of DTD Element declaration specification.
053: * @author <a href="mailto:totok@intalio.com">Alexander Totok</a>
054: * @version $Revision: 5951 $ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
055: */
056: public class Element {
057:
058: private static final short ANY = 0;
059: private static final short EMPTY = 1;
060: private static final short MIXED = 2;
061: private static final short ELEMENTS_ONLY = 3;
062:
063: /**
064: * Name of the element.
065: */
066: private String name;
067:
068: /**
069: * DTD document owning this element
070: */
071: private DTDdocument document;
072:
073: /**
074: * Content type of the element. Value may be {@link #ANY ANY}, {@link #EMPTY EMPTY},
075: * {@link #MIXED MIXED}, {@link #ELEMENTS_ONLY ELEMENTS_ONLY} or
076: * -1, if unspecified.
077: */
078: private short contentType = -1;
079:
080: /**
081: * Set of names of children of the element,
082: * if the element has <tt>MIXED</tt> content.
083: */
084: private HashSet mixedChildren = null;
085:
086: /**
087: * Content Particle representing content of the element, if the element has
088: * <tt>ELEMENTS_ONLY</tt> content.
089: */
090: private ContentParticle content = null;
091:
092: /**
093: * Attributes of the element.
094: */
095: private Hashtable attributes;
096:
097: /**
098: * Constructor, setting the name of the element and owning DTD document.
099: * @param document must not be null.
100: */
101: public Element(DTDdocument document, String name) {
102:
103: if (document == null) {
104: String err = "Element constructor: document must not be null.";
105: throw new IllegalArgumentException(err);
106: }
107:
108: this .name = name;
109: this .document = document;
110: attributes = new Hashtable();
111: } //-- Element
112:
113: /**
114: * Constructor, setting owning DTD document of the element.
115: * @param document must not be null.
116: */
117: public Element(DTDdocument document) {
118: this (document, null);
119: } //-- Element
120:
121: /**
122: * Returns the name of the element.
123: */
124: public String getName() {
125: return name;
126: } //-- getName
127:
128: /**
129: * Returns DTD document owning this element.
130: */
131: public DTDdocument getDocument() {
132: return document;
133: } //-- getDocument
134:
135: /**
136: * Returns {@link java.util.Iterator iterator} of the set of mixed children,
137: * if of <tt>MIXED</tt> content, <tt>null</tt> otherwise.
138: */
139: public Iterator getMixedContentChildren() {
140: if (isMixedContent())
141: return mixedChildren.iterator();
142: return null;
143: } //-- getMixedContentChildren
144:
145: /**
146: * Returns enumeration of the attributes of the element.
147: */
148: public Enumeration getAttributes() {
149: return attributes.elements();
150: } //-- getAttributes
151:
152: /**
153: * Returns {@link org.exolab.castor.xml.dtd.ContentParticle Content Particle},
154: * representing the content of the element, if has <tt>ELEMENTS_ONLY</tt>
155: * content, <tt>null</tt> otherwise.
156: */
157: public ContentParticle getContent() {
158: if (isElemOnlyContent())
159: return content;
160: return null;
161: } //-- getContent
162:
163: /**
164: * Sets the name of the element.
165: */
166: public void setName(String name) {
167: this .name = name;
168: } //-- setName
169:
170: /**
171: * Sets the content type of the element to <tt>ANY</tt>.
172: */
173: public void setAnyContent() {
174: contentType = ANY;
175: } //-- setAnyContent
176:
177: /**
178: * <b>True</b> if the element is of <tt>ANY</tt> content type,
179: * <b>false</b> otherwise.
180: */
181: public boolean isAnyContent() {
182: return contentType == ANY;
183: } //-- isAnyContent
184:
185: /**
186: * Sets the content type of the element to <tt>EMPTY</tt>.
187: */
188: public void setEmptyContent() {
189: contentType = EMPTY;
190: } //-- setEmptyContent
191:
192: /**
193: * <b>True</b> if the element is of <tt>EMPTY</tt> content type, <b>false</b> otherwise.
194: */
195: public boolean isEmptyContent() {
196: return contentType == EMPTY;
197: } //-- isEmptyContent
198:
199: /**
200: * Sets the content type of the element to <tt>MIXED</tt>.
201: */
202: public void setMixedContent() {
203: contentType = MIXED;
204: mixedChildren = new HashSet();
205: } //-- setMixedContent
206:
207: /**
208: * <b>True</b> if the element is of <tt>MIXED</tt> content type, <b>false</b> otherwise.
209: */
210: public boolean isMixedContent() {
211: return contentType == MIXED;
212: } //-- isMixedContent
213:
214: /**
215: * Sets the content type of the element to <tt>ELEMENTS_ONLY</tt>.
216: * @param cp Content Particle representing content of the element.
217: */
218: public void setElemOnlyContent(ContentParticle cp) {
219: contentType = ELEMENTS_ONLY;
220: content = cp;
221: } //-- setChildrenContent
222:
223: /**
224: * <b>True</b> if the element is of <tt>ELEMENTS_ONLY</tt> content type,
225: * <b>false</b> otherwise.
226: */
227: public boolean isElemOnlyContent() {
228: return contentType == ELEMENTS_ONLY;
229: } //-- isChildrenContent
230:
231: /**
232: * Adds name of a <tt>child</tt> to the set of children's names.
233: * @throws DTDException if there already exists the child with the same name.
234: */
235: public synchronized void addMixedContentChild(String child)
236: throws DTDException {
237: if (mixedChildren.contains(child)) {
238: String err = "Element \"" + name
239: + "\" already contains child element ";
240: err += "\"" + child + "\".";
241: throw new DTDException(err);
242: }
243: mixedChildren.add(child);
244: } //-- addChild
245:
246: /**
247: * Adds attribute to the element. If the element already has the attribute
248: * with the same name, does nothing.
249: */
250: public synchronized void addAttribute(Attribute attribute) {
251: String name = attribute.getName();
252: if (!attributes.containsKey(name))
253: attributes.put(name, attribute);
254: } //-- addAttribute
255:
256: } //-- Element
|