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 <com/sun/star/ucb/XCommandEnvironment.hpp>
23 :
24 : #include <comphelper/componentcontext.hxx>
25 : #include <xmloff/attrlist.hxx>
26 : #include <ucbhelper/content.hxx>
27 :
28 : #include <libwps/libwps.h>
29 :
30 : #include "filter/FilterInternal.hxx"
31 : #include "filter/DocumentHandler.hxx"
32 : #include "filter/OdtGenerator.hxx"
33 : #include "MSWorksImportFilter.hxx"
34 : #include "stream/WPXSvStream.h"
35 :
36 : #include <iostream>
37 :
38 : using namespace ::com::sun::star::uno;
39 : using rtl::OString;
40 : using rtl::OUString;
41 : using com::sun::star::uno::Sequence;
42 : using com::sun::star::uno::Reference;
43 : using com::sun::star::uno::Any;
44 : using com::sun::star::uno::UNO_QUERY;
45 : using com::sun::star::uno::XInterface;
46 : using com::sun::star::uno::Exception;
47 : using com::sun::star::uno::RuntimeException;
48 : using com::sun::star::beans::PropertyValue;
49 : using com::sun::star::document::XFilter;
50 : using com::sun::star::document::XExtendedFilterDetection;
51 : using com::sun::star::ucb::XCommandEnvironment;
52 :
53 : using com::sun::star::io::XInputStream;
54 : using com::sun::star::document::XImporter;
55 : using com::sun::star::xml::sax::InputSource;
56 : using com::sun::star::xml::sax::XAttributeList;
57 : using com::sun::star::xml::sax::XDocumentHandler;
58 : using com::sun::star::xml::sax::XParser;
59 :
60 : void callHandler(Reference < XDocumentHandler > xDocHandler);
61 :
62 0 : sal_Bool SAL_CALL MSWorksImportFilter::importImpl( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
63 : throw (RuntimeException)
64 : {
65 : WRITER_DEBUG_MSG(("MSWorksImportFilter::importImpl: Got here!\n"));
66 :
67 0 : sal_Int32 nLength = aDescriptor.getLength();
68 0 : const PropertyValue *pValue = aDescriptor.getConstArray();
69 0 : OUString sURL;
70 0 : Reference < XInputStream > xInputStream;
71 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
72 : {
73 0 : if ( pValue[i].Name == "InputStream" )
74 0 : pValue[i].Value >>= xInputStream;
75 0 : else if ( pValue[i].Name == "URL" )
76 0 : pValue[i].Value >>= sURL;
77 : }
78 0 : if ( !xInputStream.is() )
79 : {
80 : OSL_ASSERT( 0 );
81 0 : return sal_False;
82 : }
83 0 : OString sFileName;
84 0 : sFileName = OUStringToOString(sURL, RTL_TEXTENCODING_INFO_ASCII);
85 :
86 : // An XML import service: what we push sax messages to..
87 0 : OUString sXMLImportService ( "com.sun.star.comp.Writer.XMLOasisImporter" );
88 0 : Reference < XDocumentHandler > xInternalHandler( comphelper::ComponentContext( mxContext ).createComponent( sXMLImportService ), UNO_QUERY );
89 :
90 : // The XImporter sets up an empty target document for XDocumentHandler to write to..
91 0 : Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
92 0 : xImporter->setTargetDocument(mxDoc);
93 :
94 : // OO Document Handler: abstract class to handle document SAX messages, concrete implementation here
95 : // writes to in-memory target doc
96 0 : DocumentHandler xHandler(xInternalHandler);
97 :
98 0 : WPXSvInputStream input( xInputStream );
99 :
100 0 : OdtGenerator collector(&xHandler, ODF_FLAT_XML);
101 0 : if (WPS_OK == WPSDocument::parse(&input, &collector))
102 0 : return sal_True;
103 0 : return sal_False;
104 : }
105 :
106 0 : sal_Bool SAL_CALL MSWorksImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
107 : throw (RuntimeException)
108 : {
109 : WRITER_DEBUG_MSG(("MSWorksImportFilter::filter: Got here!\n"));
110 0 : return importImpl ( aDescriptor );
111 : }
112 0 : void SAL_CALL MSWorksImportFilter::cancel( )
113 : throw (RuntimeException)
114 : {
115 : WRITER_DEBUG_MSG(("MSWorksImportFilter::cancel: Got here!\n"));
116 0 : }
117 :
118 : // XImporter
119 0 : void SAL_CALL MSWorksImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
120 : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException)
121 : {
122 : WRITER_DEBUG_MSG(("MSWorksImportFilter::getTargetDocument: Got here!\n"));
123 0 : mxDoc = xDoc;
124 0 : }
125 :
126 : // XExtendedFilterDetection
127 0 : OUString SAL_CALL MSWorksImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
128 : throw( com::sun::star::uno::RuntimeException )
129 : {
130 : WRITER_DEBUG_MSG(("MSWorksImportFilter::detect: Got here!\n"));
131 :
132 0 : WPSConfidence confidence = WPS_CONFIDENCE_NONE;
133 0 : OUString sTypeName;
134 0 : sal_Int32 nLength = Descriptor.getLength();
135 0 : sal_Int32 location = nLength;
136 0 : OUString sURL;
137 0 : const PropertyValue *pValue = Descriptor.getConstArray();
138 0 : Reference < XInputStream > xInputStream;
139 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
140 : {
141 0 : if ( pValue[i].Name == "TypeName" )
142 0 : location=i;
143 0 : else if ( pValue[i].Name == "InputStream" )
144 0 : pValue[i].Value >>= xInputStream;
145 0 : else if ( pValue[i].Name == "URL" )
146 0 : pValue[i].Value >>= sURL;
147 : }
148 :
149 0 : Reference< com::sun::star::ucb::XCommandEnvironment > xEnv;
150 0 : if (!xInputStream.is())
151 : {
152 : try
153 : {
154 0 : ::ucbhelper::Content aContent(sURL, xEnv, mxContext);
155 0 : xInputStream = aContent.openStream();
156 : }
157 0 : catch ( ... )
158 : {
159 0 : return ::rtl::OUString();
160 : }
161 :
162 0 : if (!xInputStream.is())
163 0 : return ::rtl::OUString();
164 : }
165 :
166 0 : WPXSvInputStream input( xInputStream );
167 :
168 0 : if (input.atEOS())
169 0 : return ::rtl::OUString();
170 :
171 0 : confidence = WPSDocument::isFileFormatSupported(&input);
172 :
173 0 : if ((confidence == WPS_CONFIDENCE_EXCELLENT) || (confidence == WPS_CONFIDENCE_GOOD))
174 0 : sTypeName = "writer_MS_Works_Document";
175 :
176 0 : if (!sTypeName.isEmpty())
177 : {
178 0 : if ( location == Descriptor.getLength() )
179 : {
180 0 : Descriptor.realloc(nLength+1);
181 0 : Descriptor[location].Name = "TypeName";
182 : }
183 :
184 0 : Descriptor[location].Value <<=sTypeName;
185 : }
186 :
187 0 : return sTypeName;
188 : }
189 :
190 :
191 : // XInitialization
192 0 : void SAL_CALL MSWorksImportFilter::initialize( const Sequence< Any >& aArguments )
193 : throw (Exception, RuntimeException)
194 : {
195 : WRITER_DEBUG_MSG(("MSWorksImportFilter::initialize: Got here!\n"));
196 0 : Sequence < PropertyValue > aAnySeq;
197 0 : sal_Int32 nLength = aArguments.getLength();
198 0 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
199 : {
200 0 : const PropertyValue *pValue = aAnySeq.getConstArray();
201 0 : nLength = aAnySeq.getLength();
202 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
203 : {
204 0 : if ( pValue[i].Name == "Type" )
205 : {
206 0 : pValue[i].Value >>= msFilterName;
207 0 : break;
208 : }
209 : }
210 0 : }
211 0 : }
212 0 : OUString MSWorksImportFilter_getImplementationName ()
213 : throw (RuntimeException)
214 : {
215 0 : return OUString ( "com.sun.star.comp.Writer.MSWorksImportFilter" );
216 : }
217 :
218 : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
219 : #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
220 0 : sal_Bool SAL_CALL MSWorksImportFilter_supportsService( const OUString &ServiceName )
221 : throw (RuntimeException)
222 : {
223 0 : return ( ServiceName == SERVICE_NAME1 || ServiceName == SERVICE_NAME2 );
224 : }
225 0 : Sequence< OUString > SAL_CALL MSWorksImportFilter_getSupportedServiceNames( )
226 : throw (RuntimeException)
227 : {
228 0 : Sequence < OUString > aRet(2);
229 0 : OUString *pArray = aRet.getArray();
230 0 : pArray[0] = OUString ( SERVICE_NAME1 );
231 0 : pArray[1] = OUString ( SERVICE_NAME2 );
232 0 : return aRet;
233 : }
234 : #undef SERVICE_NAME2
235 : #undef SERVICE_NAME1
236 :
237 0 : Reference< XInterface > SAL_CALL MSWorksImportFilter_createInstance( const Reference< XComponentContext > & rContext)
238 : throw( Exception )
239 : {
240 0 : return (cppu::OWeakObject *) new MSWorksImportFilter( rContext );
241 : }
242 :
243 : // XServiceInfo
244 0 : OUString SAL_CALL MSWorksImportFilter::getImplementationName( )
245 : throw (RuntimeException)
246 : {
247 0 : return MSWorksImportFilter_getImplementationName();
248 : }
249 0 : sal_Bool SAL_CALL MSWorksImportFilter::supportsService( const OUString &rServiceName )
250 : throw (RuntimeException)
251 : {
252 0 : return MSWorksImportFilter_supportsService( rServiceName );
253 : }
254 0 : Sequence< OUString > SAL_CALL MSWorksImportFilter::getSupportedServiceNames( )
255 : throw (RuntimeException)
256 : {
257 0 : return MSWorksImportFilter_getSupportedServiceNames();
258 0 : }
259 :
260 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|