1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <rtl/ustring.hxx>
23 #include <xmlsec/saxhelper.hxx>
24 #include <libxml/parserInternals.h>
26 #include <com/sun/star/xml/csax/XMLAttribute.hpp>
27 #include <com/sun/star/uno/Sequence.hxx>
29 #ifndef XMLSEC_NO_XSLT
30 #include "libxslt/xslt.h"
33 namespace cssu
= com::sun::star::uno
;
34 namespace cssxcsax
= com::sun::star::xml::csax
;
37 * The return value is NULL terminated. The application has the responsibility to
38 * deallocate the return value.
40 static xmlChar
* ous_to_xmlstr( const OUString
& oustr
)
42 OString ostr
= OUStringToOString( oustr
, RTL_TEXTENCODING_UTF8
) ;
43 return xmlStrndup( reinterpret_cast<xmlChar
const *>(ostr
.getStr()), static_cast<int>(ostr
.getLength()) ) ;
47 * The return value is NULL terminated. The application has the responsibility to
48 * deallocate the return value.
50 static xmlChar
* ous_to_nxmlstr( const OUString
& oustr
, int& length
)
52 OString ostr
= OUStringToOString( oustr
, RTL_TEXTENCODING_UTF8
) ;
53 length
= ostr
.getLength();
55 return xmlStrndup( reinterpret_cast<xmlChar
const *>(ostr
.getStr()), length
) ;
59 * The return value and the referenced value must be NULL terminated.
60 * The application has the responsibility to deallocate the return value.
62 static const xmlChar
** attrlist_to_nxmlstr( const cssu::Sequence
< cssxcsax::XMLAttribute
>& aAttributes
)
64 xmlChar
* attname
= nullptr ;
65 xmlChar
* attvalue
= nullptr ;
66 const xmlChar
** attrs
= nullptr ;
68 sal_Int32 nLength
= aAttributes
.getLength();
72 attrs
= static_cast<const xmlChar
**>(xmlMalloc( ( nLength
* 2 + 2 ) * sizeof( xmlChar
* ) ));
80 for( const auto& rAttr
: aAttributes
)
82 attname
= ous_to_xmlstr( rAttr
.sName
) ;
83 attvalue
= ous_to_xmlstr( rAttr
.sValue
) ;
85 if( attname
!= nullptr && attvalue
!= nullptr )
87 attrs
[i
++] = attname
;
88 attrs
[i
++] = attvalue
;
90 attrs
[i
+1] = nullptr ;
94 if( attname
!= nullptr )
96 if( attvalue
!= nullptr )
107 * In this constructor, a libxml sax parser context is initialized. a libxml
108 * default sax handler is initialized with the context.
110 SAXHelper::SAXHelper( )
111 : m_pParserCtxt( nullptr ),
112 m_pSaxHandler( nullptr )
115 LIBXML_TEST_VERSION
;
119 * xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS ;
121 xmlSubstituteEntitiesDefault(0) ;
123 #ifndef XMLSEC_NO_XSLT
124 xmlIndentTreeOutput
= 1 ;
125 #endif /* XMLSEC_NO_XSLT */
127 m_pParserCtxt
= xmlNewParserCtxt() ;
129 if( m_pParserCtxt
== nullptr )
131 // see issue i74334, we cannot call xmlCleanupParser when libxml is still used
132 // in other parts of the office.
133 // xmlCleanupParser() ;
134 // and neither can we call xsltCleanupGlobals()
135 throw cssu::RuntimeException() ;
138 xmlSAXVersion(m_pParserCtxt
->sax
, 1);
140 if (m_pParserCtxt
->inputTab
!= nullptr)
142 m_pParserCtxt
->inputTab
[0] = nullptr ;
145 if( m_pParserCtxt
->sax
== nullptr )
147 xmlFreeParserCtxt( m_pParserCtxt
) ;
149 // see issue i74334, we cannot call xmlCleanupParser when libxml is still used
150 // in other parts of the office.
151 // xmlCleanupParser() ;
152 // and neither can we call xsltCleanupGlobals()
153 m_pParserCtxt
= nullptr ;
154 throw cssu::RuntimeException() ;
158 m_pSaxHandler
= m_pParserCtxt
->sax
;
161 m_pParserCtxt
->recovery
= 1 ;
168 * In this destructor, a libxml sax parser context is destructed. The XML tree
169 * in the context is not deallocated because the tree is bind with a document
170 * model by the setTargetDocument method, which delegate the target document to
171 * destruct the xml tree.
173 SAXHelper::~SAXHelper() {
174 if( m_pParserCtxt
!= nullptr )
177 * In the situation that no object refer the Document, this destructor
178 * must deallocate the Document memory
180 if( m_pSaxHandler
== m_pParserCtxt
->sax
)
182 m_pSaxHandler
= nullptr ;
185 xmlFreeParserCtxt( m_pParserCtxt
) ;
186 m_pParserCtxt
= nullptr ;
189 if( m_pSaxHandler
!= nullptr )
191 xmlFree( m_pSaxHandler
) ;
192 m_pSaxHandler
= nullptr ;
194 // see issue i74334, we cannot call xmlCleanupParser when libxml is still used
195 // in other parts of the office.
196 // xmlCleanupParser() ;
200 void SAXHelper::setCurrentNode(const xmlNodePtr pNode
)
203 * This is really a black trick.
204 * When the current node is replaced, the nodeTab
205 * stack's top has to been replaced with the same
206 * node, in order to make compatibility.
208 m_pParserCtxt
->nodeTab
[m_pParserCtxt
->nodeNr
- 1]
209 = m_pParserCtxt
->node
215 * XDocumentHandler -- start an xml document
217 void SAXHelper::startDocument()
219 if( m_pParserCtxt
== nullptr)
221 throw cssu::RuntimeException() ;
226 xmlParserInputPtr pInput
= xmlNewInputStream( m_pParserCtxt
) ;
228 if( m_pParserCtxt
->inputTab
!= nullptr && m_pParserCtxt
->inputMax
!= 0 )
230 m_pParserCtxt
->inputTab
[0] = pInput
;
231 m_pParserCtxt
->input
= pInput
;
234 m_pSaxHandler
->startDocument( m_pParserCtxt
) ;
236 if( m_pParserCtxt
->myDoc
== nullptr )
238 throw cssu::RuntimeException() ;
243 * XDocumentHandler -- end an xml document
245 void SAXHelper::endDocument()
247 m_pSaxHandler
->endDocument( m_pParserCtxt
) ;
251 * XDocumentHandler -- start an xml element
253 void SAXHelper::startElement(
254 const OUString
& aName
,
255 const cssu::Sequence
< cssxcsax::XMLAttribute
>& aAttributes
)
257 const xmlChar
* fullName
= nullptr ;
258 const xmlChar
** attrs
= nullptr ;
260 fullName
= ous_to_xmlstr( aName
) ;
261 attrs
= attrlist_to_nxmlstr( aAttributes
) ;
263 if( fullName
!= nullptr || attrs
!= nullptr )
265 m_pSaxHandler
->startElement( m_pParserCtxt
, fullName
, attrs
) ;
268 if( fullName
!= nullptr )
270 xmlFree( const_cast<xmlChar
*>(fullName
) ) ;
274 if( attrs
!= nullptr )
276 for( int i
= 0 ; attrs
[i
] != nullptr ; ++i
)
278 xmlFree( const_cast<xmlChar
*>(attrs
[i
]) ) ;
282 xmlFree( static_cast<void*>(attrs
) ) ;
288 * XDocumentHandler -- end an xml element
290 void SAXHelper::endElement( const OUString
& aName
)
292 xmlChar
* fullname
= ous_to_xmlstr( aName
) ;
293 m_pSaxHandler
->endElement( m_pParserCtxt
, fullname
) ;
295 if( fullname
!= nullptr )
297 xmlFree( fullname
) ;
303 * XDocumentHandler -- an xml element or cdata characters
305 void SAXHelper::characters( const OUString
& aChars
)
307 const xmlChar
* chars
= nullptr ;
310 chars
= ous_to_nxmlstr( aChars
, length
) ;
311 m_pSaxHandler
->characters( m_pParserCtxt
, chars
, length
) ;
313 if( chars
!= nullptr )
315 xmlFree( const_cast<xmlChar
*>(chars
) ) ;
320 * XDocumentHandler -- ignorable xml white space
322 void SAXHelper::ignorableWhitespace( const OUString
& aWhitespaces
)
324 const xmlChar
* chars
= nullptr ;
327 chars
= ous_to_nxmlstr( aWhitespaces
, length
) ;
328 m_pSaxHandler
->ignorableWhitespace( m_pParserCtxt
, chars
, length
) ;
330 if( chars
!= nullptr )
332 xmlFree( const_cast<xmlChar
*>(chars
) ) ;
337 * XDocumentHandler -- preprocessing instruction
339 void SAXHelper::processingInstruction(
340 const OUString
& aTarget
,
341 const OUString
& aData
)
343 xmlChar
* target
= nullptr ;
344 xmlChar
* data
= nullptr ;
346 target
= ous_to_xmlstr( aTarget
) ;
347 data
= ous_to_xmlstr( aData
) ;
349 m_pSaxHandler
->processingInstruction( m_pParserCtxt
, target
, data
) ;
351 if( target
!= nullptr )
357 if( data
!= nullptr )
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */