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 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include <rtl/ustring.hxx>
26 #include <xmlsec/saxhelper.hxx>
27 #include <libxml/parserInternals.h>
29 #include <com/sun/star/xml/csax/XMLAttribute.hpp>
30 #include <com/sun/star/uno/Sequence.hxx>
32 #ifndef XMLSEC_NO_XSLT
33 #include "libxslt/xslt.h"
37 * The return value is NULL terminated. The application has the responsibility to
38 * deallocate the return value.
40 static xmlChar
* ous_to_xmlstr( std::u16string_view 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( std::u16string_view 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 css::uno::Sequence
< css::xml::csax::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 SAL_WNODEPRECATED_DECLARATIONS_PUSH
122 xmlSubstituteEntitiesDefault(0) ;
123 #ifndef XMLSEC_NO_XSLT
124 xmlIndentTreeOutput
= 1 ;
125 #endif /* XMLSEC_NO_XSLT */
126 SAL_WNODEPRECATED_DECLARATIONS_POP
128 m_pParserCtxt
= xmlNewParserCtxt() ;
130 if( m_pParserCtxt
== nullptr )
132 // see issue i74334, we cannot call xmlCleanupParser when libxml is still used
133 // in other parts of the office.
134 // xmlCleanupParser() ;
135 // and neither can we call xsltCleanupGlobals()
136 throw css::uno::RuntimeException() ;
139 xmlSAXVersion(m_pParserCtxt
->sax
, 1);
141 if (m_pParserCtxt
->inputTab
!= nullptr)
143 m_pParserCtxt
->inputTab
[0] = nullptr ;
146 if( m_pParserCtxt
->sax
== nullptr )
148 xmlFreeParserCtxt( m_pParserCtxt
) ;
150 // see issue i74334, we cannot call xmlCleanupParser when libxml is still used
151 // in other parts of the office.
152 // xmlCleanupParser() ;
153 // and neither can we call xsltCleanupGlobals()
154 m_pParserCtxt
= nullptr ;
155 throw css::uno::RuntimeException() ;
159 m_pSaxHandler
= m_pParserCtxt
->sax
;
162 m_pParserCtxt
->recovery
= 1 ;
169 * In this destructor, a libxml sax parser context is destructed. The XML tree
170 * in the context is not deallocated because the tree is bind with a document
171 * model by the setTargetDocument method, which delegate the target document to
172 * destruct the xml tree.
174 SAXHelper::~SAXHelper() {
175 if( m_pParserCtxt
!= nullptr )
178 * In the situation that no object refer the Document, this destructor
179 * must deallocate the Document memory
181 if( m_pSaxHandler
== m_pParserCtxt
->sax
)
183 m_pSaxHandler
= nullptr ;
186 xmlFreeParserCtxt( m_pParserCtxt
) ;
187 m_pParserCtxt
= nullptr ;
190 if( m_pSaxHandler
!= nullptr )
192 xmlFree( m_pSaxHandler
) ;
193 m_pSaxHandler
= nullptr ;
195 // see issue i74334, we cannot call xmlCleanupParser when libxml is still used
196 // in other parts of the office.
197 // xmlCleanupParser() ;
201 void SAXHelper::setCurrentNode(const xmlNodePtr pNode
)
204 * This is really a black trick.
205 * When the current node is replaced, the nodeTab
206 * stack's top has to been replaced with the same
207 * node, in order to make compatibility.
209 m_pParserCtxt
->nodeTab
[m_pParserCtxt
->nodeNr
- 1]
210 = m_pParserCtxt
->node
216 * XDocumentHandler -- start an xml document
218 void SAXHelper::startDocument()
220 if( m_pParserCtxt
== nullptr)
222 throw css::uno::RuntimeException() ;
227 xmlParserInputPtr pInput
= xmlNewInputStream( m_pParserCtxt
) ;
229 if( m_pParserCtxt
->inputTab
!= nullptr && m_pParserCtxt
->inputMax
!= 0 )
231 m_pParserCtxt
->inputTab
[0] = pInput
;
232 m_pParserCtxt
->input
= pInput
;
235 m_pSaxHandler
->startDocument( m_pParserCtxt
) ;
237 if( m_pParserCtxt
->myDoc
== nullptr )
239 throw css::uno::RuntimeException() ;
244 * XDocumentHandler -- end an xml document
246 void SAXHelper::endDocument()
248 m_pSaxHandler
->endDocument( m_pParserCtxt
) ;
252 * XDocumentHandler -- start an xml element
254 void SAXHelper::startElement(
255 std::u16string_view aName
,
256 const css::uno::Sequence
< css::xml::csax::XMLAttribute
>& aAttributes
)
258 const xmlChar
* fullName
= nullptr ;
259 const xmlChar
** attrs
= nullptr ;
261 fullName
= ous_to_xmlstr( aName
) ;
262 attrs
= attrlist_to_nxmlstr( aAttributes
) ;
264 if( fullName
!= nullptr || attrs
!= nullptr )
266 m_pSaxHandler
->startElement( m_pParserCtxt
, fullName
, attrs
) ;
269 if( fullName
!= nullptr )
271 xmlFree( const_cast<xmlChar
*>(fullName
) ) ;
275 if( attrs
!= nullptr )
277 for( int i
= 0 ; attrs
[i
] != nullptr ; ++i
)
279 xmlFree( const_cast<xmlChar
*>(attrs
[i
]) ) ;
283 xmlFree( static_cast<void*>(attrs
) ) ;
289 * XDocumentHandler -- end an xml element
291 void SAXHelper::endElement( std::u16string_view aName
)
293 xmlChar
* fullname
= ous_to_xmlstr( aName
) ;
294 m_pSaxHandler
->endElement( m_pParserCtxt
, fullname
) ;
296 if( fullname
!= nullptr )
298 xmlFree( fullname
) ;
304 * XDocumentHandler -- an xml element or cdata characters
306 void SAXHelper::characters( std::u16string_view aChars
)
308 const xmlChar
* chars
= nullptr ;
311 chars
= ous_to_nxmlstr( aChars
, length
) ;
312 m_pSaxHandler
->characters( m_pParserCtxt
, chars
, length
) ;
314 if( chars
!= nullptr )
316 xmlFree( const_cast<xmlChar
*>(chars
) ) ;
321 * XDocumentHandler -- ignorable xml white space
323 void SAXHelper::ignorableWhitespace( std::u16string_view aWhitespaces
)
325 const xmlChar
* chars
= nullptr ;
328 chars
= ous_to_nxmlstr( aWhitespaces
, length
) ;
329 m_pSaxHandler
->ignorableWhitespace( m_pParserCtxt
, chars
, length
) ;
331 if( chars
!= nullptr )
333 xmlFree( const_cast<xmlChar
*>(chars
) ) ;
338 * XDocumentHandler -- preprocessing instruction
340 void SAXHelper::processingInstruction(
341 std::u16string_view aTarget
,
342 std::u16string_view aData
)
344 xmlChar
* target
= nullptr ;
345 xmlChar
* data
= nullptr ;
347 target
= ous_to_xmlstr( aTarget
) ;
348 data
= ous_to_xmlstr( aData
) ;
350 m_pSaxHandler
->processingInstruction( m_pParserCtxt
, target
, data
) ;
352 if( target
!= nullptr )
358 if( data
!= nullptr )
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */