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