KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom > EntityImpl


1 /**
2  * org/ozone-db/xml/dom/EntityImpl.java
3  *
4  * The contents of this file are subject to the OpenXML Public
5  * License Version 1.0; you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at
7  * http://www.openxml.org/license.html
8  *
9  * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
10  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
11  * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
12  * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
13  * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
14  * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
15  *
16  * The Initial Developer of this code under the License is Assaf Arkin.
17  * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
18  * All Rights Reserved.
19  */

20
21 /**
22  * Changes for Persistent DOM running with ozone are
23  * Copyright 1999 by SMB GmbH. All rights reserved.
24  */

25
26 package org.ozoneDB.xml.dom;
27
28 import java.io.*;
29 import org.w3c.dom.*;
30
31
32 /**
33  * Implements an entity.
34  * <P>
35  * Notes:
36  * <OL>
37  * <LI>Node type is {@link org.w3c.dom.Node#ENTITY_NODE}
38  * <LI>Node supports childern
39  * <LI>Node does not have a value
40  * <LI>Node only accessible from {@link org.w3c.dom.DocumentType}
41  * </OL>
42  *
43  *
44  * @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:14 $
45  * @author <a HREF="mailto:arkin@trendline.co.il">Assaf Arkin</a>
46  * @see org.w3c.dom.Entity
47  * @see NodeImpl
48  */

