xref: /trunk/main/xmlsecurity/tools/uno/SAXEventCollector.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.xml.security.uno;
29 
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.parsers.ParserConfigurationException;
33 
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.Node;
37 import org.w3c.dom.Text;
38 import org.w3c.dom.ProcessingInstruction;
39 
40 /* uno classes */
41 import com.sun.star.xml.sax.XDocumentHandler;
42 import com.sun.star.xml.sax.XAttributeList;
43 
44 /*
45  * this class is used to collect all received SAX events
46  * into a DOM document.
47  */
48 class SAXEventCollector implements XDocumentHandler
49 {
50     /*
51      * the document which keeps received SAX events
52      */
53     private Document m_document;
54 
55     /*
56      * the current Element to which the next received
57      * SAX event will be added.
58      */
59     private Node m_currentElement;
60 
61     /*
62      * the TestTool which receives UI feedbacks
63      */
64     private TestTool m_testTool;
65 
66     /*
67      * whether displays information on console.
68      */
69     private boolean m_systemDisplay;
70 
71     SAXEventCollector(TestTool testTool)
72     {
73         this(testTool, false);
74     }
75 
76     SAXEventCollector(TestTool testTool, boolean sysDis)
77     {
78         m_systemDisplay = sysDis;
79         m_testTool = testTool;
80 
81         DocumentBuilderFactory factory =
82             DocumentBuilderFactory.newInstance();
83 
84         try
85         {
86             DocumentBuilder builder = factory.newDocumentBuilder();
87             m_document = builder.newDocument();
88 
89             m_currentElement = m_document;
90         }
91         catch (ParserConfigurationException pce) {
92             pce.printStackTrace();
93         }
94     }
95 
96     protected Document getDocument()
97     {
98         return m_document;
99     }
100 
101     protected Node getCurrentElement()
102     {
103         return m_currentElement;
104     }
105 
106         /*
107          * XDocumentHandler
108          */
109     public void  startDocument ()
110     {
111     }
112 
113     public void endDocument()
114     {
115     }
116 
117     public void startElement (String str, com.sun.star.xml.sax.XAttributeList xattribs)
118     {
119         Element newElement = m_document.createElement(str);
120 
121         if (xattribs !=null)
122         {
123             int length = xattribs.getLength();
124             for (short i=0; i<length; ++i)
125             {
126                 newElement.setAttribute(
127                     xattribs.getNameByIndex(i),
128                     xattribs.getValueByIndex(i));
129             }
130         }
131 
132         if (m_systemDisplay)
133         {
134             System.out.println("startElement:"+m_currentElement.toString());
135         }
136 
137         m_currentElement.appendChild(newElement);
138         m_currentElement = newElement;
139 
140         if (m_testTool != null)
141         {
142             m_testTool.updatesUIs();
143         }
144     }
145 
146     public void endElement(String str)
147     {
148         if (m_systemDisplay)
149         {
150             System.out.println("endElement:"+str+" "+m_currentElement.toString());
151         }
152 
153             m_currentElement = m_currentElement.getParentNode();
154         if (m_systemDisplay)
155         {
156             System.out.println("----> "+m_currentElement.toString());
157         }
158 
159         if (m_testTool != null)
160         {
161             m_testTool.updatesUIs();
162         }
163     }
164 
165     public void characters(String str)
166     {
167             Text newText = m_document.createTextNode(str);
168             m_currentElement.appendChild(newText);
169         if (m_testTool != null)
170         {
171             m_testTool.updatesUIs();
172         }
173     }
174 
175     public void ignorableWhitespace(String str)
176     {
177     }
178 
179     public void processingInstruction(String aTarget, String aData)
180     {
181         ProcessingInstruction newPI
182             = m_document.createProcessingInstruction(aTarget, aData);
183         m_currentElement.appendChild(newPI);
184         if (m_testTool != null)
185         {
186             m_testTool.updatesUIs();
187         }
188     }
189 
190     public void setDocumentLocator (com.sun.star.xml.sax.XLocator xLocator )
191         throws com.sun.star.xml.sax.SAXException
192     {
193     }
194 }
195 
196