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 44 : Reference< XInterface > CSAXDocumentBuilder::_getInstance(const Reference< XMultiServiceFactory >& rSMgr)
32 : {
33 44 : 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 44 : CSAXDocumentBuilder::CSAXDocumentBuilder(const Reference< XMultiServiceFactory >& mgr)
43 : : m_aServiceManager(mgr)
44 44 : , m_aState( SAXDocumentBuilderState_READY)
45 44 : {}
46 :
47 38 : OUString CSAXDocumentBuilder::_getImplementationName()
48 : {
49 38 : return OUString::createFromAscii(aImplementationName);
50 : }
51 11 : Sequence<OUString> CSAXDocumentBuilder::_getSupportedServiceNames()
52 : {
53 11 : Sequence<OUString> aSequence;
54 22 : for (int i=0; aSupportedServiceNames[i]!=NULL; i++) {
55 11 : aSequence.realloc(i+1);
56 11 : aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i]));
57 : }
58 11 : 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 44 : Reference< XDocument > SAL_CALL CSAXDocumentBuilder::getDocument()
106 : throw (RuntimeException)
107 : {
108 44 : ::osl::MutexGuard g(m_Mutex);
109 :
110 44 : if (m_aState != SAXDocumentBuilderState_DOCUMENT_FINISHED)
111 0 : throw RuntimeException();
112 :
113 44 : 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 44 : void SAL_CALL CSAXDocumentBuilder::startDocument() throw (RuntimeException, SAXException)
162 : {
163 44 : ::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 44 : if (!m_aState == SAXDocumentBuilderState_READY)
168 0 : throw SAXException();
169 :
170 44 : Reference< XDocumentBuilder > aBuilder(DocumentBuilder::create(comphelper::getComponentContext(m_aServiceManager)));
171 44 : Reference< XDocument > aDocument = aBuilder->newDocument();
172 44 : m_aNodeStack.push(Reference< XNode >(aDocument, UNO_QUERY));
173 44 : m_aDocument = aDocument;
174 44 : m_aState = SAXDocumentBuilderState_BUILDING_DOCUMENT;
175 44 : }
176 :
177 44 : void SAL_CALL CSAXDocumentBuilder::endDocument() throw (RuntimeException, SAXException)
178 : {
179 44 : ::osl::MutexGuard g(m_Mutex);
180 :
181 : // there should only be the document left on the node stack
182 44 : if (!m_aState == SAXDocumentBuilderState_BUILDING_DOCUMENT)
183 0 : throw SAXException();
184 :
185 44 : Reference< XNode > aNode = m_aNodeStack.top();
186 44 : if ( aNode->getNodeType() != NodeType_DOCUMENT_NODE)
187 0 : throw SAXException();
188 44 : m_aNodeStack.pop();
189 44 : m_aState = SAXDocumentBuilderState_DOCUMENT_FINISHED;
190 44 : }
191 :
192 426 : void SAL_CALL CSAXDocumentBuilder::startElement(const OUString& aName, const Reference< XAttributeList>& attribs)
193 : throw (RuntimeException, SAXException)
194 : {
195 426 : ::osl::MutexGuard g(m_Mutex);
196 :
197 426 : 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 426 : NSMap aNSMap;
205 426 : if (!m_aNSStack.empty())
206 382 : aNSMap = NSMap(m_aNSStack.top());
207 :
208 : // handle xmlns: attributes and add to mappings
209 426 : OUString attr_qname;
210 426 : OUString attr_value;
211 426 : OUString newprefix;
212 426 : AttrMap aAttrMap;
213 426 : sal_Int32 idx=-1;
214 426 : sal_Int16 nAttributes = attribs->getLength();
215 1033 : for (sal_Int16 i=0; i<nAttributes; i++)
216 : {
217 607 : attr_qname = attribs->getNameByIndex(i);
218 607 : attr_value = attribs->getValueByIndex(i);
219 : // new prefix mapping
220 607 : if (attr_qname.indexOf("xmlns:") == 0)
221 : {
222 272 : newprefix = attr_qname.copy(attr_qname.indexOf(':')+1);
223 272 : aNSMap.insert(NSMap::value_type(newprefix, attr_value));
224 : }
225 335 : 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 335 : aAttrMap.insert(AttrMap::value_type(attr_qname, attr_value));
233 : }
234 : }
235 :
236 : // does the element have a prefix?
237 426 : OUString aPrefix;
238 426 : OUString aURI;
239 426 : Reference< XElement > aElement;
240 426 : idx = aName.indexOf(':');
241 426 : if (idx != -1)
242 : {
243 426 : aPrefix = aName.copy(0, idx);
244 : }
245 : else
246 0 : aPrefix = OUString();
247 :
248 426 : NSMap::const_iterator result = aNSMap.find(aPrefix);
249 426 : if ( result != aNSMap.end())
250 : {
251 : // found a URI for prefix
252 : // qualified name
253 426 : 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 426 : m_aNodeStack.top()->appendChild(Reference< XNode >(aElement, UNO_QUERY)),
262 426 : UNO_QUERY);
263 426 : m_aNodeStack.push(Reference< XNode >(aElement, UNO_QUERY));
264 :
265 : // set non xmlns attributes
266 426 : aPrefix = OUString();
267 426 : aURI = OUString();
268 426 : AttrMap::const_iterator a = aAttrMap.begin();
269 1187 : while (a != aAttrMap.end())
270 : {
271 335 : attr_qname = a->first;
272 335 : attr_value = a->second;
273 335 : idx = attr_qname.indexOf(':');
274 335 : if (idx != -1)
275 335 : aPrefix = attr_qname.copy(0, idx);
276 : else
277 0 : aPrefix = OUString();
278 :
279 335 : result = aNSMap.find(aPrefix);
280 335 : if (result != aNSMap.end())
281 : {
282 : // set attribute with namespace
283 335 : 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 335 : ++a;
291 : }
292 426 : m_aNSStack.push(aNSMap);
293 426 : }
294 :
295 426 : void SAL_CALL CSAXDocumentBuilder::endElement(const OUString& aName)
296 : throw (RuntimeException, SAXException)
297 : {
298 426 : ::osl::MutexGuard g(m_Mutex);
299 :
300 : // pop the current element from the stack
301 426 : if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
302 : m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
303 0 : throw SAXException();
304 :
305 426 : Reference< XNode > aNode(m_aNodeStack.top());
306 426 : if (aNode->getNodeType() != NodeType_ELEMENT_NODE)
307 0 : throw SAXException();
308 :
309 426 : Reference< XElement > aElement(aNode, UNO_QUERY);
310 426 : OUString aRefName;
311 426 : OUString aPrefix = aElement->getPrefix();
312 426 : if (!aPrefix.isEmpty())
313 426 : aRefName = aPrefix + ":" + aElement->getTagName();
314 : else
315 0 : aRefName = aElement->getTagName();
316 426 : if (aRefName != aName) // consistency check
317 0 : throw SAXException();
318 :
319 : // pop it
320 426 : m_aNodeStack.pop();
321 426 : m_aNSStack.pop();
322 426 : }
323 :
324 276 : void SAL_CALL CSAXDocumentBuilder::characters(const OUString& aChars)
325 : throw (RuntimeException, SAXException)
326 : {
327 276 : ::osl::MutexGuard g(m_Mutex);
328 :
329 : // append text node to the current top element
330 276 : if (m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
331 : m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
332 0 : throw SAXException();
333 :
334 276 : Reference< XText > aText = m_aDocument->createTextNode(aChars);
335 276 : m_aNodeStack.top()->appendChild(Reference< XNode >(aText, UNO_QUERY));
336 276 : }
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: */
|