SmartArt: text alignment
[LibreOffice.git] / oox / source / core / fastparser.cxx
blobd31ee7067f1c0bee6abc321c786ab1cbe0e2ae77
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() :
67 mrNamespaceMap( StaticNamespaceMap::get() )
69 // create a fast parser instance
70 mxParser = new sax_fastparser::FastSaxParser;
72 // create the fast tokenhandler
73 mxTokenHandler.set( new FastTokenHandler );
75 // create the fast token handler based on the OOXML token list
76 mxParser->setTokenHandler( mxTokenHandler );
79 FastParser::~FastParser()
83 void FastParser::registerNamespace( sal_Int32 nNamespaceId )
85 if( !mxParser.is() )
86 throw RuntimeException();
88 // add handling for OOXML strict here
89 const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap.maTransitionalNamespaceMap, nNamespaceId );
90 if( !pNamespaceUrl )
91 throw IllegalArgumentException();
93 mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
95 //also register the OOXML strict namespaces for the same id
96 const OUString* pNamespaceStrictUrl = ContainerHelper::getMapElement( mrNamespaceMap.maStrictNamespaceMap, nNamespaceId );
97 if(pNamespaceStrictUrl && (*pNamespaceUrl != *pNamespaceStrictUrl))
99 mxParser->registerNamespace( *pNamespaceStrictUrl, nNamespaceId );
103 void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler )
105 if( !mxParser.is() )
106 throw RuntimeException();
107 mxParser->setFastDocumentHandler( rxDocHandler );
110 void FastParser::clearDocumentHandler()
112 if (!mxParser.is())
113 return;
114 mxParser->setFastDocumentHandler(nullptr);
117 void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream )
119 // guard closing the input stream also when exceptions are thrown
120 InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
121 if( !mxParser.is() )
122 throw RuntimeException();
123 mxParser->parseStream( rInputSource );
126 void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName )
128 InputSource aInputSource;
129 aInputSource.sSystemId = rStreamName;
130 aInputSource.aInputStream = rxInStream;
131 parseStream( aInputSource );
134 void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName )
136 parseStream( rStorage.openInputStream( rStreamName ), rStreamName );
139 } // namespace core
140 } // namespace oox
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */