Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifdef _MSC_VER
20 : #pragma warning(disable : 4701)
21 : #endif
22 :
23 : #include "saxbuilder.hxx"
24 :
25 : #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
26 : #include <comphelper/processfactory.hxx>
27 :
28 :
29 : namespace DOM
30 : {
31 107 : Reference< XInterface > CSAXDocumentBuilder::_getInstance(const Reference< XMultiServiceFactory >& rSMgr)
32 : {
33 107 : return static_cast< XSAXDocumentBuilder* >(new CSAXDocumentBuilder(rSMgr));
34 : }
35 :
36 : const char* CSAXDocumentBuilder::aImplementationName = "com.sun.star.comp.xml.dom.SAXDocumentBuilder";
37 : const char* CSAXDocumentBuilder::aSupportedServiceNames[] = {
38 : "com.sun.star.xml.dom.SAXDocumentBuilder",
39 : NULL
40 : };
41 :
42 107 : CSAXDocumentBuilder::CSAXDocumentBuilder(const Reference< XMultiServiceFactory >& mgr)
43 : : m_aServiceManager(mgr)
44 107 : , m_aState( SAXDocumentBuilderState_READY)
45 107 : {}
46 :
47 140 : OUString CSAXDocumentBuilder::_getImplementationName()
48 : {
49 140 : return OUString::createFromAscii(aImplementationName);
50 : }
51 43 : Sequence<OUString> CSAXDocumentBuilder::_getSupportedServiceNames()
52 : {
53 43 : Sequence<OUString> aSequence;
54 86 : for (int i=0; aSupportedServiceNames[i]!=NULL; i++) {
55 43 : aSequence.realloc(i+1);
56 43 : aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i]));
57 : }
58 43 : return aSequence;
59 : }
60 :
61 0 : Sequence< OUString > SAL_CALL CSAXDocumentBuilder::getSupportedServiceNames()
62 : throw (RuntimeException)
63 : {
64 0 : return CSAXDocumentBuilder::_getSupportedServiceNames();
65 : }
66 :
67 0 : OUString SAL_CALL CSAXDocumentBuilder::getImplementationName()
68 : throw (RuntimeException)
69 : {
70 0 : return CSAXDocumentBuilder::_getImplementationName();
71 : }
72 :
73 0 : sal_Bool SAL_CALL CSAXDocumentBuilder::supportsService(const OUString& aServiceName)
74 : throw (RuntimeException)
75 : {
76 0 : Sequence< OUString > supported = CSAXDocumentBuilder::_getSupportedServiceNames();
77 0 : for (sal_Int32 i=0; i<supported.getLength(); i++)
78 : {
79 0 : if (supported[i] == aServiceName) return sal_True;
80 : }
81 0 : return sal_False;
82 : }
83 :
84 :
85 0 : SAXDocumentBuilderState SAL_CALL CSAXDocumentBuilder::getState()
86 : throw (RuntimeException)
87 : {
88 0 : ::osl::MutexGuard g(m_Mutex);
89 :
90 0 : return m_aState;
91 : }
92 :
93 0 : void SAL_CALL CSAXDocumentBuilder::reset()
94 : throw (RuntimeException)
95 : {
96 0 : ::osl::MutexGuard g(m_Mutex);
97 :
98 0 : m_aDocument = Reference< XDocument >();
99 0 : m_aFragment = Reference< XDocumentFragment >();
100 0 : while (!m_aNodeStack.empty()) m_aNodeStack.pop();
101 0 : while (!m_aNSStack.empty()) m_aNSStack.pop();
102 0 : m_aState = SAXDocumentBuilderState_READY;
103 0 : }
104 :
105 107 : Reference< XDocument > SAL_CALL CSAXDocumentBuilder::getDocument()
106 : throw (RuntimeException)
107 : {
108 107 : ::osl::MutexGuard g(m_Mutex);
109 :
110 107 : if (m_aState != SAXDocumentBuilderState_DOCUMENT_FINISHED)
111 0 : throw RuntimeException();
112 :
113 107 : return m_aDocument;
114 : }
115 :
116 0 : Reference< XDocumentFragment > SAL_CALL CSAXDocumentBuilder::getDocumentFragment()
117 : throw (RuntimeException)
118 : {
119 0 : ::osl::MutexGuard g(m_Mutex);
120 :
121 0 : if (m_aState != SAXDocumentBuilderState_FRAGMENT_FINISHED)
122 0 : throw RuntimeException();
123 0 : return m_aFragment;
124 : }
125 :
126 0 : void SAL_CALL CSAXDocumentBuilder::startDocumentFragment(const Reference< XDocument >& ownerDoc)
127 : throw (RuntimeException)
128 : {
129 0 : ::osl::MutexGuard g(m_Mutex);
130 :
131 : // start a new document fragment and push it onto the stack
132 : // we have to be in a clean state to do this
133 0 : if (!m_aState == SAXDocumentBuilderState_READY)
134 0 : throw RuntimeException();
135 :
136 0 : m_aDocument = ownerDoc;
137 0 : Reference< XDocumentFragment > aFragment = m_aDocument->createDocumentFragment();
138 0 : m_aNodeStack.push(Reference< XNode >(aFragment, UNO_QUERY));
139 0 : m_aFragment = aFragment;
140 0 : m_aState = SAXDocumentBuilderState_BUILDING_FRAGMENT;
141 0 : }
142 :
143 0 : void SAL_CALL CSAXDocumentBuilder::endDocumentFragment()
144 : throw (RuntimeException)
145 : {
146 0 : ::osl::MutexGuard g(m_Mutex);
147 :
148 : // there should only be the document left on the node stack
149 0 : if (m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
150 0 : throw RuntimeException();
151 :
152 0 : Reference< XNode > aNode = m_aNodeStack.top();
153 0 : if ( aNode->getNodeType() != NodeType_DOCUMENT_FRAGMENT_NODE)
154 0 : throw RuntimeException();
155 0 : m_aNodeStack.pop();
156 0 : m_aState = SAXDocumentBuilderState_FRAGMENT_FINISHED;
157 0 : }
158 :
159 : // document handler
160 :
161 107 : void SAL_CALL CSAXDocumentBuilder::startDocument() throw (RuntimeException, SAXException)
162 : {
163 107 : ::osl::MutexGuard g(m_Mutex);
164 :
165 : // start a new document and push it onto the stack
166 : // we have to be in a clean state to do this
167 107 : if (!m_aState == SAXDocumentBuilderState_READY)
168 0 : throw SAXException();
169 :
170 107 : Reference< XDocumentBuilder > aBuilder(DocumentBuilder::create(comphelper::getComponentContext(m_aServiceManager)));
171 107 : Reference< XDocument > aDocument = aBuilder->newDocument();
172 107 : m_aNodeStack.push(Reference< XNode >(aDocument, UNO_QUERY));
173 107 : m_aDocument = aDocument;
174 107 : m_aState = SAXDocumentBuilderState_BUILDING_DOCUMENT;
175 107 : }
176 :
177 107 : void SAL_CALL CSAXDocumentBuilder::endDocument() throw (RuntimeException, SAXException)
178 : {
179 107 : ::osl::MutexGuard g(m_Mutex);
180 :
181 : // there should only be the document left on the node stack
182 107 : if (!m_aState == SAXDocumentBuilderState_BUILDING_DOCUMENT)
183 0 : throw SAXException();
184 :
185 107 : Reference< XNode > aNode = m_aNodeStack.top();
186 107 : if ( aNode->getNodeType() != NodeType_DOCUMENT_NODE)
187 0 : throw SAXException();
188 107 : m_aNodeStack.pop();
189 107 : m_aState = SAXDocumentBuilderState_DOCUMENT_FINISHED;
190 107 : }
191 :
192 1040 : void SAL_CALL CSAXDocumentBuilder::startElement(const OUString& aName, const Reference< XAttributeList>& attribs)
193 : throw (RuntimeException, SAXException)
194 : {
195 1040 : ::osl::MutexGuard g(m_Mutex);
196 :
197 1040 : if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
198 : m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
199 : {
200 0 : throw SAXException();
201 : }
202 :
203 : // start with mappings in effect for last level
204 1040 : NSMap aNSMap;
205 1040 : if (!m_aNSStack.empty())
206 933 : aNSMap = NSMap(m_aNSStack.top());
207 :
208 : // handle xmlns: attributes and add to mappings
209 1040 : OUString attr_qname;
210 1040 : OUString attr_value;
211 1040 : OUString newprefix;
212 1040 : AttrMap aAttrMap;
213 1040 : sal_Int32 idx=-1;
214 1040 : sal_Int16 nAttributes = attribs->getLength();
215 2433 : for (sal_Int16 i=0; i<nAttributes; i++)
216 : {
217 1393 : attr_qname = attribs->getNameByIndex(i);
218 1393 : attr_value = attribs->getValueByIndex(i);
219 : // new prefix mapping
220 1393 : if (attr_qname.indexOf("xmlns:") == 0)
221 : {
222 657 : newprefix = attr_qname.copy(attr_qname.indexOf(':')+1);
223 657 : aNSMap.insert(NSMap::value_type(newprefix, attr_value));
224 : }
225 736 : else if ( attr_qname == "xmlns" )
226 : {
227 : // new default prefix
228 0 : aNSMap.insert(NSMap::value_type(OUString(), attr_value));
229 : }
230 : else
231 : {
232 736 : aAttrMap.insert(AttrMap::value_type(attr_qname, attr_value));
233 : }
234 : }
235 :
236 : // does the element have a prefix?
237 1040 : OUString aPrefix;
238 1040 : OUString aURI;
239 1040 : Reference< XElement > aElement;
240 1040 : idx = aName.indexOf(':');
241 1040 : if (idx != -1)
242 : {
243 1040 : aPrefix = aName.copy(0, idx);
244 : }
245 : else
246 0 : aPrefix = OUString();
247 :
248 1040 : NSMap::const_iterator result = aNSMap.find(aPrefix);
249 1040 : if ( result != aNSMap.end())
250 : {
251 : // found a URI for prefix
252 : // qualified name
253 1040 : aElement = m_aDocument->createElementNS( result->second, aName);
254 : }
255 : else
256 : {
257 : // no URI for prefix
258 0 : aElement = m_aDocument->createElement(aName);
259 : }
260 : aElement = Reference< XElement > (
261 1040 : m_aNodeStack.top()->appendChild(Reference< XNode >(aElement, UNO_QUERY)),
262 1040 : UNO_QUERY);
263 1040 : m_aNodeStack.push(Reference< XNode >(aElement, UNO_QUERY));
264 :
265 : // set non xmlns attributes
266 1040 : aPrefix = OUString();
267 1040 : aURI = OUString();
268 1040 : AttrMap::const_iterator a = aAttrMap.begin();
269 2816 : while (a != aAttrMap.end())
270 : {
271 736 : attr_qname = a->first;
272 736 : attr_value = a->second;
273 736 : idx = attr_qname.indexOf(':');
274 736 : if (idx != -1)
275 736 : aPrefix = attr_qname.copy(0, idx);
276 : else
277 0 : aPrefix = OUString();
278 :
279 736 : result = aNSMap.find(aPrefix);
280 736 : if (result != aNSMap.end())
281 : {
282 : // set attribute with namespace
283 736 : aElement->setAttributeNS(result->second, attr_qname, attr_value);
284 : }
285 : else
286 : {
287 : // set attribute without namespace
288 0 : aElement->setAttribute(attr_qname, attr_value);
289 : }
290 736 : ++a;
291 : }
292 1040 : m_aNSStack.push(aNSMap);
293 1040 : }
294 :
295 1040 : void SAL_CALL CSAXDocumentBuilder::endElement(const OUString& aName)
296 : throw (RuntimeException, SAXException)
297 : {
298 1040 : ::osl::MutexGuard g(m_Mutex);
299 :
300 : // pop the current element from the stack
301 1040 : if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
302 : m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
303 0 : throw SAXException();
304 :
305 1040 : Reference< XNode > aNode(m_aNodeStack.top());
306 1040 : if (aNode->getNodeType() != NodeType_ELEMENT_NODE)
307 0 : throw SAXException();
308 :
309 1040 : Reference< XElement > aElement(aNode, UNO_QUERY);
310 1040 : OUString aRefName;
311 1040 : OUString aPrefix = aElement->getPrefix();
312 1040 : if (!aPrefix.isEmpty())
313 1040 : aRefName = aPrefix + ":" + aElement->getTagName();
314 : else
315 0 : aRefName = aElement->getTagName();
316 1040 : if (aRefName != aName) // consistency check
317 0 : throw SAXException();
318 :
319 : // pop it
320 1040 : m_aNodeStack.pop();
321 1040 : m_aNSStack.pop();
322 1040 : }
323 :
324 679 : void SAL_CALL CSAXDocumentBuilder::characters(const OUString& aChars)
325 : throw (RuntimeException, SAXException)
326 : {
327 679 : ::osl::MutexGuard g(m_Mutex);
328 :
329 : // append text node to the current top element
330 679 : if (m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
331 : m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
332 0 : throw SAXException();
333 :
334 679 : Reference< XText > aText = m_aDocument->createTextNode(aChars);
335 679 : m_aNodeStack.top()->appendChild(Reference< XNode >(aText, UNO_QUERY));
336 679 : }
337 :
338 0 : void SAL_CALL CSAXDocumentBuilder::ignorableWhitespace(const OUString& )
339 : throw (RuntimeException, SAXException)
340 : {
341 0 : ::osl::MutexGuard g(m_Mutex);
342 :
343 : // ignore ignorable whitespace
344 0 : if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
345 : m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
346 0 : throw SAXException();
347 0 : }
348 :
349 0 : void SAL_CALL CSAXDocumentBuilder::processingInstruction(const OUString& aTarget, const OUString& aData)
350 : throw (RuntimeException, SAXException)
351 : {
352 0 : ::osl::MutexGuard g(m_Mutex);
353 :
354 : // append PI node to the current top
355 0 : if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
356 : m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
357 0 : throw SAXException();
358 :
359 0 : Reference< XProcessingInstruction > aInstruction = m_aDocument->createProcessingInstruction(
360 0 : aTarget, aData);
361 0 : m_aNodeStack.top()->appendChild(Reference< XNode >(aInstruction, UNO_QUERY));
362 0 : }
363 :
364 0 : void SAL_CALL CSAXDocumentBuilder::setDocumentLocator(const Reference< XLocator >& aLocator)
365 : throw (RuntimeException, SAXException)
366 : {
367 0 : ::osl::MutexGuard g(m_Mutex);
368 :
369 : // set the document locator...
370 0 : m_aLocator = aLocator;
371 0 : }
372 : }
373 :
374 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|