001: /**
002: * org/ozone-db/xml/dom/ParamEntity.java
003: *
004: * The contents of this file are subject to the OpenXML Public
005: * License Version 1.0; you may not use this file except in compliance
006: * with the License. You may obtain a copy of the License at
007: * http://www.openxml.org/license.html
008: *
009: * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
010: * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
011: * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
012: * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
013: * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
014: * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
015: *
016: * The Initial Developer of this code under the License is Assaf Arkin.
017: * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
018: * All Rights Reserved.
019: */
020:
021: /**
022: * Changes for Persistent DOM running with ozone are
023: * Copyright 1999 by SMB GmbH. All rights reserved.
024: */package org.ozoneDB.xml.dom;
025:
026: import java.io.*;
027: import org.w3c.dom.*;
028:
029: /**
030: * Implements a parameter entity.
031: * <P>
032: * Notes:
033: * <OL>
034: * <LI>Node type is {@link NodeImpl#PARAM_ENTITY_NODE}
035: * <LI>Node supports childern
036: * <LI>Node does not have a value
037: * <LI>Node only accessible from {@link org.w3c.dom.DocumentType}
038: * </OL>
039: *
040: *
041: * @version $Revision: 1.1 $ $Date: 2001/12/18 11:03:24 $
042: * @author <a href="mailto:arkin@trendline.co.il">Assaf Arkin</a>
043: * @see NodeImpl
044: */
045: public class ParamEntity extends NodeImpl implements ParamEntityProxy {
046:
047: final static long serialVersionUID = 1;
048:
049: public short getNodeType() {
050: return PARAM_ENTITY_NODE;
051: }
052:
053: public String getPublicId() {
054: return _publicId;
055: }
056:
057: public void setPublicId(String publicId) {
058: _publicId = publicId;
059: }
060:
061: public String getSystemId() {
062: return _systemId;
063: }
064:
065: public void setSystemId(String systemId) {
066: _systemId = systemId;
067: }
068:
069: /**
070: * Returns true if entity is an internal entity. An internal entity is one
071: * for which a value has been defined. An external entity is one for which
072: * an external entity has been assigned through either system or public
073: * identifiers.
074: *
075: * @return True if internal entity
076: */
077: public boolean isInternal() {
078: return _internalValue != null;
079: }
080:
081: /**
082: * Returns the parsing state of this entity.
083: *
084: * @return State of entity
085: */
086: public short getState() {
087: return _state;
088: }
089:
090: /**
091: * Changes the parsing state of this entity. Note that only some changes
092: * are allowed: from declared to parsing, parsed or not found; from parsing
093: * to parsed or not found; from not found to declared.
094: *
095: * @param newState New state of entity
096: */
097: public void setState(short newState) {
098: if (_state == STATE_DECLARED && newState == STATE_PARSING
099: || _state == STATE_NOT_FOUND
100: && newState == STATE_DECLARED) {
101: _state = newState;
102: } else if ((_state == STATE_DECLARED || _state == STATE_PARSING)
103: && (newState == STATE_PARSED || newState == STATE_NOT_FOUND)) {
104: _state = newState;
105: } else {
106: throw new IllegalStateException("Cannot switch from state "
107: + _state + " to state " + newState + ".");
108: }
109: }
110:
111: public final String getInternal() {
112: return _internalValue;
113: }
114:
115: public final void setInternal(String internalValue) {
116: _internalValue = internalValue;
117: }
118:
119: public synchronized boolean equals(Object other) {
120: ParamEntityProxy otherX;
121:
122: // Test for node equality (this covers entity name and all its children)
123: // and then test for specific entity qualities.
124: if (super .equals(other)) {
125: otherX = (ParamEntityProxy) other;
126: // If this entity is internal, both entities must be internal and have
127: // equal internal value.
128: if (this .isInternal()) {
129: return otherX.isInternal()
130: && this .getInternal().equals(
131: otherX.getInternal());
132: }
133: // External or unparsed: either public id are both null, or public id
134: // equals in both (and same for system id).
135: return (this .getPublicId() == null
136: && otherX.getPublicId() == null || this
137: .getPublicId() != null
138: && this .getPublicId().equals(otherX.getPublicId()))
139: && (this .getSystemId() == null
140: && otherX.getSystemId() == null || this
141: .getSystemId() != null
142: && this .getSystemId().equals(
143: otherX.getSystemId()));
144: }
145: return false;
146: }
147:
148: public final Object clone() {
149: ParamEntityProxy clone = null;
150: try {
151: clone = (ParamEntityProxy) database().createObject(
152: ParamEntity.class.getName());
153: ((NodeProxy) clone).init(_ownerDocument, getNodeName(),
154: null, true);
155: cloneInto((NodeProxy) clone, true);
156: } catch (Exception except) {
157: }
158: return clone;
159: }
160:
161: public final Node cloneNode(boolean deep) {
162: ParamEntityProxy clone = null;
163: try {
164: clone = (ParamEntityProxy) database().createObject(
165: ParamEntity.class.getName());
166: ((NodeProxy) clone).init(_ownerDocument, getNodeName(),
167: null, true);
168: cloneInto((NodeProxy) clone, deep);
169: } catch (Exception except) {
170: }
171: return clone;
172: }
173:
174: public String toString() {
175: String name;
176: String value;
177:
178: name = getNodeName();
179: if (name.length() > 32) {
180: name = name.substring(0, 32) + "..";
181: }
182: if (isInternal()) {
183: value = getInternal();
184: if (value.length() > 64) {
185: value = value.substring(0, 64) + "..";
186: }
187: name = name + "] [" + value;
188: } else {
189: if (getSystemId() != null) {
190: name = name + "] SYSTEM [" + getSystemId();
191: }
192: if (getPublicId() != null) {
193: name = name + "] PUBLIC [" + getPublicId();
194: }
195: }
196: return "PEntity decl: [" + name + "]";
197: }
198:
199: public synchronized void cloneInto(NodeProxy into, boolean deep) {
200: super .cloneInto(into, deep);
201: ((ParamEntityProxy) into).setSystemId(this .getSystemId());
202: ((ParamEntityProxy) into).setPublicId(this .getPublicId());
203: ((ParamEntityProxy) into).setInternal(this .getInternal());
204: ((ParamEntityProxy) into).setState(this .getState());
205: }
206:
207: /**
208: * Constructs an external parameter entity. Entity system identifier must be
209: * provided, public identifier is optional.
210: *
211: * @param owner The owner document
212: * @param name The entity name
213: * @param systemId The system identifier
214: * @param publicId The public identifier, if specified
215: */
216: public ParamEntity(DocumentImpl owner, String name,
217: String systemId, String publicId) {
218: super (owner, name, null, true);
219: if (systemId == null) {
220: throw new NullPointerException(
221: "Argument 'systemId' cannot be null.");
222: }
223: _systemId = systemId;
224: _publicId = publicId;
225: _state = STATE_DECLARED;
226: }
227:
228: /**
229: * Constructs an internal parameter entity. Entity value must be provided.
230: *
231: * @param owner The owner document
232: * @param name The entity name
233: * @param internalValue The unparsed entity value
234: */
235: public ParamEntity(DocumentImpl owner, String name,
236: String internalValue) {
237: super (owner, name, null, true);
238: init(owner, name, internalValue);
239: }
240:
241: private ParamEntity(DocumentImpl owner, String name) {
242: super (owner, name, null, true);
243: }
244:
245: public ParamEntity() {
246: super ();
247: }
248:
249: public void init(DocumentProxy owner, String name) {
250: super .init(owner, name, null, true);
251: }
252:
253: public void init(DocumentProxy owner, String name, String value) {
254: super .init(owner, name, null, true);
255: if (value == null) {
256: throw new NullPointerException(
257: "Argument 'internalValue' cannot be null.");
258: }
259: _systemId = null;
260: _publicId = null;
261: _state = STATE_DECLARED;
262: _internalValue = value;
263: }
264:
265: public void init(DocumentProxy owner, String name, String systemId,
266: String publicId) {
267: }
268:
269: /**
270: * The system identifier of this entity, if specified.
271: */
272: private String _systemId;
273:
274: /**
275: * The public identifier of this entity, if specified.
276: */
277: private String _publicId;
278:
279: /**
280: * Identifies the state of this entity as yet to be parsed, being parsed,
281: * has been parsed, or cannot be found.
282: */
283: private short _state;
284:
285: /**
286: * Holds the internal value of the entity.
287: */
288: private String _internalValue;
289:
290: /**
291: * Entity has been declared but not parsed. This is the initial state for
292: * an entity after it has been declared in the DTD but before it is used
293: * in the document contents.
294: */
295: public final static short STATE_DECLARED = 0;
296:
297: /**
298: * Entity is being parsed. This state designates that the entity is being
299: * parsed right now. State is used to identify circular references.
300: */
301: public final static short STATE_PARSING = 1;
302:
303: /**
304: * Entity has been parsed. This state indicates that entity has been parsed
305: * and it's parsed contents is contained in its child nodes.
306: */
307: public final static short STATE_PARSED = 2;
308:
309: /**
310: * Entity not found. The entity could not be parsed before.
311: */
312: public final static short STATE_NOT_FOUND = 3;
313:
314: }
|