update dev300-m58
[ooovba.git] / writerperfect / source / wpgimp / WPGImportFilter.cxx
blobb8c1871af1f61b075b3033f756e9a992be11f071
1 /* WPGImportFilter: Sets up the filter, and calls OdgExporter
2 * to do the actual filtering
4 * Copyright (C) 2000 by Sun Microsystems, Inc.
5 * Copyright (C) 2002-2004 William Lachance (wlach@interlog.com)
6 * Copyright (C) 2004 Net Integration Technologies (http://www.net-itech.com)
7 * Copyright (C) 2004-2006 Fridrich Strba <fridrich.strba@bluewin.ch>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
24 * Contributor(s): Martin Gallwey (gallwey@sun.com)
28 /* "This product is not manufactured, approved, or supported by
29 * Corel Corporation or Corel Corporation Limited."
32 #ifndef _OSL_DIAGNOSE_H_
33 #include <osl/diagnose.h>
34 #endif
35 #ifndef _RTL_TENCINFO_H_
36 #include <rtl/tencinfo.h>
37 #endif
38 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #endif
41 #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HPP_
42 #include <com/sun/star/io/XInputStream.hpp>
43 #endif
44 #ifndef _COM_SUN_STAR_XML_SAX_XATTRIBUTELIST_HPP_
45 #include <com/sun/star/xml/sax/XAttributeList.hpp>
46 #endif
47 #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HPP_
48 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
49 #endif
50 #ifndef _COM_SUN_STAR_XML_SAX_INPUTSOURCE_HPP_
51 #include <com/sun/star/xml/sax/InputSource.hpp>
52 #endif
53 #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP_
54 #include <com/sun/star/xml/sax/XParser.hpp>
55 #endif
57 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
58 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
59 #endif
60 #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HPP_
61 #include <com/sun/star/io/XInputStream.hpp>
62 #endif
64 #ifndef _COM_SUN_STAR_IO_XSEEKABLE_HPP_
65 #include <com/sun/star/io/XSeekable.hpp>
66 #endif
68 #include <com/sun/star/uno/Reference.h>
70 #ifndef _ATTRLIST_HPP_
71 #include <xmloff/attrlist.hxx>
72 #endif
73 #ifndef _XMLKYWD_HPP
74 #include <xmloff/xmlkywd.hxx>
75 #endif
77 #include "filter/DocumentHandler.hxx"
78 #include "OdgExporter.hxx"
79 #include "WPGImportFilter.hxx"
80 #include "stream/WPXSvStream.h"
82 #include <iostream>
84 using namespace ::com::sun::star::uno;
85 using com::sun::star::uno::Reference;
86 using com::sun::star::io::XInputStream;
87 using com::sun::star::io::XSeekable;
88 using com::sun::star::uno::Sequence;
89 using namespace ::rtl;
90 using rtl::OString;
91 using rtl::OUString;
92 using com::sun::star::uno::Sequence;
93 using com::sun::star::uno::Reference;
94 using com::sun::star::uno::Any;
95 using com::sun::star::uno::UNO_QUERY;
96 using com::sun::star::uno::XInterface;
97 using com::sun::star::uno::Exception;
98 using com::sun::star::uno::RuntimeException;
99 using com::sun::star::lang::XMultiServiceFactory;
100 using com::sun::star::beans::PropertyValue;
101 using com::sun::star::document::XFilter;
102 using com::sun::star::document::XExtendedFilterDetection;
104 using com::sun::star::io::XInputStream;
105 using com::sun::star::document::XImporter;
106 using com::sun::star::xml::sax::InputSource;
107 using com::sun::star::xml::sax::XAttributeList;
108 using com::sun::star::xml::sax::XDocumentHandler;
109 using com::sun::star::xml::sax::XParser;
112 sal_Bool SAL_CALL WPGImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
113 throw (RuntimeException)
115 #ifdef DEBUG
116 std::cerr << "WPGImportFilter::filter" << std::endl;
117 #endif
118 sal_Int32 nLength = aDescriptor.getLength();
119 const PropertyValue * pValue = aDescriptor.getConstArray();
120 OUString sURL;
121 Reference < XInputStream > xInputStream;
122 for ( sal_Int32 i = 0 ; i < nLength; i++)
124 if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "InputStream" ) ) )
125 pValue[i].Value >>= xInputStream;
126 else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "URL" ) ) )
127 pValue[i].Value >>= sURL;
129 if ( !xInputStream.is() )
131 OSL_ASSERT( 0 );
132 return sal_False;
134 OString sFileName;
135 sFileName = OUStringToOString(sURL, RTL_TEXTENCODING_INFO_ASCII);
137 // An XML import service: what we push sax messages to..
138 OUString sXMLImportService ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Draw.XMLOasisImporter" ) );
139 Reference < XDocumentHandler > xInternalHandler( mxMSF->createInstance( sXMLImportService ), UNO_QUERY );
141 // The XImporter sets up an empty target document for XDocumentHandler to write to..
142 Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
143 xImporter->setTargetDocument( mxDoc );
145 // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
146 // writes to in-memory target doc
147 DocumentHandler xHandler(xInternalHandler);
149 WPXInputStream* input = new WPXSvInputStream( xInputStream );
151 if (input->isOLEStream())
153 WPXInputStream* olestream = input->getDocumentOLEStream();
154 if (olestream)
156 delete input;
157 input = olestream;
161 OdgExporter exporter(&xHandler);
162 bool tmpParseResult = libwpg::WPGraphics::parse(input, &exporter);
163 if (input)
164 delete input;
165 xInputStream->closeInput();
166 return tmpParseResult;
169 void SAL_CALL WPGImportFilter::cancel( )
170 throw (RuntimeException)
172 #ifdef DEBUG
173 std::cerr << "WPGImportFilter::cancel" << std::endl;
174 #endif
177 // XImporter
178 void SAL_CALL WPGImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
179 throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException)
181 #ifdef DEBUG
182 std::cerr << "WPGImportFilter::setTargetDocument" << std::endl;
183 #endif
184 meType = FILTER_IMPORT;
185 mxDoc = xDoc;
188 // XExtendedFilterDetection
189 OUString SAL_CALL WPGImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
190 throw( com::sun::star::uno::RuntimeException )
192 #ifdef DEBUG
193 std::cerr << "WPGImportFilter::detect" << std::endl;
194 #endif
195 OUString sTypeName = OUString( RTL_CONSTASCII_USTRINGPARAM ( "" ) );
196 sal_Int32 nLength = Descriptor.getLength();
197 sal_Int32 location = nLength;
198 const PropertyValue * pValue = Descriptor.getConstArray();
199 Reference < XInputStream > xInputStream;
200 for ( sal_Int32 i = 0 ; i < nLength; i++)
202 if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TypeName" ) ) )
203 location=i;
204 else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "InputStream" ) ) )
205 pValue[i].Value >>= xInputStream;
208 WPXInputStream* input = new WPXSvInputStream( xInputStream );
210 if (input->isOLEStream())
212 WPXInputStream* olestream = input->getDocumentOLEStream();
213 if (olestream)
215 delete input;
216 input = olestream;
220 if (libwpg::WPGraphics::isSupported(input))
221 sTypeName = OUString( RTL_CONSTASCII_USTRINGPARAM ( "draw_WordPerfect_Graphics" ) );
223 if (input)
224 delete input;
226 if (!sTypeName.equalsAscii(""))
228 if ( location == Descriptor.getLength() )
230 Descriptor.realloc(nLength+1);
231 Descriptor[location].Name = ::rtl::OUString::createFromAscii( "TypeName" );
234 Descriptor[location].Value <<=sTypeName;
236 return sTypeName;
240 // XInitialization
241 void SAL_CALL WPGImportFilter::initialize( const Sequence< Any >& aArguments )
242 throw (Exception, RuntimeException)
244 #ifdef DEBUG
245 std::cerr << "WPGImportFilter::initialize" << std::endl;
246 #endif
247 Sequence < PropertyValue > aAnySeq;
248 sal_Int32 nLength = aArguments.getLength();
249 if ( nLength && ( aArguments[0] >>= aAnySeq ) )
251 const PropertyValue * pValue = aAnySeq.getConstArray();
252 nLength = aAnySeq.getLength();
253 for ( sal_Int32 i = 0 ; i < nLength; i++)
255 if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) )
257 pValue[i].Value >>= msFilterName;
258 break;
263 OUString WPGImportFilter_getImplementationName ()
264 throw (RuntimeException)
266 #ifdef DEBUG
267 std::cerr << "WPGImportFilter_getImplementationName" << std::endl;
268 #endif
269 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Draw.WPGImportFilter" ) );
272 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
273 #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
274 sal_Bool SAL_CALL WPGImportFilter_supportsService( const OUString& ServiceName )
275 throw (RuntimeException)
277 #ifdef DEBUG
278 std::cerr << "WPGImportFilter_supportsService" << std::endl;
279 #endif
280 return (ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ) ||
281 ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME2 ) ) );
283 Sequence< OUString > SAL_CALL WPGImportFilter_getSupportedServiceNames( )
284 throw (RuntimeException)
286 #ifdef DEBUG
287 std::cerr << "WPGImportFilter_getSupportedServiceNames" << std::endl;
288 #endif
289 Sequence < OUString > aRet(2);
290 OUString* pArray = aRet.getArray();
291 pArray[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1 ) );
292 pArray[1] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME2 ) );
293 return aRet;
295 #undef SERVICE_NAME2
296 #undef SERVICE_NAME1
298 Reference< XInterface > SAL_CALL WPGImportFilter_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
299 throw( Exception )
301 #ifdef DEBUG
302 std::cerr << "WPGImportFilter_createInstance" << std::endl;
303 #endif
304 return (cppu::OWeakObject*) new WPGImportFilter( rSMgr );
307 // XServiceInfo
308 OUString SAL_CALL WPGImportFilter::getImplementationName( )
309 throw (RuntimeException)
311 #ifdef DEBUG
312 std::cerr << "WPGImportFilter::getImplementationName" << std::endl;
313 #endif
314 return WPGImportFilter_getImplementationName();
316 sal_Bool SAL_CALL WPGImportFilter::supportsService( const OUString& rServiceName )
317 throw (RuntimeException)
319 #ifdef DEBUG
320 std::cerr << "WPGImportFilter::supportsService" << std::endl;
321 #endif
322 return WPGImportFilter_supportsService( rServiceName );
324 Sequence< OUString > SAL_CALL WPGImportFilter::getSupportedServiceNames( )
325 throw (RuntimeException)
327 #ifdef DEBUG
328 std::cerr << "WPGImportFilter::getSupportedServiceNames" << std::endl;
329 #endif
330 return WPGImportFilter_getSupportedServiceNames();