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