1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: ElemContext.java,v 1.2 2004/02/17 04:18:19 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.serializer;
  20. /**
  21. * This class is a stack frame that consists of
  22. * information about the element currently being processed
  23. * by a serializer. Consider this example:
  24. * <pre>
  25. * <A>
  26. * <B1>
  27. * </B1>
  28. * <B2>
  29. * </B2>
  30. * <A>
  31. * </pre>
  32. *
  33. * A stack frame will be pushed for "A" at depth 1,
  34. * then another one for "B1" at depth 2.
  35. * Then "B1" stackframe is popped. When the stack frame for "B2" is
  36. * pushed, this implementation re-uses the old stack fram object used
  37. * by "B1" to be efficient at not creating too many of these object.
  38. *
  39. * This is by no means a public class, and neither are its fields or methods,
  40. * they are all helper fields for a serializer.
  41. *
  42. * The purpose of this class is to be more consistent with pushing information
  43. * when a new element is being serialized and more quickly restoring the old
  44. * information about the parent element with a simple pop() when the
  45. * child element is done. Previously there was some redundant and error-prone
  46. * calculations going on to retore information.
  47. *
  48. */
  49. class ElemContext
  50. {
  51. // Fields that form the context of the element
  52. /**
  53. * The nesting depth of the element inside other elements.
  54. */
  55. final int m_currentElemDepth;
  56. /** HTML field, the element description of the HTML element */
  57. ElemDesc m_elementDesc = null;
  58. /**
  59. * The local name of the element.
  60. */
  61. String m_elementLocalName = null;
  62. /**
  63. * The fully qualified name of the element (with prefix, if any).
  64. */
  65. String m_elementName = null;
  66. /**
  67. * The URI of the element.
  68. */
  69. String m_elementURI = null;
  70. /** If the element is in the cdata-section-names list
  71. * then the value is true. If it is true the text children of the element
  72. * should be output in CDATA section blocks.
  73. */
  74. boolean m_isCdataSection;
  75. /** True if the current element has output escaping disabled.
  76. * This is true for SCRIPT and STYLE elements.
  77. */
  78. boolean m_isRaw = false;
  79. /** The next element "stack frame". This value will only be
  80. * set once as deeper stack frames are not deleted when popped off,
  81. * but are rather re-used when a push is required.
  82. *
  83. * This makes for very fast pushing and popping of stack frames
  84. * because very few stack frame objects are ever created, they are
  85. * mostly re-used. This re-use saves object creation but it also means
  86. * that connections between the frames via m_next and m_prev
  87. * never changes either. Just the contents of the frames change
  88. * as they are re-used. Only the reference to the current stack frame, which
  89. * is held by the serializer is changed via a quick pop() or push().
  90. */
  91. private ElemContext m_next;
  92. /** The previous element "stack frame". */
  93. final ElemContext m_prev;
  94. /**
  95. * Set to true when a start tag is started, or open, but not all the
  96. * attributes or namespace information is yet collected.
  97. */
  98. boolean m_startTagOpen = false;
  99. /**
  100. * Constructor to create the root of the element contexts.
  101. *
  102. */
  103. ElemContext()
  104. {
  105. // this assignment means can never pop this context off
  106. m_prev = this;
  107. // depth 0 because it doesn't correspond to any element
  108. m_currentElemDepth = 0;
  109. }
  110. /**
  111. * Constructor to create the "stack frame" for a given element depth.
  112. *
  113. * This implementation will re-use the context at each depth. If
  114. * a documents deepest element depth is N then there will be (N+1)
  115. * such objects created, no more than that.
  116. *
  117. * @param previous The "stack frame" corresponding to the new
  118. * elements parent element.
  119. */
  120. private ElemContext(final ElemContext previous)
  121. {
  122. m_prev = previous;
  123. m_currentElemDepth = previous.m_currentElemDepth + 1;
  124. }
  125. /**
  126. * Pop the current "stack frame".
  127. * @return Returns the parent "stack frame" of the one popped.
  128. */
  129. final ElemContext pop()
  130. {
  131. /* a very simple pop. No clean up is done of the deeper
  132. * stack frame. All deeper stack frames are still attached
  133. * but dormant, just waiting to be re-used.
  134. */
  135. return this.m_prev;
  136. }
  137. /**
  138. * This method pushes an element "stack frame"
  139. * but with no initialization of values in that frame.
  140. * This method is used for optimization purposes, like when pushing
  141. * a stack frame for an HTML "IMG" tag which has no children and
  142. * the stack frame will almost immediately be popped.
  143. */
  144. final ElemContext push()
  145. {
  146. ElemContext frame = this.m_next;
  147. if (frame == null)
  148. {
  149. /* We have never been at this depth yet, and there is no
  150. * stack frame to re-use, so we now make a new one.
  151. */
  152. frame = new ElemContext(this);
  153. this.m_next = frame;
  154. }
  155. /*
  156. * We shouldn't need to set this true because we should just
  157. * be pushing a dummy stack frame that will be instantly popped.
  158. * Yet we need to be ready in case this element does have
  159. * unexpected children.
  160. */
  161. frame.m_startTagOpen = true;
  162. return frame;
  163. }
  164. /**
  165. * Push an element context on the stack. This context keeps track of
  166. * information gathered about the element.
  167. * @param uri The URI for the namespace for the element name,
  168. * can be null if it is not yet known.
  169. * @param localName The local name of the element (no prefix),
  170. * can be null.
  171. * @param qName The qualified name (with prefix, if any)
  172. * of the element, this parameter is required.
  173. */
  174. final ElemContext push(
  175. final String uri,
  176. final String localName,
  177. final String qName)
  178. {
  179. ElemContext frame = this.m_next;
  180. if (frame == null)
  181. {
  182. /* We have never been at this depth yet, and there is no
  183. * stack frame to re-use, so we now make a new one.
  184. */
  185. frame = new ElemContext(this);
  186. this.m_next = frame;
  187. }
  188. // Initialize, or reset values in the new or re-used stack frame.
  189. frame.m_elementName = qName;
  190. frame.m_elementLocalName = localName;
  191. frame.m_elementURI = uri;
  192. frame.m_isCdataSection = false;
  193. frame.m_startTagOpen = true;
  194. // is_Raw is already set in the HTML startElement() method
  195. // frame.m_isRaw = false;
  196. return frame;
  197. }
  198. }