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