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 <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"
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
;
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 FastParser::FastParser( const Reference
< XComponentContext
>& rxContext
) throw( RuntimeException
) :
67 mrNamespaceMap( StaticNamespaceMap::get() ),
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
)
88 throw RuntimeException();
90 // add handling for OOXML strict here
91 const OUString
* pNamespaceUrl
= ContainerHelper::getMapElement( mrNamespaceMap
.maTransitionalNamespaceMap
, nNamespaceId
);
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
)
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
);
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
)
137 throw RuntimeException();
138 return mxParser
->getNamespaceURL( rPrefix
);
141 bool FastParser::hasNamespaceURL( const OUString
& rPrefix
) const
144 throw RuntimeException();
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
)
159 for( NamespaceMap::const_iterator aIt
= mrNamespaceMap
.maStrictNamespaceMap
.begin(),
160 aEnd
= mrNamespaceMap
.maStrictNamespaceMap
.end(); aIt
!= aEnd
; ++aIt
)
161 if( rUrl
== aIt
->second
)
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */