001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: ElemContext.java,v 1.3 2004/10/14 21:45:06 minchau Exp $
018: */
019: package org.apache.xml.serializer;
020:
021: /**
022: * This class is a stack frame that consists of
023: * information about the element currently being processed
024: * by a serializer. Consider this example:
025: * <pre>
026: * <A>
027: * <B1>
028: * </B1>
029: * <B2>
030: * </B2>
031: * <A>
032: * </pre>
033: *
034: * A stack frame will be pushed for "A" at depth 1,
035: * then another one for "B1" at depth 2.
036: * Then "B1" stackframe is popped. When the stack frame for "B2" is
037: * pushed, this implementation re-uses the old stack fram object used
038: * by "B1" to be efficient at not creating too many of these object.
039: *
040: * This is by no means a public class, and neither are its fields or methods,
041: * they are all helper fields for a serializer.
042: *
043: * The purpose of this class is to be more consistent with pushing information
044: * when a new element is being serialized and more quickly restoring the old
045: * information about the parent element with a simple pop() when the
046: * child element is done. Previously there was some redundant and error-prone
047: * calculations going on to retore information.
048: *
049: * @xsl.usage internal
050: */
051: final class ElemContext {
052: // Fields that form the context of the element
053:
054: /**
055: * The nesting depth of the element inside other elements.
056: */
057: final int m_currentElemDepth;
058:
059: /** HTML field, the element description of the HTML element */
060: ElemDesc m_elementDesc = null;
061:
062: /**
063: * The local name of the element.
064: */
065: String m_elementLocalName = null;
066:
067: /**
068: * The fully qualified name of the element (with prefix, if any).
069: */
070: String m_elementName = null;
071:
072: /**
073: * The URI of the element.
074: */
075: String m_elementURI = null;
076:
077: /** If the element is in the cdata-section-names list
078: * then the value is true. If it is true the text children of the element
079: * should be output in CDATA section blocks.
080: */
081: boolean m_isCdataSection;
082:
083: /** True if the current element has output escaping disabled.
084: * This is true for SCRIPT and STYLE elements.
085: */
086: boolean m_isRaw = false;
087:
088: /** The next element "stack frame". This value will only be
089: * set once as deeper stack frames are not deleted when popped off,
090: * but are rather re-used when a push is required.
091: *
092: * This makes for very fast pushing and popping of stack frames
093: * because very few stack frame objects are ever created, they are
094: * mostly re-used. This re-use saves object creation but it also means
095: * that connections between the frames via m_next and m_prev
096: * never changes either. Just the contents of the frames change
097: * as they are re-used. Only the reference to the current stack frame, which
098: * is held by the serializer is changed via a quick pop() or push().
099: */
100: private ElemContext m_next;
101:
102: /** The previous element "stack frame". */
103: final ElemContext m_prev;
104:
105: /**
106: * Set to true when a start tag is started, or open, but not all the
107: * attributes or namespace information is yet collected.
108: */
109: boolean m_startTagOpen = false;
110:
111: /**
112: * Constructor to create the root of the element contexts.
113: *
114: */
115: ElemContext() {
116: // this assignment means can never pop this context off
117: m_prev = this ;
118: // depth 0 because it doesn't correspond to any element
119: m_currentElemDepth = 0;
120: }
121:
122: /**
123: * Constructor to create the "stack frame" for a given element depth.
124: *
125: * This implementation will re-use the context at each depth. If
126: * a documents deepest element depth is N then there will be (N+1)
127: * such objects created, no more than that.
128: *
129: * @param previous The "stack frame" corresponding to the new
130: * elements parent element.
131: */
132: private ElemContext(final ElemContext previous) {
133: m_prev = previous;
134: m_currentElemDepth = previous.m_currentElemDepth + 1;
135: }
136:
137: /**
138: * Pop the current "stack frame".
139: * @return Returns the parent "stack frame" of the one popped.
140: */
141: final ElemContext pop() {
142: /* a very simple pop. No clean up is done of the deeper
143: * stack frame. All deeper stack frames are still attached
144: * but dormant, just waiting to be re-used.
145: */
146: return this .m_prev;
147: }
148:
149: /**
150: * This method pushes an element "stack frame"
151: * but with no initialization of values in that frame.
152: * This method is used for optimization purposes, like when pushing
153: * a stack frame for an HTML "IMG" tag which has no children and
154: * the stack frame will almost immediately be popped.
155: */
156: final ElemContext push() {
157: ElemContext frame = this .m_next;
158: if (frame == null) {
159: /* We have never been at this depth yet, and there is no
160: * stack frame to re-use, so we now make a new one.
161: */
162: frame = new ElemContext(this );
163: this .m_next = frame;
164: }
165: /*
166: * We shouldn't need to set this true because we should just
167: * be pushing a dummy stack frame that will be instantly popped.
168: * Yet we need to be ready in case this element does have
169: * unexpected children.
170: */
171: frame.m_startTagOpen = true;
172: return frame;
173: }
174:
175: /**
176: * Push an element context on the stack. This context keeps track of
177: * information gathered about the element.
178: * @param uri The URI for the namespace for the element name,
179: * can be null if it is not yet known.
180: * @param localName The local name of the element (no prefix),
181: * can be null.
182: * @param qName The qualified name (with prefix, if any)
183: * of the element, this parameter is required.
184: */
185: final ElemContext push(final String uri, final String localName,
186: final String qName) {
187: ElemContext frame = this .m_next;
188: if (frame == null) {
189: /* We have never been at this depth yet, and there is no
190: * stack frame to re-use, so we now make a new one.
191: */
192: frame = new ElemContext(this );
193: this .m_next = frame;
194: }
195:
196: // Initialize, or reset values in the new or re-used stack frame.
197: frame.m_elementName = qName;
198: frame.m_elementLocalName = localName;
199: frame.m_elementURI = uri;
200: frame.m_isCdataSection = false;
201: frame.m_startTagOpen = true;
202:
203: // is_Raw is already set in the HTML startElement() method
204: // frame.m_isRaw = false;
205: return frame;
206: }
207: }
|