xref: /trunk/main/oox/source/core/fastparser.cxx (revision 79aad27f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
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 namespace oox {
33 namespace core {
34 
35 // ============================================================================
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 using ::rtl::OUString;
43 
44 // ============================================================================
45 
46 namespace {
47 
48 class InputStreamCloseGuard
49 {
50 public:
51     explicit            InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream );
52                         ~InputStreamCloseGuard();
53 private:
54     Reference< XInputStream > mxInStream;
55     bool                mbCloseStream;
56 };
57 
InputStreamCloseGuard(const Reference<XInputStream> & rxInStream,bool bCloseStream)58 InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
59     mxInStream( rxInStream ),
60     mbCloseStream( bCloseStream )
61 {
62 }
63 
~InputStreamCloseGuard()64 InputStreamCloseGuard::~InputStreamCloseGuard()
65 {
66     if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
67 }
68 
69 } // namespace
70 
71 // ============================================================================
72 
FastParser(const Reference<XComponentContext> & rxContext)73 FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
74     mrNamespaceMap( StaticNamespaceMap::get() )
75 {
76     // create a fast parser instance
77     Reference< XMultiComponentFactory > xFactory( rxContext->getServiceManager(), UNO_SET_THROW );
78     mxParser.set( xFactory->createInstanceWithContext( CREATE_OUSTRING( "com.sun.star.xml.sax.FastParser" ), rxContext ), UNO_QUERY_THROW );
79 
80     // create the fast token handler based on the OOXML token list
81     mxParser->setTokenHandler( new FastTokenHandler );
82 }
83 
~FastParser()84 FastParser::~FastParser()
85 {
86 }
87 
registerNamespace(sal_Int32 nNamespaceId)88 void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException )
89 {
90     if( !mxParser.is() )
91         throw RuntimeException();
92 
93     const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap, nNamespaceId );
94     if( !pNamespaceUrl )
95         throw IllegalArgumentException();
96 
97     mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
98 }
99 
setDocumentHandler(const Reference<XFastDocumentHandler> & rxDocHandler)100 void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException )
101 {
102     if( !mxParser.is() )
103         throw RuntimeException();
104     mxParser->setFastDocumentHandler( rxDocHandler );
105 }
106 
parseStream(const InputSource & rInputSource,bool bCloseStream)107 void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
108 {
109     // guard closing the input stream also when exceptions are thrown
110     InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
111     if( !mxParser.is() )
112         throw RuntimeException();
113     mxParser->parseStream( rInputSource );
114 }
115 
parseStream(const Reference<XInputStream> & rxInStream,const OUString & rStreamName,bool bCloseStream)116 void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
117 {
118     InputSource aInputSource;
119     aInputSource.sSystemId = rStreamName;
120     aInputSource.aInputStream = rxInStream;
121     parseStream( aInputSource, bCloseStream );
122 }
123 
parseStream(StorageBase & rStorage,const OUString & rStreamName,bool bCloseStream)124 void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
125 {
126     parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream );
127 }
128 
129 // ============================================================================
130 
131 } // namespace core
132 } // namespace oox
133