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