Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / xmlsecurity / source / xmlsec / saxhelper.cxx
blob1410eb5b0874d5ed75e4bc8a4dbbf074d12c605b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
31 #endif
33 namespace cssu = com::sun::star::uno;
34 namespace cssxcsax = com::sun::star::xml::csax;
36 /**
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()) ) ;
46 /**
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 ) ;
58 /**
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();
70 if( nLength != 0 )
72 attrs = static_cast<const xmlChar**>(xmlMalloc( ( nLength * 2 + 2 ) * sizeof( xmlChar* ) ));
74 else
76 return nullptr ;
79 int i = 0;
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 ;
89 attrs[i] = nullptr ;
90 attrs[i+1] = nullptr ;
92 else
94 if( attname != nullptr )
95 xmlFree( attname ) ;
96 if( attvalue != nullptr )
97 xmlFree( attvalue ) ;
101 return attrs ;
105 * Constructor
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 )
114 xmlInitParser() ;
115 LIBXML_TEST_VERSION ;
118 * compile error:
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() ;
156 else
158 m_pSaxHandler = m_pParserCtxt->sax ;
160 //Adjust the context
161 m_pParserCtxt->recovery = 1 ;
166 * Destructor
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
210 = pNode;
215 * XDocumentHandler -- start an xml document
217 void SAXHelper::startDocument()
219 if( m_pParserCtxt == nullptr)
221 throw cssu::RuntimeException() ;
224 * Adjust inputTab
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) ) ;
271 fullName = nullptr ;
274 if( attrs != nullptr )
276 for( int i = 0 ; attrs[i] != nullptr ; ++i )
278 xmlFree( const_cast<xmlChar*>(attrs[i]) ) ;
279 attrs[i] = nullptr ;
282 xmlFree( static_cast<void*>(attrs) ) ;
283 attrs = nullptr ;
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 ) ;
298 fullname = nullptr ;
303 * XDocumentHandler -- an xml element or cdata characters
305 void SAXHelper::characters( const OUString& aChars )
307 const xmlChar* chars = nullptr ;
308 int length = 0 ;
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 ;
325 int length = 0 ;
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 )
353 xmlFree( target ) ;
354 target = nullptr ;
357 if( data != nullptr )
359 xmlFree( data ) ;
360 data = nullptr ;
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */