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
;
50 bool const mbCloseStream
;
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() :
67 mrNamespaceMap( StaticNamespaceMap::get() )
69 // create a fast parser instance
70 mxParser
= new sax_fastparser::FastSaxParser
;
72 // create the fast tokenhandler
73 mxTokenHandler
.set( new FastTokenHandler
);
75 // create the fast token handler based on the OOXML token list
76 mxParser
->setTokenHandler( mxTokenHandler
);
79 FastParser::~FastParser()
83 void FastParser::registerNamespace( sal_Int32 nNamespaceId
)
86 throw RuntimeException();
88 // add handling for OOXML strict here
89 const OUString
* pNamespaceUrl
= ContainerHelper::getMapElement( mrNamespaceMap
.maTransitionalNamespaceMap
, nNamespaceId
);
91 throw IllegalArgumentException();
93 mxParser
->registerNamespace( *pNamespaceUrl
, nNamespaceId
);
95 //also register the OOXML strict namespaces for the same id
96 const OUString
* pNamespaceStrictUrl
= ContainerHelper::getMapElement( mrNamespaceMap
.maStrictNamespaceMap
, nNamespaceId
);
97 if(pNamespaceStrictUrl
&& (*pNamespaceUrl
!= *pNamespaceStrictUrl
))
99 mxParser
->registerNamespace( *pNamespaceStrictUrl
, nNamespaceId
);
103 void FastParser::setDocumentHandler( const Reference
< XFastDocumentHandler
>& rxDocHandler
)
106 throw RuntimeException();
107 mxParser
->setFastDocumentHandler( rxDocHandler
);
110 void FastParser::clearDocumentHandler()
114 mxParser
->setFastDocumentHandler(nullptr);
117 void FastParser::parseStream( const InputSource
& rInputSource
, bool bCloseStream
)
119 // guard closing the input stream also when exceptions are thrown
120 InputStreamCloseGuard
aGuard( rInputSource
.aInputStream
, bCloseStream
);
122 throw RuntimeException();
123 mxParser
->parseStream( rInputSource
);
126 void FastParser::parseStream( const Reference
< XInputStream
>& rxInStream
, const OUString
& rStreamName
)
128 InputSource aInputSource
;
129 aInputSource
.sSystemId
= rStreamName
;
130 aInputSource
.aInputStream
= rxInStream
;
131 parseStream( aInputSource
);
134 void FastParser::parseStream( StorageBase
& rStorage
, const OUString
& rStreamName
)
136 parseStream( rStorage
.openInputStream( rStreamName
), rStreamName
);
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */