bump product version to 4.1.6.2
[LibreOffice.git] / xmlsecurity / source / xmlsec / saxhelper.cxx
blob8a6447dbe92fe348dfb474c5642e63ac6cacce30
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 "saxhelper.hxx"
24 #include "libxml/parserInternals.h"
26 #ifndef XMLSEC_NO_XSLT
27 #include "libxslt/xslt.h"
28 #endif
30 namespace cssu = com::sun::star::uno;
31 namespace cssxs = com::sun::star::xml::sax;
32 namespace cssxcsax = com::sun::star::xml::csax;
34 /**
35 * The return value is NULL terminated. The application has the responsibilty to
36 * deallocte the return value.
38 xmlChar* ous_to_xmlstr( const OUString& oustr )
40 OString ostr = OUStringToOString( oustr , RTL_TEXTENCODING_UTF8 ) ;
41 return xmlStrndup( ( xmlChar* )ostr.getStr(), ( int )ostr.getLength() ) ;
44 /**
45 * The return value is NULL terminated. The application has the responsibilty to
46 * deallocte the return value.
48 xmlChar* ous_to_nxmlstr( const OUString& oustr, int& length )
50 OString ostr = OUStringToOString( oustr , RTL_TEXTENCODING_UTF8 ) ;
51 length = ostr.getLength();
53 return xmlStrndup( ( xmlChar* )ostr.getStr(), length ) ;
56 /**
57 * The return value and the referenced value must be NULL terminated.
58 * The application has the responsibilty to deallocte the return value.
60 const xmlChar** attrlist_to_nxmlstr( const cssu::Sequence< cssxcsax::XMLAttribute >& aAttributes )
62 xmlChar* attname = NULL ;
63 xmlChar* attvalue = NULL ;
64 const xmlChar** attrs = NULL ;
66 sal_Int32 nLength = aAttributes.getLength();
68 if( nLength != 0 )
70 attrs = ( const xmlChar** )xmlMalloc( ( nLength * 2 + 2 ) * sizeof( xmlChar* ) ) ;
72 else
74 return NULL ;
77 for( int i = 0 , j = 0 ; j < nLength ; ++j )
79 attname = ous_to_xmlstr( aAttributes[j].sName ) ;
80 attvalue = ous_to_xmlstr( aAttributes[j].sValue ) ;
82 if( attname != NULL && attvalue != NULL )
84 attrs[i++] = attname ;
85 attrs[i++] = attvalue ;
86 attrs[i] = NULL ;
87 attrs[i+1] = NULL ;
89 else
91 if( attname != NULL )
92 xmlFree( attname ) ;
93 if( attvalue != NULL )
94 xmlFree( attvalue ) ;
98 return attrs ;
102 * Constructor
104 * In this constructor, a libxml sax parser context is initialized. a libxml
105 * default sax handler is initialized with the context.
107 SAXHelper::SAXHelper( )
108 : m_pParserCtxt( NULL ),
109 m_pSaxHandler( NULL )
111 xmlInitParser() ;
112 LIBXML_TEST_VERSION ;
115 * compile error:
116 * xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS ;
118 xmlSubstituteEntitiesDefault( 1 ) ;
120 #ifndef XMLSEC_NO_XSLT
121 xmlIndentTreeOutput = 1 ;
122 #endif /* XMLSEC_NO_XSLT */
124 m_pParserCtxt = xmlNewParserCtxt() ;
126 if( m_pParserCtxt == NULL )
128 #ifndef XMLSEC_NO_XSLT
129 xsltCleanupGlobals() ;
130 #endif
131 // see issue i74334, we cannot call xmlCleanupParser when libxml is still used
132 // in other parts of the office.
133 // xmlCleanupParser() ;
134 throw cssu::RuntimeException() ;
136 else
138 xmlSAXVersion(m_pParserCtxt->sax, 1);
140 if( m_pParserCtxt->inputTab[0] != NULL )
142 m_pParserCtxt->inputTab[0] = NULL ;
145 if( m_pParserCtxt->sax == NULL )
147 xmlFreeParserCtxt( m_pParserCtxt ) ;
149 #ifndef XMLSEC_NO_XSLT
150 xsltCleanupGlobals() ;
151 #endif
152 // see issue i74334, we cannot call xmlCleanupParser when libxml is still used
153 // in other parts of the office.
154 // xmlCleanupParser() ;
155 m_pParserCtxt = NULL ;
156 throw cssu::RuntimeException() ;
158 else
160 m_pSaxHandler = m_pParserCtxt->sax ;
162 //Adjust the context
163 m_pParserCtxt->recovery = 1 ;
169 * Destructor
171 * In this destructor, a libxml sax parser context is desturcted. The XML tree
172 * in the context is not deallocated because the tree is bind with a document
173 * model by the setTargetDocument method, which delegate the target document to
174 * destruct the xml tree.
176 SAXHelper::~SAXHelper() {
177 if( m_pParserCtxt != NULL )
180 * In the situation that no object refer the Document, this destructor
181 * must deallocate the Document memory
183 if( m_pSaxHandler == m_pParserCtxt->sax )
185 m_pSaxHandler = NULL ;
188 xmlFreeParserCtxt( m_pParserCtxt ) ;
189 m_pParserCtxt = NULL ;
192 if( m_pSaxHandler != NULL )
194 xmlFree( m_pSaxHandler ) ;
195 m_pSaxHandler = NULL ;
197 // see issue i74334, we cannot call xmlCleanupParser when libxml is still used
198 // in other parts of the office.
199 // xmlCleanupParser() ;
202 xmlNodePtr SAXHelper::getCurrentNode()
204 return m_pParserCtxt->node;
207 void SAXHelper::setCurrentNode(const xmlNodePtr pNode)
210 * This is really a black trick.
211 * When the current node is replaced, the nodeTab
212 * stack's top has to been replaced with the same
213 * node, in order to make compatibility.
215 m_pParserCtxt->nodeTab[m_pParserCtxt->nodeNr - 1]
216 = m_pParserCtxt->node
217 = pNode;
220 xmlDocPtr SAXHelper::getDocument()
222 return m_pParserCtxt->myDoc;
226 * XDocumentHandler -- start an xml document
228 void SAXHelper::startDocument( void )
229 throw( cssxs::SAXException , cssu::RuntimeException )
231 if( m_pParserCtxt == NULL)
233 throw cssu::RuntimeException() ;
236 * Adjust inputTab
238 xmlParserInputPtr pInput = xmlNewInputStream( m_pParserCtxt ) ;
240 if( m_pParserCtxt->inputTab != NULL && m_pParserCtxt->inputMax != 0 )
242 m_pParserCtxt->inputTab[0] = pInput ;
243 m_pParserCtxt->input = pInput ;
246 m_pSaxHandler->startDocument( m_pParserCtxt ) ;
248 if( m_pParserCtxt->myDoc == NULL )
250 throw cssu::RuntimeException() ;
255 * XDocumentHandler -- end an xml document
257 void SAXHelper::endDocument( void )
258 throw( cssxs::SAXException , cssu::RuntimeException )
260 m_pSaxHandler->endDocument( m_pParserCtxt ) ;
264 * XDocumentHandler -- start an xml element
266 void SAXHelper::startElement(
267 const OUString& aName,
268 const cssu::Sequence< cssxcsax::XMLAttribute >& aAttributes )
269 throw( cssxs::SAXException , cssu::RuntimeException )
271 const xmlChar* fullName = NULL ;
272 const xmlChar** attrs = NULL ;
274 fullName = ous_to_xmlstr( aName ) ;
275 attrs = attrlist_to_nxmlstr( aAttributes ) ;
277 if( fullName != NULL || attrs != NULL )
279 m_pSaxHandler->startElement( m_pParserCtxt , fullName , attrs ) ;
282 if( fullName != NULL )
284 xmlFree( ( xmlChar* )fullName ) ;
285 fullName = NULL ;
288 if( attrs != NULL )
290 for( int i = 0 ; attrs[i] != NULL ; ++i )
292 xmlFree( ( xmlChar* )attrs[i] ) ;
293 attrs[i] = NULL ;
296 xmlFree( ( void* ) attrs ) ;
297 attrs = NULL ;
302 * XDocumentHandler -- end an xml element
304 void SAXHelper::endElement( const OUString& aName )
305 throw( cssxs::SAXException , cssu::RuntimeException )
307 xmlChar* fullname = NULL ;
309 fullname = ous_to_xmlstr( aName ) ;
310 m_pSaxHandler->endElement( m_pParserCtxt , fullname ) ;
312 if( fullname != NULL )
314 xmlFree( ( xmlChar* )fullname ) ;
315 fullname = NULL ;
320 * XDocumentHandler -- an xml element or cdata characters
322 void SAXHelper::characters( const OUString& aChars )
323 throw( cssxs::SAXException , cssu::RuntimeException )
325 const xmlChar* chars = NULL ;
326 int length = 0 ;
328 chars = ous_to_nxmlstr( aChars, length ) ;
329 m_pSaxHandler->characters( m_pParserCtxt , chars , length ) ;
331 if( chars != NULL )
333 xmlFree( ( xmlChar* )chars ) ;
338 * XDocumentHandler -- ignorable xml white space
340 void SAXHelper::ignorableWhitespace( const OUString& aWhitespaces )
341 throw( cssxs::SAXException , cssu::RuntimeException )
343 const xmlChar* chars = NULL ;
344 int length = 0 ;
346 chars = ous_to_nxmlstr( aWhitespaces, length ) ;
347 m_pSaxHandler->ignorableWhitespace( m_pParserCtxt , chars , length ) ;
349 if( chars != NULL )
351 xmlFree( ( xmlChar* )chars ) ;
356 * XDocumentHandler -- preaorocessing instruction
358 void SAXHelper::processingInstruction(
359 const OUString& aTarget,
360 const OUString& aData )
361 throw( cssxs::SAXException , cssu::RuntimeException )
363 xmlChar* target = NULL ;
364 xmlChar* data = NULL ;
366 target = ous_to_xmlstr( aTarget ) ;
367 data = ous_to_xmlstr( aData ) ;
369 m_pSaxHandler->processingInstruction( m_pParserCtxt , target , data ) ;
371 if( target != NULL )
373 xmlFree( ( xmlChar* )target ) ;
374 target = NULL ;
377 if( data != NULL )
379 xmlFree( ( xmlChar* )data ) ;
380 data = NULL ;
385 * XDocumentHandler -- set document locator
386 * In this case, locator is useless.
388 void SAXHelper::setDocumentLocator(
389 const cssu::Reference< cssxs::XLocator > &)
390 throw( cssxs::SAXException , cssu::RuntimeException )
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */