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: XalanTransformState.java 468645 2006-10-28 06:57:24Z minchau $
020     */
021    
022    package org.apache.xalan.transformer;
023    
024    import javax.xml.transform.Transformer;
025    
026    import org.apache.xalan.templates.ElemTemplate;
027    import org.apache.xalan.templates.ElemTemplateElement;
028    import org.apache.xml.dtm.DTM;
029    import org.apache.xml.dtm.DTMIterator;
030    import org.w3c.dom.Node;
031    import org.w3c.dom.traversal.NodeIterator;
032    
033    /**
034     * Before the serializer merge, the TransformState interface was
035     * implemented by ResultTreeHandler.
036     */
037    public class XalanTransformState
038        implements TransformState {
039            
040        Node m_node = null;
041        ElemTemplateElement m_currentElement = null;
042        ElemTemplate m_currentTemplate = null;
043        ElemTemplate m_matchedTemplate = null;
044        int m_currentNodeHandle = DTM.NULL;
045        Node m_currentNode = null;
046        int m_matchedNode = DTM.NULL;
047        DTMIterator m_contextNodeList = null;
048        boolean m_elemPending = false;    
049        TransformerImpl m_transformer = null;
050    
051        /**
052         * @see org.apache.xml.serializer.TransformStateSetter#setCurrentNode(Node)
053         */
054        public void setCurrentNode(Node n) {
055            m_node = n;
056        }
057    
058        /**
059         * @see org.apache.xml.serializer.TransformStateSetter#resetState(Transformer)
060         */
061        public void resetState(Transformer transformer) {
062            if ((transformer != null) && (transformer instanceof TransformerImpl)) {
063               m_transformer = (TransformerImpl)transformer;
064               m_currentElement = m_transformer.getCurrentElement();
065               m_currentTemplate = m_transformer.getCurrentTemplate();
066               m_matchedTemplate = m_transformer.getMatchedTemplate();
067               int currentNodeHandle = m_transformer.getCurrentNode();
068               DTM dtm = m_transformer.getXPathContext().getDTM(currentNodeHandle);
069               m_currentNode = dtm.getNode(currentNodeHandle);
070               m_matchedNode = m_transformer.getMatchedNode();
071               m_contextNodeList = m_transformer.getContextNodeList();    
072            }       
073        }
074    
075        /**
076         * @see org.apache.xalan.transformer.TransformState#getCurrentElement()
077         */
078        public ElemTemplateElement getCurrentElement() {
079          if (m_elemPending)
080             return m_currentElement;
081          else
082             return m_transformer.getCurrentElement();
083        }
084    
085        /**
086         * @see org.apache.xalan.transformer.TransformState#getCurrentNode()
087         */
088        public Node getCurrentNode() {
089          if (m_currentNode != null) {
090             return m_currentNode;
091          } else {
092             DTM dtm = m_transformer.getXPathContext().getDTM(m_transformer.getCurrentNode());
093             return dtm.getNode(m_transformer.getCurrentNode());
094          }
095        }
096        
097        /**
098         * @see org.apache.xalan.transformer.TransformState#getCurrentTemplate()
099         */
100        public ElemTemplate getCurrentTemplate() {
101           if (m_elemPending)
102             return m_currentTemplate;
103           else
104             return m_transformer.getCurrentTemplate();
105        }
106    
107        /**
108         * @see org.apache.xalan.transformer.TransformState#getMatchedTemplate()
109         */
110        public ElemTemplate getMatchedTemplate() {
111          if (m_elemPending)
112             return m_matchedTemplate;
113          else
114             return m_transformer.getMatchedTemplate();
115        }
116    
117        /**
118         * @see org.apache.xalan.transformer.TransformState#getMatchedNode()
119         */
120        public Node getMatchedNode() {
121     
122           if (m_elemPending) {
123             DTM dtm = m_transformer.getXPathContext().getDTM(m_matchedNode);
124             return dtm.getNode(m_matchedNode);
125           } else {
126             DTM dtm = m_transformer.getXPathContext().getDTM(m_transformer.getMatchedNode());
127             return dtm.getNode(m_transformer.getMatchedNode());
128           }
129        }
130    
131        /**
132         * @see org.apache.xalan.transformer.TransformState#getContextNodeList()
133         */
134        public NodeIterator getContextNodeList() {
135          if (m_elemPending) {
136              return new org.apache.xml.dtm.ref.DTMNodeIterator(m_contextNodeList);
137          } else {
138              return new org.apache.xml.dtm.ref.DTMNodeIterator(m_transformer.getContextNodeList());
139          }
140        }
141        /**
142         * @see org.apache.xalan.transformer.TransformState#getTransformer()
143         */
144        public Transformer getTransformer() {
145            return m_transformer;
146        }
147    
148    }