49 public class EntityImpl extends NodeImpl implements EntityProxy, Externalizable {
50     
51     final static long serialVersionUID = 1;
52     
53     
54     public short getNodeType() {
55         return ENTITY_NODE;
56     }
57     
58     
59     public final void setNodeValue( String JavaDoc value ) {
60         throw new DOMExceptionImpl( DOMException.NO_DATA_ALLOWED_ERR, "This node type does not support values." );
61     }
62     
63     
64     public String JavaDoc getPublicId() {
65         return _publicId;
66     }
67     
68     
69     public void setPublicId( String JavaDoc publicId ) {
70         _publicId = publicId;
71     }
72     
73     
74     public String JavaDoc getSystemId() {
75         return _systemId;
76     }
77     
78     
79     public void setSystemId( String JavaDoc systemId ) {
80         _systemId = systemId;
81     }
82     
83     
84     public String JavaDoc getNotationName() {
85         return _notation;
86     }
87     
88     
89     public void setNotationName( String JavaDoc notation ) {
90         _notation = notation;
91     }
92     
93     
94     public final String JavaDoc getInternal() {
95         return _internalValue;
96     }
97     
98     
99     /**
100      * Returns true if entity is an unparsed general entity. An unparsed entity
101      * is one for which a notation has been specified.
102      *
103      * @return True if unparsed general entity
104      */

105     public boolean isUnparsed() {
106         return _notation != null;
107     }
108     
109     
110     /**
111      * Returns true if entity is an internal general entity. An internal entity
112      * is one for which a value has been defined. An external entity is one for
113      * which an external entity has been assigned through either system or
114      * public identifiers.
115      * <P>
116      * If true is returned, then {@link #getInternal} will return a string
117      * (might be empty).
118      *
119      * @return True if internal general entity
120      */

121     public boolean isInternal() {
122         return _internalValue != null;
123     }
124     
125     
126     public void setInternal( String JavaDoc internalValue ) {
127         _internalValue = internalValue;
128     }
129     
130     
131     /**
132      * Returns the parsing state of this entity.
133      *
134      * @return State of entity
135      */

136     public short getState() {
137         return _state;
138     }
139     
140     
141     /**
142      * Changes the parsing state of this entity. Note that only some changes
143      * are allowed: from declared to parsing, parsed or not found; from parsing
144      * to parsed or not found; from not found to declared.
145      *
146      * @param newState New state of entity
147      */

148     public void setState( short newState ) {
149         if (_state == STATE_DECLARED && newState == STATE_PARSING || _state == STATE_NOT_FOUND && newState
150                 == STATE_DECLARED) {
151             _state = newState;
152         } else if ((_state == STATE_DECLARED || _state == STATE_PARSING) && (newState == STATE_PARSED || newState
153                 == STATE_NOT_FOUND)) {
154             _state = newState;
155         } else {
156             throw new IllegalStateException JavaDoc( "Cannot switch from state " + _state + " to state " + newState + "." );
157         }
158     }
159     
160     
161     public synchronized boolean equals( Object JavaDoc other ) {
162         EntityProxy otherX;
163         
164         // Test for node equality (this covers entity name and all its children)
165
// and then test for specific entity qualities.
166
if (super.equals( other )) {
167             otherX = (EntityProxy)other;
168             // If this entity is internal, both entities must be internal and have
169
// equal internal value.
170
if (this.isInternal()) {
171                 return otherX.isInternal() && this.getInternal().equals( otherX.getInternal() );
172             }
173             // External or unparsed: either public id are both null, or public id
174
// equals in both (and same for system id and notation).
175
return
176                     (this.getPublicId() == null && otherX.getPublicId() == null || this.getPublicId() != null
177                     && this.getPublicId().equals( otherX.getPublicId() )) && (this.getSystemId() == null
178                     && otherX.getSystemId() == null || this.getSystemId() != null && this.getSystemId().equals(
179                     otherX.getSystemId() )) && (this.getNotationName() == null && otherX.getNotationName() == null
180                     || this.getNotationName() != null && this.getNotationName().equals( otherX.getNotationName() ));
181         }
182         return false;
183     }
184     
185     
186     public final Object JavaDoc clone() {
187         EntityProxy clone = null;
188         try {
189             clone = (EntityProxy)database().createObject( EntityImpl.class.getName() );
190             clone.init( _ownerDocument, getNodeName() );
191             cloneInto( clone, true );
192         } catch (Exception JavaDoc except) {
193             throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() );
194         }
195         return clone;
196     }
197     
198     
199     public final Node cloneNode( boolean deep ) {
200         EntityProxy clone = null;
201         try {
202             clone = (EntityProxy)database().createObject( EntityImpl.class.getName() );
203             clone.init( _ownerDocument, getNodeName() );
204             cloneInto( clone, deep );
205         } catch (Exception JavaDoc except) {
206             throw new DOMExceptionImpl( DOMExceptionImpl.PDOM_ERR, except.getMessage() );
207         }
208         return clone;
209     }
210     
211     
212     public String JavaDoc toString() {
213         String JavaDoc name;
214         String JavaDoc value;
215         
216         name = getNodeName();
217         if (name.length() > 32) {
218             name = name.substring( 0, 32 ) + "..";
219         }
220         if (isInternal()) {
221             value = getInternal();
222             if (value.length() > 64) {
223                 value = value.substring( 0, 64 ) + "..";
224             }
225             name = name + "] [" + value;
226         } else {
227             if (getSystemId() != null) {
228                 name = name + "] SYSTEM [" + getSystemId();
229             }
230             if (getPublicId() != null) {
231                 name = name + "] PUBLIC [" + getPublicId();
232             }
233             if (getNotationName() != null) {
234                 name = name + "] NDECL [" + getNotationName();
235             }
236         }
237         return "Entity decl: [" + name + "]";
238     }
239     
240     
241     protected final boolean supportsChildern() {
242         return true;
243     }
244     
245     
246     public synchronized void cloneInto( NodeProxy into, boolean deep ) {
247         super.cloneInto( into, deep );
248         ((EntityProxy)into).setSystemId( this._systemId );
249         ((EntityProxy)into).setPublicId( this._publicId );
250         ((EntityProxy)into).setNotationName( this._notation );
251         ((EntityProxy)into).setInternal( this._internalValue );
252         ((EntityProxy)into).setState( this._state );
253     }
254     
255     
256     /**
257      * Constructs an unparsed general entity. Entity system identifier and notation
258      * name must be provided, public identifier is optional.
259      *
260      * @param owner The owner document
261      * @param name The entity name
262      * @param systemId The system identifier
263      * @param publicId The public identifier, if specified
264      * @param notation The notation, if specified
265      */

266     public EntityImpl( DocumentImpl owner, String JavaDoc name, String JavaDoc systemId, String JavaDoc publicId, String JavaDoc notation ) {
267         super( owner, name, null, true );
268         if (notation == null) {
269             throw new NullPointerException JavaDoc( "Argument 'notation' cannot be null." );
270         } else {
271             init( owner, name, systemId, publicId, notation );
272         }
273     }
274     
275     
276     /**
277      * Constructs an external general entity. Entity system identifier must be
278      * provided, public identifier is optional.
279      *
280      * @param owner The owner document
281      * @param name The entity name
282      * @param systemId The system identifier
283      * @param publicId The public identifier, if specified
284      */

