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>
23 #include <com/sun/star/xml/sax/FastParser.hpp>
24 #include <oox/core/fastparser.hxx>
25 
26 #include <oox/core/fasttokenhandler.hxx>
27 #include <oox/helper/containerhelper.hxx>
28 #include <oox/helper/helper.hxx>
29 #include <oox/helper/storagebase.hxx>
30 #include <oox/token/namespacemap.hxx>
31 
32 #include <sax/fastparser.hxx>
33 
34 namespace oox {
35 namespace core {
36 
37 using namespace ::com::sun::star::io;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::xml::sax;
41 
42 namespace {
43 
44 class InputStreamCloseGuard
45 {
46 public:
47     explicit            InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream );
48                         ~InputStreamCloseGuard();
49 private:
50     Reference< XInputStream > mxInStream;
51     bool const          mbCloseStream;
52 };
53 
InputStreamCloseGuard(const Reference<XInputStream> & rxInStream,bool bCloseStream)54 InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
55     mxInStream( rxInStream ),
56     mbCloseStream( bCloseStream )
57 {
58 }
59 
~InputStreamCloseGuard()60 InputStreamCloseGuard::~InputStreamCloseGuard()
61 {
62     if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
63 }
64 
65 } // namespace
66 
FastParser()67 FastParser::FastParser() :
68     mrNamespaceMap( StaticNamespaceMap::get() )
69 {
70     // create a fast parser instance
71     mxParser = new sax_fastparser::FastSaxParser;
72 
73     // create the fast tokenhandler
74     mxTokenHandler.set( new FastTokenHandler );
75 
76     // create the fast token handler based on the OOXML token list
77     mxParser->setTokenHandler( mxTokenHandler );
78 }
79 
~FastParser()80 FastParser::~FastParser()
81 {
82 }
83 
registerNamespace(sal_Int32 nNamespaceId)84 void FastParser::registerNamespace( sal_Int32 nNamespaceId )
85 {
86     if( !mxParser.is() )
87         throw RuntimeException();
88 
89     // add handling for OOXML strict here
90     const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap.maTransitionalNamespaceMap, nNamespaceId );
91     if( !pNamespaceUrl )
92         throw IllegalArgumentException();
93 
94     mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
95 
96     //also register the OOXML strict namespaces for the same id
97     const OUString* pNamespaceStrictUrl = ContainerHelper::getMapElement( mrNamespaceMap.maStrictNamespaceMap, nNamespaceId );
98     if(pNamespaceStrictUrl && (*pNamespaceUrl != *pNamespaceStrictUrl))
99     {
100         mxParser->registerNamespace( *pNamespaceStrictUrl, nNamespaceId );
101     }
102 }
103 
setDocumentHandler(const Reference<XFastDocumentHandler> & rxDocHandler)104 void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler )
105 {
106     if( !mxParser.is() )
107         throw RuntimeException();
108     mxParser->setFastDocumentHandler( rxDocHandler );
109 }
110 
clearDocumentHandler()111 void FastParser::clearDocumentHandler()
112 {
113     if (!mxParser.is())
114         return;
115     mxParser->setFastDocumentHandler(nullptr);
116 }
117 
parseStream(const InputSource & rInputSource,bool bCloseStream)118 void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream )
119 {
120     // guard closing the input stream also when exceptions are thrown
121     InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
122     if( !mxParser.is() )
123         throw RuntimeException();
124     mxParser->parseStream( rInputSource );
125 }
126 
parseStream(const Reference<XInputStream> & rxInStream,const OUString & rStreamName)127 void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName )
128 {
129     InputSource aInputSource;
130     aInputSource.sSystemId = rStreamName;
131     aInputSource.aInputStream = rxInStream;
132     parseStream( aInputSource );
133 }
134 
parseStream(StorageBase & rStorage,const OUString & rStreamName)135 void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName )
136 {
137     parseStream( rStorage.openInputStream( rStreamName ), rStreamName );
138 }
139 
140 } // namespace core
141 } // namespace oox
142 
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
144