View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000 (C) Intalio Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  
46  package org.exolab.castor.xml.dtd;
47  
48  import java.util.Hashtable;
49  import java.util.Enumeration;
50  
51  /**
52   * Implementation of DTD document specification.
53   * @author <a href="mailto:totok@intalio.com">Alexander Totok</a>
54   * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
55   */
56  public class DTDdocument {
57  
58     /**
59      * Name of the document.
60      */
61     private String name = null;
62  
63     /**
64      * Element declarations in the document.
65      */
66     private Hashtable elements;
67  
68     /**
69      * Notation declarations in the document.
70      */
71     private Hashtable notations;
72  
73     /**
74      * General Entity declarations in the document.
75      */
76     private Hashtable generalEntities;
77  
78     public DTDdocument() {
79        elements = new Hashtable();
80        notations = new Hashtable();
81        generalEntities = new Hashtable();
82     } //-- DTDdocument
83  
84     /**
85      * Constructor, setting the name of the document.
86      */
87     public DTDdocument(String name) {
88        this();
89        this.name = name;
90     } //-- DTDdocument
91  
92     /**
93      * Returns the name of the document.
94      */
95     public String getName() {
96        return name;
97     } //-- getName;
98  
99     /**
100     * Adds Element Declaration to the document.
101     * @throws DTDException if an element has no name or there already exists
102     * element with the same name in the document.
103     */
104    public synchronized void addElement(Element element) throws DTDException {
105 
106       String name = element.getName();
107 
108       if (name == null) {
109          String err = "An element declaration must contain a name.";
110          throw new DTDException(err);
111       }
112       if (elements.containsKey(name)) {
113          String err = "An element declaration already exists with the given name: ";
114          throw new DTDException(err + name);
115       }
116 
117       elements.put(name,element);
118 
119    } //-- addElement
120 
121    /**
122     * Returns requested Element Declaration.
123     * @return Element Declaration with the given <tt>name</tt>,
124     * <tt>null</tt> if there is no Element with this name in the document.
125     */
126    public Element getElement(String name) {
127       return (Element)elements.get(name);
128    } //-- getElement
129 
130    /**
131     * Adds a Notation Declaration to the document.
132     * @throws DTDException if a notation has no name or there already exists
133     * notation with the same name in the document.
134     */
135    public synchronized void addNotation(Notation notation)
136                                      throws DTDException {
137 
138       String name = notation.getName();
139 
140       if (name == null) {
141          String err = "A notation declaration must contain a name.";
142          throw new DTDException(err);
143       }
144       if (notations.containsKey(name)) {
145          String err = "A notation declaration already exists with the given name: ";
146          throw new DTDException(err + name);
147       }
148 
149       notations.put(name,notation);
150 
151    } //-- addNotation
152 
153    /**
154     * Returns requested Notation Declaration.
155     * @return Notation Declaration with the given <tt>name</tt>,
156     * <tt>null</tt> if there is no Notation with this name in the document.
157     */
158    public Notation getNotation(String name) {
159       return (Notation)notations.get(name);
160    } //-- getNotation
161 
162    /**
163     * Adds General Entity Declaration to the document. If there already exists
164     * General Entity with the same name in the document, does nothing.
165     */
166    public synchronized void addGeneralEntity(GeneralEntity generalEntity) {
167 
168       String name = generalEntity.getName();
169 
170       if (!generalEntities.containsKey(name))
171          generalEntities.put(name,generalEntity);
172 
173    } //-- addGeneralEntity
174 
175    /**
176     * Returns requested Genaral Entity Declaration.
177     * @return General Entity Declaration with the given <tt>name</tt>,
178     * <tt>null</tt> if there is no General Entity with this name in the document.
179     */
180    public GeneralEntity getGeneralEntity(String name) {
181       return (GeneralEntity)generalEntities.get(name);
182    } //-- getGeneralEntities
183 
184    /**
185     * Returns enumeration of the Element declarations in the DTD document.
186     */
187    public Enumeration getElements() {
188       return elements.elements();
189    } //-- getElements
190 
191    /**
192     * Returns enumeration of the General Entity declarations in the DTD document.
193     */
194    public Enumeration getGeneralEntities() {
195       return generalEntities.elements();
196    } //-- getGeneralEntities
197 
198    /**
199     * Returns enumeration of the Notation declarations in the DTD document.
200     */
201    public Enumeration getNotations() {
202       return notations.elements();
203    } //-- getNotations
204 
205 } //-- DTDdocument