xref: /aoo4110/main/oox/inc/oox/core/filterdetect.hxx (revision b1cdbd2c)
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 "oox/dllapi.h"
34 
35 namespace com { namespace sun { namespace star {
36     namespace io { class XInputStream; }
37     namespace uno { class XComponentContext; }
38 } } }
39 
40 namespace comphelper { class MediaDescriptor; }
41 
42 namespace oox { class AttributeList; }
43 
44 namespace oox {
45 namespace core {
46 
47 // ============================================================================
48 
49 /** Document handler specifically designed for detecting OOXML file formats.
50 
51     It takes a reference to the filter string object via its constructor, and
52     puts the name of the detected filter to it, if it successfully finds one.
53  */
54 class FilterDetectDocHandler : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XFastDocumentHandler >
55 {
56 public:
57     explicit            FilterDetectDocHandler( ::rtl::OUString& rFilter );
58     virtual             ~FilterDetectDocHandler();
59 
60     // XFastDocumentHandler
61     virtual void SAL_CALL startDocument() throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
62     virtual void SAL_CALL endDocument() throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
63     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);
64 
65     // XFastContextHandler
66     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);
67     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);
68     virtual void SAL_CALL endFastElement( sal_Int32 Element ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
69     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);
70     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);
71     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);
72     virtual void SAL_CALL characters( const ::rtl::OUString& aChars ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
73     virtual void SAL_CALL ignorableWhitespace( const ::rtl::OUString& aWhitespaces ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
74     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);
75 
76 private:
77     void                parseRelationship( const AttributeList& rAttribs );
78 
79     ::rtl::OUString     getFilterNameFromContentType( const ::rtl::OUString& rContentType ) const;
80     void                parseContentTypesDefault( const AttributeList& rAttribs );
81     void                parseContentTypesOverride( const AttributeList& rAttribs );
82 
83 private:
84     typedef ::std::vector< sal_Int32 > ContextVector;
85 
86     ::rtl::OUString&    mrFilterName;
87     ContextVector       maContextStack;
88     ::rtl::OUString     maTargetPath;
89 };
90 
91 // ============================================================================
92 
93 class OOX_DLLPUBLIC FilterDetect : public ::cppu::WeakImplHelper2< ::com::sun::star::document::XExtendedFilterDetection, ::com::sun::star::lang::XServiceInfo >
94 {
95 public:
96     explicit            FilterDetect( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext )
97                             throw( ::com::sun::star::uno::RuntimeException );
98     virtual             ~FilterDetect();
99 
100     /** Tries to extract an unencrypted ZIP package from the passed media
101         descriptor.
102 
103         First, this function checks if the input stream provided by the media
104         descriptor property 'InputStream' contains a ZIP package. If yes, this
105         stream is returned.
106 
107         Second, this function checks if the 'ComponentData' property exists and
108         contains a sequence of com.sun.star.beans.NamedValue. If yes, a named
109         value is searched with the name 'DecryptedPackage' and a value of type
110         com.sun.star.io.XStream. If the input stream provided by this XStream
111         contains a ZIP package, this input stream is returned.
112 
113         Third, this function checks if the input stream of the media descriptor
114         contains an OLE package. If yes, it checks the existence of the streams
115         'EncryptionInfo' and 'EncyptedPackage' and tries to decrypt the package
116         into a temporary file. This may include requesting a password from the
117         media descriptor property 'Password' or from the user, using the
118         interaction handler provided by the descriptor. On success, and if the
119         decrypted package is a ZIP package, the XStream of the temporary file
120         is stored in the property 'ComponentData' of the media descriptor and
121         its input stream is returned.
122      */
123     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >
124                         extractUnencryptedPackage( ::comphelper::MediaDescriptor& rMediaDesc ) const;
125 
126     // com.sun.star.lang.XServiceInfo interface -------------------------------
127 
128     virtual ::rtl::OUString SAL_CALL getImplementationName() throw( ::com::sun::star::uno::RuntimeException );
129     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& rServiceName ) throw( ::com::sun::star::uno::RuntimeException );
130     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException );
131 
132     // com.sun.star.document.XExtendedFilterDetection interface ---------------
133 
134     /** Detects MS Office 2007 file types and supports package decryption.
135 
136         The following file types are detected:
137         - MS Word 2007 XML Document (*.docx, *.docm)
138         - MS Word 2007 XML Template (*.dotx, *.dotm)
139         - MS Excel 2007 XML Document (*.xlsx, *.xlsm)
140         - MS Excel 2007 BIFF12 Document (*.xlsb)
141         - MS Excel 2007 XML Template (*.xltx, *.xltm)
142         - MS Powerpoint 2007 XML Document (*.pptx, *.pptm)
143         - MS Powerpoint 2007 XML Template (*.potx, *.potm)
144 
145         If the package is encrypted, the detection tries to decrypt it into a
146         temporary file. The user may be asked for a password. The XStream
147         interface of the temporary file will be stored in the 'ComponentData'
148         property of the passed media descriptor.
149      */
150     virtual ::rtl::OUString SAL_CALL
151                         detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rMediaDescSeq )
152                             throw( ::com::sun::star::uno::RuntimeException );
153 
154 private:
155     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > mxContext;
156 };
157 
158 // ============================================================================
159 
160 } // namespace core
161 } // namespace oox
162 
163 #endif
164