001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.dom;
059:
060: import org.w3c.dom.DocumentType;
061: import org.w3c.dom.Entity;
062: import org.w3c.dom.NamedNodeMap;
063: import org.w3c.dom.Node;
064:
065: /**
066: * EntityReference models the XML &entityname; syntax, when used for
067: * entities defined by the DOM. Entities hardcoded into XML, such as
068: * character entities, should instead have been translated into text
069: * by the code which generated the DOM tree.
070: * <P>
071: * An XML processor has the alternative of fully expanding Entities
072: * into the normal document tree. If it does so, no EntityReference nodes
073: * will appear.
074: * <P>
075: * Similarly, non-validating XML processors are not required to read
076: * or process entity declarations made in the external subset or
077: * declared in external parameter entities. Hence, some applications
078: * may not make the replacement value available for Parsed Entities
079: * of these types.
080: * <P>
081: * EntityReference behaves as a read-only node, and the children of
082: * the EntityReference (which reflect those of the Entity, and should
083: * also be read-only) give its replacement value, if any. They are
084: * supposed to automagically stay in synch if the DocumentType is
085: * updated with new values for the Entity.
086: * <P>
087: * The defined behavior makes efficient storage difficult for the DOM
088: * implementor. We can't just look aside to the Entity's definition
089: * in the DocumentType since those nodes have the wrong parent (unless
090: * we can come up with a clever "imaginary parent" mechanism). We
091: * must at least appear to clone those children... which raises the
092: * issue of keeping the reference synchronized with its parent.
093: * This leads me back to the "cached image of centrally defined data"
094: * solution, much as I dislike it.
095: * <P>
096: * For now I have decided, since REC-DOM-Level-1-19980818 doesn't
097: * cover this in much detail, that synchronization doesn't have to be
098: * considered while the user is deep in the tree. That is, if you're
099: * looking within one of the EntityReferennce's children and the Entity
100: * changes, you won't be informed; instead, you will continue to access
101: * the same object -- which may or may not still be part of the tree.
102: * This is the same behavior that obtains elsewhere in the DOM if the
103: * subtree you're looking at is deleted from its parent, so it's
104: * acceptable here. (If it really bothers folks, we could set things
105: * up so deleted subtrees are walked and marked invalid, but that's
106: * not part of the DOM's defined behavior.)
107: * <P>
108: * As a result, only the EntityReference itself has to be aware of
109: * changes in the Entity. And it can take advantage of the same
110: * structure-change-monitoring code I implemented to support
111: * DeepNodeList.
112: *
113: * @version
114: * @since PR-DOM-Level-1-19980818.
115: */
116: public class DeferredEntityReferenceImpl extends EntityReferenceImpl
117: implements DeferredNode {
118:
119: //
120: // Constants
121: //
122:
123: /** Serialization version. */
124: static final long serialVersionUID = 390319091370032223L;
125:
126: //
127: // Data
128: //
129:
130: /** Node index. */
131: protected transient int fNodeIndex;
132:
133: //
134: // Constructors
135: //
136:
137: /**
138: * This is the deferred constructor. Only the fNodeIndex is given here.
139: * All other data, can be requested from the ownerDocument via the index.
140: */
141: DeferredEntityReferenceImpl(DeferredDocumentImpl ownerDocument,
142: int nodeIndex) {
143: super (ownerDocument, null);
144:
145: fNodeIndex = nodeIndex;
146: needsSyncData(true);
147: needsSyncChildren(true);
148:
149: } // <init>(DeferredDocumentImpl,int)
150:
151: //
152: // DeferredNode methods
153: //
154:
155: /** Returns the node index. */
156: public int getNodeIndex() {
157: return fNodeIndex;
158: }
159:
160: //
161: // Protected methods
162: //
163:
164: /**
165: * Synchronize the entity data. This is special because of the way
166: * that the "fast" version stores the information.
167: */
168: protected void synchronizeData() {
169:
170: // no need to sychronize again
171: needsSyncData(false);
172:
173: // get the node data
174: DeferredDocumentImpl ownerDocument = (DeferredDocumentImpl) this .ownerDocument;
175: name = ownerDocument.getNodeNameString(fNodeIndex);
176:
177: } // synchronizeData()
178:
179: /** Synchronize the children. */
180: protected void synchronizeChildren() {
181:
182: // no need to synchronize again
183: needsSyncChildren(false);
184:
185: // get children
186: DocumentType doctype = ownerDocument.getDoctype();
187: boolean found = false;
188: if (doctype != null) {
189:
190: // we don't want to generate any event for this so turn them off
191: boolean orig = ownerDocument.getMutationEvents();
192: ownerDocument.setMutationEvents(false);
193:
194: NamedNodeMap entities = doctype.getEntities();
195: if (entities != null) {
196: Entity entity = (Entity) entities
197: .getNamedItem(getNodeName());
198: if (entity != null) {
199:
200: // we found the entity
201: found = true;
202:
203: // clone entity at this reference
204: boolean ro = isReadOnly();
205: isReadOnly(false);
206: Node child = entity.getFirstChild();
207: while (child != null) {
208: appendChild(child.cloneNode(true));
209: child = child.getNextSibling();
210: }
211: // set it back to readonly if what it was
212: if (ro) {
213: setReadOnly(true, true);
214: }
215: }
216: }
217: // set mutation events flag back to its original value
218: ownerDocument.setMutationEvents(orig);
219: }
220:
221: // if not found, create entity at this reference
222: if (!found) {
223: isReadOnly(false);
224: DeferredDocumentImpl ownerDocument = (DeferredDocumentImpl) ownerDocument();
225: ownerDocument.synchronizeChildren(this , fNodeIndex);
226: setReadOnly(true, true);
227: }
228:
229: } // synchronizeChildren()
230:
231: // inhibit the synchronize inherited from EntityReferenceImpl
232: protected void synchronize() {
233: }
234:
235: } // class DeferredEntityReferenceImpl
|