285     public EntityImpl( DocumentImpl owner, String JavaDoc name, String JavaDoc systemId, String JavaDoc publicId ) {
286         super( owner, name, null, true );
287         if (systemId == null) {
288             throw new NullPointerException JavaDoc( "Argument 'systemId' cannot be null." );
289         } else {
290             init( owner, name, systemId, publicId );
291         }
292     }
293     
294     
295     /**
296      * Constructs an internal general entity. Entity value must be provided.
297      *
298      * @param owner The owner document
299      * @param name The entity name
300      * @param internalValue The unparsed entity value
301      */

302     public EntityImpl( DocumentImpl owner, String JavaDoc name, String JavaDoc internalValue ) {
303         super( owner, name, null, true );
304         if (internalValue == null) {
305             throw new NullPointerException JavaDoc( "Argument 'internalValue' cannot be null." );
306         } else {
307             init( owner, name, internalValue );
308         }
309     }
310     
311     
312     private EntityImpl( DocumentImpl owner, String JavaDoc name ) {
313         super( owner, name, null, true );
314     }
315     
316     
317     public EntityImpl() {
318         super();
319     }
320     
321     
322     public void init( DocumentProxy owner, String JavaDoc name ) {
323         super.init( owner, name, null, true );
324     }
325     
326     
327     public void init( DocumentProxy owner, String JavaDoc name, String JavaDoc value ) {
328         _systemId = null;
329         _publicId = null;
330         _notation = null;
331         _state = STATE_DECLARED;
332         _internalValue = value;
333     }
334     
335     
336     public void init( DocumentProxy owner, String JavaDoc name, String JavaDoc systemId, String JavaDoc publicId ) {
337         _systemId = systemId;
338         _publicId = publicId;
339         _notation = null;
340         _state = STATE_DECLARED;
341     }
342     
343     
344     public void init( DocumentProxy owner, String JavaDoc name, String JavaDoc systemId, String JavaDoc publicId, String JavaDoc notation ) {
345         _systemId = systemId;
346         _publicId = publicId;
347     }
348     
349     
350     /** */
351     public void writeExternal( ObjectOutput out ) throws IOException {
352         super.writeExternal( out );
353         out.writeObject( _notation );
354         out.writeObject( _systemId );
355         out.writeObject( _publicId );
356         out.writeShort( _state );
357         out.writeObject( _internalValue );
358     }
359     
360     
361     /** */
362     public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException JavaDoc {
363         super.readExternal( in );
364         _notation = (String JavaDoc)in.readObject();
365         _systemId = (String JavaDoc)in.readObject();
366         _publicId = (String JavaDoc)in.readObject();
367         _state = in.readShort();
368         _internalValue = (String JavaDoc)in.readObject();
369     }
370     
371     
372     /**
373      * The notation of this entity, if specified.
374      */

375     protected String JavaDoc _notation;
376     
377     /**
378      * The system identifier of this entity, if specified.
379      */

380     protected String JavaDoc _systemId;
381     
382     /**
383      * The public identifier of this entity, if specified.
384      */

385     protected String JavaDoc _publicId;
386     
387     /**
388      * Identifies the state of this entity as yet to be parsed, being parsed,
389      * has been parsed, or cannot be found.
390      */

391     private short _state;
392     
393     /**
394      * Holds the internal value of the entity.
395      */

396     private String JavaDoc _internalValue;
397     
398     /**
399      * Entity has been declared but not parsed. This is the initial state for
400      * an entity after it has been declared in the DTD but before it is used
401      * in the document contents.
402      */

403     public final static short STATE_DECLARED = 0;
404     
405     /**
406      * Entity is being parsed. This state designates that the entity is being
407      * parsed right now. State is used to identify circular references.
408      */

409     public final static short STATE_PARSING = 1;
410     
411     /**
412      * Entity has been parsed. This state indicates that entity has been parsed
413      * and it's parsed contents is contained in its child nodes.
414      */

415     public final static short STATE_PARSED = 2;
416     
417     /**
418      * Entity not found. The entity could not be parsed before.
419      */

420     public final static short STATE_NOT_FOUND = 3;
421     
422 }
423
Popular Tags