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 // Class header file.
19 #include "FormatterTreeWalker.hpp"
20 
21 
22 
23 // Xerces header files...
24 #include <xalanc/XalanDOM/XalanElement.hpp>
25 #include <xalanc/XalanDOM/XalanNamedNodeMap.hpp>
26 
27 
28 
29 #include <xalanc/PlatformSupport/AttributeListImpl.hpp>
30 #include <xalanc/PlatformSupport/DOMStringHelper.hpp>
31 #include <xalanc/PlatformSupport/FormatterListener.hpp>
32 #include <xalanc/PlatformSupport/NamedNodeMapAttributeList.hpp>
33 
34 
35 
36 namespace XALAN_CPP_NAMESPACE {
37 
38 
39 
FormatterTreeWalker(FormatterListener & formatterListener,MemoryManager & theManager)40 FormatterTreeWalker::FormatterTreeWalker(
41             FormatterListener&  formatterListener,
42             MemoryManager&      theManager) :
43     TreeWalker(),
44     m_formatterListener(formatterListener),
45     m_memoryManager(theManager)
46 {
47 }
48 
49 
50 
~FormatterTreeWalker()51 FormatterTreeWalker::~FormatterTreeWalker()
52 {
53 }
54 
55 
56 
57 bool
startNode(const XalanNode * node)58 FormatterTreeWalker::startNode(const XalanNode*     node)
59 {
60     assert(node != 0);
61 
62     switch(node->getNodeType())
63     {
64     case XalanNode::COMMENT_NODE:
65         {
66             m_formatterListener.comment(node->getNodeValue().c_str());
67         }
68         break;
69 
70     case XalanNode::DOCUMENT_FRAGMENT_NODE:
71         // ??
72         break;
73 
74     case XalanNode::DOCUMENT_NODE:
75         m_formatterListener.startDocument();
76         break;
77 
78     case XalanNode::ELEMENT_NODE:
79         {
80             const XalanElement* theElementNode =
81                 static_cast<const XalanElement*>(node);
82 
83             const XalanNamedNodeMap*    atts = theElementNode->getAttributes();
84             assert(atts != 0);
85 
86             NamedNodeMapAttributeList   theAttributeList(*atts, m_memoryManager);
87 
88             m_formatterListener.startElement(
89                 theElementNode->getNodeName().c_str(),
90                 theAttributeList);
91         }
92         break;
93 
94     case XalanNode::PROCESSING_INSTRUCTION_NODE:
95         {
96             m_formatterListener.processingInstruction(
97                 node->getNodeName().c_str(),
98                 node->getNodeValue().c_str());
99         }
100         break;
101 
102     case XalanNode::CDATA_SECTION_NODE:
103         {
104             const XalanDOMString&   data = node->getNodeValue();
105 
106             assert(data.length() == static_cast<FormatterListener::size_type>(data.length()));
107 
108             m_formatterListener.cdata(
109                 data.c_str(),
110                 static_cast<FormatterListener::size_type>(data.length()));
111         }
112         break;
113 
114     case XalanNode::TEXT_NODE:
115         {
116             const XalanDOMString&   data = node->getNodeValue();
117 
118             assert(data.length() == static_cast<FormatterListener::size_type>(data.length()));
119 
120             m_formatterListener.characters(
121                 data.c_str(),
122                 static_cast<FormatterListener::size_type>(data.length()));
123         }
124         break;
125 
126     case XalanNode::ENTITY_REFERENCE_NODE:
127         m_formatterListener.entityReference(node->getNodeName().c_str());
128         break;
129 
130     default:
131         // Do nothing...
132         break;
133     }
134 
135     return false;
136 }
137 
138 
139 
140 bool
startNode(XalanNode * node)141 FormatterTreeWalker::startNode(XalanNode*   node)
142 {
143     assert(node != 0);
144 
145     return startNode(const_cast<const XalanNode*>(node));
146 }
147 
148 
149 
150 bool
endNode(const XalanNode * node)151 FormatterTreeWalker::endNode(const XalanNode*   node)
152 {
153     assert(node != 0);
154 
155     switch(node->getNodeType())
156     {
157     case XalanNode::DOCUMENT_NODE:
158         m_formatterListener.endDocument();
159         break;
160 
161     case XalanNode::ELEMENT_NODE:
162         m_formatterListener.endElement(node->getNodeName().c_str());
163         break;
164 
165     default:
166         // Do nothing
167         break;
168     }
169 
170     return false;
171 }
172 
173 
174 
175 bool
endNode(XalanNode * node)176 FormatterTreeWalker::endNode(XalanNode*     node)
177 {
178     return endNode(const_cast<const XalanNode*>(node));
179 }
180 
181 
182 
183 }
184