1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.xml.security.uno;
25 
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.parsers.ParserConfigurationException;
29 
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.Text;
34 import org.w3c.dom.ProcessingInstruction;
35 
36 /* uno classes */
37 import com.sun.star.xml.sax.XDocumentHandler;
38 import com.sun.star.xml.sax.XAttributeList;
39 
40 /*
41  * this class is used to collect all received SAX events
42  * into a DOM document.
43  */
44 class SAXEventCollector implements XDocumentHandler
45 {
46 	/*
47 	 * the document which keeps received SAX events
48 	 */
49 	private Document m_document;
50 
51 	/*
52 	 * the current Element to which the next received
53 	 * SAX event will be added.
54 	 */
55 	private Node m_currentElement;
56 
57 	/*
58 	 * the TestTool which receives UI feedbacks
59 	 */
60 	private TestTool m_testTool;
61 
62 	/*
63 	 * whether displays information on console.
64 	 */
65 	private boolean m_systemDisplay;
66 
SAXEventCollector(TestTool testTool)67 	SAXEventCollector(TestTool testTool)
68 	{
69 		this(testTool, false);
70 	}
71 
SAXEventCollector(TestTool testTool, boolean sysDis)72 	SAXEventCollector(TestTool testTool, boolean sysDis)
73 	{
74 		m_systemDisplay = sysDis;
75 		m_testTool = testTool;
76 
77 		DocumentBuilderFactory factory =
78 			DocumentBuilderFactory.newInstance();
79 
80 		try
81 		{
82 			DocumentBuilder builder = factory.newDocumentBuilder();
83 			m_document = builder.newDocument();
84 
85 			m_currentElement = m_document;
86 		}
87 		catch (ParserConfigurationException pce) {
88 			pce.printStackTrace();
89 		}
90 	}
91 
getDocument()92 	protected Document getDocument()
93 	{
94 		return m_document;
95 	}
96 
getCurrentElement()97 	protected Node getCurrentElement()
98 	{
99 		return m_currentElement;
100 	}
101 
102         /*
103          * XDocumentHandler
104          */
startDocument()105 	public void  startDocument ()
106 	{
107 	}
108 
endDocument()109 	public void endDocument()
110 	{
111 	}
112 
startElement(String str, com.sun.star.xml.sax.XAttributeList xattribs)113 	public void startElement (String str, com.sun.star.xml.sax.XAttributeList xattribs)
114 	{
115 		Element newElement = m_document.createElement(str);
116 
117 		if (xattribs !=null)
118 		{
119 			int length = xattribs.getLength();
120 			for (short i=0; i<length; ++i)
121 			{
122 				newElement.setAttribute(
123 					xattribs.getNameByIndex(i),
124 					xattribs.getValueByIndex(i));
125 			}
126 		}
127 
128 		if (m_systemDisplay)
129 		{
130 			System.out.println("startElement:"+m_currentElement.toString());
131 		}
132 
133 		m_currentElement.appendChild(newElement);
134 		m_currentElement = newElement;
135 
136 		if (m_testTool != null)
137 		{
138 			m_testTool.updatesUIs();
139 		}
140 	}
141 
endElement(String str)142 	public void endElement(String str)
143 	{
144 		if (m_systemDisplay)
145 		{
146 			System.out.println("endElement:"+str+" "+m_currentElement.toString());
147 		}
148 
149         	m_currentElement = m_currentElement.getParentNode();
150 		if (m_systemDisplay)
151 		{
152 			System.out.println("----> "+m_currentElement.toString());
153 		}
154 
155 		if (m_testTool != null)
156 		{
157 			m_testTool.updatesUIs();
158 		}
159 	}
160 
characters(String str)161 	public void characters(String str)
162 	{
163         	Text newText = m_document.createTextNode(str);
164         	m_currentElement.appendChild(newText);
165 		if (m_testTool != null)
166 		{
167 			m_testTool.updatesUIs();
168 		}
169 	}
170 
ignorableWhitespace(String str)171 	public void ignorableWhitespace(String str)
172 	{
173 	}
174 
processingInstruction(String aTarget, String aData)175 	public void processingInstruction(String aTarget, String aData)
176 	{
177 		ProcessingInstruction newPI
178 			= m_document.createProcessingInstruction(aTarget, aData);
179 		m_currentElement.appendChild(newPI);
180 		if (m_testTool != null)
181 		{
182 			m_testTool.updatesUIs();
183 		}
184 	}
185 
setDocumentLocator(com.sun.star.xml.sax.XLocator xLocator )186 	public void setDocumentLocator (com.sun.star.xml.sax.XLocator xLocator )
187 		throws com.sun.star.xml.sax.SAXException
188 	{
189 	}
190 }
191 
192