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>
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
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>
35 #ifndef _RTL_TENCINFO_H_
36 #include <rtl/tencinfo.h>
38 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HPP_
42 #include <com/sun/star/io/XInputStream.hpp>
44 #ifndef _COM_SUN_STAR_XML_SAX_XATTRIBUTELIST_HPP_
45 #include <com/sun/star/xml/sax/XAttributeList.hpp>
47 #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HPP_
48 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
50 #ifndef _COM_SUN_STAR_XML_SAX_INPUTSOURCE_HPP_
51 #include <com/sun/star/xml/sax/InputSource.hpp>
53 #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP_
54 #include <com/sun/star/xml/sax/XParser.hpp>
57 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
58 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
60 #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HPP_
61 #include <com/sun/star/io/XInputStream.hpp>
64 #ifndef _COM_SUN_STAR_IO_XSEEKABLE_HPP_
65 #include <com/sun/star/io/XSeekable.hpp>
68 #include <com/sun/star/uno/Reference.h>
70 #ifndef _ATTRLIST_HPP_
71 #include <xmloff/attrlist.hxx>
74 #include <xmloff/xmlkywd.hxx>
77 #include "filter/DocumentHandler.hxx"
78 #include "OdgExporter.hxx"
79 #include "WPGImportFilter.hxx"
80 #include "stream/WPXSvStream.h"
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
;
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
)
116 std::cerr
<< "WPGImportFilter::filter" << std::endl
;
118 sal_Int32 nLength
= aDescriptor
.getLength();
119 const PropertyValue
* pValue
= aDescriptor
.getConstArray();
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() )
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();
161 OdgExporter
exporter(&xHandler
);
162 bool tmpParseResult
= libwpg::WPGraphics::parse(input
, &exporter
);
165 xInputStream
->closeInput();
166 return tmpParseResult
;
169 void SAL_CALL
WPGImportFilter::cancel( )
170 throw (RuntimeException
)
173 std::cerr
<< "WPGImportFilter::cancel" << std::endl
;
178 void SAL_CALL
WPGImportFilter::setTargetDocument( const Reference
< ::com::sun::star::lang::XComponent
>& xDoc
)
179 throw (::com::sun::star::lang::IllegalArgumentException
, RuntimeException
)
182 std::cerr
<< "WPGImportFilter::setTargetDocument" << std::endl
;
184 meType
= FILTER_IMPORT
;
188 // XExtendedFilterDetection
189 OUString SAL_CALL
WPGImportFilter::detect( com::sun::star::uno::Sequence
< PropertyValue
>& Descriptor
)
190 throw( com::sun::star::uno::RuntimeException
)
193 std::cerr
<< "WPGImportFilter::detect" << std::endl
;
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" ) ) )
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();
220 if (libwpg::WPGraphics::isSupported(input
))
221 sTypeName
= OUString( RTL_CONSTASCII_USTRINGPARAM ( "draw_WordPerfect_Graphics" ) );
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
;
241 void SAL_CALL
WPGImportFilter::initialize( const Sequence
< Any
>& aArguments
)
242 throw (Exception
, RuntimeException
)
245 std::cerr
<< "WPGImportFilter::initialize" << std::endl
;
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
;
263 OUString
WPGImportFilter_getImplementationName ()
264 throw (RuntimeException
)
267 std::cerr
<< "WPGImportFilter_getImplementationName" << std::endl
;
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
)
278 std::cerr
<< "WPGImportFilter_supportsService" << std::endl
;
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
)
287 std::cerr
<< "WPGImportFilter_getSupportedServiceNames" << std::endl
;
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
) );
298 Reference
< XInterface
> SAL_CALL
WPGImportFilter_createInstance( const Reference
< XMultiServiceFactory
> & rSMgr
)
302 std::cerr
<< "WPGImportFilter_createInstance" << std::endl
;
304 return (cppu::OWeakObject
*) new WPGImportFilter( rSMgr
);
308 OUString SAL_CALL
WPGImportFilter::getImplementationName( )
309 throw (RuntimeException
)
312 std::cerr
<< "WPGImportFilter::getImplementationName" << std::endl
;
314 return WPGImportFilter_getImplementationName();
316 sal_Bool SAL_CALL
WPGImportFilter::supportsService( const OUString
& rServiceName
)
317 throw (RuntimeException
)
320 std::cerr
<< "WPGImportFilter::supportsService" << std::endl
;
322 return WPGImportFilter_supportsService( rServiceName
);
324 Sequence
< OUString
> SAL_CALL
WPGImportFilter::getSupportedServiceNames( )
325 throw (RuntimeException
)
328 std::cerr
<< "WPGImportFilter::getSupportedServiceNames" << std::endl
;
330 return WPGImportFilter_getSupportedServiceNames();