1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.xml.security.uno;
25 
26 import org.w3c.dom.Node;
27 import com.sun.star.xml.sax.XDocumentHandler;
28 import org.w3c.dom.Attr;
29 import org.w3c.dom.NamedNodeMap;
30 
31 /*
32  * this class is used to parse a document into SAX events
33  */
34 class ParsingThread
35 {
36 	/*
37 	 * the Node which will be handled with in the next step
38 	 */
39 	private Node m_node;
40 
41 	/*
42 	 * the event to be handled in the next step.
43 	 * true means endElement event, false otherwise.
44 	 */
45 	private boolean m_bIsEndEvent;
46 
47 	/*
48 	 * the document handler which receives generated SAX events
49 	 */
50 	private XDocumentHandler m_xDocumentHandler;
51 
52 	/*
53 	 * the TestTool which receives UI feedbacks
54 	 */
55 	private TestTool m_testTool;
56 
57 
ParsingThread(Node node, XDocumentHandler xDocumentHandler, TestTool testTool)58         ParsingThread(Node node, XDocumentHandler xDocumentHandler, TestTool testTool)
59         {
60         	m_node = node;
61         	m_xDocumentHandler = xDocumentHandler;
62         	m_testTool = testTool;
63 
64         	m_bIsEndEvent = false;
65         }
66 
67         /*
68          * changes the document handler.
69          */
setHandler(XDocumentHandler xDocumentHandler)70         protected void setHandler(XDocumentHandler xDocumentHandler)
71         {
72         	this.m_xDocumentHandler = xDocumentHandler;
73         }
74 
75         /*
76          * sends the next SAX event.
77          * when there is no further step, then false is returned,
78          * otherwise, true returned.
79          */
nextStep()80         protected boolean nextStep()
81         {
82         	boolean rc = true;
83 
84         	try
85         	{
86 	        	String message;
87 			int type = m_node.getNodeType();
88 			if (!m_bIsEndEvent)
89 			/*
90 			 * the next event is not a endElement event.
91 			 */
92 			{
93 				switch (type)
94 				{
95 				case Node.DOCUMENT_NODE: /* startDocument */
96 					m_testTool.updatesCurrentSAXEventInformation("startDocument");
97 					m_xDocumentHandler.startDocument();
98 					m_testTool.updatesUIs();
99 					break;
100 				case Node.ELEMENT_NODE: /* startElement */
101 					String nodeName = m_node.getNodeName();
102 					message = "startElement:"+nodeName;
103 					NamedNodeMap attrs = m_node.getAttributes();
104 
105 					AttributeListHelper attributeListHelper = new AttributeListHelper();
106 
107 					int length = attrs.getLength();
108 					for (int i=0; i<length; ++i)
109 					{
110 						Attr attr = (Attr)attrs.item(i);
111 						attributeListHelper.setAttribute(attr.getName(), "CDATA", attr.getValue());
112 						message += " "+attr.getName()+"='"+attr.getValue()+"'";
113 					}
114 
115 					m_testTool.updatesCurrentSAXEventInformation(message);
116 					m_xDocumentHandler.startElement(m_node.getNodeName(), attributeListHelper);
117 
118 					m_testTool.updatesUIs();
119 					break;
120 				case Node.TEXT_NODE: /* characters */
121 					message = m_node.getNodeValue();
122 					if (message != null)
123 					{
124 						m_testTool.updatesCurrentSAXEventInformation("characters:"+message);
125 						m_xDocumentHandler.characters(message);
126 						m_testTool.updatesUIs();
127 					}
128 					break;
129 				case Node.COMMENT_NODE: /* comment */
130 					break;
131 				case Node.PROCESSING_INSTRUCTION_NODE: /* PI */
132 					m_testTool.updatesCurrentSAXEventInformation("processingInstruction:"+m_node.getNodeName()+" "+m_node.getNodeValue());
133 					m_xDocumentHandler.processingInstruction(m_node.getNodeName(), m_node.getNodeValue());
134 					m_testTool.updatesUIs();
135 					break;
136 				}
137 
138 				/*
139 				 * figures out the event for the next step.
140 				 */
141 				switch (type)
142 				{
143 					case Node.DOCUMENT_NODE:
144 					case Node.ELEMENT_NODE:
145 						if (m_node.hasChildNodes())
146 						/*
147 						 * for a Document node or an Element node,
148 						 * if the node has children, then the next event will be for its
149 						 * first child node.
150 						 */
151 						{
152 							m_node = m_node.getFirstChild();
153 						}
154 						else
155 						/*
156 						 * otherwise, the next event will be endElement.
157 						 */
158 						{
159 							m_bIsEndEvent = true;
160 						}
161 						break;
162 					case Node.TEXT_NODE:
163 					case Node.PROCESSING_INSTRUCTION_NODE:
164 					case Node.COMMENT_NODE:
165 						Node nextNode = m_node.getNextSibling();
166 						if (nextNode != null)
167 						/*
168 						 * for other kinds of node,
169 						 * if it has a next sibling, then the next event will be for that
170 						 * sibling.
171 						 */
172 						{
173 							m_node = nextNode;
174 						}
175 						else
176 						/*
177 						 * otherwise, the next event will be the endElement for the node's
178 						 * parent node.
179 						 */
180 						{
181 							m_node = m_node.getParentNode();
182 							m_bIsEndEvent = true;
183 						}
184 						break;
185 				}
186 			}
187 			else
188 			/*
189 			 * the next event is an endElement event.
190 			 */
191 			{
192 				switch (type)
193 				{
194 					case Node.DOCUMENT_NODE: /* endDocument */
195 						m_testTool.updatesCurrentSAXEventInformation("endDocument");
196 						m_xDocumentHandler.endDocument();
197 						m_testTool.updatesUIs();
198 
199 						/*
200 						 * no further steps.
201 						 */
202 						rc = false;
203 						break;
204 					case Node.ELEMENT_NODE: /* endElement */
205 						m_testTool.updatesCurrentSAXEventInformation("endElement:"+m_node.getNodeName());
206 						m_xDocumentHandler.endElement(m_node.getNodeName());
207 						m_testTool.updatesUIs();
208 
209 						Node nextNode = m_node.getNextSibling();
210 						if (nextNode != null)
211 						/*
212 						 * if the node has a next sibling, then the next event will be the
213 						 * start event for that sibling node.
214 						 */
215 						{
216 							m_node = nextNode;
217 							m_bIsEndEvent = false;
218 						}
219 						else
220 						/*
221 						 * otherwise, the next event will be the endElement for the node's
222 						 * parent node.
223 						 */
224 						{
225 							m_node = m_node.getParentNode();
226 						}
227 						break;
228 				}
229 			}
230 		}
231 		catch(  com.sun.star.xml.sax.SAXException e)
232 		{
233 			e.printStackTrace();
234 
235 			/*
236 			 * forces to end.
237 			 */
238 			rc = false;
239 		}
240 
241 		return rc;
242 	}
243 }
244 
245