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