1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ParsingThread.java,v $
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
44 * the Node which will be handled with in the next step
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
)
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()
94 int type
= m_node
.getNodeType();
97 * the next event is not a endElement event.
102 case Node
.DOCUMENT_NODE
: /* startDocument */
103 m_testTool
.updatesCurrentSAXEventInformation("startDocument");
104 m_xDocumentHandler
.startDocument();
105 m_testTool
.updatesUIs();
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();
127 case Node
.TEXT_NODE
: /* characters */
128 message
= m_node
.getNodeValue();
131 m_testTool
.updatesCurrentSAXEventInformation("characters:"+message
);
132 m_xDocumentHandler
.characters(message
);
133 m_testTool
.updatesUIs();
136 case Node
.COMMENT_NODE
: /* comment */
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();
146 * figures out the event for the next step.
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
159 m_node
= m_node
.getFirstChild();
163 * otherwise, the next event will be endElement.
166 m_bIsEndEvent
= true;
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
184 * otherwise, the next event will be the endElement for the node's
188 m_node
= m_node
.getParentNode();
189 m_bIsEndEvent
= true;
196 * the next event is an endElement event.
201 case Node
.DOCUMENT_NODE
: /* endDocument */
202 m_testTool
.updatesCurrentSAXEventInformation("endDocument");
203 m_xDocumentHandler
.endDocument();
204 m_testTool
.updatesUIs();
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.
224 m_bIsEndEvent
= false;
228 * otherwise, the next event will be the endElement for the node's
232 m_node
= m_node
.getParentNode();
238 catch( com
.sun
.star
.xml
.sax
.SAXException e
)