merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / uno / ParsingThread.java
blobf4bfea4a442e9e9fba167c3f0a8da743c7b773d0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package com.sun.star.xml.security.uno;
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;
36 * this class is used to parse a document into SAX events
38 class ParsingThread
41 * the Node which will be handled with in the next step
43 private Node m_node;
46 * the event to be handled in the next step.
47 * true means endElement event, false otherwise.
49 private boolean m_bIsEndEvent;
52 * the document handler which receives generated SAX events
54 private XDocumentHandler m_xDocumentHandler;
57 * the TestTool which receives UI feedbacks
59 private TestTool m_testTool;
62 ParsingThread(Node node, XDocumentHandler xDocumentHandler, TestTool testTool)
64 m_node = node;
65 m_xDocumentHandler = xDocumentHandler;
66 m_testTool = testTool;
68 m_bIsEndEvent = false;
72 * changes the document handler.
74 protected void setHandler(XDocumentHandler xDocumentHandler)
76 this.m_xDocumentHandler = xDocumentHandler;
80 * sends the next SAX event.
81 * when there is no further step, then false is returned,
82 * otherwise, true returned.
84 protected boolean nextStep()
86 boolean rc = true;
88 try
90 String message;
91 int type = m_node.getNodeType();
92 if (!m_bIsEndEvent)
94 * the next event is not a endElement event.
97 switch (type)
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();
109 AttributeListHelper attributeListHelper = new AttributeListHelper();
111 int length = attrs.getLength();
112 for (int i=0; i<length; ++i)
114 Attr attr = (Attr)attrs.item(i);
115 attributeListHelper.setAttribute(attr.getName(), "CDATA", attr.getValue());
116 message += " "+attr.getName()+"='"+attr.getValue()+"'";
119 m_testTool.updatesCurrentSAXEventInformation(message);
120 m_xDocumentHandler.startElement(m_node.getNodeName(), attributeListHelper);
122 m_testTool.updatesUIs();
123 break;
124 case Node.TEXT_NODE: /* characters */
125 message = m_node.getNodeValue();
126 if (message != null)
128 m_testTool.updatesCurrentSAXEventInformation("characters:"+message);
129 m_xDocumentHandler.characters(message);
130 m_testTool.updatesUIs();
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;
143 * figures out the event for the next step.
145 switch (type)
147 case Node.DOCUMENT_NODE:
148 case Node.ELEMENT_NODE:
149 if (m_node.hasChildNodes())
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.
156 m_node = m_node.getFirstChild();
158 else
160 * otherwise, the next event will be endElement.
163 m_bIsEndEvent = true;
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)
172 * for other kinds of node,
173 * if it has a next sibling, then the next event will be for that
174 * sibling.
177 m_node = nextNode;
179 else
181 * otherwise, the next event will be the endElement for the node's
182 * parent node.
185 m_node = m_node.getParentNode();
186 m_bIsEndEvent = true;
188 break;
191 else
193 * the next event is an endElement event.
196 switch (type)
198 case Node.DOCUMENT_NODE: /* endDocument */
199 m_testTool.updatesCurrentSAXEventInformation("endDocument");
200 m_xDocumentHandler.endDocument();
201 m_testTool.updatesUIs();
204 * no further steps.
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();
213 Node nextNode = m_node.getNextSibling();
214 if (nextNode != null)
216 * if the node has a next sibling, then the next event will be the
217 * start event for that sibling node.
220 m_node = nextNode;
221 m_bIsEndEvent = false;
223 else
225 * otherwise, the next event will be the endElement for the node's
226 * parent node.
229 m_node = m_node.getParentNode();
231 break;
235 catch( com.sun.star.xml.sax.SAXException e)
237 e.printStackTrace();
240 * forces to end.
242 rc = false;
245 return rc;