View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 1999-2002,2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.dom;
22  
23  import org.w3c.dom.Entity;
24  import org.w3c.dom.Node;
25  import org.w3c.dom.DOMException;
26  
27  /**
28   * Entity nodes hold the reference data for an XML Entity -- either
29   * parsed or unparsed. The nodeName (inherited from Node) will contain
30   * the name (if any) of the Entity. Its data will be contained in the
31   * Entity's children, in exactly the structure which an
32   * EntityReference to this name will present within the document's
33   * body.
34   * <P>
35   * Note that this object models the actual entity, _not_ the entity
36   * declaration or the entity reference.
37   * <P>
38   * An XML processor may choose to completely expand entities before
39   * the structure model is passed to the DOM; in this case, there will
40   * be no EntityReferences in the DOM tree.
41   * <P>
42   * Quoting the 10/01 DOM Proposal,
43   * <BLOCKQUOTE>
44   * "The DOM Level 1 does not support editing Entity nodes; if a user
45   * wants to make changes to the contents of an Entity, every related
46   * EntityReference node has to be replaced in the structure model by
47   * a clone of the Entity's contents, and then the desired changes
48   * must be made to each of those clones instead. All the
49   * descendants of an Entity node are readonly."
50   * </BLOCKQUOTE>
51   * I'm interpreting this as: It is the parser's responsibilty to call
52   * the non-DOM operation setReadOnly(true,true) after it constructs
53   * the Entity. Since the DOM explicitly decided not to deal with this,
54   * _any_ answer will involve a non-DOM operation, and this is the
55   * simplest solution.
56   *
57   * @xerces.internal
58   *
59   * @author Elena Litani, IBM
60   * @since PR-DOM-Level-1-19980818.
61   */
62  public class EntityImpl
63      extends ParentNode
64      implements Entity {
65  
66      //
67      // Constants
68      //
69  
70      /** Serialization version. */
71      static final long serialVersionUID = -3575760943444303423L;
72  
73      //
74      // Data
75      //
76  
77      /** Entity name. */
78      protected String name;
79  
80      /** Public identifier. */
81      protected String publicId;
82  
83      /** System identifier. */
84      protected String systemId;
85  
86      /** Encoding */
87      protected String encoding;
88  
89  
90      /** Input Encoding */
91      protected String inputEncoding;
92  
93      /** Version */
94      protected String version;
95  
96  
97      /** Notation name. */
98      protected String notationName;
99  
100     /** base uri*/
101     protected String baseURI;
102 
103     //
104     // Constructors
105     //
106 
107     /** Factory constructor. */
108     public EntityImpl(CoreDocumentImpl ownerDoc, String name) {
109         super(ownerDoc);
110         this.name = name;
111         isReadOnly(true);
112     }
113 
114     //
115     // Node methods
116     //
117 
118     /**
119      * A short integer indicating what type of node this is. The named
120      * constants for this value are defined in the org.w3c.dom.Node interface.
121      */
122     public short getNodeType() {
123         return Node.ENTITY_NODE;
124     }
125 
126     /**
127      * Returns the entity name
128      */
129     public String getNodeName() {
130         if (needsSyncData()) {
131             synchronizeData();
132         }
133         return name;
134     }
135     /**
136      * Sets the node value.
137      * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR)
138      */
139     public void setNodeValue(String x)
140         throws DOMException {
141         if (ownerDocument.errorChecking && isReadOnly()) {
142             String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
143             throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
144         }
145     }
146     /**
147      * The namespace prefix of this node
148      * @exception DOMException
149      *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
150      */
151     public void setPrefix(String prefix)
152         throws DOMException
153     {
154         if (ownerDocument.errorChecking && isReadOnly()) {
155             throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
156                   DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
157                     "NO_MODIFICATION_ALLOWED_ERR", null));
158         }
159     }
160     /** Clone node. */
161     public Node cloneNode(boolean deep) {
162         EntityImpl newentity = (EntityImpl)super.cloneNode(deep);
163         newentity.setReadOnly(true, deep);
164         return newentity;
165     }
166 
167     //
168     // Entity methods
169     //
170 
171     /**
172      * The public identifier associated with the entity. If not specified,
173      * this will be null.
174      */
175     public String getPublicId() {
176 
177         if (needsSyncData()) {
178             synchronizeData();
179         }
180         return publicId;
181 
182     } // getPublicId():String
183 
184     /**
185      * The system identifier associated with the entity. If not specified,
186      * this will be null.
187      */
188     public String getSystemId() {
189 
190         if (needsSyncData()) {
191             synchronizeData();
192         }
193         return systemId;
194 
195     } // getSystemId():String
196 
197     /**
198       * DOM Level 3 WD - experimental
199       * the version number of this entity, when it is an external parsed entity.
200       */
201     public String getXmlVersion() {
202 
203        if (needsSyncData()) {
204            synchronizeData();
205        }
206        return version;
207 
208    } // getVersion():String
209 
210 
211     /**
212      * DOM Level 3 WD - experimental
213      * the encoding of this entity, when it is an external parsed entity.
214      */
215     public String getXmlEncoding() {
216 
217        if (needsSyncData()) {
218            synchronizeData();
219        }
220 
221        return encoding;
222 
223    } // getVersion():String
224 
225 
226 
227 
228 
229     /**
230      * Unparsed entities -- which contain non-XML data -- have a
231      * "notation name" which tells applications how to deal with them.
232      * Parsed entities, which <em>are</em> in XML format, don't need this and
233      * set it to null.
234      */
235     public String getNotationName() {
236 
237         if (needsSyncData()) {
238             synchronizeData();
239         }
240         return notationName;
241 
242     } // getNotationName():String
243 
244     //
245     // Public methods
246     //
247 
248     /**
249      * DOM Level 2: The public identifier associated with the entity. If not specified,
250      * this will be null. */
251     public void setPublicId(String id) {
252 
253         if (needsSyncData()) {
254             synchronizeData();
255         }
256         publicId = id;
257 
258     } // setPublicId(String)
259 
260     /**
261      * NON-DOM
262      * encoding - An attribute specifying, as part of the text declaration,
263      * the encoding of this entity, when it is an external parsed entity.
264      * This is null otherwise
265      *
266      */
267     public void setXmlEncoding(String value) {
268         if (needsSyncData()) {
269             synchronizeData();
270         }
271         encoding = value;
272     } // setEncoding (String)
273 
274 
275     /**
276      * An attribute specifying the encoding used for this entity at the tiome
277      * of parsing, when it is an external parsed entity. This is
278      * <code>null</code> if it an entity from the internal subset or if it
279      * is not known..
280      * @since DOM Level 3
281      */
282     public String getInputEncoding(){
283         if (needsSyncData()) {
284             synchronizeData();
285         }
286         return inputEncoding;
287     }
288 
289     /**
290      * NON-DOM, used to set the input encoding.
291      */
292     public void setInputEncoding(String inputEncoding){
293         if (needsSyncData()) {
294             synchronizeData();
295         }
296         this.inputEncoding = inputEncoding;
297     }
298 
299     /**
300       * NON-DOM
301       * version - An attribute specifying, as part of the text declaration,
302       * the version number of this entity, when it is an external parsed entity.
303       * This is null otherwise
304       */
305     public void setXmlVersion(String value) {
306         if (needsSyncData()) {
307             synchronizeData();
308         }
309         version = value;
310     } // setVersion (String)
311 
312 
313     /**
314      * DOM Level 2: The system identifier associated with the entity. If not
315      * specified, this will be null.
316      */
317     public void setSystemId(String id) {
318         if (needsSyncData()) {
319             synchronizeData();
320         }
321         systemId = id;
322 
323     } // setSystemId(String)
324 
325     /**
326      * DOM Level 2: Unparsed entities -- which contain non-XML data -- have a
327      * "notation name" which tells applications how to deal with them.
328      * Parsed entities, which <em>are</em> in XML format, don't need this and
329      * set it to null.
330      */
331     public void setNotationName(String name) {
332         if (needsSyncData()) {
333             synchronizeData();
334         }
335         notationName = name;
336 
337     } // setNotationName(String)
338 
339 
340 
341     /**
342      * Returns the absolute base URI of this node or null if the implementation
343      * wasn't able to obtain an absolute URI. Note: If the URI is malformed, a
344      * null is returned.
345      *
346      * @return The absolute base URI of this node or null.
347      * @since DOM Level 3
348      */
349     public String getBaseURI() {
350 
351         if (needsSyncData()) {
352             synchronizeData();
353         }
354         return (baseURI!=null)?baseURI:((CoreDocumentImpl)getOwnerDocument()).getBaseURI();
355     }
356 
357     /** NON-DOM: set base uri*/
358     public void setBaseURI(String uri){
359         if (needsSyncData()) {
360             synchronizeData();
361         }
362         baseURI = uri;
363     }
364 
365 
366 
367 } // class EntityImpl