Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* MWAWImportFilter: 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 <com/sun/star/io/XInputStream.hpp>
13 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
14 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
15 : #include <com/sun/star/xml/sax/InputSource.hpp>
16 : #include <com/sun/star/xml/sax/XParser.hpp>
17 : #include <com/sun/star/io/XSeekable.hpp>
18 : #include <com/sun/star/uno/Reference.h>
19 : #include <cppuhelper/supportsservice.hxx>
20 :
21 : #include <xmloff/attrlist.hxx>
22 : #include <ucbhelper/content.hxx>
23 :
24 : #include <libmwaw/libmwaw.hxx>
25 : #include <libodfgen/libodfgen.hxx>
26 :
27 : #include "common/DocumentHandler.hxx"
28 : #include "common/WPXSvStream.hxx"
29 : #include "MWAWImportFilter.hxx"
30 :
31 : using namespace ::com::sun::star::uno;
32 : using com::sun::star::uno::Sequence;
33 : using com::sun::star::uno::Reference;
34 : using com::sun::star::uno::Any;
35 : using com::sun::star::uno::UNO_QUERY;
36 : using com::sun::star::uno::XInterface;
37 : using com::sun::star::uno::Exception;
38 : using com::sun::star::uno::RuntimeException;
39 : using com::sun::star::beans::PropertyValue;
40 : using com::sun::star::document::XFilter;
41 : using com::sun::star::document::XExtendedFilterDetection;
42 : using com::sun::star::ucb::XCommandEnvironment;
43 :
44 : using com::sun::star::io::XInputStream;
45 : using com::sun::star::document::XImporter;
46 : using com::sun::star::xml::sax::InputSource;
47 : using com::sun::star::xml::sax::XAttributeList;
48 : using com::sun::star::xml::sax::XDocumentHandler;
49 : using com::sun::star::xml::sax::XParser;
50 :
51 0 : static bool handleEmbeddedMWAWObject(const WPXBinaryData &data, OdfDocumentHandler *pHandler, const OdfStreamType streamType)
52 : {
53 0 : OdgGenerator exporter(pHandler, streamType);
54 0 : return MWAWDocument::decodeGraphic(data, &exporter);
55 : }
56 :
57 0 : sal_Bool SAL_CALL MWAWImportFilter::importImpl( const Sequence< ::com::sun::star::beans::PropertyValue > &aDescriptor )
58 : throw (RuntimeException)
59 : {
60 : SAL_INFO("writerperfect", "MWAWImportFilter::importImpl");
61 :
62 0 : sal_Int32 nLength = aDescriptor.getLength();
63 0 : const PropertyValue *pValue = aDescriptor.getConstArray();
64 0 : Reference < XInputStream > xInputStream;
65 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
66 : {
67 0 : if ( pValue[i].Name == "InputStream" )
68 0 : pValue[i].Value >>= xInputStream;
69 : }
70 0 : if ( !xInputStream.is() )
71 : {
72 : OSL_ASSERT( false );
73 0 : return sal_False;
74 : }
75 :
76 : // An XML import service: what we push sax messages to..
77 : Reference < XDocumentHandler > xInternalHandler(
78 0 : mxContext->getServiceManager()->createInstanceWithContext(
79 0 : "com.sun.star.comp.Writer.XMLOasisImporter", mxContext),
80 0 : css::uno::UNO_QUERY_THROW);
81 :
82 : // The XImporter sets up an empty target document for XDocumentHandler to write to..
83 0 : Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
84 0 : xImporter->setTargetDocument(mxDoc);
85 :
86 : // OO Document Handler: abstract class to handle document SAX messages, concrete implementation here
87 : // writes to in-memory target doc
88 0 : DocumentHandler xHandler(xInternalHandler);
89 :
90 0 : WPXSvInputStream input( xInputStream );
91 :
92 0 : OdtGenerator collector(&xHandler, ODF_FLAT_XML);
93 0 : collector.registerEmbeddedObjectHandler("image/mwaw-odg", &handleEmbeddedMWAWObject);
94 0 : if (MWAWDocument::MWAW_R_OK == MWAWDocument::parse(&input, &collector))
95 0 : return sal_True;
96 0 : return sal_False;
97 : }
98 :
99 0 : sal_Bool SAL_CALL MWAWImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue > &aDescriptor )
100 : throw (RuntimeException, std::exception)
101 : {
102 : SAL_INFO("writerperfect", "MWAWImportFilter::filter");
103 0 : return importImpl ( aDescriptor );
104 : }
105 0 : void SAL_CALL MWAWImportFilter::cancel( )
106 : throw (RuntimeException, std::exception)
107 : {
108 : SAL_INFO("writerperfect", "MWAWImportFilter::cancel");
109 0 : }
110 :
111 : // XImporter
112 0 : void SAL_CALL MWAWImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent > &xDoc )
113 : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
114 : {
115 : SAL_INFO("writerperfect", "MWAWImportFilter::getTargetDocument");
116 0 : mxDoc = xDoc;
117 0 : }
118 :
119 : // XExtendedFilterDetection
120 0 : OUString SAL_CALL MWAWImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue > &Descriptor )
121 : throw( com::sun::star::uno::RuntimeException, std::exception )
122 : {
123 : SAL_INFO("writerperfect", "MWAWImportFilter::detect");
124 :
125 0 : MWAWDocument::Confidence confidence = MWAWDocument::MWAW_C_NONE;
126 0 : MWAWDocument::Type docType = MWAWDocument::MWAW_T_UNKNOWN;
127 0 : MWAWDocument::Kind docKind = MWAWDocument::MWAW_K_UNKNOWN;
128 0 : OUString sTypeName;
129 0 : sal_Int32 nLength = Descriptor.getLength();
130 0 : sal_Int32 location = nLength;
131 0 : const PropertyValue *pValue = Descriptor.getConstArray();
132 0 : Reference < XInputStream > xInputStream;
133 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
134 : {
135 0 : if ( pValue[i].Name == "TypeName" )
136 0 : location=i;
137 0 : else if ( pValue[i].Name == "InputStream" )
138 0 : pValue[i].Value >>= xInputStream;
139 : }
140 :
141 0 : if (!xInputStream.is())
142 0 : return OUString();
143 :
144 0 : WPXSvInputStream input( xInputStream );
145 :
146 0 : confidence = MWAWDocument::isFileFormatSupported(&input, docType, docKind);
147 :
148 0 : if (confidence == MWAWDocument::MWAW_C_EXCELLENT)
149 : {
150 0 : if ( docKind == MWAWDocument::MWAW_K_TEXT )
151 : {
152 0 : switch (docType)
153 : {
154 : case MWAWDocument::MWAW_T_ACTA:
155 0 : sTypeName = "writer_Mac_Acta";
156 0 : break;
157 : case MWAWDocument::MWAW_T_BEAGLEWORKS:
158 0 : sTypeName = "writer_Beagle_Works";
159 0 : break;
160 : case MWAWDocument::MWAW_T_CLARISWORKS:
161 0 : sTypeName = "writer_ClarisWorks";
162 0 : break;
163 : case MWAWDocument::MWAW_T_DOCMAKER:
164 0 : sTypeName = "writer_DocMaker";
165 0 : break;
166 : case MWAWDocument::MWAW_T_EDOC:
167 0 : sTypeName = "writer_eDoc_Document";
168 0 : break;
169 : case MWAWDocument::MWAW_T_GREATWORKS:
170 0 : sTypeName = "writer_Great_Works";
171 0 : break;
172 : case MWAWDocument::MWAW_T_FULLWRITE:
173 0 : sTypeName = "writer_FullWrite_Professional";
174 0 : break;
175 : case MWAWDocument::MWAW_T_HANMACWORDJ:
176 0 : sTypeName = "writer_HanMac_Word_J";
177 0 : break;
178 : case MWAWDocument::MWAW_T_HANMACWORDK:
179 0 : sTypeName = "writer_HanMac_Word_K";
180 0 : break;
181 : case MWAWDocument::MWAW_T_LIGHTWAYTEXT:
182 0 : sTypeName = "writer_LightWayText";
183 0 : break;
184 : case MWAWDocument::MWAW_T_MACDOC:
185 0 : sTypeName = "writer_MacDoc";
186 0 : break;
187 : case MWAWDocument::MWAW_T_MARINERWRITE:
188 0 : sTypeName = "writer_Mariner_Write";
189 0 : break;
190 : case MWAWDocument::MWAW_T_MINDWRITE:
191 0 : sTypeName = "writer_MindWrite";
192 0 : break;
193 : case MWAWDocument::MWAW_T_MACWRITE:
194 0 : sTypeName = "writer_MacWrite";
195 0 : break;
196 : case MWAWDocument::MWAW_T_MACWRITEPRO:
197 0 : sTypeName = "writer_MacWritePro";
198 0 : break;
199 : case MWAWDocument::MWAW_T_MICROSOFTWORD:
200 0 : sTypeName = "writer_Mac_Word";
201 0 : break;
202 : case MWAWDocument::MWAW_T_MICROSOFTWORKS:
203 0 : sTypeName = "writer_Mac_Works";
204 0 : break;
205 : case MWAWDocument::MWAW_T_MORE:
206 0 : sTypeName = "writer_Mac_More";
207 0 : break;
208 : case MWAWDocument::MWAW_T_NISUSWRITER:
209 0 : sTypeName = "writer_Nisus_Writer";
210 0 : break;
211 : case MWAWDocument::MWAW_T_TEACHTEXT:
212 0 : sTypeName = "writer_TeachText";
213 0 : break;
214 : case MWAWDocument::MWAW_T_TEXEDIT:
215 0 : sTypeName = "writer_TexEdit";
216 0 : break;
217 : case MWAWDocument::MWAW_T_WRITENOW:
218 0 : sTypeName = "writer_WriteNow";
219 0 : break;
220 : case MWAWDocument::MWAW_T_WRITERPLUS:
221 0 : sTypeName = "writer_WriterPlus";
222 0 : break;
223 : case MWAWDocument::MWAW_T_ZWRITE:
224 0 : sTypeName = "writer_ZWrite";
225 0 : break;
226 :
227 : case MWAWDocument::MWAW_T_FRAMEMAKER:
228 : case MWAWDocument::MWAW_T_MACDRAW:
229 : case MWAWDocument::MWAW_T_MACPAINT:
230 : case MWAWDocument::MWAW_T_PAGEMAKER:
231 : case MWAWDocument::MWAW_T_READYSETGO:
232 : case MWAWDocument::MWAW_T_RAGTIME:
233 : case MWAWDocument::MWAW_T_XPRESS:
234 : case MWAWDocument::MWAW_T_RESERVED1:
235 : case MWAWDocument::MWAW_T_RESERVED2:
236 : case MWAWDocument::MWAW_T_RESERVED3:
237 : case MWAWDocument::MWAW_T_RESERVED4:
238 : case MWAWDocument::MWAW_T_RESERVED5:
239 : case MWAWDocument::MWAW_T_RESERVED6:
240 : case MWAWDocument::MWAW_T_RESERVED7:
241 : case MWAWDocument::MWAW_T_RESERVED8:
242 : case MWAWDocument::MWAW_T_RESERVED9:
243 : case MWAWDocument::MWAW_T_UNKNOWN:
244 : default:
245 0 : break;
246 : }
247 : }
248 : }
249 :
250 0 : if (!sTypeName.isEmpty())
251 : {
252 0 : if ( location == nLength )
253 : {
254 0 : Descriptor.realloc(nLength+1);
255 0 : Descriptor[location].Name = "TypeName";
256 : }
257 :
258 0 : Descriptor[location].Value <<=sTypeName;
259 : }
260 :
261 0 : return sTypeName;
262 : }
263 :
264 :
265 : // XInitialization
266 0 : void SAL_CALL MWAWImportFilter::initialize( const Sequence< Any > &aArguments )
267 : throw (Exception, RuntimeException, std::exception)
268 : {
269 : SAL_INFO("writerperfect", "MWAWImportFilter::initialize");
270 0 : Sequence < PropertyValue > aAnySeq;
271 0 : sal_Int32 nLength = aArguments.getLength();
272 0 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
273 : {
274 0 : const PropertyValue *pValue = aAnySeq.getConstArray();
275 0 : nLength = aAnySeq.getLength();
276 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
277 : {
278 0 : if ( pValue[i].Name == "Type" )
279 : {
280 0 : pValue[i].Value >>= msFilterName;
281 0 : break;
282 : }
283 : }
284 0 : }
285 0 : }
286 0 : OUString MWAWImportFilter_getImplementationName ()
287 : throw (RuntimeException)
288 : {
289 0 : return OUString ( "com.sun.star.comp.Writer.MWAWImportFilter" );
290 : }
291 :
292 0 : Sequence< OUString > SAL_CALL MWAWImportFilter_getSupportedServiceNames( )
293 : throw (RuntimeException)
294 : {
295 0 : Sequence < OUString > aRet(2);
296 0 : OUString *pArray = aRet.getArray();
297 0 : pArray[0] = "com.sun.star.document.ImportFilter";
298 0 : pArray[1] = "com.sun.star.document.ExtendedTypeDetection";
299 0 : return aRet;
300 : }
301 :
302 0 : Reference< XInterface > SAL_CALL MWAWImportFilter_createInstance( const Reference< XComponentContext > &rContext)
303 : throw( Exception )
304 : {
305 0 : return (cppu::OWeakObject *) new MWAWImportFilter( rContext );
306 : }
307 :
308 : // XServiceInfo
309 0 : OUString SAL_CALL MWAWImportFilter::getImplementationName( )
310 : throw (RuntimeException, std::exception)
311 : {
312 0 : return MWAWImportFilter_getImplementationName();
313 : }
314 0 : sal_Bool SAL_CALL MWAWImportFilter::supportsService( const OUString &rServiceName )
315 : throw (RuntimeException, std::exception)
316 : {
317 0 : return cppu::supportsService( this, rServiceName );
318 : }
319 0 : Sequence< OUString > SAL_CALL MWAWImportFilter::getSupportedServiceNames( )
320 : throw (RuntimeException, std::exception)
321 : {
322 0 : return MWAWImportFilter_getSupportedServiceNames();
323 : }
324 :
325 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|