Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* MSWorksImportFilter: Sets up the filter, and calls DocumentCollector
3 : * to do the actual filtering
4 : *
5 : * This file is part of the LibreOffice project.
6 : *
7 : * This Source Code Form is subject to the terms of the Mozilla Public
8 : * License, v. 2.0. If a copy of the MPL was not distributed with this
9 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 : */
11 :
12 : #include <osl/diagnose.h>
13 : #include <rtl/tencinfo.h>
14 :
15 : #include <com/sun/star/io/XInputStream.hpp>
16 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
17 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
18 : #include <com/sun/star/xml/sax/InputSource.hpp>
19 : #include <com/sun/star/xml/sax/XParser.hpp>
20 : #include <com/sun/star/io/XSeekable.hpp>
21 : #include <com/sun/star/uno/Reference.h>
22 : #include <cppuhelper/supportsservice.hxx>
23 :
24 : #include <xmloff/attrlist.hxx>
25 : #include <ucbhelper/content.hxx>
26 :
27 : #include <libwps/libwps.h>
28 :
29 : #include <libodfgen/libodfgen.hxx>
30 :
31 : #include "common/DocumentHandler.hxx"
32 : #include "common/WPXSvStream.hxx"
33 : #include "MSWorksImportFilter.hxx"
34 :
35 : #include <iostream>
36 :
37 : using namespace ::com::sun::star::uno;
38 : using com::sun::star::uno::Sequence;
39 : using com::sun::star::uno::Reference;
40 : using com::sun::star::uno::Any;
41 : using com::sun::star::uno::UNO_QUERY;
42 : using com::sun::star::uno::XInterface;
43 : using com::sun::star::uno::Exception;
44 : using com::sun::star::uno::RuntimeException;
45 : using com::sun::star::beans::PropertyValue;
46 : using com::sun::star::document::XFilter;
47 : using com::sun::star::document::XExtendedFilterDetection;
48 : using com::sun::star::ucb::XCommandEnvironment;
49 :
50 : using com::sun::star::io::XInputStream;
51 : using com::sun::star::document::XImporter;
52 : using com::sun::star::xml::sax::InputSource;
53 : using com::sun::star::xml::sax::XAttributeList;
54 : using com::sun::star::xml::sax::XDocumentHandler;
55 : using com::sun::star::xml::sax::XParser;
56 :
57 :
58 0 : sal_Bool SAL_CALL MSWorksImportFilter::importImpl( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
59 : throw (RuntimeException)
60 : {
61 : SAL_INFO("writerperfect", "MSWorksImportFilter::importImpl");
62 :
63 0 : sal_Int32 nLength = aDescriptor.getLength();
64 0 : const PropertyValue *pValue = aDescriptor.getConstArray();
65 0 : Reference < XInputStream > xInputStream;
66 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
67 : {
68 0 : if ( pValue[i].Name == "InputStream" )
69 0 : pValue[i].Value >>= xInputStream;
70 : }
71 0 : if ( !xInputStream.is() )
72 : {
73 : OSL_ASSERT( false );
74 0 : return sal_False;
75 : }
76 :
77 : // An XML import service: what we push sax messages to..
78 : Reference < XDocumentHandler > xInternalHandler(
79 0 : mxContext->getServiceManager()->createInstanceWithContext(
80 0 : "com.sun.star.comp.Writer.XMLOasisImporter", mxContext),
81 0 : css::uno::UNO_QUERY_THROW);
82 :
83 : // The XImporter sets up an empty target document for XDocumentHandler to write to..
84 0 : Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
85 0 : xImporter->setTargetDocument(mxDoc);
86 :
87 : // OO Document Handler: abstract class to handle document SAX messages, concrete implementation here
88 : // writes to in-memory target doc
89 0 : DocumentHandler xHandler(xInternalHandler);
90 :
91 0 : WPXSvInputStream input( xInputStream );
92 :
93 0 : OdtGenerator collector(&xHandler, ODF_FLAT_XML);
94 0 : if (WPS_OK == WPSDocument::parse(&input, &collector))
95 0 : return sal_True;
96 0 : return sal_False;
97 : }
98 :
99 0 : sal_Bool SAL_CALL MSWorksImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
100 : throw (RuntimeException, std::exception)
101 : {
102 : SAL_INFO("writerperfect", "MSWorksImportFilter::filter");
103 0 : return importImpl ( aDescriptor );
104 : }
105 0 : void SAL_CALL MSWorksImportFilter::cancel( )
106 : throw (RuntimeException, std::exception)
107 : {
108 : SAL_INFO("writerperfect", "MSWorksImportFilter::cancel");
109 0 : }
110 :
111 : // XImporter
112 0 : void SAL_CALL MSWorksImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
113 : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
114 : {
115 : SAL_INFO("writerperfect", "MSWorksImportFilter::getTargetDocument");
116 0 : mxDoc = xDoc;
117 0 : }
118 :
119 : // XExtendedFilterDetection
120 0 : OUString SAL_CALL MSWorksImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
121 : throw( com::sun::star::uno::RuntimeException, std::exception )
122 : {
123 : SAL_INFO("writerperfect", "MSWorksImportFilter::detect");
124 :
125 0 : WPSConfidence confidence = WPS_CONFIDENCE_NONE;
126 0 : OUString sTypeName;
127 0 : sal_Int32 nLength = Descriptor.getLength();
128 0 : sal_Int32 location = nLength;
129 0 : const PropertyValue *pValue = Descriptor.getConstArray();
130 0 : Reference < XInputStream > xInputStream;
131 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
132 : {
133 0 : if ( pValue[i].Name == "TypeName" )
134 0 : location=i;
135 0 : else if ( pValue[i].Name == "InputStream" )
136 0 : pValue[i].Value >>= xInputStream;
137 : }
138 :
139 0 : if (!xInputStream.is())
140 0 : return OUString();
141 :
142 0 : WPXSvInputStream input( xInputStream );
143 :
144 0 : confidence = WPSDocument::isFileFormatSupported(&input);
145 :
146 0 : if ((confidence == WPS_CONFIDENCE_EXCELLENT) || (confidence == WPS_CONFIDENCE_GOOD))
147 0 : sTypeName = "writer_MS_Works_Document";
148 :
149 0 : if (!sTypeName.isEmpty())
150 : {
151 0 : if ( location == nLength )
152 : {
153 0 : Descriptor.realloc(nLength+1);
154 0 : Descriptor[location].Name = "TypeName";
155 : }
156 :
157 0 : Descriptor[location].Value <<=sTypeName;
158 : }
159 :
160 0 : return sTypeName;
161 : }
162 :
163 :
164 : // XInitialization
165 0 : void SAL_CALL MSWorksImportFilter::initialize( const Sequence< Any >& aArguments )
166 : throw (Exception, RuntimeException, std::exception)
167 : {
168 : SAL_INFO("writerperfect", "MSWorksImportFilter::initialize");
169 0 : Sequence < PropertyValue > aAnySeq;
170 0 : sal_Int32 nLength = aArguments.getLength();
171 0 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
172 : {
173 0 : const PropertyValue *pValue = aAnySeq.getConstArray();
174 0 : nLength = aAnySeq.getLength();
175 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
176 : {
177 0 : if ( pValue[i].Name == "Type" )
178 : {
179 0 : pValue[i].Value >>= msFilterName;
180 0 : break;
181 : }
182 : }
183 0 : }
184 0 : }
185 11 : OUString MSWorksImportFilter_getImplementationName ()
186 : throw (RuntimeException)
187 : {
188 11 : return OUString ( "com.sun.star.comp.Writer.MSWorksImportFilter" );
189 : }
190 :
191 1 : Sequence< OUString > SAL_CALL MSWorksImportFilter_getSupportedServiceNames( )
192 : throw (RuntimeException)
193 : {
194 1 : Sequence < OUString > aRet(2);
195 1 : OUString *pArray = aRet.getArray();
196 1 : pArray[0] = "com.sun.star.document.ImportFilter";
197 1 : pArray[1] = "com.sun.star.document.ExtendedTypeDetection";
198 1 : return aRet;
199 : }
200 : #undef SERVICE_NAME2
201 : #undef SERVICE_NAME1
202 :
203 1 : Reference< XInterface > SAL_CALL MSWorksImportFilter_createInstance( const Reference< XComponentContext > & rContext)
204 : throw( Exception )
205 : {
206 1 : return (cppu::OWeakObject *) new MSWorksImportFilter( rContext );
207 : }
208 :
209 : // XServiceInfo
210 0 : OUString SAL_CALL MSWorksImportFilter::getImplementationName( )
211 : throw (RuntimeException, std::exception)
212 : {
213 0 : return MSWorksImportFilter_getImplementationName();
214 : }
215 0 : sal_Bool SAL_CALL MSWorksImportFilter::supportsService( const OUString &rServiceName )
216 : throw (RuntimeException, std::exception)
217 : {
218 0 : return cppu::supportsService( this, rServiceName );
219 : }
220 0 : Sequence< OUString > SAL_CALL MSWorksImportFilter::getSupportedServiceNames( )
221 : throw (RuntimeException, std::exception)
222 : {
223 0 : return MSWorksImportFilter_getSupportedServiceNames();
224 9 : }
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|