diff options
Diffstat (limited to 'include/oox/core')
-rw-r--r-- | include/oox/core/binarycodec.hxx | 316 | ||||
-rw-r--r-- | include/oox/core/contexthandler.hxx | 117 | ||||
-rw-r--r-- | include/oox/core/contexthandler2.hxx | 272 | ||||
-rw-r--r-- | include/oox/core/fastparser.hxx | 97 | ||||
-rw-r--r-- | include/oox/core/fasttokenhandler.hxx | 67 | ||||
-rw-r--r-- | include/oox/core/filterbase.hxx | 293 | ||||
-rw-r--r-- | include/oox/core/filterdetect.hxx | 162 | ||||
-rw-r--r-- | include/oox/core/fragmenthandler.hxx | 131 | ||||
-rw-r--r-- | include/oox/core/fragmenthandler2.hxx | 121 | ||||
-rw-r--r-- | include/oox/core/recordparser.hxx | 90 | ||||
-rw-r--r-- | include/oox/core/relations.hxx | 103 | ||||
-rw-r--r-- | include/oox/core/relationshandler.hxx | 54 | ||||
-rw-r--r-- | include/oox/core/xmlfilterbase.hxx | 256 |
13 files changed, 2079 insertions, 0 deletions
diff --git a/include/oox/core/binarycodec.hxx b/include/oox/core/binarycodec.hxx new file mode 100644 index 000000000000..64b06d4244ef --- /dev/null +++ b/include/oox/core/binarycodec.hxx @@ -0,0 +1,316 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_BINARYCODEC_HXX +#define OOX_CORE_BINARYCODEC_HXX + +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/beans/NamedValue.hpp> + +#include <rtl/cipher.h> +#include <rtl/digest.h> +#include "oox/dllapi.h" + +namespace oox { class AttributeList; } + +namespace oox { +namespace core { + +// ============================================================================ + +class OOX_DLLPUBLIC CodecHelper +{ +public: + /** Returns the password hash if it is in the required 16-bit limit. */ + static sal_uInt16 getPasswordHash( const AttributeList& rAttribs, sal_Int32 nElement ); + +private: + CodecHelper(); + ~CodecHelper(); +}; + +// ============================================================================ + +/** Encodes and decodes data from/to protected MS Office documents. + + Implements a simple XOR encoding/decoding algorithm used in MS Office + versions up to MSO 95. + */ +class OOX_DLLPUBLIC BinaryCodec_XOR +{ +public: + /** Enumerates codec types supported by this XOR codec implementation. */ + enum CodecType + { + CODEC_WORD, ///< MS Word XOR codec. + CODEC_EXCEL ///< MS Excel XOR codec. + }; + +public: + /** Default constructor. + + Two-step construction in conjunction with the initKey() and verifyKey() + functions allows to try to initialize with different passwords (e.g. + built-in default password used for Excel workbook protection). + */ + explicit BinaryCodec_XOR( CodecType eCodecType ); + + ~BinaryCodec_XOR(); + + /** Initializes the algorithm with the specified password. + + @param pnPassData + Character array containing the password. Must be zero terminated, + which results in a maximum length of 15 characters. + */ + void initKey( const sal_uInt8 pnPassData[ 16 ] ); + + /** Initializes the algorithm with the encryption data. + + @param aData + The sequence contains the necessary data to initialize + the codec. + */ + bool initCodec( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& aData ); + + /** Retrieves the encryption data + + @return + The sequence contains the necessary data to initialize + the codec. + */ + ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > getEncryptionData(); + + /** Verifies the validity of the password using the passed key and hash. + + @precond + The codec must be initialized with the initKey() function before + this function can be used. + + @param nKey + Password key value read from the file. + @param nHash + Password hash value read from the file. + + @return + True = test was successful. + */ + bool verifyKey( sal_uInt16 nKey, sal_uInt16 nHash ) const; + + /** Reinitializes the codec to start a new memory block. + + Resets the internal key offset to 0. + + @precond + The codec must be initialized with the initKey() function before + this function can be used. + */ + void startBlock(); + + /** Decodes a block of memory. + + @precond + The codec must be initialized with the initKey() function before + this function can be used. + + @param pnDestData + Destination buffer. Will contain the decrypted data afterwards. + @param pnSrcData + Encrypted data block. + @param nBytes + Size of the passed data blocks. pnDestData and pnSrcData must be of + this size. + + @return + True = decoding was successful (no error occurred). + */ + bool decode( + sal_uInt8* pnDestData, + const sal_uInt8* pnSrcData, + sal_Int32 nBytes ); + + /** Lets the cipher skip a specific amount of bytes. + + This function sets the cipher to the same state as if the specified + amount of data has been decoded with one or more calls of decode(). + + @precond + The codec must be initialized with the initKey() function before + this function can be used. + + @param nBytes + Number of bytes to be skipped (cipher "seeks" forward). + + @return + True = skip was successful (no error occurred). + */ + bool skip( sal_Int32 nBytes ); + +private: + CodecType meCodecType; ///< Codec type. + sal_uInt8 mpnKey[ 16 ]; ///< Encryption key. + sal_Int32 mnOffset; ///< Key offset. + sal_uInt16 mnBaseKey; ///< Base key from password. + sal_uInt16 mnHash; ///< Hash value from password. +}; + +// ============================================================================ + +/** Encodes and decodes data from protected MSO 97+ documents. + + This is a wrapper class around low level cryptographic functions from RTL. + Implementation is based on the wvDecrypt package by Caolan McNamara: + http://www.csn.ul.ie/~caolan/docs/wvDecrypt.html + */ +class OOX_DLLPUBLIC BinaryCodec_RCF +{ +public: + /** Default constructor. + + Two-step construction in conjunction with the initKey() and verifyKey() + functions allows to try to initialize with different passwords (e.g. + built-in default password used for Excel workbook protection). + */ + explicit BinaryCodec_RCF(); + + ~BinaryCodec_RCF(); + + /** Initializes the algorithm with the encryption data. + + @param aData + The sequence contains the necessary data to initialize + the codec. + */ + bool initCodec( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& aData ); + + /** Retrieves the encryption data + + @return + The sequence contains the necessary data to initialize + the codec. + */ + ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > getEncryptionData(); + + /** Initializes the algorithm with the specified password and document ID. + + @param pnPassData + Unicode character array containing the password. Must be zero + terminated, which results in a maximum length of 15 characters. + @param pnSalt + Random salt data block read from or written to the file. + */ + void initKey( + const sal_uInt16 pnPassData[ 16 ], + const sal_uInt8 pnSalt[ 16 ] ); + + /** Verifies the validity of the password using the passed salt data. + + @precond + The codec must be initialized with the initKey() function before + this function can be used. + + @param pnVerifier + Verifier block read from the file. + @param pnVerifierHash + Verifier hash read from the file. + + @return + True = test was successful. + */ + bool verifyKey( + const sal_uInt8 pnVerifier[ 16 ], + const sal_uInt8 pnVerifierHash[ 16 ] ); + + /** Rekeys the codec using the specified counter. + + After reading a specific amount of data the cipher algorithm needs to + be rekeyed using a counter that counts the data blocks. + + The block size is for example 512 bytes for MS Word files and 1024 + bytes for MS Excel files. + + @precond + The codec must be initialized with the initKey() function before + this function can be used. + + @param nCounter + Block counter used to rekey the cipher. + */ + bool startBlock( sal_Int32 nCounter ); + + /** Decodes a block of memory. + + @see rtl_cipher_decode() + + @precond + The codec must be initialized with the initKey() function before + this function can be used. + + @param pnDestData + Destination buffer. Will contain the decrypted data afterwards. + @param pnSrcData + Encrypted data block. + @param nBytes + Size of the passed data blocks. pnDestData and pnSrcData must be of + this size. + + @return + True = decoding was successful (no error occurred). + */ + bool decode( + sal_uInt8* pnDestData, + const sal_uInt8* pnSrcData, + sal_Int32 nBytes ); + + /** Lets the cipher skip a specific amount of bytes. + + This function sets the cipher to the same state as if the specified + amount of data has been decoded with one or more calls of decode(). + + @precond + The codec must be initialized with the initKey() function before + this function can be used. + + @param nBytes + Number of bytes to be skipped (cipher "seeks" forward). + + @return + True = skip was successful (no error occurred). + */ + bool skip( sal_Int32 nBytes ); + +private: + void InitKeyImpl( + const sal_uInt8 pKeyData[64], + const sal_uInt8 pUnique[16] ); + + rtlCipher mhCipher; + rtlDigest mhDigest; + sal_uInt8 mpnDigestValue[ RTL_DIGEST_LENGTH_MD5 ]; + sal_uInt8 mpnUnique[16]; +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/contexthandler.hxx b/include/oox/core/contexthandler.hxx new file mode 100644 index 000000000000..f11dc1d6260a --- /dev/null +++ b/include/oox/core/contexthandler.hxx @@ -0,0 +1,117 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_CONTEXTHANDLER_HXX +#define OOX_CORE_CONTEXTHANDLER_HXX + +#include <com/sun/star/xml/sax/XFastContextHandler.hpp> +#include <boost/shared_ptr.hpp> +#include <cppuhelper/implbase1.hxx> +#include <rtl/ref.hxx> +#include "oox/token/namespaces.hxx" +#include "oox/token/tokens.hxx" +#include "oox/dllapi.h" + +namespace com { namespace sun { namespace star { + namespace xml { namespace sax { class XLocator; } } +} } } + +namespace oox { class SequenceInputStream; } + +namespace oox { +namespace core { + +class XmlFilterBase; +class FragmentHandler; +struct Relation; +class Relations; + +// ============================================================================ + +class ContextHandler; +typedef ::rtl::Reference< ContextHandler > ContextHandlerRef; + +struct FragmentBaseData; +typedef ::boost::shared_ptr< FragmentBaseData > FragmentBaseDataRef; + +typedef ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XFastContextHandler > ContextHandler_BASE; + +class OOX_DLLPUBLIC ContextHandler : public ContextHandler_BASE +{ +public: + explicit ContextHandler( const ContextHandler& rParent ); + virtual ~ContextHandler(); + + /** Returns the filter instance. */ + XmlFilterBase& getFilter() const; + /** Returns the relations of the current fragment. */ + const Relations& getRelations() const; + /** Returns the full path of the current fragment. */ + const OUString& getFragmentPath() const; + + /** Returns the full fragment path for the target of the passed relation. */ + OUString getFragmentPathFromRelation( const Relation& rRelation ) const; + /** Returns the full fragment path for the passed relation identifier. */ + OUString getFragmentPathFromRelId( const OUString& rRelId ) const; + /** Returns the full fragment path for the first relation of the passed type. */ + OUString getFragmentPathFromFirstType( const OUString& rType ) const; + + // com.sun.star.xml.sax.XFastContextHandler interface --------------------- + + virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL startUnknownElement( const OUString& Namespace, const OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL endFastElement( ::sal_Int32 Element ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL endUnknownElement( const OUString& Namespace, const OUString& Name ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& Namespace, const OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL characters( const OUString& aChars ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL processingInstruction( const OUString& aTarget, const OUString& aData ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + + // record context interface ----------------------------------------------- + + virtual ContextHandlerRef createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ); + virtual void startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm ); + virtual void endRecord( sal_Int32 nRecId ); + +protected: + /** Helper constructor for the FragmentHandler. */ + explicit ContextHandler( const FragmentBaseDataRef& rxBaseData ); + + void implSetLocator( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >& rxLocator ); + +#ifdef _MSC_VER + ContextHandler() {} // workaround +#endif + +private: + ContextHandler& operator=( const ContextHandler& ); + +private: + FragmentBaseDataRef mxBaseData; ///< Base data of the fragment. +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/contexthandler2.hxx b/include/oox/core/contexthandler2.hxx new file mode 100644 index 000000000000..f1a0c7db38b2 --- /dev/null +++ b/include/oox/core/contexthandler2.hxx @@ -0,0 +1,272 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_CONTEXTHANDLER2_HXX +#define OOX_CORE_CONTEXTHANDLER2_HXX + +#include <vector> +#include <boost/shared_ptr.hpp> +#include "oox/helper/attributelist.hxx" +#include "oox/helper/binaryinputstream.hxx" +#include "oox/core/contexthandler.hxx" +#include "oox/dllapi.h" + +namespace oox { +namespace core { + +// ============================================================================ + +const sal_Int32 XML_ROOT_CONTEXT = SAL_MAX_INT32; + +// ============================================================================ + +struct ElementInfo; + +/** Helper class that provides a context stack. + + Fragment handlers and context handlers derived from this helper class will + track the identifiers of the visited elements in a stack. The idea is to + use the same instance of a fragment handler or context handler to process + several nested elements in an XML stream. For that, the abstract function + onCreateContext() has to return 'this' for the passed element. + + Derived classes have to implement the createFastChildContext(), + startFastElement(), characters(), and endFastElement() functions from the + com.sun.star.xml.sax.XFastContextHandler interface by simply forwarding + them to the respective implCreateChildContext(), implStartElement(), + implCharacters(), and implEndElement() functions of this helper. This is + implemented already in the classes ContextHandler2 and FragmentHandler2. + The new abstract functions have to be implemented according to the elements + to be processed. + + Similarly, for binary import, derived classes have to forward the + createRecordContext(), startRecord(), and endRecord() functions from the + ContextHandler class to the implCreateRecordContext(), implStartRecord(), + and implEndRecord() functions of this helper. Again, this is implemented + already in the classes ContextHandler2 and FragmentHandler2. + */ +class OOX_DLLPUBLIC ContextHandler2Helper +{ +public: + explicit ContextHandler2Helper( bool bEnableTrimSpace ); + explicit ContextHandler2Helper( const ContextHandler2Helper& rParent ); + virtual ~ContextHandler2Helper(); + + // allow instances to be stored in ::rtl::Reference + virtual void SAL_CALL acquire() throw() = 0; + virtual void SAL_CALL release() throw() = 0; + + // interface -------------------------------------------------------------- + + /** Will be called to create a context handler for the passed element. + + Usually 'this' can be returned to improve performance by reusing the + same instance to process several elements. Used by OOXML import only. + */ + virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) = 0; + + /** Will be called when a new element has been started. + + This function is called at the context handler returned from + onCreateContext(), or, for root elements of an XML stream, at the + fragment handler itself. + + The current element identifier can be accessed with getCurrentElement() + or isCurrentElement(). Used by OOXML import only. + */ + virtual void onStartElement( const AttributeList& rAttribs ) = 0; + + /** Will be called before a new child element starts, or if the current + element is about to be left. + + This helper function collects all text fragments received by the + characters() function (such as encoded characters which are passed in + separate calls to the characters() function), and passes the + concatenated and trimmed string. + + The current element identifier can be accessed with getCurrentElement() + or isCurrentElement(). Used by OOXML import only. + */ + virtual void onCharacters( const OUString& rChars ) = 0; + + /** Will be called when the current element is about to be left. + + The current element identifier can be accessed with getCurrentElement() + or isCurrentElement(). Used by OOXML import only. + */ + virtual void onEndElement() = 0; + + /** Will be called to create a context handler for the passed record. + + Usually 'this' can be returned to improve performance by reusing the + same instance to process several records. Used by BIFF import only. + */ + virtual ContextHandlerRef onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ) = 0; + + /** Will be called when a new record block in a binary stream has been + started. + + The current record identifier can be accessed with getCurrentElement() + or isCurrentElement(). Used by BIFF import only. + */ + virtual void onStartRecord( SequenceInputStream& rStrm ) = 0; + + /** Will be called when the current record block is about to be left. + + The current record identifier can be accessed with getCurrentElement() + or isCurrentElement(). Used by BIFF import only. + */ + virtual void onEndRecord() = 0; + + // helpers ---------------------------------------------------------------- + + /** Returns the identifier of the currently processed element. Ignores MCE elements in stack */ + sal_Int32 getCurrentElement() const; + + /** Returns the identifier of the currently processed element - Including MCE root elements */ + sal_Int32 getCurrentElementWithMce() const; + + /** Returns true, if nElement contains the identifier of the currently + processed element. */ + inline bool isCurrentElement( sal_Int32 nElement ) const + { return getCurrentElement() == nElement; } + + /** Returns true, if either nElement1 or nElement2 contain the identifier + of the currently processed element. */ + inline bool isCurrentElement( sal_Int32 nElement1, sal_Int32 nElement2 ) const + { return isCurrentElement( nElement1 ) || isCurrentElement( nElement2 ); } + + /** Returns the identifier of the specified parent element. */ + sal_Int32 getParentElement( sal_Int32 nCountBack = 1 ) const; + + /** Returns true, if nElement contains the identifier of the specified + parent element. */ + inline sal_Int32 isParentElement( sal_Int32 nElement, sal_Int32 nCountBack = 1 ) const + { return getParentElement( nCountBack ) == nElement; } + + /** Returns true, if the element currently processed is the root element of + the context or fragment handler. */ + bool isRootElement() const; + + // implementation --------------------------------------------------------- + +protected: + /** Must be called from createFastChildContext() in derived classes. */ + ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > + implCreateChildContext( + sal_Int32 nElement, + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs ); + + /** Must be called from startFastElement() in derived classes. */ + void implStartElement( + sal_Int32 nElement, + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs ); + + /** Must be called from characters() in derived classes. */ + void implCharacters( const OUString& rChars ); + + /** Must be called from endFastElement() in derived classes. */ + void implEndElement( sal_Int32 nElement ); + + /** Must be called from createRecordContext() in derived classes. */ + ContextHandlerRef implCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ); + + /** Must be called from startRecord() in derived classes. */ + void implStartRecord( sal_Int32 nRecId, SequenceInputStream& rStrm ); + + /** Must be called from endRecord() in derived classes. */ + void implEndRecord( sal_Int32 nRecId ); + +private: + ContextHandler2Helper& operator=( const ContextHandler2Helper& ); + + ElementInfo& pushElementInfo( sal_Int32 nElement ); + void popElementInfo(); + void processCollectedChars(); + +private: + typedef ::std::vector< ElementInfo > ContextStack; + typedef ::boost::shared_ptr< ContextStack > ContextStackRef; + + ContextStackRef mxContextStack; ///< Stack of all processed elements. + size_t mnRootStackSize; ///< Stack size on construction time. + bool mbEnableTrimSpace; ///< True = trim whitespace in characters(). +}; + +// ============================================================================ + +class OOX_DLLPUBLIC ContextHandler2 : public ContextHandler, public ContextHandler2Helper +{ +public: + explicit ContextHandler2( ContextHandler2Helper& rParent ); + virtual ~ContextHandler2(); + + // resolve ambiguity from base classes + virtual void SAL_CALL acquire() throw() { ContextHandler::acquire(); } + virtual void SAL_CALL release() throw() { ContextHandler::release(); } + + // com.sun.star.xml.sax.XFastContextHandler interface --------------------- + + virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL + createFastChildContext( + sal_Int32 nElement, + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs ) + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + virtual void SAL_CALL startFastElement( + sal_Int32 nElement, + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs ) + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + virtual void SAL_CALL characters( const OUString& rChars ) + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + virtual void SAL_CALL endFastElement( sal_Int32 nElement ) + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + // oox.core.ContextHandler interface -------------------------------------- + + virtual ContextHandlerRef createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ); + virtual void startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm ); + virtual void endRecord( sal_Int32 nRecId ); + + // oox.core.ContextHandler2Helper interface ------------------------------- + + virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ); + virtual void onStartElement( const AttributeList& rAttribs ); + virtual void onCharacters( const OUString& rChars ); + virtual void onEndElement(); + + virtual ContextHandlerRef onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ); + virtual void onStartRecord( SequenceInputStream& rStrm ); + virtual void onEndRecord(); +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/fastparser.hxx b/include/oox/core/fastparser.hxx new file mode 100644 index 000000000000..40b5e763998f --- /dev/null +++ b/include/oox/core/fastparser.hxx @@ -0,0 +1,97 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_FASTPARSER_HXX +#define OOX_CORE_FASTPARSER_HXX + +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/xml/sax/XFastParser.hpp> + +namespace oox { + struct NamespaceMap; + class StorageBase; +} + +namespace oox { +namespace core { + +// ============================================================================ + +/** Wrapper for a fast SAX parser that works on automatically generated OOXML + token and namespace identifiers. + */ +class FastParser +{ +public: + explicit FastParser( + const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext ) + throw( ::com::sun::star::uno::RuntimeException ); + + virtual ~FastParser(); + + /** Registers an OOXML namespace at the parser. */ + void registerNamespace( sal_Int32 nNamespaceId ) + throw( ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException ); + + /** Sets the passed document handler that will receive the SAX parser events. */ + void setDocumentHandler( + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastDocumentHandler >& rxDocHandler ) + throw( ::com::sun::star::uno::RuntimeException ); + + /** Parses the passed SAX input source. + @param bCloseStream True = closes the stream in the input source after parsing. */ + void parseStream( const ::com::sun::star::xml::sax::InputSource& rInputSource, bool bCloseStream = false ) + throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException ); + + /** Parses the passed input stream. + @param bCloseStream True = closes the passed stream after parsing. */ + void parseStream( + const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStream, + const OUString& rStreamName, bool bCloseStream = false ) + throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException ); + + /** Parses a stream from the passed storage with the specified name. + @param bCloseStream True = closes the stream after parsing. */ + void parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream = false ) + throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException ); + + OUString getNamespaceURL( const OUString& rPrefix ) + throw( ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException ); + + sal_Int32 getNamespaceId( const OUString& aUrl ); + + ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > + getTokenHandler() const { return mxTokenHandler; } + +private: + ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastParser > + mxParser; + ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > + mxTokenHandler; + const NamespaceMap& mrNamespaceMap; +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/fasttokenhandler.hxx b/include/oox/core/fasttokenhandler.hxx new file mode 100644 index 000000000000..05c7063d3925 --- /dev/null +++ b/include/oox/core/fasttokenhandler.hxx @@ -0,0 +1,67 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_FASTTOKENHANDLER_HXX +#define OOX_CORE_FASTTOKENHANDLER_HXX + +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/xml/sax/XFastTokenHandler.hpp> +#include <cppuhelper/implbase2.hxx> + +namespace oox { class TokenMap; } + +namespace oox { +namespace core { + +// ============================================================================ + +typedef ::cppu::WeakImplHelper2< ::com::sun::star::lang::XServiceInfo, ::com::sun::star::xml::sax::XFastTokenHandler > FastTokenHandler_BASE; + +/** Wrapper implementing the com.sun.star.xml.sax.XFastTokenHandler API interface + that provides access to the tokens generated from the internal token name list. + */ +class FastTokenHandler : public FastTokenHandler_BASE +{ +public: + explicit FastTokenHandler(); + virtual ~FastTokenHandler(); + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName() throw (::com::sun::star::uno::RuntimeException); + virtual sal_Bool SAL_CALL supportsService( const OUString& rServiceName ) throw (::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (::com::sun::star::uno::RuntimeException); + + // XFastTokenHandler + virtual sal_Int32 SAL_CALL getToken( const OUString& rIdentifier ) throw (::com::sun::star::uno::RuntimeException); + virtual OUString SAL_CALL getIdentifier( sal_Int32 nToken ) throw (::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getUTF8Identifier( sal_Int32 nToken ) throw (::com::sun::star::uno::RuntimeException); + virtual sal_Int32 SAL_CALL getTokenFromUTF8( const ::com::sun::star::uno::Sequence< sal_Int8 >& Identifier ) throw (::com::sun::star::uno::RuntimeException); + +private: + const TokenMap& mrTokenMap; ///< Reference to global token map singleton. +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/filterbase.hxx b/include/oox/core/filterbase.hxx new file mode 100644 index 000000000000..6f4fc40499c7 --- /dev/null +++ b/include/oox/core/filterbase.hxx @@ -0,0 +1,293 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_FILTERBASE_HXX +#define OOX_CORE_FILTERBASE_HXX + +#include <memory> +#include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/document/XExporter.hpp> +#include <com/sun/star/document/XFilter.hpp> +#include <com/sun/star/document/XImporter.hpp> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/io/XStream.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <cppuhelper/basemutex.hxx> +#include <cppuhelper/implbase5.hxx> +#include <comphelper/sequenceashashmap.hxx> +#include "oox/helper/binarystreambase.hxx" +#include "oox/helper/storagebase.hxx" +#include "oox/dllapi.h" + +namespace com { namespace sun { namespace star { + namespace awt { struct DeviceInfo; } + namespace frame { class XFrame; } + namespace frame { class XModel; } + namespace drawing { class XShape; } + namespace graphic { class XGraphic; } + namespace io { class XInputStream; } + namespace io { class XOutputStream; } + namespace io { class XStream; } + namespace lang { class XMultiComponentFactory; } + namespace lang { class XMultiServiceFactory; } + namespace task { class XInteractionHandler; } + namespace task { class XStatusIndicator; } + namespace uno { class XComponentContext; } +} } } + +namespace comphelper { + class IDocPasswordVerifier; + class MediaDescriptor; +} + +namespace oox { + class GraphicHelper; + class ModelObjectHelper; +} + +namespace oox { namespace ole { + class OleObjectHelper; + class VbaProject; +} } + +namespace oox { +namespace core { + +// ============================================================================ + +enum OoxmlVersion +{ + ECMA_DIALECT, + ISOIEC_29500_2008 +}; + +struct FilterBaseImpl; + +typedef ::cppu::WeakImplHelper5< + ::com::sun::star::lang::XServiceInfo, + ::com::sun::star::lang::XInitialization, + ::com::sun::star::document::XImporter, + ::com::sun::star::document::XExporter, + ::com::sun::star::document::XFilter > + FilterBase_BASE; + +class OOX_DLLPUBLIC FilterBase : public FilterBase_BASE, public ::cppu::BaseMutex +{ +public: + explicit FilterBase( + const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext ) + throw( ::com::sun::star::uno::RuntimeException ); + + virtual ~FilterBase(); + + /** Returns true, if filter is an import filter. */ + bool isImportFilter() const; + /** Returns true, if filter is an export filter. */ + bool isExportFilter() const; + + OoxmlVersion getVersion() const; + + /** Derived classes implement import of the entire document. */ + virtual bool importDocument() = 0; + + /** Derived classes implement export of the entire document. */ + virtual bool exportDocument() = 0; + + // ------------------------------------------------------------------------ + + /** Returns the component context passed in the filter constructor (always existing). */ + const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& + getComponentContext() const; + + /** Returns the multi service factory of the component (always existing). */ + const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& + getServiceFactory() const; + + /** Returns the document model (always existing). */ + const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& + getModel() const; + + /** Returns the service factory provided by the document model (always existing). */ + const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& + getModelFactory() const; + + /** Returns the frame that will contain the document model (may be null). */ + const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& + getTargetFrame() const; + + /// Returns the parent shape to load into (if any) + const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& + getParentShape() const; + + /** Returns the status indicator (may be null). */ + const ::com::sun::star::uno::Reference< ::com::sun::star::task::XStatusIndicator >& + getStatusIndicator() const; + + /** Returns the FilterData */ + ::comphelper::SequenceAsHashMap& getFilterData() const; + + /** Returns the media descriptor. */ + ::comphelper::MediaDescriptor& getMediaDescriptor() const; + + /** Returns the URL of the imported or exported file. */ + const OUString& getFileUrl() const; + + /** Returns an absolute URL for the passed relative or absolute URL. */ + OUString getAbsoluteUrl( const OUString& rUrl ) const; + + /** Returns the base storage of the imported/exported file. */ + StorageRef getStorage() const; + + /** Opens and returns the specified input stream from the base storage. + + @param rStreamName + The name of the embedded storage stream. The name may contain + slashes to open streams from embedded substorages. If base stream + access has been enabled in the storage, the base stream can be + accessed by passing an empty string as stream name. + */ + ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > + openInputStream( const OUString& rStreamName ) const; + + /** Opens and returns the specified output stream from the base storage. + + @param rStreamName + The name of the embedded storage stream. The name may contain + slashes to open streams from embedded substorages. If base stream + access has been enabled in the storage, the base stream can be + accessed by passing an empty string as stream name. + */ + ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > + openOutputStream( const OUString& rStreamName ) const; + + /** Commits changes to base storage (and substorages) */ + void commitStorage() const; + + // helpers ---------------------------------------------------------------- + + /** Returns a helper for the handling of graphics and graphic objects. */ + GraphicHelper& getGraphicHelper() const; + + /** Returns a helper with containers for various named drawing objects for + the imported document. */ + ModelObjectHelper& getModelObjectHelper() const; + + /** Returns a helper for the handling of OLE objects. */ + ::oox::ole::OleObjectHelper& getOleObjectHelper() const; + + /** Returns the VBA project manager. */ + ::oox::ole::VbaProject& getVbaProject() const; + + /** Imports the raw binary data from the specified stream. + @return True, if the data could be imported from the stream. */ + bool importBinaryData( StreamDataSequence& orDataSeq, const OUString& rStreamName ); + + // com.sun.star.lang.XServiceInfo interface ------------------------------- + + virtual OUString SAL_CALL + getImplementationName() + throw( ::com::sun::star::uno::RuntimeException ); + + virtual sal_Bool SAL_CALL + supportsService( const OUString& rServiceName ) + throw( ::com::sun::star::uno::RuntimeException ); + + virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL + getSupportedServiceNames() + throw( ::com::sun::star::uno::RuntimeException ); + + // com.sun.star.lang.XInitialization interface ---------------------------- + + /** Receives user defined arguments. + + @param rArgs + the sequence of arguments passed to the filter. The implementation + expects one or two arguments. The first argument shall be the + com.sun.star.lang.XMultiServiceFactory interface of the global + service factory. The optional second argument may contain a + sequence of com.sun.star.beans.NamedValue objects. The different + filter implemetations may support different arguments. + */ + virtual void SAL_CALL initialize( + const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& rArgs ) + throw( ::com::sun::star::uno::Exception, + ::com::sun::star::uno::RuntimeException ); + + // com.sun.star.document.XImporter interface ------------------------------ + + virtual void SAL_CALL setTargetDocument( + const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& rxDocument ) + throw( ::com::sun::star::lang::IllegalArgumentException, + ::com::sun::star::uno::RuntimeException ); + + // com.sun.star.document.XExporter interface ------------------------------ + + virtual void SAL_CALL setSourceDocument( + const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& rxDocument ) + throw( ::com::sun::star::lang::IllegalArgumentException, + ::com::sun::star::uno::RuntimeException ); + + // com.sun.star.document.XFilter interface -------------------------------- + + virtual sal_Bool SAL_CALL filter( + const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rMediaDescSeq ) + throw( ::com::sun::star::uno::RuntimeException ); + + virtual void SAL_CALL cancel() + throw( ::com::sun::star::uno::RuntimeException ); + + // ------------------------------------------------------------------------ +protected: + virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > + implGetInputStream( ::comphelper::MediaDescriptor& rMediaDesc ) const; + virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream > + implGetOutputStream( ::comphelper::MediaDescriptor& rMediaDesc ) const; + +private: + void setMediaDescriptor( + const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rMediaDescSeq ); + + /** Derived classes may create a specialized graphic helper, e.g. for + resolving palette colors. */ + virtual GraphicHelper* implCreateGraphicHelper() const; + + /** Derived classes create a VBA project manager object. */ + virtual ::oox::ole::VbaProject* implCreateVbaProject() const = 0; + + virtual OUString implGetImplementationName() const = 0; + + virtual StorageRef implCreateStorage( + const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStream ) const = 0; + virtual StorageRef implCreateStorage( + const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream >& rxOutStream ) const = 0; + +private: + ::std::auto_ptr< FilterBaseImpl > mxImpl; +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/filterdetect.hxx b/include/oox/core/filterdetect.hxx new file mode 100644 index 000000000000..cd9573b0f0bd --- /dev/null +++ b/include/oox/core/filterdetect.hxx @@ -0,0 +1,162 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_FILTERDETECT_HXX +#define OOX_CORE_FILTERDETECT_HXX + +#include <vector> +#include <com/sun/star/document/XExtendedFilterDetection.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/xml/sax/XFastDocumentHandler.hpp> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/implbase2.hxx> +#include "oox/dllapi.h" + +namespace com { namespace sun { namespace star { + namespace io { class XInputStream; } + namespace uno { class XComponentContext; } +} } } + +namespace comphelper { class MediaDescriptor; } + +namespace oox { class AttributeList; } + +namespace oox { +namespace core { + +// ============================================================================ + +/** Document handler specifically designed for detecting OOXML file formats. + + It takes a reference to the filter string object via its constructor, and + puts the name of the detected filter to it, if it successfully finds one. + */ +class FilterDetectDocHandler : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XFastDocumentHandler > +{ +public: + explicit FilterDetectDocHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext, OUString& rFilter ); + virtual ~FilterDetectDocHandler(); + + // XFastDocumentHandler + virtual void SAL_CALL startDocument() throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL endDocument() throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL setDocumentLocator( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >& xLocator ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + + // XFastContextHandler + virtual void SAL_CALL startFastElement( sal_Int32 nElement, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL startUnknownElement( const OUString& Namespace, const OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL endFastElement( sal_Int32 Element ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL endUnknownElement( const OUString& Namespace, const OUString& Name ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Reference< XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& Namespace, const OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL characters( const OUString& aChars ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL processingInstruction( const OUString& aTarget, const OUString& aData ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + +private: + void parseRelationship( const AttributeList& rAttribs ); + + OUString getFilterNameFromContentType( const OUString& rContentType ) const; + void parseContentTypesDefault( const AttributeList& rAttribs ); + void parseContentTypesOverride( const AttributeList& rAttribs ); + +private: + typedef ::std::vector< sal_Int32 > ContextVector; + + OUString& mrFilterName; + ContextVector maContextStack; + OUString maTargetPath; + ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > mxContext; +}; + +// ============================================================================ + +class OOX_DLLPUBLIC FilterDetect : public ::cppu::WeakImplHelper2< ::com::sun::star::document::XExtendedFilterDetection, ::com::sun::star::lang::XServiceInfo > +{ +public: + explicit FilterDetect( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext ) + throw( ::com::sun::star::uno::RuntimeException ); + virtual ~FilterDetect(); + + /** Tries to extract an unencrypted ZIP package from the passed media + descriptor. + + First, this function checks if the input stream provided by the media + descriptor property 'InputStream' contains a ZIP package. If yes, this + stream is returned. + + Second, this function checks if the 'ComponentData' property exists and + contains a sequence of com.sun.star.beans.NamedValue. If yes, a named + value is searched with the name 'DecryptedPackage' and a value of type + com.sun.star.io.XStream. If the input stream provided by this XStream + contains a ZIP package, this input stream is returned. + + Third, this function checks if the input stream of the media descriptor + contains an OLE package. If yes, it checks the existence of the streams + 'EncryptionInfo' and 'EncyptedPackage' and tries to decrypt the package + into a temporary file. This may include requesting a password from the + media descriptor property 'Password' or from the user, using the + interaction handler provided by the descriptor. On success, and if the + decrypted package is a ZIP package, the XStream of the temporary file + is stored in the property 'ComponentData' of the media descriptor and + its input stream is returned. + */ + ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > + extractUnencryptedPackage( ::comphelper::MediaDescriptor& rMediaDesc ) const; + + // com.sun.star.lang.XServiceInfo interface ------------------------------- + + virtual OUString SAL_CALL getImplementationName() throw( ::com::sun::star::uno::RuntimeException ); + virtual sal_Bool SAL_CALL supportsService( const OUString& rServiceName ) throw( ::com::sun::star::uno::RuntimeException ); + virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException ); + + // com.sun.star.document.XExtendedFilterDetection interface --------------- + + /** Detects MS Office 2007 file types and supports package decryption. + + The following file types are detected: + - MS Word 2007 XML Document (*.docx, *.docm) + - MS Word 2007 XML Template (*.dotx, *.dotm) + - MS Excel 2007 XML Document (*.xlsx, *.xlsm) + - MS Excel 2007 BIFF12 Document (*.xlsb) + - MS Excel 2007 XML Template (*.xltx, *.xltm) + - MS Powerpoint 2007 XML Document (*.pptx, *.pptm) + - MS Powerpoint 2007 XML Template (*.potx, *.potm) + + If the package is encrypted, the detection tries to decrypt it into a + temporary file. The user may be asked for a password. The XStream + interface of the temporary file will be stored in the 'ComponentData' + property of the passed media descriptor. + */ + virtual OUString SAL_CALL + detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rMediaDescSeq ) + throw( ::com::sun::star::uno::RuntimeException ); + +private: + ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > mxContext; +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/fragmenthandler.hxx b/include/oox/core/fragmenthandler.hxx new file mode 100644 index 000000000000..236e212e6e32 --- /dev/null +++ b/include/oox/core/fragmenthandler.hxx @@ -0,0 +1,131 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_FRAGMENTHANDLER_HXX +#define OOX_CORE_FRAGMENTHANDLER_HXX + +#include <com/sun/star/xml/sax/XFastDocumentHandler.hpp> +#include <cppuhelper/implbase1.hxx> +#include "oox/core/contexthandler.hxx" +#include "oox/core/relations.hxx" +#include "oox/dllapi.h" + +namespace com { namespace sun { namespace star { + namespace io { class XInputStream; } +} } } + +namespace oox { +namespace core { + +// ============================================================================ + +/** Base data of a fragment. + + This data is stored in a separate struct to make it accessible in every + child context handler of the fragment. + */ +struct FragmentBaseData +{ + XmlFilterBase& mrFilter; + const OUString maFragmentPath; + ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator > + mxLocator; + RelationsRef mxRelations; + + explicit FragmentBaseData( + XmlFilterBase& rFilter, + const OUString& rFragmentPath, + RelationsRef xRelations ); +}; + +// ============================================================================ + +/** Describes record identifiers used to create contexts in a binary stream. + + If a record is used to start a new context, usually the record identifier + increased by 1 is used to mark the end of this context, e.g. the Excel + record SHEETDATA == 0x0091 starts the <sheetData> context, and the record + SHEETDATA_END == 0x0092 ends this context. But some records are used to + start a new context, though there is no identifier to end this context, + e.g. the ROW or EXTROW records. These record identifiers can be marked by + setting the mnEndRecId member of this struct to -1. + */ +struct RecordInfo +{ + sal_Int32 mnStartRecId; ///< Record identifier for context start. + sal_Int32 mnEndRecId; ///< Record identifier for context end, -1 = no record. +}; + +// ============================================================================ + +typedef ::cppu::ImplInheritanceHelper1< ContextHandler, ::com::sun::star::xml::sax::XFastDocumentHandler > FragmentHandler_BASE; + +class OOX_DLLPUBLIC FragmentHandler : public FragmentHandler_BASE +{ +public: + explicit FragmentHandler( XmlFilterBase& rFilter, const OUString& rFragmentPath ); + virtual ~FragmentHandler(); + + /** Returns the com.sun.star.xml.sax.XFastContextHandler interface of this context. */ + inline ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > + getFastContextHandler() { return static_cast< ContextHandler* >( this ); } + + // com.sun.star.xml.sax.XFastDocumentHandler interface -------------------- + + virtual void SAL_CALL startDocument() throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL endDocument() throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL setDocumentLocator( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >& rxLocator ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + + // com.sun.star.xml.sax.XFastContextHandler interface --------------------- + + virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL startUnknownElement( const OUString& Namespace, const OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL endFastElement( ::sal_Int32 Element ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL endUnknownElement( const OUString& Namespace, const OUString& Name ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& Namespace, const OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL characters( const OUString& aChars ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL processingInstruction( const OUString& aTarget, const OUString& aData ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + + // XML stream handling ---------------------------------------------------- + + /** Opens the fragment stream referred by the own fragment path. Derived + classes may provide specilized stream implementations. */ + virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > + openFragmentStream() const; + + // binary records --------------------------------------------------------- + + virtual const RecordInfo* getRecordInfos() const; + +protected: + explicit FragmentHandler( XmlFilterBase& rFilter, const OUString& rFragmentPath, RelationsRef xRelations ); +}; + +typedef ::rtl::Reference< FragmentHandler > FragmentHandlerRef; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/fragmenthandler2.hxx b/include/oox/core/fragmenthandler2.hxx new file mode 100644 index 000000000000..21ad03619fd1 --- /dev/null +++ b/include/oox/core/fragmenthandler2.hxx @@ -0,0 +1,121 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_FRAGMENTHANDLER2_HXX +#define OOX_CORE_FRAGMENTHANDLER2_HXX + +#include "oox/core/contexthandler2.hxx" +#include "oox/core/fragmenthandler.hxx" +#include <vector> +#include "oox/dllapi.h" + +namespace oox { +namespace core { + +// ============================================================================ + +class OOX_DLLPUBLIC FragmentHandler2 : public FragmentHandler, public ContextHandler2Helper +{ +protected: + enum MCE_STATE + { + MCE_UNUSED, + MCE_STARTED, + MCE_FOUND_CHOICE + }; + ::std::vector<MCE_STATE> aMceState; + + bool prepareMceContext( sal_Int32 nElement, const AttributeList& rAttribs ); + + +public: + explicit FragmentHandler2( + XmlFilterBase& rFilter, + const OUString& rFragmentPath, + bool bEnableTrimSpace = true ); + virtual ~FragmentHandler2(); + + // resolve ambiguity from base classes + virtual void SAL_CALL acquire() throw() { FragmentHandler::acquire(); } + virtual void SAL_CALL release() throw() { FragmentHandler::release(); } + + // com.sun.star.xml.sax.XFastContextHandler interface --------------------- + + virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL + createFastChildContext( + sal_Int32 nElement, + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs ) + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + virtual void SAL_CALL startFastElement( + sal_Int32 nElement, + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs ) + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + virtual void SAL_CALL characters( const OUString& rChars ) + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + virtual void SAL_CALL endFastElement( sal_Int32 nElement ) + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + // com.sun.star.xml.sax.XFastDocumentHandler interface -------------------- + + virtual void SAL_CALL startDocument() + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + virtual void SAL_CALL endDocument() + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::uno::RuntimeException ); + + // oox.core.ContextHandler interface -------------------------------------- + + virtual ContextHandlerRef createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ); + virtual void startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm ); + virtual void endRecord( sal_Int32 nRecId ); + + // oox.core.ContextHandler2Helper interface ------------------------------- + + virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ); + virtual void onStartElement( const AttributeList& rAttribs ); + virtual void onCharacters( const OUString& rChars ); + virtual void onEndElement(); + + virtual ContextHandlerRef onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ); + virtual void onStartRecord( SequenceInputStream& rStrm ); + virtual void onEndRecord(); + + // oox.core.FragmentHandler2 interface ------------------------------------ + + virtual void initializeImport(); + virtual void finalizeImport(); +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/recordparser.hxx b/include/oox/core/recordparser.hxx new file mode 100644 index 000000000000..9e417c03d01a --- /dev/null +++ b/include/oox/core/recordparser.hxx @@ -0,0 +1,90 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_RECORDPARSER_HXX +#define OOX_CORE_RECORDPARSER_HXX + +#include <map> +#include <com/sun/star/io/IOException.hpp> +#include <com/sun/star/xml/sax/SAXException.hpp> +#include <rtl/ref.hxx> +#include "oox/helper/binaryinputstream.hxx" + +namespace oox { +namespace core { + +class FragmentHandler; +struct RecordInfo; + +namespace prv { class Locator; } +namespace prv { class ContextStack; } + +// ============================================================================ + +struct RecordInputSource +{ + BinaryInputStreamRef mxInStream; + OUString maPublicId; + OUString maSystemId; +}; + +// ============================================================================ + +class RecordParser +{ +public: + explicit RecordParser(); + virtual ~RecordParser(); + + void setFragmentHandler( const ::rtl::Reference< FragmentHandler >& rxHandler ); + + void parseStream( const RecordInputSource& rInputSource ) + throw( ::com::sun::star::xml::sax::SAXException, + ::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException ); + + inline const RecordInputSource& getInputSource() const { return maSource; } + +private: + /** Returns a RecordInfo struct that contains the passed record identifier + as context start identifier. */ + const RecordInfo* getStartRecordInfo( sal_Int32 nRecId ) const; + /** Returns a RecordInfo struct that contains the passed record identifier + as context end identifier. */ + const RecordInfo* getEndRecordInfo( sal_Int32 nRecId ) const; + +private: + typedef ::std::map< sal_Int32, RecordInfo > RecordInfoMap; + + RecordInputSource maSource; + ::rtl::Reference< FragmentHandler > mxHandler; + ::rtl::Reference< prv::Locator > mxLocator; + ::std::auto_ptr< prv::ContextStack > mxStack; + RecordInfoMap maStartMap; + RecordInfoMap maEndMap; +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/relations.hxx b/include/oox/core/relations.hxx new file mode 100644 index 000000000000..b78d0d3056eb --- /dev/null +++ b/include/oox/core/relations.hxx @@ -0,0 +1,103 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_RELATIONS_HXX +#define OOX_CORE_RELATIONS_HXX + +#include <map> +#include <boost/shared_ptr.hpp> +#include "oox/helper/helper.hxx" +#include "oox/dllapi.h" + +namespace oox { +namespace core { + +// ============================================================================ + +/** Expands to an OUString containing an 'officeDocument' relation type created + from the passed literal(!) ASCII(!) character array. */ +#define CREATE_OFFICEDOC_RELATION_TYPE( ascii ) \ + ( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/" ascii ) + +/** Expands to an OUString containing a 'package' relation type created from + the passed literal(!) ASCII(!) character array. */ +#define CREATE_PACKAGE_RELATION_TYPE( ascii ) \ + ( "http://schemas.openxmlformats.org/package/2006/relationships/" ascii ) + +/** Expands to an OUString containing an MS Office specific relation type + created from the passed literal(!) ASCII(!) character array. */ +#define CREATE_MSOFFICE_RELATION_TYPE( ascii ) \ + ( "http://schemas.microsoft.com/office/2006/relationships/" ascii ) + +// ============================================================================ + +struct Relation +{ + OUString maId; + OUString maType; + OUString maTarget; + bool mbExternal; + + inline explicit Relation() : mbExternal( false ) {} +}; + +// ============================================================================ + +class Relations; +typedef ::boost::shared_ptr< Relations > RelationsRef; + +class OOX_DLLPUBLIC Relations : public ::std::map< OUString, Relation > +{ +public: + explicit Relations( const OUString& rFragmentPath ); + + /** Returns the path of the fragment this relations collection is related to. */ + inline const OUString& getFragmentPath() const { return maFragmentPath; } + + /** Returns the relation with the passed relation identifier. */ + const Relation* getRelationFromRelId( const OUString& rId ) const; + /** Returns the first relation with the passed type. */ + const Relation* getRelationFromFirstType( const OUString& rType ) const; + /** Finds all relations associated with the passed type. */ + RelationsRef getRelationsFromType( const OUString& rType ) const; + + /** Returns the external target of the relation with the passed relation identifier. */ + OUString getExternalTargetFromRelId( const OUString& rRelId ) const; + /** Returns the internal target of the relation with the passed relation identifier. */ + OUString getInternalTargetFromRelId( const OUString& rRelId ) const; + + /** Returns the full fragment path for the target of the passed relation. */ + OUString getFragmentPathFromRelation( const Relation& rRelation ) const; + /** Returns the full fragment path for the passed relation identifier. */ + OUString getFragmentPathFromRelId( const OUString& rRelId ) const; + /** Returns the full fragment path for the first relation of the passed type. */ + OUString getFragmentPathFromFirstType( const OUString& rType ) const; + +private: + OUString maFragmentPath; +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/relationshandler.hxx b/include/oox/core/relationshandler.hxx new file mode 100644 index 000000000000..d42e5d1ad8b7 --- /dev/null +++ b/include/oox/core/relationshandler.hxx @@ -0,0 +1,54 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_RELATIONSHANDLER_HXX +#define OOX_CORE_RELATIONSHANDLER_HXX + +#include "oox/core/fragmenthandler.hxx" + +namespace oox { +namespace core { + +// ============================================================================ + +class RelationsFragment : public FragmentHandler +{ +public: + explicit RelationsFragment( + XmlFilterBase& rFilter, + RelationsRef xRelations ); + + virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL + createFastChildContext( + sal_Int32 nElement, + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs ) + throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException); + +private: + RelationsRef mxRelations; +}; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/oox/core/xmlfilterbase.hxx b/include/oox/core/xmlfilterbase.hxx new file mode 100644 index 000000000000..4b5c1e093657 --- /dev/null +++ b/include/oox/core/xmlfilterbase.hxx @@ -0,0 +1,256 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef OOX_CORE_XMLFILTERBASE_HXX +#define OOX_CORE_XMLFILTERBASE_HXX + +#include <com/sun/star/text/XText.hpp> +#include <com/sun/star/text/XTextCursor.hpp> +#include <com/sun/star/text/XTextField.hpp> +#include <rtl/ref.hxx> +#include <rtl/string.hxx> +#include <rtl/ustring.hxx> +#include "oox/core/filterbase.hxx" +#include "oox/core/relations.hxx" +#include "oox/drawingml/table/tablestylelist.hxx" +#include "oox/dllapi.h" + +namespace com { namespace sun { namespace star { + namespace container { class XNameContainer; } + namespace document { class XDocumentProperties; } + namespace xml { namespace dom { class XDocument; } } + namespace xml { namespace sax { class XLocator; } } + namespace xml { namespace sax { class XFastDocumentHandler; } } + namespace xml { namespace sax { class XFastSAXSerializable; } } +} } } + +namespace oox { + namespace drawingml { class Theme; } + namespace drawingml { namespace chart { class ChartConverter; } } + namespace vml { class Drawing; } +} + +namespace sax_fastparser { + class FastSerializerHelper; + + typedef boost::shared_ptr< FastSerializerHelper > FSHelperPtr; +} + +namespace oox { +namespace core { + +class FragmentHandler; + +// ============================================================================ + +struct TextField { + com::sun::star::uno::Reference< com::sun::star::text::XText > xText; + com::sun::star::uno::Reference< com::sun::star::text::XTextCursor > xTextCursor; + com::sun::star::uno::Reference< com::sun::star::text::XTextField > xTextField; +}; +typedef std::vector< TextField > TextFieldStack; + +// ============================================================================ + +struct XmlFilterBaseImpl; + +class OOX_DLLPUBLIC XmlFilterBase : public FilterBase +{ +public: + explicit XmlFilterBase( + const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext ) + throw( ::com::sun::star::uno::RuntimeException ); + + virtual ~XmlFilterBase(); + + /** Has to be implemented by each filter, returns the current theme. */ + virtual const ::oox::drawingml::Theme* + getCurrentTheme() const = 0; + + /** Has to be implemented by each filter to return the collection of VML shapes. */ + virtual ::oox::vml::Drawing* getVmlDrawing() = 0; + + /** Has to be implemented by each filter, returns a filter-specific chart + converter object, that should be global per imported document. */ + virtual ::oox::drawingml::chart::ChartConverter* getChartConverter() = 0; + + /** Has to be implemented by each filter to return the table style list. */ + virtual const ::oox::drawingml::table::TableStyleListPtr getTableStyles() = 0; + + // ------------------------------------------------------------------------ + + /** Returns the fragment path from the first relation of the passed type, + used for fragments referred by the root relations. */ + OUString getFragmentPathFromFirstType( const OUString& rType ); + + /** Imports a fragment using the passed fragment handler, which contains + the full path to the fragment stream. + + @return True, if the fragment could be imported. + */ + bool importFragment( const ::rtl::Reference< FragmentHandler >& rxHandler ); + + /** Imports a fragment into an xml::dom::XDocument. + + @param rFragmentPath path to fragment + + @return a non-empty reference to the XDocument, if the + fragment could be imported. + */ + ::com::sun::star::uno::Reference< + ::com::sun::star::xml::dom::XDocument> importFragment( const OUString& rFragmentPath ); + + /** Imports a fragment from an xml::dom::XDocument using the + passed fragment handler + + @param rxHandler fragment handler; path to fragment is + ignored, input source is the rxSerializer + + @param rxSerializer usually retrieved from a + xml::dom::XDocument, will get serialized into rxHandler + + @return true, if the fragment could be imported. + */ + bool importFragment( const ::rtl::Reference< FragmentHandler >& rxHandler, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastSAXSerializable >& rxSerializer ); + + /** Imports the relations fragment associated with the specified fragment. + + @return The relations collection of the specified fragment. + */ + RelationsRef importRelations( const OUString& rFragmentPath ); + + /** Adds new relation. + + @param rType + Relation type. + + @param rTarget + Relation target. + + @return Added relation Id. + */ + OUString addRelation( const OUString& rType, const OUString& rTarget, bool bExternal = false ); + + /** Adds new relation to part's relations. + + @param rPartName + Part name the relations are related to. The relations will be stored in <rPartName::path>/_rels/<rPartName::name>.rels. + + @param rType + Relation type. + + @param rTarget + Relation target. + + @return Added relation Id. + */ + OUString addRelation( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > xOutputStream, const OUString& rType, const OUString& rTarget, bool bExternal = false ); + + /** Returns a stack of used textfields, used by the pptx importer to replace links to slidepages with rhe real page name */ + TextFieldStack& getTextFieldStack() const; + + /** Opens and returns the specified output stream from the base storage with specified media type. + + @param rStreamName + The name of the embedded storage stream. The name may contain + slashes to open streams from embedded substorages. If base stream + access has been enabled in the storage, the base stream can be + accessed by passing an empty string as stream name. + + @param rMediaType + The media type string, used in [Content_Types].xml stream in base + storage. + + @return The opened output stream. + */ + ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > + openFragmentStream( + const OUString& rStreamName, + const OUString& rMediaType ); + + /** Opens specified output stream from the base storage with specified + media type and returns new fast serializer for that stream. + + @param rStreamName + The name of the embedded storage stream. The name may contain + slashes to open streams from embedded substorages. If base stream + access has been enabled in the storage, the base stream can be + accessed by passing an empty string as stream name. + + @param rMediaType + The media type string, used in [Content_Types].xml stream in base + storage. + + @return newly created serializer helper. + */ + ::sax_fastparser::FSHelperPtr + openFragmentStreamWithSerializer( + const OUString& rStreamName, + const OUString& rMediaType ); + + /** Returns new unique ID for exported document. + + @return newly created ID. + */ + inline sal_Int32 GetUniqueId() { return mnMaxDocId++; } + inline OString GetUniqueIdOString() { return OString::valueOf( mnMaxDocId++ ); } + inline OUString GetUniqueIdOUString() { return OUString::valueOf( mnMaxDocId++ ); } + + /** Write the document properties into into the current OPC package. + + @param xProperties The document properties to export. + + @return *this + */ + XmlFilterBase& exportDocumentProperties( ::com::sun::star::uno::Reference< ::com::sun::star::document::XDocumentProperties > xProperties ); + + OUString getNamespaceURL( const OUString& rPrefix ); + + sal_Int32 getNamespaceId( const OUString& rUrl ); + + void importDocumentProperties(); + +protected: + virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > + implGetInputStream( ::comphelper::MediaDescriptor& rMediaDesc ) const; + +private: + virtual StorageRef implCreateStorage( + const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStream ) const; + virtual StorageRef implCreateStorage( + const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream >& rxOutStream ) const; + +private: + ::std::auto_ptr< XmlFilterBaseImpl > mxImpl; + sal_Int32 mnRelId; + sal_Int32 mnMaxDocId; +}; + +typedef ::rtl::Reference< XmlFilterBase > XmlFilterRef; + +// ============================================================================ + +} // namespace core +} // namespace oox + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |