xref: /AOO41X/main/writerperfect/source/wpdimp/WordPerfectImportFilter.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /* WordPerfectImportFilter: Sets up the filter, and calls DocumentCollector
2  * to do the actual filtering
3  *
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 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.
13  *
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.
18  *
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.
23  *
24  *  Contributor(s): Martin Gallwey (gallwey@sun.com)
25  *
26  */
27 /* "This product is not manufactured, approved, or supported by
28  * Corel Corporation or Corel Corporation Limited."
29  */
30 
31 #include <osl/diagnose.h>
32 #ifndef _RTL_TENCINFO_H_
33 #include <rtl/tencinfo.h>
34 #endif
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/io/XInputStream.hpp>
37 #include <com/sun/star/xml/sax/XAttributeList.hpp>
38 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
39 #include <com/sun/star/xml/sax/InputSource.hpp>
40 #include <com/sun/star/xml/sax/XParser.hpp>
41 
42 #ifndef _COM_SUN_STAR_UCB_XCOMMANDENVIRONMENT_HPP
43 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
44 #endif
45 
46 #ifndef _ATTRLIST_HPP_
47 #include <xmloff/attrlist.hxx>
48 #endif
49 #include <ucbhelper/content.hxx>
50 
51 #include "filter/FilterInternal.hxx"
52 #include "filter/DocumentHandler.hxx"
53 #include "filter/DocumentCollector.hxx"
54 #include "stream/WPXSvStream.h"
55 
56 #if defined _MSC_VER
57 #pragma warning( push, 1 )
58 #endif
59 #include <libwpd/WPDocument.h>
60 #if defined _MSC_VER
61 #pragma warning( pop )
62 #endif
63 
64 #include "WordPerfectCollector.hxx"
65 #include "WordPerfectImportFilter.hxx"
66 
67 using namespace ::rtl;
68 using namespace ::com::sun::star;
69 
70 using rtl::OString;
71 using rtl::OUString;
72 using com::sun::star::uno::Sequence;
73 using com::sun::star::uno::Reference;
74 using com::sun::star::uno::Any;
75 using com::sun::star::uno::UNO_QUERY;
76 using com::sun::star::uno::XInterface;
77 using com::sun::star::uno::Exception;
78 using com::sun::star::uno::RuntimeException;
79 using com::sun::star::lang::XMultiServiceFactory;
80 using com::sun::star::beans::PropertyValue;
81 using com::sun::star::document::XFilter;
82 using com::sun::star::document::XExtendedFilterDetection;
83 using com::sun::star::ucb::XCommandEnvironment;
84 
85 using com::sun::star::io::XInputStream;
86 using com::sun::star::document::XImporter;
87 using com::sun::star::xml::sax::InputSource;
88 using com::sun::star::xml::sax::XAttributeList;
89 using com::sun::star::xml::sax::XDocumentHandler;
90 using com::sun::star::xml::sax::XParser;
91 
92 void callHandler(uno::Reference < XDocumentHandler > xDocHandler);
93 
importImpl(const Sequence<::com::sun::star::beans::PropertyValue> & aDescriptor)94 sal_Bool SAL_CALL WordPerfectImportFilter::importImpl( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
95     throw (RuntimeException)
96 {
97     WRITER_DEBUG_MSG(("WordPerfectImportFilter::importImpl: Got here!\n"));
98 
99     sal_Int32 nLength = aDescriptor.getLength();
100     const PropertyValue * pValue = aDescriptor.getConstArray();
101     OUString sURL;
102     uno::Reference < XInputStream > xInputStream;
103     for ( sal_Int32 i = 0 ; i < nLength; i++)
104     {
105         if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "InputStream" ) ) )
106         pValue[i].Value >>= xInputStream;
107         else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "URL" ) ) )
108         pValue[i].Value >>= sURL;
109     }
110     if ( !xInputStream.is() )
111     {
112         OSL_ASSERT( 0 );
113         return sal_False;
114     }
115     OString sFileName;
116     sFileName = OUStringToOString(sURL, RTL_TEXTENCODING_INFO_ASCII);
117 
118     // An XML import service: what we push sax messages to..
119     OUString sXMLImportService ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.XMLImporter" ) );
120     uno::Reference < XDocumentHandler > xInternalHandler( mxMSF->createInstance( sXMLImportService ), UNO_QUERY );
121 
122     // The XImporter sets up an empty target document for XDocumentHandler to write to..
123     uno::Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
124     xImporter->setTargetDocument(mxDoc);
125 
126         // OO Document Handler: abstract class to handle document SAX messages, concrete implementation here
127         // writes to in-memory target doc
128         DocumentHandler xHandler(xInternalHandler);
129 
130     WPXSvInputStream input( xInputStream );
131 
132     WordPerfectCollector collector(&input, &xHandler);
133     collector.filter();
134 
135     return true;
136 }
137 
filter(const Sequence<::com::sun::star::beans::PropertyValue> & aDescriptor)138 sal_Bool SAL_CALL WordPerfectImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
139     throw (RuntimeException)
140 {
141     WRITER_DEBUG_MSG(("WordPerfectImportFilter::filter: Got here!\n"));
142     return importImpl ( aDescriptor );
143 }
cancel()144 void SAL_CALL WordPerfectImportFilter::cancel(  )
145     throw (RuntimeException)
146 {
147     WRITER_DEBUG_MSG(("WordPerfectImportFilter::cancel: Got here!\n"));
148 }
149 
150 // XImporter
setTargetDocument(const uno::Reference<::com::sun::star::lang::XComponent> & xDoc)151 void SAL_CALL WordPerfectImportFilter::setTargetDocument( const uno::Reference< ::com::sun::star::lang::XComponent >& xDoc )
152     throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException)
153 {
154     WRITER_DEBUG_MSG(("WordPerfectImportFilter::getTargetDocument: Got here!\n"));
155     meType = FILTER_IMPORT;
156     mxDoc = xDoc;
157 }
158 
159 // XExtendedFilterDetection
detect(com::sun::star::uno::Sequence<PropertyValue> & Descriptor)160 OUString SAL_CALL WordPerfectImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
161     throw( com::sun::star::uno::RuntimeException )
162 {
163     WRITER_DEBUG_MSG(("WordPerfectImportFilter::detect: Got here!\n"));
164 
165     WPDConfidence confidence = WPD_CONFIDENCE_NONE;
166     OUString sTypeName = OUString( RTL_CONSTASCII_USTRINGPARAM ( "" ) );
167     sal_Int32 nLength = Descriptor.getLength();
168     sal_Int32 location = nLength;
169     OUString sURL;
170     const PropertyValue * pValue = Descriptor.getConstArray();
171     uno::Reference < XInputStream > xInputStream;
172     for ( sal_Int32 i = 0 ; i < nLength; i++)
173     {
174         if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TypeName" ) ) )
175             location=i;
176         else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "InputStream" ) ) )
177             pValue[i].Value >>= xInputStream;
178         else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "URL" ) ) )
179             pValue[i].Value >>= sURL;
180     }
181 
182         uno::Reference< com::sun::star::ucb::XCommandEnvironment > xEnv;
183         if (!xInputStream.is())
184         {
185         try
186         {
187             ::ucbhelper::Content aContent(sURL, xEnv);
188             xInputStream = aContent.openStream();
189         }
190         catch ( ... )
191         {
192             return ::rtl::OUString();
193         }
194 
195                 if (!xInputStream.is())
196                     return ::rtl::OUString();
197         }
198 
199     WPXSvInputStream input( xInputStream );
200 
201     if (input.atEOS())
202         return ::rtl::OUString();
203 
204     confidence = WPDocument::isFileFormatSupported(&input, false);
205 
206     if (confidence == WPD_CONFIDENCE_EXCELLENT)
207         sTypeName = OUString( RTL_CONSTASCII_USTRINGPARAM ( "writer_WordPerfect_Document" ) );
208 
209     if (sTypeName.getLength())
210     {
211         if ( location == Descriptor.getLength() )
212         {
213             Descriptor.realloc(nLength+1);
214             Descriptor[location].Name = ::rtl::OUString::createFromAscii( "TypeName" );
215         }
216 
217         Descriptor[location].Value <<=sTypeName;
218     }
219 
220     return sTypeName;
221 }
222 
223 
224 // XInitialization
initialize(const Sequence<Any> & aArguments)225 void SAL_CALL WordPerfectImportFilter::initialize( const Sequence< Any >& aArguments )
226     throw (Exception, RuntimeException)
227 {
228     WRITER_DEBUG_MSG(("WordPerfectImportFilter::initialize: Got here!\n"));
229     Sequence < PropertyValue > aAnySeq;
230     sal_Int32 nLength = aArguments.getLength();
231     if ( nLength && ( aArguments[0] >>= aAnySeq ) )
232     {
233         const PropertyValue * pValue = aAnySeq.getConstArray();
234         nLength = aAnySeq.getLength();
235         for ( sal_Int32 i = 0 ; i < nLength; i++)
236         {
237             if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) )
238             {
239                 pValue[i].Value >>= msFilterName;
240                 break;
241             }
242         }
243     }
244 }
WordPerfectImportFilter_getImplementationName()245 OUString WordPerfectImportFilter_getImplementationName ()
246     throw (RuntimeException)
247 {
248     return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.WordPerfectImportFilter" ) );
249 }
250 
251 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
252 #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
WordPerfectImportFilter_supportsService(const OUString & ServiceName)253 sal_Bool SAL_CALL WordPerfectImportFilter_supportsService( const OUString& ServiceName )
254     throw (RuntimeException)
255 {
256     return (ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ) ||
257         ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME2 ) ) );
258 }
WordPerfectImportFilter_getSupportedServiceNames()259 Sequence< OUString > SAL_CALL WordPerfectImportFilter_getSupportedServiceNames(  )
260     throw (RuntimeException)
261 {
262     Sequence < OUString > aRet(2);
263 //  Sequence < OUString > aRet(1);
264         OUString* pArray = aRet.getArray();
265         pArray[0] =  OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1 ) );
266     pArray[1] =  OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME2 ) );
267         return aRet;
268 }
269 #undef SERVICE_NAME2
270 #undef SERVICE_NAME1
271 
WordPerfectImportFilter_createInstance(const uno::Reference<XMultiServiceFactory> & rSMgr)272 uno::Reference< XInterface > SAL_CALL WordPerfectImportFilter_createInstance( const uno::Reference< XMultiServiceFactory > & rSMgr)
273     throw( Exception )
274 {
275     return (cppu::OWeakObject*) new WordPerfectImportFilter( rSMgr );
276 }
277 
278 // XServiceInfo
getImplementationName()279 OUString SAL_CALL WordPerfectImportFilter::getImplementationName(  )
280     throw (RuntimeException)
281 {
282     return WordPerfectImportFilter_getImplementationName();
283 }
supportsService(const OUString & rServiceName)284 sal_Bool SAL_CALL WordPerfectImportFilter::supportsService( const OUString& rServiceName )
285     throw (RuntimeException)
286 {
287     return WordPerfectImportFilter_supportsService( rServiceName );
288 }
getSupportedServiceNames()289 Sequence< OUString > SAL_CALL WordPerfectImportFilter::getSupportedServiceNames(  )
290     throw (RuntimeException)
291 {
292     return WordPerfectImportFilter_getSupportedServiceNames();
293 }
294