xref: /trunk/main/oox/inc/oox/core/filterdetect.hxx (revision 506fa58b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef OOX_CORE_FILTERDETECT_HXX
25 #define OOX_CORE_FILTERDETECT_HXX
26 
27 #include <vector>
28 #include <com/sun/star/document/XExtendedFilterDetection.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
31 #include <cppuhelper/implbase1.hxx>
32 #include <cppuhelper/implbase2.hxx>
33 #include <comphelper/logging.hxx>
34 #include "oox/dllapi.h"
35 
36 namespace com { namespace sun { namespace star {
37     namespace io { class XInputStream; }
38     namespace uno { class XComponentContext; }
39 } } }
40 
41 namespace comphelper { class MediaDescriptor; }
42 
43 namespace oox { class AttributeList; }
44 
45 namespace oox {
46 namespace core {
47 
48 // ============================================================================
49 
50 /** Document handler specifically designed for detecting OOXML file formats.
51 
52     It takes a reference to the filter string object via its constructor, and
53     puts the name of the detected filter to it, if it successfully finds one.
54  */
55 class FilterDetectDocHandler : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XFastDocumentHandler >
56 {
57 public:
58     explicit            FilterDetectDocHandler( ::rtl::OUString& rFilter );
59     virtual             ~FilterDetectDocHandler();
60 
61     // XFastDocumentHandler
62     virtual void SAL_CALL startDocument() throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
63     virtual void SAL_CALL endDocument() throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
64     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);
65 
66     // XFastContextHandler
67     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);
68     virtual void SAL_CALL startUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::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);
69     virtual void SAL_CALL endFastElement( sal_Int32 Element ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
70     virtual void SAL_CALL endUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
71     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);
72     virtual ::com::sun::star::uno::Reference< XFastContextHandler > SAL_CALL createUnknownChildContext( const ::rtl::OUString& Namespace, const ::rtl::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);
73     virtual void SAL_CALL characters( const ::rtl::OUString& aChars ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
74     virtual void SAL_CALL ignorableWhitespace( const ::rtl::OUString& aWhitespaces ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
75     virtual void SAL_CALL processingInstruction( const ::rtl::OUString& aTarget, const ::rtl::OUString& aData ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
76 
77 private:
78     void                parseRelationship( const AttributeList& rAttribs );
79 
80     ::rtl::OUString     getFilterNameFromContentType( const ::rtl::OUString& rContentType ) const;
81     void                parseContentTypesDefault( const AttributeList& rAttribs );
82     void                parseContentTypesOverride( const AttributeList& rAttribs );
83 
84 private:
85     typedef ::std::vector< sal_Int32 > ContextVector;
86 
87     ::rtl::OUString&    mrFilterName;
88     ContextVector       maContextStack;
89     ::rtl::OUString     maTargetPath;
90 };
91 
92 // ============================================================================
93 
94 class OOX_DLLPUBLIC FilterDetect : public ::cppu::WeakImplHelper2< ::com::sun::star::document::XExtendedFilterDetection, ::com::sun::star::lang::XServiceInfo >
95 {
96 public:
97     explicit            FilterDetect( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext )
98                             throw( ::com::sun::star::uno::RuntimeException );
99     virtual             ~FilterDetect();
100 
101     /** Tries to extract an unencrypted ZIP package from the passed media
102         descriptor.
103 
104         First, this function checks if the input stream provided by the media
105         descriptor property 'InputStream' contains a ZIP package. If yes, this
106         stream is returned.
107 
108         Second, this function checks if the 'ComponentData' property exists and
109         contains a sequence of com.sun.star.beans.NamedValue. If yes, a named
110         value is searched with the name 'DecryptedPackage' and a value of type
111         com.sun.star.io.XStream. If the input stream provided by this XStream
112         contains a ZIP package, this input stream is returned.
113 
114         Third, this function checks if the input stream of the media descriptor
115         contains an OLE package. If yes, it checks the existence of the streams
116         'EncryptionInfo' and 'EncyptedPackage' and tries to decrypt the package
117         into a temporary file. This may include requesting a password from the
118         media descriptor property 'Password' or from the user, using the
119         interaction handler provided by the descriptor. On success, and if the
120         decrypted package is a ZIP package, the XStream of the temporary file
121         is stored in the property 'ComponentData' of the media descriptor and
122         its input stream is returned.
123      */
124     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >
125                         extractUnencryptedPackage( ::comphelper::MediaDescriptor& rMediaDesc ) const;
126 
127     // com.sun.star.lang.XServiceInfo interface -------------------------------
128 
129     virtual ::rtl::OUString SAL_CALL getImplementationName() throw( ::com::sun::star::uno::RuntimeException );
130     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& rServiceName ) throw( ::com::sun::star::uno::RuntimeException );
131     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException );
132 
133     // com.sun.star.document.XExtendedFilterDetection interface ---------------
134 
135     /** Detects MS Office 2007 file types and supports package decryption.
136 
137         The following file types are detected:
138         - MS Word 2007 XML Document (*.docx, *.docm)
139         - MS Word 2007 XML Template (*.dotx, *.dotm)
140         - MS Excel 2007 XML Document (*.xlsx, *.xlsm)
141         - MS Excel 2007 BIFF12 Document (*.xlsb)
142         - MS Excel 2007 XML Template (*.xltx, *.xltm)
143         - MS PowerPoint 2007 XML Document (*.pptx, *.pptm)
144         - MS PowerPoint 2007 XML Template (*.potx, *.potm)
145 
146         If the package is encrypted, the detection tries to decrypt it into a
147         temporary file. The user may be asked for a password. The XStream
148         interface of the temporary file will be stored in the 'ComponentData'
149         property of the passed media descriptor.
150      */
151     virtual ::rtl::OUString SAL_CALL
152                         detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rMediaDescSeq )
153                             throw( ::com::sun::star::uno::RuntimeException );
154 
155 private:
156     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > mxContext;
157     ::comphelper::EventLogger logger;
158 };
159 
160 // ============================================================================
161 
162 } // namespace core
163 } // namespace oox
164 
165 #endif
166