LibreOffice Module unoxml (master) 1
saxbuilder.cxx
Go to the documentation of this file.
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
20#include "saxbuilder.hxx"
21
22#include <com/sun/star/xml/dom/DocumentBuilder.hpp>
23#include <com/sun/star/xml/sax/SAXException.hpp>
25#include <sax/fastattribs.hxx>
26#include <xmloff/xmlimp.hxx>
27
28using namespace css::lang;
29using namespace css::uno;
30using namespace css::xml::dom;
31using namespace css::xml::sax;
32
33namespace DOM
34{
35 CSAXDocumentBuilder::CSAXDocumentBuilder(const Reference< XComponentContext >& ctx)
37 , m_aState( SAXDocumentBuilderState_READY)
38 {}
39
40 Sequence< OUString > SAL_CALL CSAXDocumentBuilder::getSupportedServiceNames()
41 {
42 return { "com.sun.star.xml.dom.SAXDocumentBuilder" };
43 }
44
46 {
47 return "com.sun.star.comp.xml.dom.SAXDocumentBuilder";
48 }
49
50 sal_Bool SAL_CALL CSAXDocumentBuilder::supportsService(const OUString& aServiceName)
51 {
52 return cppu::supportsService(this, aServiceName);
53 }
54
55 SAXDocumentBuilderState SAL_CALL CSAXDocumentBuilder::getState()
56 {
57 std::scoped_lock g(m_Mutex);
58
59 return m_aState;
60 }
61
63 {
64 std::scoped_lock g(m_Mutex);
65
66 m_aDocument.clear();
67 m_aFragment.clear();
68 while (!m_aNodeStack.empty()) m_aNodeStack.pop();
69 m_aState = SAXDocumentBuilderState_READY;
70 }
71
72 Reference< XDocument > SAL_CALL CSAXDocumentBuilder::getDocument()
73 {
74 std::scoped_lock g(m_Mutex);
75
76 if (m_aState != SAXDocumentBuilderState_DOCUMENT_FINISHED)
77 throw RuntimeException();
78
79 return m_aDocument;
80 }
81
82 Reference< XDocumentFragment > SAL_CALL CSAXDocumentBuilder::getDocumentFragment()
83 {
84 std::scoped_lock g(m_Mutex);
85
86 if (m_aState != SAXDocumentBuilderState_FRAGMENT_FINISHED)
87 throw RuntimeException();
88 return m_aFragment;
89 }
90
91 void SAL_CALL CSAXDocumentBuilder::startDocumentFragment(const Reference< XDocument >& ownerDoc)
92 {
93 std::scoped_lock g(m_Mutex);
94
95 // start a new document fragment and push it onto the stack
96 // we have to be in a clean state to do this
97 if (m_aState != SAXDocumentBuilderState_READY)
98 throw RuntimeException();
99
100 m_aDocument = ownerDoc;
101 Reference< XDocumentFragment > aFragment = m_aDocument->createDocumentFragment();
102 m_aNodeStack.push(aFragment);
103 m_aFragment = aFragment;
104 m_aState = SAXDocumentBuilderState_BUILDING_FRAGMENT;
105 }
106
108 {
109 std::scoped_lock g(m_Mutex);
110
111 // there should only be the document left on the node stack
112 if (m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
113 throw RuntimeException();
114
115 Reference< XNode > aNode = m_aNodeStack.top();
116 if ( aNode->getNodeType() != NodeType_DOCUMENT_FRAGMENT_NODE)
117 throw RuntimeException();
118 m_aNodeStack.pop();
119 m_aState = SAXDocumentBuilderState_FRAGMENT_FINISHED;
120 }
121
122 //XFastDocumentHandler
124 {
125 std::scoped_lock g(m_Mutex);
126
127 // start a new document and push it onto the stack
128 // we have to be in a clean state to do this
129 if (m_aState != SAXDocumentBuilderState_READY)
130 throw SAXException();
131
132 Reference< XDocumentBuilder > aBuilder(DocumentBuilder::create(m_xContext));
133 Reference< XDocument > aDocument = aBuilder->newDocument();
136 m_aState = SAXDocumentBuilderState_BUILDING_DOCUMENT;
137 }
138
140 {
141 std::scoped_lock g(m_Mutex);
142
143 // there should only be the document left on the node stack
144 if (m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT)
145 throw SAXException();
146
147 Reference< XNode > aNode = m_aNodeStack.top();
148 if ( aNode->getNodeType() != NodeType_DOCUMENT_NODE)
149 throw SAXException();
150 m_aNodeStack.pop();
151 m_aState = SAXDocumentBuilderState_DOCUMENT_FINISHED;
152 }
153
154 void SAL_CALL CSAXDocumentBuilder::processingInstruction( const OUString& rTarget, const OUString& rData )
155 {
156 std::scoped_lock g(m_Mutex);
157
158 // append PI node to the current top
159 if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
160 m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
161 throw SAXException();
162
163 Reference< XProcessingInstruction > aInstruction = m_aDocument->createProcessingInstruction(
164 rTarget, rData);
165 m_aNodeStack.top()->appendChild(aInstruction);
166 }
167
168 void SAL_CALL CSAXDocumentBuilder::setDocumentLocator( const Reference< XLocator >& )
169 {
170 }
171
172 void SAL_CALL CSAXDocumentBuilder::startFastElement( sal_Int32 nElement , const Reference< XFastAttributeList >& xAttribs )
173 {
174 std::scoped_lock g(m_Mutex);
175
176 if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
177 m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
178 {
179 throw SAXException();
180 }
181
182 Reference< XElement > aElement;
183 const OUString& aPrefix(SvXMLImport::getNamespacePrefixFromToken(nElement, nullptr));
184 const OUString& aURI( SvXMLImport::getNamespaceURIFromToken( nElement ) );
185 OUString aQualifiedName( SvXMLImport::getNameFromToken( nElement ) );
186 if( !aPrefix.isEmpty() )
187 aQualifiedName = aPrefix + SvXMLImport::aNamespaceSeparator + aQualifiedName;
188
189 if ( !aURI.isEmpty() )
190 {
191 // found a URI for prefix
192 // qualified name
193 aElement = m_aDocument->createElementNS( aURI, aQualifiedName );
194 }
195 else
196 {
197 // no URI for prefix
198 aElement = m_aDocument->createElement( aQualifiedName );
199 }
200 aElement.set( m_aNodeStack.top()->appendChild(aElement), UNO_QUERY);
201 m_aNodeStack.push(aElement);
202
203 if (xAttribs.is())
204 setElementFastAttributes(aElement, xAttribs);
205 }
206
207 // For arbitrary meta elements
208 void SAL_CALL CSAXDocumentBuilder::startUnknownElement( const OUString& rNamespace, const OUString& rName, const Reference< XFastAttributeList >& xAttribs )
209 {
210 std::scoped_lock g(m_Mutex);
211
212 if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
213 m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
214 {
215 throw SAXException();
216 }
217
218 Reference< XElement > aElement;
219 if ( !rNamespace.isEmpty() )
220 aElement = m_aDocument->createElementNS( rNamespace, rName );
221 else
222 aElement = m_aDocument->createElement( rName );
223
224 aElement.set( m_aNodeStack.top()->appendChild(aElement), UNO_QUERY);
225 m_aNodeStack.push(aElement);
226
227 if (!xAttribs.is())
228 return;
229
230 setElementFastAttributes(aElement, xAttribs);
231 const Sequence< css::xml::Attribute > unknownAttribs = xAttribs->getUnknownAttributes();
232 for ( const auto& rUnknownAttrib : unknownAttribs )
233 {
234 const OUString& rAttrValue = rUnknownAttrib.Value;
235 const OUString& rAttrName = rUnknownAttrib.Name;
236 const OUString& rAttrNamespace = rUnknownAttrib.NamespaceURL;
237 if ( !rAttrNamespace.isEmpty() )
238 aElement->setAttributeNS( rAttrNamespace, rAttrName, rAttrValue );
239 else
240 aElement->setAttribute( rAttrName, rAttrValue );
241 }
242 }
243
244 void CSAXDocumentBuilder::setElementFastAttributes(const Reference< XElement >& aElement, const Reference< XFastAttributeList >& xAttribs)
245 {
246 for (auto &it : sax_fastparser::castToFastAttributeList( xAttribs ))
247 {
248 sal_Int32 nAttrToken = it.getToken();
249 const OUString& aAttrPrefix(SvXMLImport::getNamespacePrefixFromToken(nAttrToken, nullptr));
250 const OUString& aAttrURI( SvXMLImport::getNamespaceURIFromToken( nAttrToken ) );
251 OUString aAttrQualifiedName( SvXMLImport::getNameFromToken( nAttrToken ) );
252 if( !aAttrPrefix.isEmpty() )
253 aAttrQualifiedName = aAttrPrefix + SvXMLImport::aNamespaceSeparator + aAttrQualifiedName;
254
255 if ( !aAttrURI.isEmpty() )
256 aElement->setAttributeNS( aAttrURI, aAttrQualifiedName, it.toString() );
257 else
258 aElement->setAttribute( aAttrQualifiedName, it.toString() );
259 }
260 }
261
262 void SAL_CALL CSAXDocumentBuilder::endFastElement( sal_Int32 nElement )
263 {
264 std::scoped_lock g(m_Mutex);
265
266 // pop the current element from the stack
267 if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
268 m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
269 throw SAXException();
270
271 Reference< XNode > aNode(m_aNodeStack.top());
272 if (aNode->getNodeType() != NodeType_ELEMENT_NODE)
273 throw SAXException();
274
275 Reference< XElement > aElement(aNode, UNO_QUERY);
276 if( aElement->getPrefix() != SvXMLImport::getNamespacePrefixFromToken(nElement, nullptr) ||
277 aElement->getTagName() != SvXMLImport::getNameFromToken( nElement ) ) // consistency check
278 throw SAXException();
279
280 // pop it
281 m_aNodeStack.pop();
282 }
283
284
285 void SAL_CALL CSAXDocumentBuilder::endUnknownElement( const OUString& /*rNamespace*/, const OUString& rName )
286 {
287 std::scoped_lock g(m_Mutex);
288
289 // pop the current element from the stack
290 if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
291 m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
292 throw SAXException();
293
294 Reference< XNode > aNode(m_aNodeStack.top());
295 if (aNode->getNodeType() != NodeType_ELEMENT_NODE)
296 throw SAXException();
297
298 Reference< XElement > aElement(aNode, UNO_QUERY);
299 OUString aRefName;
300 const OUString& aPrefix = aElement->getPrefix();
301 if (!aPrefix.isEmpty())
302 aRefName = aPrefix + SvXMLImport::aNamespaceSeparator + aElement->getTagName();
303 else
304 aRefName = aElement->getTagName();
305 if (aRefName != rName) // consistency check
306 throw SAXException();
307
308 // pop it
309 m_aNodeStack.pop();
310 }
311
312 Reference< XFastContextHandler > SAL_CALL CSAXDocumentBuilder::createFastChildContext( sal_Int32/* nElement */, const Reference< XFastAttributeList >&/* xAttribs */ )
313 {
314 return nullptr;
315 }
316
317
318 Reference< XFastContextHandler > SAL_CALL CSAXDocumentBuilder::createUnknownChildContext( const OUString&/* rNamespace */, const OUString&/* rName */, const Reference< XFastAttributeList >&/* xAttribs */ )
319 {
320 return nullptr;
321 }
322
323 void SAL_CALL CSAXDocumentBuilder::characters( const OUString& rChars )
324 {
325 std::scoped_lock g(m_Mutex);
326
327 // append text node to the current top element
328 if (m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
329 m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
330 throw SAXException();
331
332 Reference< XText > aText = m_aDocument->createTextNode(rChars);
333 m_aNodeStack.top()->appendChild(aText);
334 }
335}
336
337extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
339 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
340{
341 return cppu::acquire(new DOM::CSAXDocumentBuilder(context));
342}
343
344/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
ScriptDocument aDocument
virtual void SAL_CALL endDocument() override
Definition: saxbuilder.cxx:139
virtual void SAL_CALL startDocumentFragment(const css::uno::Reference< css::xml::dom::XDocument > &ownerDoc) override
Definition: saxbuilder.cxx:91
virtual void SAL_CALL startUnknownElement(const OUString &Namespace, const OUString &Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: saxbuilder.cxx:208
css::uno::Reference< css::xml::dom::XDocumentFragment > m_aFragment
Definition: saxbuilder.hxx:52
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: saxbuilder.cxx:172
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: saxbuilder.cxx:50
css::uno::Reference< css::xml::dom::XDocument > m_aDocument
Definition: saxbuilder.hxx:51
virtual void SAL_CALL setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator > &xLocator) override
Definition: saxbuilder.cxx:168
virtual void SAL_CALL endDocumentFragment() override
Definition: saxbuilder.cxx:107
virtual void SAL_CALL reset() override
Definition: saxbuilder.cxx:62
CSAXDocumentBuilder(const css::uno::Reference< css::uno::XComponentContext > &)
Definition: saxbuilder.cxx:35
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: saxbuilder.cxx:312
virtual css::uno::Reference< css::xml::dom::XDocument > SAL_CALL getDocument() override
Definition: saxbuilder.cxx:72
virtual void SAL_CALL startDocument() override
Definition: saxbuilder.cxx:123
virtual void SAL_CALL processingInstruction(const OUString &rTarget, const OUString &rData) override
Definition: saxbuilder.cxx:154
virtual void SAL_CALL endUnknownElement(const OUString &Namespace, const OUString &Name) override
Definition: saxbuilder.cxx:285
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createUnknownChildContext(const OUString &Namespace, const OUString &Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: saxbuilder.cxx:318
virtual void SAL_CALL characters(const OUString &aChars) override
Definition: saxbuilder.cxx:323
virtual css::xml::dom::SAXDocumentBuilderState SAL_CALL getState() override
Definition: saxbuilder.cxx:55
const css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: saxbuilder.hxx:46
std::stack< css::uno::Reference< css::xml::dom::XNode > > m_aNodeStack
Definition: saxbuilder.hxx:49
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: saxbuilder.cxx:40
css::xml::dom::SAXDocumentBuilderState m_aState
Definition: saxbuilder.hxx:48
virtual css::uno::Reference< css::xml::dom::XDocumentFragment > SAL_CALL getDocumentFragment() override
Definition: saxbuilder.cxx:82
static void setElementFastAttributes(const css::uno::Reference< css::xml::dom::XElement > &aElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttribs)
Definition: saxbuilder.cxx:244
virtual OUString SAL_CALL getImplementationName() override
Definition: saxbuilder.cxx:45
virtual void SAL_CALL endFastElement(sal_Int32 Element) override
Definition: saxbuilder.cxx:262
FilterGroup & rTarget
Definition: attr.cxx:38
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
ctx
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * unoxml_CSAXDocumentBuilder_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: saxbuilder.cxx:338
unsigned char sal_Bool