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 <oox/core/fastparser.hxx>
25 #include <oox/core/fasttokenhandler.hxx>
26 #include <oox/helper/containerhelper.hxx>
27 #include <oox/helper/storagebase.hxx>
28 #include <oox/token/namespacemap.hxx>
30 #include <sax/fastparser.hxx>
34 using namespace ::com::sun::star::io
;
35 using namespace ::com::sun::star::lang
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::xml::sax
;
41 class InputStreamCloseGuard
44 explicit InputStreamCloseGuard( const Reference
< XInputStream
>& rxInStream
, bool bCloseStream
);
45 ~InputStreamCloseGuard();
47 Reference
< XInputStream
> mxInStream
;
51 InputStreamCloseGuard::InputStreamCloseGuard( const Reference
< XInputStream
>& rxInStream
, bool bCloseStream
) :
52 mxInStream( rxInStream
),
53 mbCloseStream( bCloseStream
)
57 InputStreamCloseGuard::~InputStreamCloseGuard()
59 if( mxInStream
.is() && mbCloseStream
) try { mxInStream
->closeInput(); } catch( Exception
& ) {}
64 FastParser::FastParser() :
65 mrNamespaceMap( StaticNamespaceMap() )
67 // create a fast parser instance
68 mxParser
= new sax_fastparser::FastSaxParser
;
70 // create the fast tokenhandler
71 mxTokenHandler
.set( new FastTokenHandler
);
73 // create the fast token handler based on the OOXML token list
74 mxParser
->setTokenHandler( mxTokenHandler
);
77 FastParser::~FastParser()
81 void FastParser::registerNamespace( sal_Int32 nNamespaceId
)
84 throw RuntimeException();
86 // add handling for OOXML strict here
87 const OUString
* pNamespaceUrl
= ContainerHelper::getMapElement( mrNamespaceMap
.maTransitionalNamespaceMap
, nNamespaceId
);
89 throw IllegalArgumentException();
91 mxParser
->registerNamespace( *pNamespaceUrl
, nNamespaceId
);
93 //also register the OOXML strict namespaces for the same id
94 const OUString
* pNamespaceStrictUrl
= ContainerHelper::getMapElement( mrNamespaceMap
.maStrictNamespaceMap
, nNamespaceId
);
95 if(pNamespaceStrictUrl
&& (*pNamespaceUrl
!= *pNamespaceStrictUrl
))
97 mxParser
->registerNamespace( *pNamespaceStrictUrl
, nNamespaceId
);
101 void FastParser::setDocumentHandler( const Reference
< XFastDocumentHandler
>& rxDocHandler
)
104 throw RuntimeException();
105 mxParser
->setFastDocumentHandler( rxDocHandler
);
108 void FastParser::clearDocumentHandler()
112 mxParser
->setFastDocumentHandler(nullptr);
115 void FastParser::parseStream( const InputSource
& rInputSource
, bool bCloseStream
)
117 // guard closing the input stream also when exceptions are thrown
118 InputStreamCloseGuard
aGuard( rInputSource
.aInputStream
, bCloseStream
);
120 throw RuntimeException();
121 mxParser
->parseStream( rInputSource
);
124 void FastParser::parseStream( const Reference
< XInputStream
>& rxInStream
, const OUString
& rStreamName
)
126 InputSource aInputSource
;
127 aInputSource
.sSystemId
= rStreamName
;
128 aInputSource
.aInputStream
= rxInStream
;
129 parseStream( aInputSource
);
132 void FastParser::parseStream( StorageBase
& rStorage
, const OUString
& rStreamName
)
134 parseStream( rStorage
.openInputStream( rStreamName
), rStreamName
);
137 } // namespace oox::core
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */