1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id: XalanTransformState.java 468645 2006-10-28 06:57:24Z minchau $
20  */
21 
22 package org.apache.xalan.transformer;
23 
24 import javax.xml.transform.Transformer;
25 
26 import org.apache.xalan.templates.ElemTemplate;
27 import org.apache.xalan.templates.ElemTemplateElement;
28 import org.apache.xml.dtm.DTM;
29 import org.apache.xml.dtm.DTMIterator;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.traversal.NodeIterator;
32 
33 /**
34  * Before the serializer merge, the TransformState interface was
35  * implemented by ResultTreeHandler.
36  */
37 public class XalanTransformState
38     implements TransformState {
39 
40     Node m_node = null;
41     ElemTemplateElement m_currentElement = null;
42     ElemTemplate m_currentTemplate = null;
43     ElemTemplate m_matchedTemplate = null;
44     int m_currentNodeHandle = DTM.NULL;
45     Node m_currentNode = null;
46     int m_matchedNode = DTM.NULL;
47     DTMIterator m_contextNodeList = null;
48     boolean m_elemPending = false;
49     TransformerImpl m_transformer = null;
50 
51     /**
52      * @see org.apache.xml.serializer.TransformStateSetter#setCurrentNode(Node)
53      */
setCurrentNode(Node n)54     public void setCurrentNode(Node n) {
55         m_node = n;
56     }
57 
58     /**
59      * @see org.apache.xml.serializer.TransformStateSetter#resetState(Transformer)
60      */
resetState(Transformer transformer)61     public void resetState(Transformer transformer) {
62         if ((transformer != null) && (transformer instanceof TransformerImpl)) {
63            m_transformer = (TransformerImpl)transformer;
64            m_currentElement = m_transformer.getCurrentElement();
65            m_currentTemplate = m_transformer.getCurrentTemplate();
66            m_matchedTemplate = m_transformer.getMatchedTemplate();
67            int currentNodeHandle = m_transformer.getCurrentNode();
68            DTM dtm = m_transformer.getXPathContext().getDTM(currentNodeHandle);
69            m_currentNode = dtm.getNode(currentNodeHandle);
70            m_matchedNode = m_transformer.getMatchedNode();
71            m_contextNodeList = m_transformer.getContextNodeList();
72         }
73     }
74 
75     /**
76      * @see org.apache.xalan.transformer.TransformState#getCurrentElement()
77      */
getCurrentElement()78     public ElemTemplateElement getCurrentElement() {
79       if (m_elemPending)
80          return m_currentElement;
81       else
82          return m_transformer.getCurrentElement();
83     }
84 
85     /**
86      * @see org.apache.xalan.transformer.TransformState#getCurrentNode()
87      */
getCurrentNode()88     public Node getCurrentNode() {
89       if (m_currentNode != null) {
90          return m_currentNode;
91       } else {
92          DTM dtm = m_transformer.getXPathContext().getDTM(m_transformer.getCurrentNode());
93          return dtm.getNode(m_transformer.getCurrentNode());
94       }
95     }
96 
97     /**
98      * @see org.apache.xalan.transformer.TransformState#getCurrentTemplate()
99      */
getCurrentTemplate()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      */
getMatchedTemplate()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      */
getMatchedNode()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      */
getContextNodeList()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      */
getTransformer()144     public Transformer getTransformer() {
145         return m_transformer;
146     }
147 
148 }
149