Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sax / source / fastparser / legacyfastparser.cxx
blob62e7b8a57a88cca3a2f5db73ceb9539169137169
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 .
20 #include <com/sun/star/lang/XServiceInfo.hpp>
21 #include <com/sun/star/xml/sax/XParser.hpp>
22 #include <com/sun/star/xml/sax/FastParser.hpp>
23 #include <com/sun/star/lang/XInitialization.hpp>
24 #include <com/sun/star/beans/Pair.hpp>
25 #include <comphelper/attributelist.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <rtl/ref.hxx>
29 #include <memory>
30 #include <utility>
31 #include <vector>
33 using namespace ::cppu;
34 using namespace css;
35 using namespace uno;
36 using namespace lang;
37 using namespace xml::sax;
38 using namespace io;
40 namespace {
42 class NamespaceHandler : public WeakImplHelper< XFastNamespaceHandler >
44 private:
45 struct NamespaceDefine
47 OUString m_aPrefix;
48 OUString m_aNamespaceURI;
50 NamespaceDefine( OUString aPrefix, OUString aNamespaceURI ) : m_aPrefix(std::move( aPrefix )), m_aNamespaceURI(std::move( aNamespaceURI )) {}
52 std::vector< std::unique_ptr< NamespaceDefine > > m_aNamespaceDefines;
54 public:
55 NamespaceHandler();
56 void addNSDeclAttributes( rtl::Reference < comphelper::AttributeList > const & rAttrList );
58 //XFastNamespaceHandler
59 virtual void SAL_CALL registerNamespace( const OUString& rNamespacePrefix, const OUString& rNamespaceURI ) override;
60 virtual OUString SAL_CALL getNamespaceURI( const OUString& rNamespacePrefix ) override;
63 NamespaceHandler::NamespaceHandler()
67 void NamespaceHandler::addNSDeclAttributes( rtl::Reference < comphelper::AttributeList > const & rAttrList )
69 for(const auto& aNamespaceDefine : m_aNamespaceDefines)
71 OUString& rPrefix = aNamespaceDefine->m_aPrefix;
72 OUString& rNamespaceURI = aNamespaceDefine->m_aNamespaceURI;
73 OUString sDecl;
74 if ( rPrefix.isEmpty() )
75 sDecl = "xmlns";
76 else
77 sDecl = "xmlns:" + rPrefix;
78 rAttrList->AddAttribute( sDecl, rNamespaceURI );
80 m_aNamespaceDefines.clear();
83 void NamespaceHandler::registerNamespace( const OUString& rNamespacePrefix, const OUString& rNamespaceURI )
85 m_aNamespaceDefines.push_back( std::make_unique<NamespaceDefine>(
86 rNamespacePrefix, rNamespaceURI) );
89 OUString NamespaceHandler::getNamespaceURI( const OUString&/* rNamespacePrefix */ )
91 return OUString();
94 class SaxLegacyFastParser : public WeakImplHelper< XInitialization, XServiceInfo, XParser >
96 private:
97 rtl::Reference< NamespaceHandler > m_aNamespaceHandler;
98 public:
99 SaxLegacyFastParser();
101 // css::lang::XInitialization:
102 virtual void SAL_CALL initialize(css::uno::Sequence<css::uno::Any> const& rArguments) override;
104 // The SAX-Parser-Interface
105 virtual void SAL_CALL parseStream( const InputSource& structSource) override;
106 virtual void SAL_CALL setDocumentHandler(const Reference< XDocumentHandler > & xHandler) override;
107 virtual void SAL_CALL setErrorHandler(const Reference< XErrorHandler > & xHandler) override;
108 virtual void SAL_CALL setDTDHandler(const Reference < XDTDHandler > & xHandler) override;
109 virtual void SAL_CALL setEntityResolver(const Reference< XEntityResolver >& xResolver) override;
110 virtual void SAL_CALL setLocale( const Locale &locale ) override;
112 // XServiceInfo
113 OUString SAL_CALL getImplementationName() override;
114 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
115 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
117 private:
118 Reference< XFastParser > m_xParser;
119 Reference< XDocumentHandler > m_xDocumentHandler;
120 Reference< XFastTokenHandler > m_xTokenHandler;
125 class CallbackDocumentHandler : public WeakImplHelper< XFastDocumentHandler >
127 private:
128 Reference< XDocumentHandler > m_xDocumentHandler;
129 Reference< XFastTokenHandler > m_xTokenHandler;
130 rtl::Reference< NamespaceHandler > m_aNamespaceHandler;
131 OUString getNamespacePrefixFromToken( sal_Int32 nToken );
132 OUString getNameFromToken( sal_Int32 nToken );
134 static constexpr OUStringLiteral aDefaultNamespace = u"";
135 static constexpr OUStringLiteral aNamespaceSeparator = u":";
137 public:
138 CallbackDocumentHandler( Reference< XDocumentHandler > const & xDocumentHandler,
139 rtl::Reference< NamespaceHandler > const & rNamespaceHandler,
140 Reference< XFastTokenHandler > const & xTokenHandler);
142 // XFastDocumentHandler
143 virtual void SAL_CALL startDocument() override;
144 virtual void SAL_CALL endDocument() override;
145 virtual void SAL_CALL processingInstruction( const OUString& rTarget, const OUString& rData ) override;
146 virtual void SAL_CALL setDocumentLocator( const Reference< XLocator >& xLocator ) override;
148 // XFastContextHandler
149 virtual void SAL_CALL startFastElement( sal_Int32 nElement, const Reference< XFastAttributeList >& Attribs ) override;
150 virtual void SAL_CALL startUnknownElement( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) override;
151 virtual void SAL_CALL endFastElement( sal_Int32 Element ) override;
152 virtual void SAL_CALL endUnknownElement( const OUString& Namespace, const OUString& Name ) override;
153 virtual Reference< XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const Reference< XFastAttributeList >& Attribs ) override;
154 virtual Reference< XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) override;
155 virtual void SAL_CALL characters( const OUString& aChars ) override;
159 OUString CallbackDocumentHandler::getNamespacePrefixFromToken( sal_Int32 nToken )
161 if ( ( nToken & 0xffff0000 ) != 0 )
163 Sequence< sal_Int8 > aSeq = m_xTokenHandler->getUTF8Identifier( nToken & 0xffff0000 );
164 return OUString( reinterpret_cast< const char* >(
165 aSeq.getConstArray() ), aSeq.getLength(), RTL_TEXTENCODING_UTF8 );
167 else
168 return OUString();
171 OUString CallbackDocumentHandler::getNameFromToken( sal_Int32 nToken )
173 Sequence< sal_Int8 > aSeq = m_xTokenHandler->getUTF8Identifier( nToken & 0xffff );
174 return OUString( reinterpret_cast< const char* >(
175 aSeq.getConstArray() ), aSeq.getLength(), RTL_TEXTENCODING_UTF8 );
178 CallbackDocumentHandler::CallbackDocumentHandler( Reference< XDocumentHandler > const & xDocumentHandler,
179 rtl::Reference< NamespaceHandler > const & rNamespaceHandler,
180 Reference< XFastTokenHandler > const & xTokenHandler)
182 m_xDocumentHandler.set( xDocumentHandler );
183 m_aNamespaceHandler = rNamespaceHandler;
184 m_xTokenHandler.set( xTokenHandler );
187 void SAL_CALL CallbackDocumentHandler::startDocument()
189 if ( m_xDocumentHandler.is() )
190 m_xDocumentHandler->startDocument();
193 void SAL_CALL CallbackDocumentHandler::endDocument()
195 if ( m_xDocumentHandler.is() )
196 m_xDocumentHandler->endDocument();
199 void SAL_CALL CallbackDocumentHandler::processingInstruction( const OUString& rTarget, const OUString& rData )
201 if ( m_xDocumentHandler.is() )
202 m_xDocumentHandler->processingInstruction( rTarget, rData );
205 void SAL_CALL CallbackDocumentHandler::setDocumentLocator( const Reference< XLocator >& xLocator )
207 if ( m_xDocumentHandler.is() )
208 m_xDocumentHandler->setDocumentLocator( xLocator );
211 void SAL_CALL CallbackDocumentHandler::startFastElement( sal_Int32 nElement , const Reference< XFastAttributeList >& Attribs )
213 const OUString& rPrefix = CallbackDocumentHandler::getNamespacePrefixFromToken( nElement );
214 const OUString& rLocalName = CallbackDocumentHandler::getNameFromToken( nElement );
215 startUnknownElement( aDefaultNamespace, (rPrefix.isEmpty())? rLocalName : rPrefix + aNamespaceSeparator + rLocalName, Attribs );
218 void SAL_CALL CallbackDocumentHandler::startUnknownElement( const OUString& /*Namespace*/, const OUString& Name, const Reference< XFastAttributeList >& Attribs )
220 if ( !m_xDocumentHandler.is() )
221 return;
223 rtl::Reference < comphelper::AttributeList > rAttrList = new comphelper::AttributeList;
224 m_aNamespaceHandler->addNSDeclAttributes( rAttrList );
226 const Sequence< xml::FastAttribute > fastAttribs = Attribs->getFastAttributes();
227 for (const auto& rAttr : fastAttribs)
229 const OUString& rAttrValue = rAttr.Value;
230 sal_Int32 nToken = rAttr.Token;
231 const OUString& rAttrNamespacePrefix = CallbackDocumentHandler::getNamespacePrefixFromToken( nToken );
232 OUString sAttrName = CallbackDocumentHandler::getNameFromToken( nToken );
233 if ( !rAttrNamespacePrefix.isEmpty() )
234 sAttrName = rAttrNamespacePrefix + aNamespaceSeparator + sAttrName;
236 rAttrList->AddAttribute( sAttrName, rAttrValue );
239 const Sequence< xml::Attribute > unknownAttribs = Attribs->getUnknownAttributes();
240 for (const auto& rAttr : unknownAttribs)
242 const OUString& rAttrValue = rAttr.Value;
243 const OUString& rAttrName = rAttr.Name;
245 rAttrList->AddAttribute( rAttrName, rAttrValue );
247 m_xDocumentHandler->startElement( Name, rAttrList );
250 void SAL_CALL CallbackDocumentHandler::endFastElement( sal_Int32 nElement )
252 const OUString& rPrefix = CallbackDocumentHandler::getNamespacePrefixFromToken( nElement );
253 const OUString& rLocalName = CallbackDocumentHandler::getNameFromToken( nElement );
254 endUnknownElement( aDefaultNamespace, (rPrefix.isEmpty())? rLocalName : rPrefix + aNamespaceSeparator + rLocalName );
258 void SAL_CALL CallbackDocumentHandler::endUnknownElement( const OUString& /*Namespace*/, const OUString& Name )
260 if ( m_xDocumentHandler.is() )
262 m_xDocumentHandler->endElement( Name );
266 Reference< XFastContextHandler > SAL_CALL CallbackDocumentHandler::createFastChildContext( sal_Int32/* nElement */, const Reference< XFastAttributeList >&/* Attribs */ )
268 return this;
272 Reference< XFastContextHandler > SAL_CALL CallbackDocumentHandler::createUnknownChildContext( const OUString&/* Namespace */, const OUString&/* Name */, const Reference< XFastAttributeList >&/* Attribs */ )
274 return this;
277 void SAL_CALL CallbackDocumentHandler::characters( const OUString& aChars )
279 if ( m_xDocumentHandler.is() )
280 m_xDocumentHandler->characters( aChars );
283 SaxLegacyFastParser::SaxLegacyFastParser( ) : m_aNamespaceHandler( new NamespaceHandler ),
284 m_xParser(FastParser::create(::comphelper::getProcessComponentContext() ))
286 m_xParser->setNamespaceHandler( m_aNamespaceHandler );
289 void SAL_CALL SaxLegacyFastParser::initialize(Sequence< Any > const& rArguments )
291 if (!rArguments.hasElements())
292 return;
294 Reference< XFastTokenHandler > xTokenHandler;
295 OUString str;
296 if ( ( rArguments[0] >>= xTokenHandler ) && xTokenHandler.is() )
298 m_xTokenHandler.set( xTokenHandler );
300 else if ( ( rArguments[0] >>= str ) && "registerNamespaces" == str )
302 css::beans::Pair< OUString, sal_Int32 > rPair;
303 for (sal_Int32 i = 1; i < rArguments.getLength(); i++ )
305 rArguments[i] >>= rPair;
306 m_xParser->registerNamespace( rPair.First, rPair.Second );
309 else
311 uno::Reference<lang::XInitialization> const xInit(m_xParser,
312 uno::UNO_QUERY_THROW);
313 xInit->initialize( rArguments );
317 void SaxLegacyFastParser::parseStream( const InputSource& structSource )
319 m_xParser->setFastDocumentHandler( new CallbackDocumentHandler( m_xDocumentHandler,
320 m_aNamespaceHandler, m_xTokenHandler ) );
321 m_xParser->setTokenHandler( m_xTokenHandler );
322 m_xParser->parseStream( structSource );
325 void SaxLegacyFastParser::setDocumentHandler( const Reference< XDocumentHandler > & xHandler )
327 m_xDocumentHandler = xHandler;
330 void SaxLegacyFastParser::setErrorHandler( const Reference< XErrorHandler > & xHandler )
332 m_xParser->setErrorHandler( xHandler );
335 void SaxLegacyFastParser::setDTDHandler( const Reference < XDTDHandler > &/* xHandler */ )
340 void SaxLegacyFastParser::setEntityResolver( const Reference< XEntityResolver >& xResolver )
342 m_xParser->setEntityResolver( xResolver );
345 void SaxLegacyFastParser::setLocale( const Locale &locale )
347 m_xParser->setLocale( locale );
350 OUString SaxLegacyFastParser::getImplementationName()
352 return "com.sun.star.comp.extensions.xml.sax.LegacyFastParser";
355 sal_Bool SaxLegacyFastParser::supportsService(const OUString& ServiceName)
357 return cppu::supportsService(this, ServiceName);
360 Sequence< OUString > SaxLegacyFastParser::getSupportedServiceNames()
362 return { "com.sun.star.xml.sax.LegacyFastParser" };
365 } //namespace
367 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
368 com_sun_star_comp_extensions_xml_sax_LegacyFastParser_get_implementation(
369 css::uno::XComponentContext *,
370 css::uno::Sequence<css::uno::Any> const &)
372 return cppu::acquire(new SaxLegacyFastParser);
375 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */