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/lang/IllegalArgumentException.hpp>
23 #include <com/sun/star/xml/sax/FastParser.hpp>
24 #include <oox/core/fastparser.hxx>
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>
32 #include <sax/fastparser.hxx>
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
;
44 class InputStreamCloseGuard
47 explicit InputStreamCloseGuard( const Reference
< XInputStream
>& rxInStream
, bool bCloseStream
);
48 ~InputStreamCloseGuard();
50 Reference
< XInputStream
> mxInStream
;
51 bool const mbCloseStream
;
54 InputStreamCloseGuard::InputStreamCloseGuard( const Reference
< XInputStream
>& rxInStream
, bool bCloseStream
) :
55 mxInStream( rxInStream
),
56 mbCloseStream( bCloseStream
)
60 InputStreamCloseGuard::~InputStreamCloseGuard()
62 if( mxInStream
.is() && mbCloseStream
) try { mxInStream
->closeInput(); } catch( Exception
& ) {}
67 FastParser::FastParser() :
68 mrNamespaceMap( StaticNamespaceMap::get() )
70 // create a fast parser instance
71 mxParser
= new sax_fastparser::FastSaxParser
;
73 // create the fast tokenhandler
74 mxTokenHandler
.set( new FastTokenHandler
);
76 // create the fast token handler based on the OOXML token list
77 mxParser
->setTokenHandler( mxTokenHandler
);
80 FastParser::~FastParser()
84 void FastParser::registerNamespace( sal_Int32 nNamespaceId
)
87 throw RuntimeException();
89 // add handling for OOXML strict here
90 const OUString
* pNamespaceUrl
= ContainerHelper::getMapElement( mrNamespaceMap
.maTransitionalNamespaceMap
, nNamespaceId
);
92 throw IllegalArgumentException();
94 mxParser
->registerNamespace( *pNamespaceUrl
, nNamespaceId
);
96 //also register the OOXML strict namespaces for the same id
97 const OUString
* pNamespaceStrictUrl
= ContainerHelper::getMapElement( mrNamespaceMap
.maStrictNamespaceMap
, nNamespaceId
);
98 if(pNamespaceStrictUrl
&& (*pNamespaceUrl
!= *pNamespaceStrictUrl
))
100 mxParser
->registerNamespace( *pNamespaceStrictUrl
, nNamespaceId
);
104 void FastParser::setDocumentHandler( const Reference
< XFastDocumentHandler
>& rxDocHandler
)
107 throw RuntimeException();
108 mxParser
->setFastDocumentHandler( rxDocHandler
);
111 void FastParser::clearDocumentHandler()
115 mxParser
->setFastDocumentHandler(nullptr);
118 void FastParser::parseStream( const InputSource
& rInputSource
, bool bCloseStream
)
120 // guard closing the input stream also when exceptions are thrown
121 InputStreamCloseGuard
aGuard( rInputSource
.aInputStream
, bCloseStream
);
123 throw RuntimeException();
124 mxParser
->parseStream( rInputSource
);
127 void FastParser::parseStream( const Reference
< XInputStream
>& rxInStream
, const OUString
& rStreamName
)
129 InputSource aInputSource
;
130 aInputSource
.sSystemId
= rStreamName
;
131 aInputSource
.aInputStream
= rxInStream
;
132 parseStream( aInputSource
);
135 void FastParser::parseStream( StorageBase
& rStorage
, const OUString
& rStreamName
)
137 parseStream( rStorage
.openInputStream( rStreamName
), rStreamName
);
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */