1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.xml.security.uno;
29 
30 import org.w3c.dom.Node;
31 import com.sun.star.xml.sax.XDocumentHandler;
32 import org.w3c.dom.Attr;
33 import org.w3c.dom.NamedNodeMap;
34 
35 /*
36  * this class is used to parse a document into SAX events
37  */
38 class ParsingThread
39 {
40 	/*
41 	 * the Node which will be handled with in the next step
42 	 */
43 	private Node m_node;
44 
45 	/*
46 	 * the event to be handled in the next step.
47 	 * true means endElement event, false otherwise.
48 	 */
49 	private boolean m_bIsEndEvent;
50 
51 	/*
52 	 * the document handler which receives generated SAX events
53 	 */
54 	private XDocumentHandler m_xDocumentHandler;
55 
56 	/*
57 	 * the TestTool which receives UI feedbacks
58 	 */
59 	private TestTool m_testTool;
60 
61 
62         ParsingThread(Node node, XDocumentHandler xDocumentHandler, TestTool testTool)
63         {
64         	m_node = node;
65         	m_xDocumentHandler = xDocumentHandler;
66         	m_testTool = testTool;
67 
68         	m_bIsEndEvent = false;
69         }
70 
71         /*
72          * changes the document handler.
73          */
74         protected void setHandler(XDocumentHandler xDocumentHandler)
75         {
76         	this.m_xDocumentHandler = xDocumentHandler;
77         }
78 
79         /*
80          * sends the next SAX event.
81          * when there is no further step, then false is returned,
82          * otherwise, true returned.
83          */
84         protected boolean nextStep()
85         {
86         	boolean rc = true;
87 
88         	try
89         	{
90 	        	String message;
91 			int type = m_node.getNodeType();
92 			if (!m_bIsEndEvent)
93 			/*
94 			 * the next event is not a endElement event.
95 			 */
96 			{
97 				switch (type)
98 				{
99 				case Node.DOCUMENT_NODE: /* startDocument */
100 					m_testTool.updatesCurrentSAXEventInformation("startDocument");
101 					m_xDocumentHandler.startDocument();
102 					m_testTool.updatesUIs();
103 					break;
104 				case Node.ELEMENT_NODE: /* startElement */
105 					String nodeName = m_node.getNodeName();
106 					message = "startElement:"+nodeName;
107 					NamedNodeMap attrs = m_node.getAttributes();
108 
109 					AttributeListHelper attributeListHelper = new AttributeListHelper();
110 
111 					int length = attrs.getLength();
112 					for (int i=0; i<length; ++i)
113 					{
114 						Attr attr = (Attr)attrs.item(i);
115 						attributeListHelper.setAttribute(attr.getName(), "CDATA", attr.getValue());
116 						message += " "+attr.getName()+"='"+attr.getValue()+"'";
117 					}
118 
119 					m_testTool.updatesCurrentSAXEventInformation(message);
120 					m_xDocumentHandler.startElement(m_node.getNodeName(), attributeListHelper);
121 
122 					m_testTool.updatesUIs();
123 					break;
124 				case Node.TEXT_NODE: /* characters */
125 					message = m_node.getNodeValue();
126 					if (message != null)
127 					{
128 						m_testTool.updatesCurrentSAXEventInformation("characters:"+message);
129 						m_xDocumentHandler.characters(message);
130 						m_testTool.updatesUIs();
131 					}
132 					break;
133 				case Node.COMMENT_NODE: /* comment */
134 					break;
135 				case Node.PROCESSING_INSTRUCTION_NODE: /* PI */
136 					m_testTool.updatesCurrentSAXEventInformation("processingInstruction:"+m_node.getNodeName()+" "+m_node.getNodeValue());
137 					m_xDocumentHandler.processingInstruction(m_node.getNodeName(), m_node.getNodeValue());
138 					m_testTool.updatesUIs();
139 					break;
140 				}
141 
142 				/*
143 				 * figures out the event for the next step.
144 				 */
145 				switch (type)
146 				{
147 					case Node.DOCUMENT_NODE:
148 					case Node.ELEMENT_NODE:
149 						if (m_node.hasChildNodes())
150 						/*
151 						 * for a Document node or an Element node,
152 						 * if the node has children, then the next event will be for its
153 						 * first child node.
154 						 */
155 						{
156 							m_node = m_node.getFirstChild();
157 						}
158 						else
159 						/*
160 						 * otherwise, the next event will be endElement.
161 						 */
162 						{
163 							m_bIsEndEvent = true;
164 						}
165 						break;
166 					case Node.TEXT_NODE:
167 					case Node.PROCESSING_INSTRUCTION_NODE:
168 					case Node.COMMENT_NODE:
169 						Node nextNode = m_node.getNextSibling();
170 						if (nextNode != null)
171 						/*
172 						 * for other kinds of node,
173 						 * if it has a next sibling, then the next event will be for that
174 						 * sibling.
175 						 */
176 						{
177 							m_node = nextNode;
178 						}
179 						else
180 						/*
181 						 * otherwise, the next event will be the endElement for the node's
182 						 * parent node.
183 						 */
184 						{
185 							m_node = m_node.getParentNode();
186 							m_bIsEndEvent = true;
187 						}
188 						break;
189 				}
190 			}
191 			else
192 			/*
193 			 * the next event is an endElement event.
194 			 */
195 			{
196 				switch (type)
197 				{
198 					case Node.DOCUMENT_NODE: /* endDocument */
199 						m_testTool.updatesCurrentSAXEventInformation("endDocument");
200 						m_xDocumentHandler.endDocument();
201 						m_testTool.updatesUIs();
202 
203 						/*
204 						 * no further steps.
205 						 */
206 						rc = false;
207 						break;
208 					case Node.ELEMENT_NODE: /* endElement */
209 						m_testTool.updatesCurrentSAXEventInformation("endElement:"+m_node.getNodeName());
210 						m_xDocumentHandler.endElement(m_node.getNodeName());
211 						m_testTool.updatesUIs();
212 
213 						Node nextNode = m_node.getNextSibling();
214 						if (nextNode != null)
215 						/*
216 						 * if the node has a next sibling, then the next event will be the
217 						 * start event for that sibling node.
218 						 */
219 						{
220 							m_node = nextNode;
221 							m_bIsEndEvent = false;
222 						}
223 						else
224 						/*
225 						 * otherwise, the next event will be the endElement for the node's
226 						 * parent node.
227 						 */
228 						{
229 							m_node = m_node.getParentNode();
230 						}
231 						break;
232 				}
233 			}
234 		}
235 		catch(  com.sun.star.xml.sax.SAXException e)
236 		{
237 			e.printStackTrace();
238 
239 			/*
240 			 * forces to end.
241 			 */
242 			rc = false;
243 		}
244 
245 		return rc;
246 	}
247 }
248 
249