update dev300-m58
[ooovba.git] / xmlsecurity / tools / uno / ParsingThread.java
blob95e5ca01aa383d09769077177ac6f82d2aa8daf2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ParsingThread.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.xml.security.uno;
33 import org.w3c.dom.Node;
34 import com.sun.star.xml.sax.XDocumentHandler;
35 import org.w3c.dom.Attr;
36 import org.w3c.dom.NamedNodeMap;
39 * this class is used to parse a document into SAX events
41 class ParsingThread
44 * the Node which will be handled with in the next step
46 private Node m_node;
49 * the event to be handled in the next step.
50 * true means endElement event, false otherwise.
52 private boolean m_bIsEndEvent;
55 * the document handler which receives generated SAX events
57 private XDocumentHandler m_xDocumentHandler;
60 * the TestTool which receives UI feedbacks
62 private TestTool m_testTool;
65 ParsingThread(Node node, XDocumentHandler xDocumentHandler, TestTool testTool)
67 m_node = node;
68 m_xDocumentHandler = xDocumentHandler;
69 m_testTool = testTool;
71 m_bIsEndEvent = false;
75 * changes the document handler.
77 protected void setHandler(XDocumentHandler xDocumentHandler)
79 this.m_xDocumentHandler = xDocumentHandler;
83 * sends the next SAX event.
84 * when there is no further step, then false is returned,
85 * otherwise, true returned.
87 protected boolean nextStep()
89 boolean rc = true;
91 try
93 String message;
94 int type = m_node.getNodeType();
95 if (!m_bIsEndEvent)
97 * the next event is not a endElement event.
100 switch (type)
102 case Node.DOCUMENT_NODE: /* startDocument */
103 m_testTool.updatesCurrentSAXEventInformation("startDocument");
104 m_xDocumentHandler.startDocument();
105 m_testTool.updatesUIs();
106 break;
107 case Node.ELEMENT_NODE: /* startElement */
108 String nodeName = m_node.getNodeName();
109 message = "startElement:"+nodeName;
110 NamedNodeMap attrs = m_node.getAttributes();
112 AttributeListHelper attributeListHelper = new AttributeListHelper();
114 int length = attrs.getLength();
115 for (int i=0; i<length; ++i)
117 Attr attr = (Attr)attrs.item(i);
118 attributeListHelper.setAttribute(attr.getName(), "CDATA", attr.getValue());
119 message += " "+attr.getName()+"='"+attr.getValue()+"'";
122 m_testTool.updatesCurrentSAXEventInformation(message);
123 m_xDocumentHandler.startElement(m_node.getNodeName(), attributeListHelper);
125 m_testTool.updatesUIs();
126 break;
127 case Node.TEXT_NODE: /* characters */
128 message = m_node.getNodeValue();
129 if (message != null)
131 m_testTool.updatesCurrentSAXEventInformation("characters:"+message);
132 m_xDocumentHandler.characters(message);
133 m_testTool.updatesUIs();
135 break;
136 case Node.COMMENT_NODE: /* comment */
137 break;
138 case Node.PROCESSING_INSTRUCTION_NODE: /* PI */
139 m_testTool.updatesCurrentSAXEventInformation("processingInstruction:"+m_node.getNodeName()+" "+m_node.getNodeValue());
140 m_xDocumentHandler.processingInstruction(m_node.getNodeName(), m_node.getNodeValue());
141 m_testTool.updatesUIs();
142 break;
146 * figures out the event for the next step.
148 switch (type)
150 case Node.DOCUMENT_NODE:
151 case Node.ELEMENT_NODE:
152 if (m_node.hasChildNodes())
154 * for a Document node or an Element node,
155 * if the node has children, then the next event will be for its
156 * first child node.
159 m_node = m_node.getFirstChild();
161 else
163 * otherwise, the next event will be endElement.
166 m_bIsEndEvent = true;
168 break;
169 case Node.TEXT_NODE:
170 case Node.PROCESSING_INSTRUCTION_NODE:
171 case Node.COMMENT_NODE:
172 Node nextNode = m_node.getNextSibling();
173 if (nextNode != null)
175 * for other kinds of node,
176 * if it has a next sibling, then the next event will be for that
177 * sibling.
180 m_node = nextNode;
182 else
184 * otherwise, the next event will be the endElement for the node's
185 * parent node.
188 m_node = m_node.getParentNode();
189 m_bIsEndEvent = true;
191 break;
194 else
196 * the next event is an endElement event.
199 switch (type)
201 case Node.DOCUMENT_NODE: /* endDocument */
202 m_testTool.updatesCurrentSAXEventInformation("endDocument");
203 m_xDocumentHandler.endDocument();
204 m_testTool.updatesUIs();
207 * no further steps.
209 rc = false;
210 break;
211 case Node.ELEMENT_NODE: /* endElement */
212 m_testTool.updatesCurrentSAXEventInformation("endElement:"+m_node.getNodeName());
213 m_xDocumentHandler.endElement(m_node.getNodeName());
214 m_testTool.updatesUIs();
216 Node nextNode = m_node.getNextSibling();
217 if (nextNode != null)
219 * if the node has a next sibling, then the next event will be the
220 * start event for that sibling node.
223 m_node = nextNode;
224 m_bIsEndEvent = false;
226 else
228 * otherwise, the next event will be the endElement for the node's
229 * parent node.
232 m_node = m_node.getParentNode();
234 break;
238 catch( com.sun.star.xml.sax.SAXException e)
240 e.printStackTrace();
243 * forces to end.
245 rc = false;
248 return rc;