fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / oox / source / core / fastparser.cxx
blobb1e24304f97f5a8d10bbc075b97c0dbf4b8d5660
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 <sal/config.h>
22 #include <com/sun/star/xml/sax/FastParser.hpp>
23 #include "oox/core/fastparser.hxx"
25 #include "oox/core/fasttokenhandler.hxx"
26 #include "oox/helper/containerhelper.hxx"
27 #include "oox/helper/helper.hxx"
28 #include "oox/helper/storagebase.hxx"
29 #include "oox/token/namespacemap.hxx"
31 #include "sax/fastparser.hxx"
33 namespace oox {
34 namespace core {
36 using namespace ::com::sun::star::io;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::xml::sax;
41 namespace {
43 class InputStreamCloseGuard
45 public:
46 explicit InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream );
47 ~InputStreamCloseGuard();
48 private:
49 Reference< XInputStream > mxInStream;
50 bool mbCloseStream;
53 InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
54 mxInStream( rxInStream ),
55 mbCloseStream( bCloseStream )
59 InputStreamCloseGuard::~InputStreamCloseGuard()
61 if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
64 } // namespace
66 FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
67 mrNamespaceMap( StaticNamespaceMap::get() ),
68 mpParser(NULL)
70 // create a fast parser instance
71 mxParser = css::xml::sax::FastParser::create(rxContext);
72 mpParser = dynamic_cast<sax_fastparser::FastSaxParser*>(mxParser.get());
74 // create the fast tokenhandler
75 mxTokenHandler.set( new FastTokenHandler );
77 // create the fast token handler based on the OOXML token list
78 mxParser->setTokenHandler( mxTokenHandler );
81 FastParser::~FastParser()
85 void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException )
87 if( !mxParser.is() )
88 throw RuntimeException();
90 // add handling for OOXML strict here
91 const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap.maTransitionalNamespaceMap, nNamespaceId );
92 if( !pNamespaceUrl )
93 throw IllegalArgumentException();
95 mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
97 //also register the OOXML strict namespaces for the same id
98 const OUString* pNamespaceStrictUrl = ContainerHelper::getMapElement( mrNamespaceMap.maStrictNamespaceMap, nNamespaceId );
99 if(pNamespaceStrictUrl && (*pNamespaceUrl != *pNamespaceStrictUrl))
101 mxParser->registerNamespace( *pNamespaceStrictUrl, nNamespaceId );
105 void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException )
107 if( !mxParser.is() )
108 throw RuntimeException();
109 mxParser->setFastDocumentHandler( rxDocHandler );
112 void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
114 // guard closing the input stream also when exceptions are thrown
115 InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
116 if( !mxParser.is() )
117 throw RuntimeException();
118 mxParser->parseStream( rInputSource );
121 void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
123 InputSource aInputSource;
124 aInputSource.sSystemId = rStreamName;
125 aInputSource.aInputStream = rxInStream;
126 parseStream( aInputSource, bCloseStream );
129 void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
131 parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream );
134 OUString FastParser::getNamespaceURL( const OUString& rPrefix ) throw( IllegalArgumentException, RuntimeException )
136 if( !mxParser.is() )
137 throw RuntimeException();
138 return mxParser->getNamespaceURL( rPrefix );
141 bool FastParser::hasNamespaceURL( const OUString& rPrefix ) const
143 if (!mxParser.is())
144 throw RuntimeException();
146 if (!mpParser)
147 return false;
149 return mpParser->hasNamespaceURL(rPrefix);
152 sal_Int32 FastParser::getNamespaceId( const OUString& rUrl )
154 for( NamespaceMap::const_iterator aIt = mrNamespaceMap.maTransitionalNamespaceMap.begin(),
155 aEnd = mrNamespaceMap.maTransitionalNamespaceMap.end(); aIt != aEnd; ++aIt )
156 if( rUrl == aIt->second )
157 return aIt->first;
159 for( NamespaceMap::const_iterator aIt = mrNamespaceMap.maStrictNamespaceMap.begin(),
160 aEnd = mrNamespaceMap.maStrictNamespaceMap.end(); aIt != aEnd; ++aIt )
161 if( rUrl == aIt->second )
162 return aIt->first;
164 return 0;
167 } // namespace core
168 } // namespace oox
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */