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"
31 // ============================================================================
33 using namespace ::com::sun::star::io
;
34 using namespace ::com::sun::star::lang
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::xml::sax
;
39 // ============================================================================
43 class InputStreamCloseGuard
46 explicit InputStreamCloseGuard( const Reference
< XInputStream
>& rxInStream
, bool bCloseStream
);
47 ~InputStreamCloseGuard();
49 Reference
< XInputStream
> mxInStream
;
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
& ) {}
66 // ============================================================================
68 FastParser::FastParser( const Reference
< XComponentContext
>& rxContext
) throw( RuntimeException
) :
69 mrNamespaceMap( StaticNamespaceMap::get() )
71 // create a fast parser instance
72 Reference
< XMultiComponentFactory
> xFactory( rxContext
->getServiceManager(), UNO_SET_THROW
);
73 mxParser
.set( xFactory
->createInstanceWithContext( "com.sun.star.xml.sax.FastParser", rxContext
), UNO_QUERY_THROW
);
75 // create the fast tokenhandler
76 mxTokenHandler
.set( new FastTokenHandler
);
78 // create the fast token handler based on the OOXML token list
79 mxParser
->setTokenHandler( mxTokenHandler
);
82 FastParser::~FastParser()
86 void FastParser::registerNamespace( sal_Int32 nNamespaceId
) throw( IllegalArgumentException
, RuntimeException
)
89 throw RuntimeException();
91 const OUString
* pNamespaceUrl
= ContainerHelper::getMapElement( mrNamespaceMap
, nNamespaceId
);
93 throw IllegalArgumentException();
95 mxParser
->registerNamespace( *pNamespaceUrl
, nNamespaceId
);
98 void FastParser::setDocumentHandler( const Reference
< XFastDocumentHandler
>& rxDocHandler
) throw( RuntimeException
)
101 throw RuntimeException();
102 mxParser
->setFastDocumentHandler( rxDocHandler
);
105 void FastParser::parseStream( const InputSource
& rInputSource
, bool bCloseStream
) throw( SAXException
, IOException
, RuntimeException
)
107 // guard closing the input stream also when exceptions are thrown
108 InputStreamCloseGuard
aGuard( rInputSource
.aInputStream
, bCloseStream
);
110 throw RuntimeException();
111 mxParser
->parseStream( rInputSource
);
114 void FastParser::parseStream( const Reference
< XInputStream
>& rxInStream
, const OUString
& rStreamName
, bool bCloseStream
) throw( SAXException
, IOException
, RuntimeException
)
116 InputSource aInputSource
;
117 aInputSource
.sSystemId
= rStreamName
;
118 aInputSource
.aInputStream
= rxInStream
;
119 parseStream( aInputSource
, bCloseStream
);
122 void FastParser::parseStream( StorageBase
& rStorage
, const OUString
& rStreamName
, bool bCloseStream
) throw( SAXException
, IOException
, RuntimeException
)
124 parseStream( rStorage
.openInputStream( rStreamName
), rStreamName
, bCloseStream
);
127 OUString
FastParser::getNamespaceURL( const OUString
& rPrefix
) throw( IllegalArgumentException
, RuntimeException
)
130 throw RuntimeException();
131 return mxParser
->getNamespaceURL( rPrefix
);
134 sal_Int32
FastParser::getNamespaceId( const OUString
& rUrl
)
136 for( NamespaceMap::const_iterator aIt
= mrNamespaceMap
.begin(), aEnd
= mrNamespaceMap
.end(); aIt
!= aEnd
; ++aIt
)
137 if( rUrl
== aIt
->second
)
142 // ============================================================================
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */