LibreOffice Module oox (master) 1
fastparser.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <com/sun/star/lang/IllegalArgumentException.hpp>
24
29
30#include <sax/fastparser.hxx>
31
32namespace oox::core {
33
34using namespace ::com::sun::star::io;
35using namespace ::com::sun::star::lang;
36using namespace ::com::sun::star::uno;
37using namespace ::com::sun::star::xml::sax;
38
39namespace {
40
41class InputStreamCloseGuard
42{
43public:
44 explicit InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream );
45 ~InputStreamCloseGuard();
46private:
47 Reference< XInputStream > mxInStream;
49};
50
51InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
52 mxInStream( rxInStream ),
53 mbCloseStream( bCloseStream )
54{
55}
56
57InputStreamCloseGuard::~InputStreamCloseGuard()
58{
59 if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
60}
61
62} // namespace
63
64FastParser::FastParser() :
65 mrNamespaceMap( StaticNamespaceMap() )
66{
67 // create a fast parser instance
69
70 // create the fast tokenhandler
72
73 // create the fast token handler based on the OOXML token list
74 mxParser->setTokenHandler( mxTokenHandler );
75}
76
78{
79}
80
81void FastParser::registerNamespace( sal_Int32 nNamespaceId )
82{
83 if( !mxParser.is() )
84 throw RuntimeException();
85
86 // add handling for OOXML strict here
87 const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap.maTransitionalNamespaceMap, nNamespaceId );
88 if( !pNamespaceUrl )
89 throw IllegalArgumentException();
90
91 mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
92
93 //also register the OOXML strict namespaces for the same id
94 const OUString* pNamespaceStrictUrl = ContainerHelper::getMapElement( mrNamespaceMap.maStrictNamespaceMap, nNamespaceId );
95 if(pNamespaceStrictUrl && (*pNamespaceUrl != *pNamespaceStrictUrl))
96 {
97 mxParser->registerNamespace( *pNamespaceStrictUrl, nNamespaceId );
98 }
99}
100
101void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler )
102{
103 if( !mxParser.is() )
104 throw RuntimeException();
105 mxParser->setFastDocumentHandler( rxDocHandler );
106}
107
109{
110 if (!mxParser.is())
111 return;
112 mxParser->setFastDocumentHandler(nullptr);
113}
114
115void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream )
116{
117 // guard closing the input stream also when exceptions are thrown
118 InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
119 if( !mxParser.is() )
120 throw RuntimeException();
121 mxParser->parseStream( rInputSource );
122}
123
124void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName )
125{
126 InputSource aInputSource;
127 aInputSource.sSystemId = rStreamName;
128 aInputSource.aInputStream = rxInStream;
129 parseStream( aInputSource );
130}
131
132void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName )
133{
134 parseStream( rStorage.openInputStream( rStreamName ), rStreamName );
135}
136
137} // namespace oox::core
138
139/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const MapType::mapped_type * getMapElement(const MapType &rMap, const typename MapType::key_type &rKey)
Returns the pointer to an existing element of the passed map, or a null pointer, if an element with t...
Base class for storage access implementations.
Definition: storagebase.hxx:52
css::uno::Reference< css::io::XInputStream > openInputStream(const OUString &rStreamName)
Opens and returns the specified input stream from the storage.
void parseStream(const css::xml::sax::InputSource &rInputSource, bool bCloseStream=false)
Parses the passed SAX input source.
void registerNamespace(sal_Int32 nNamespaceId)
Registers an OOXML namespace at the parser.
Definition: fastparser.cxx:81
void setDocumentHandler(const css::uno::Reference< css::xml::sax::XFastDocumentHandler > &rxDocHandler)
Sets the passed document handler that will receive the SAX parser events.
Definition: fastparser.cxx:101
const NamespaceMap & mrNamespaceMap
Definition: fastparser.hxx:109
css::uno::Reference< css::xml::sax::XFastTokenHandler > mxTokenHandler
Definition: fastparser.hxx:108
rtl::Reference< sax_fastparser::FastSaxParser > mxParser
Definition: fastparser.hxx:110
Wrapper implementing the com.sun.star.xml.sax.XFastTokenHandler API interface that provides access to...
bool mbCloseStream
Definition: fastparser.cxx:48
Reference< XInputStream > mxInStream
Definition: fastparser.cxx:47
NamespaceMap & StaticNamespaceMap()
Thread-safe singleton of a map of all supported XML namespace URLs.
std::map< sal_Int32, OUString > maStrictNamespaceMap
std::map< sal_Int32, OUString > maTransitionalNamespaceMap