1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "oox/core/fastparser.hxx"
22 #include "oox/core/fasttokenhandler.hxx"
23 #include "oox/helper/containerhelper.hxx"
24 #include "oox/helper/helper.hxx"
25 #include "oox/helper/storagebase.hxx"
26 #include "oox/token/namespacemap.hxx"
28 #include "sax/fastparser.hxx"
33 // ============================================================================
35 using namespace ::com::sun::star::io
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::xml::sax
;
41 // ============================================================================
45 class InputStreamCloseGuard
48 explicit InputStreamCloseGuard( const Reference
< XInputStream
>& rxInStream
, bool bCloseStream
);
49 ~InputStreamCloseGuard();
51 Reference
< XInputStream
> mxInStream
;
55 InputStreamCloseGuard::InputStreamCloseGuard( const Reference
< XInputStream
>& rxInStream
, bool bCloseStream
) :
56 mxInStream( rxInStream
),
57 mbCloseStream( bCloseStream
)
61 InputStreamCloseGuard::~InputStreamCloseGuard()
63 if( mxInStream
.is() && mbCloseStream
) try { mxInStream
->closeInput(); } catch( Exception
& ) {}
68 // ============================================================================
70 FastParser::FastParser( const Reference
< XComponentContext
>& rxContext
) throw( RuntimeException
) :
71 mrNamespaceMap( StaticNamespaceMap::get() ),
74 // create a fast parser instance
75 Reference
< XMultiComponentFactory
> xFactory( rxContext
->getServiceManager(), UNO_SET_THROW
);
76 mxParser
.set( xFactory
->createInstanceWithContext( "com.sun.star.xml.sax.FastParser", rxContext
), UNO_QUERY_THROW
);
77 mpParser
= dynamic_cast<sax_fastparser::FastSaxParser
*>(mxParser
.get());
79 // create the fast tokenhandler
80 mxTokenHandler
.set( new FastTokenHandler
);
82 // create the fast token handler based on the OOXML token list
83 mxParser
->setTokenHandler( mxTokenHandler
);
86 FastParser::~FastParser()
90 void FastParser::registerNamespace( sal_Int32 nNamespaceId
) throw( IllegalArgumentException
, RuntimeException
)
93 throw RuntimeException();
95 const OUString
* pNamespaceUrl
= ContainerHelper::getMapElement( mrNamespaceMap
, nNamespaceId
);
97 throw IllegalArgumentException();
99 mxParser
->registerNamespace( *pNamespaceUrl
, nNamespaceId
);
102 void FastParser::setDocumentHandler( const Reference
< XFastDocumentHandler
>& rxDocHandler
) throw( RuntimeException
)
105 throw RuntimeException();
106 mxParser
->setFastDocumentHandler( rxDocHandler
);
109 void FastParser::parseStream( const InputSource
& rInputSource
, bool bCloseStream
) throw( SAXException
, IOException
, RuntimeException
)
111 // guard closing the input stream also when exceptions are thrown
112 InputStreamCloseGuard
aGuard( rInputSource
.aInputStream
, bCloseStream
);
114 throw RuntimeException();
115 mxParser
->parseStream( rInputSource
);
118 void FastParser::parseStream( const Reference
< XInputStream
>& rxInStream
, const OUString
& rStreamName
, bool bCloseStream
) throw( SAXException
, IOException
, RuntimeException
)
120 InputSource aInputSource
;
121 aInputSource
.sSystemId
= rStreamName
;
122 aInputSource
.aInputStream
= rxInStream
;
123 parseStream( aInputSource
, bCloseStream
);
126 void FastParser::parseStream( StorageBase
& rStorage
, const OUString
& rStreamName
, bool bCloseStream
) throw( SAXException
, IOException
, RuntimeException
)
128 parseStream( rStorage
.openInputStream( rStreamName
), rStreamName
, bCloseStream
);
131 OUString
FastParser::getNamespaceURL( const OUString
& rPrefix
) throw( IllegalArgumentException
, RuntimeException
)
134 throw RuntimeException();
135 return mxParser
->getNamespaceURL( rPrefix
);
138 bool FastParser::hasNamespaceURL( const OUString
& rPrefix
) const
141 throw RuntimeException();
146 return mpParser
->hasNamespaceURL(rPrefix
);
149 sal_Int32
FastParser::getNamespaceId( const OUString
& rUrl
)
151 for( NamespaceMap::const_iterator aIt
= mrNamespaceMap
.begin(), aEnd
= mrNamespaceMap
.end(); aIt
!= aEnd
; ++aIt
)
152 if( rUrl
== aIt
->second
)
157 // ============================================================================
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */