1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.dom;
  58. /**
  59. * EntityReference models the XML &entityname; syntax, when used for
  60. * entities defined by the DOM. Entities hardcoded into XML, such as
  61. * character entities, should instead have been translated into text
  62. * by the code which generated the DOM tree.
  63. * <P>
  64. * An XML processor has the alternative of fully expanding Entities
  65. * into the normal document tree. If it does so, no EntityReference nodes
  66. * will appear.
  67. * <P>
  68. * Similarly, non-validating XML processors are not required to read
  69. * or process entity declarations made in the external subset or
  70. * declared in external parameter entities. Hence, some applications
  71. * may not make the replacement value available for Parsed Entities
  72. * of these types.
  73. * <P>
  74. * EntityReference behaves as a read-only node, and the children of
  75. * the EntityReference (which reflect those of the Entity, and should
  76. * also be read-only) give its replacement value, if any. They are
  77. * supposed to automagically stay in synch if the DocumentType is
  78. * updated with new values for the Entity.
  79. * <P>
  80. * The defined behavior makes efficient storage difficult for the DOM
  81. * implementor. We can't just look aside to the Entity's definition
  82. * in the DocumentType since those nodes have the wrong parent (unless
  83. * we can come up with a clever "imaginary parent" mechanism). We
  84. * must at least appear to clone those children... which raises the
  85. * issue of keeping the reference synchronized with its parent.
  86. * This leads me back to the "cached image of centrally defined data"
  87. * solution, much as I dislike it.
  88. * <P>
  89. * For now I have decided, since REC-DOM-Level-1-19980818 doesn't
  90. * cover this in much detail, that synchronization doesn't have to be
  91. * considered while the user is deep in the tree. That is, if you're
  92. * looking within one of the EntityReferennce's children and the Entity
  93. * changes, you won't be informed; instead, you will continue to access
  94. * the same object -- which may or may not still be part of the tree.
  95. * This is the same behavior that obtains elsewhere in the DOM if the
  96. * subtree you're looking at is deleted from its parent, so it's
  97. * acceptable here. (If it really bothers folks, we could set things
  98. * up so deleted subtrees are walked and marked invalid, but that's
  99. * not part of the DOM's defined behavior.)
  100. * <P>
  101. * As a result, only the EntityReference itself has to be aware of
  102. * changes in the Entity. And it can take advantage of the same
  103. * structure-change-monitoring code I implemented to support
  104. * DeepNodeList.
  105. *
  106. * @version $Id: DeferredEntityReferenceImpl.java,v 1.19 2003/05/08 19:52:40 elena Exp $
  107. * @since PR-DOM-Level-1-19980818.
  108. */
  109. public class DeferredEntityReferenceImpl
  110. extends EntityReferenceImpl
  111. implements DeferredNode {
  112. //
  113. // Constants
  114. //
  115. /** Serialization version. */
  116. static final long serialVersionUID = 390319091370032223L;
  117. //
  118. // Data
  119. //
  120. /** Node index. */
  121. protected transient int fNodeIndex;
  122. //
  123. // Constructors
  124. //
  125. /**
  126. * This is the deferred constructor. Only the fNodeIndex is given here.
  127. * All other data, can be requested from the ownerDocument via the index.
  128. */
  129. DeferredEntityReferenceImpl(DeferredDocumentImpl ownerDocument,
  130. int nodeIndex) {
  131. super(ownerDocument, null);
  132. fNodeIndex = nodeIndex;
  133. needsSyncData(true);
  134. } // <init>(DeferredDocumentImpl,int)
  135. //
  136. // DeferredNode methods
  137. //
  138. /** Returns the node index. */
  139. public int getNodeIndex() {
  140. return fNodeIndex;
  141. }
  142. //
  143. // Protected methods
  144. //
  145. /**
  146. * Synchronize the entity data. This is special because of the way
  147. * that the "fast" version stores the information.
  148. */
  149. protected void synchronizeData() {
  150. // no need to sychronize again
  151. needsSyncData(false);
  152. // get the node data
  153. DeferredDocumentImpl ownerDocument =
  154. (DeferredDocumentImpl)this.ownerDocument;
  155. name = ownerDocument.getNodeName(fNodeIndex);
  156. baseURI = ownerDocument.getNodeValue(fNodeIndex);
  157. } // synchronizeData()
  158. /** Synchronize the children. */
  159. protected void synchronizeChildren() {
  160. // no need to synchronize again
  161. needsSyncChildren(false);
  162. // get children
  163. isReadOnly(false);
  164. DeferredDocumentImpl ownerDocument =
  165. (DeferredDocumentImpl) ownerDocument();
  166. ownerDocument.synchronizeChildren(this, fNodeIndex);
  167. setReadOnly(true, true);
  168. } // synchronizeChildren()
  169. } // class DeferredEntityReferenceImpl