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
41 * the Node which will be handled with in the next step
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
)
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()
91 int type
= m_node
.getNodeType();
94 * the next event is not a endElement event.
99 case Node
.DOCUMENT_NODE
: /* startDocument */
100 m_testTool
.updatesCurrentSAXEventInformation("startDocument");
101 m_xDocumentHandler
.startDocument();
102 m_testTool
.updatesUIs();
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();
124 case Node
.TEXT_NODE
: /* characters */
125 message
= m_node
.getNodeValue();
128 m_testTool
.updatesCurrentSAXEventInformation("characters:"+message
);
129 m_xDocumentHandler
.characters(message
);
130 m_testTool
.updatesUIs();
133 case Node
.COMMENT_NODE
: /* comment */
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();
143 * figures out the event for the next step.
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
156 m_node
= m_node
.getFirstChild();
160 * otherwise, the next event will be endElement.
163 m_bIsEndEvent
= true;
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
181 * otherwise, the next event will be the endElement for the node's
185 m_node
= m_node
.getParentNode();
186 m_bIsEndEvent
= true;
193 * the next event is an endElement event.
198 case Node
.DOCUMENT_NODE
: /* endDocument */
199 m_testTool
.updatesCurrentSAXEventInformation("endDocument");
200 m_xDocumentHandler
.endDocument();
201 m_testTool
.updatesUIs();
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.
221 m_bIsEndEvent
= false;
225 * otherwise, the next event will be the endElement for the node's
229 m_node
= m_node
.getParentNode();
235 catch( com
.sun
.star
.xml
.sax
.SAXException e
)