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  /**
39   * Implementation of DTD General Entity declaration specification.
40   * 
41   * @author <a href="mailto:totok@intalio.com">Alexander Totok</a>
42   * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
43   */
44  public class GeneralEntity {
45  
46    private static final short INTERNAL = 0;
47    private static final short EXTERNAL_PUBLIC = 1;
48    private static final short EXTERNAL_SYSTEM = 2;
49  
50    /**
51     * Name of the general entity.
52     */
53    private String name = null;
54  
55    /**
56     * DTD document owning this General Entity declaration.
57     */
58    private DTDdocument document = null;
59  
60    /**
61     * Value of the general entity - <b>replacement text</b>.
62     */
63    private String value = null;
64  
65    /**
66     * Type of the general entity. Value may be {@link #INTERNAL INTERNAL} - the entity is internal
67     * and the {@link #value value} is specified, {@link #EXTERNAL_PUBLIC EXTERNAL_PUBLIC} - the
68     * {@link #pubIdentifier pubIdentifier} and {@link #sysIdentifier sysIdentifier} are specified,
69     * {@link #EXTERNAL_SYSTEM EXTERNAL_SYSTEM} - the {@link #sysIdentifier sysIdentifier} only is
70     * specified, or -1 if unspecified.
71     */
72    private short type = -1;
73  
74    /**
75     * <b>Public identifier</b> of the general entity.
76     */
77    private String pubIdentifier = null;
78  
79    /**
80     * <b>System identifier</b> of the general entity.
81     */
82    private String sysIdentifier = null;
83  
84    /**
85     * Name of associated <b>NOTATION</b>.
86     */
87    private String ndata = null;
88  
89    /**
90     * Default constructor.
91     */
92    public GeneralEntity() {}
93  
94    /**
95     * Constructor, setting name and owning DTD document of the general entity.
96     * 
97     * @param document must not be <tt>null</tt>.
98     * @param name must not be <tt>null</tt> or equal to empty String.
99     */
100   public GeneralEntity(DTDdocument document, String name) {
101 
102     if (document == null) {
103       String err = "GeneralEntity constructor: document must not be null.";
104       throw new IllegalArgumentException(err);
105     }
106 
107     if (name == null || name.equals("")) {
108       String err = "GeneralEntity constructor: name must not be empty.";
109       throw new IllegalArgumentException(err);
110     }
111 
112     this.name = name;
113     this.document = document;
114   } // -- GeneralEntity
115 
116   /**
117    * Returns the name of the general entity.
118    */
119   public String getName() {
120     return name;
121   } // -- getName
122 
123 
124   /**
125    * Return DTD document owning this General Entity declaration.
126    */
127   public DTDdocument getDocument() {
128     return document;
129   } // -- getDocument
130 
131   /**
132    * Sets the value (replacement text) of the general entity, making it internal parsed entity.
133    * 
134    * @param value must not be <tt>null</tt>.
135    */
136   public void setValue(String value) {
137     if (value == null) {
138       String err = "GeneralEntity: can not set null value.";
139       throw new IllegalArgumentException(err);
140     }
141     type = INTERNAL;
142     this.value = value;
143   } // -- setValue
144 
145   /**
146    * <b>True</b> if internal entity, <b>false</b> otherwise.
147    */
148   public boolean isInternal() {
149     return type == INTERNAL;
150   } // -- isInternal
151 
152   /**
153    * Returns the value of the entity (replacement text) if internal entity, <tt>null</tt> otherwise.
154    */
155   public String getValue() {
156     if (isInternal())
157       return value;
158     return null;
159   } // -- getValue
160 
161   /**
162    * Sets the general entity to <tt>EXTERNAL_PUBLIC</tt>.
163    * 
164    * @param pubId public identifier - must not be <tt>null</tt>.
165    * @param sysId system identifier - must not be <tt>null</tt>.
166    */
167   public void setExternalPublic(String pubId, String sysId) {
168     if (pubId == null) {
169       String err = "GeneralEntity: can not set null public ID.";
170       throw new IllegalArgumentException(err);
171     }
172 
173     if (sysId == null) {
174       String err = "GeneralEntity: can not set null system ID.";
175       throw new IllegalArgumentException(err);
176     }
177 
178     type = EXTERNAL_PUBLIC;
179     pubIdentifier = pubId;
180     sysIdentifier = sysId;
181   } // -- setExternalPublic
182 
183   /**
184    * <b>True</b> if <tt>EXTERNAL_PUBLIC</tt> entity, <b>false</b> otherwise.
185    */
186   public boolean isExternalPublic() {
187     return type == EXTERNAL_PUBLIC;
188   } // -- isExternalPublic
189 
190   /**
191    * Sets the general entity to <tt>EXTERNAL_SYSTEM</tt>.
192    * 
193    * @param sysId system identifier - must not be <tt>null</tt>.
194    */
195   public void setExternalSystem(String sysId) {
196     if (sysId == null) {
197       String err = "GeneralEntity: can not set null system ID.";
198       throw new IllegalArgumentException(err);
199     }
200 
201     type = EXTERNAL_SYSTEM;
202     sysIdentifier = sysId;
203   } // -- setExternalSystem
204 
205   /**
206    * <b>True</b> if <tt>EXTERNAL_SYSTEM</tt> entity, <b>false</b> otherwise.
207    */
208   public boolean isExternalSystem() {
209     return type == EXTERNAL_SYSTEM;
210   } // -- isExternalSystem
211 
212   /**
213    * Returns system identifier, if <tt>EXTERNAL_PUBLIC</tt> or <tt>EXTERNAL_SYSTEM</tt> entity,
214    * <tt>null</tt> otherwise.
215    */
216   public String getSysIdentifier() {
217     if (isExternalSystem() || isExternalPublic())
218       return sysIdentifier;
219     return null;
220   } // -- getSysIdentifier
221 
222   /**
223    * Returns public identifier, if <tt>EXTERNAL_PUBLIC</tt> entity, <tt>null</tt> otherwise.
224    */
225   public String getPubIdentifier() {
226     if (isExternalPublic())
227       return pubIdentifier;
228     return null;
229   } // -- getPubIdentifier
230 
231   /**
232    * Sets the associated notation.
233    * 
234    * @param notationName - must not be <tt>null</tt> or equal to empty String.
235    */
236   public void setNDATA(String notationName) {
237     if (notationName == null || notationName.equals("")) {
238       String err = "General Entity: can not set empty associated notation name.";
239       throw new IllegalArgumentException(err);
240     }
241     ndata = notationName;
242   } // -- setNDATA
243 
244   /**
245    * <b>True</b> if external unparsed entity, that is if external and associated notation name is
246    * specified, <b>false</b> otherwise.
247    */
248   public boolean isExternalUnparsed() {
249     if ((isExternalPublic() || isExternalSystem()) && ndata != null)
250       return true;
251     return false;
252   } // -- isUnparsed
253 
254   /**
255    * Returns name of associated notation, if external entity, <tt>null</tt> otherwise.
256    */
257   public String getNotation() {
258     if (isExternalPublic() || isExternalSystem())
259       return ndata;
260     return null;
261   } // -- getNotation
262 
263 } // -- GeneralEntity