Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/io/XInputStream.hpp>
21 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
23 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
24 : #include <com/sun/star/xml/sax/XParser.hpp>
25 : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
26 : #include <com/sun/star/uno/Reference.hxx>
27 : #include <comphelper/processfactory.hxx>
28 : #include <cppuhelper/supportsservice.hxx>
29 : #include <osl/diagnose.h>
30 : #include <rtl/tencinfo.h>
31 : #include <sal/macros.h>
32 : #include <tools/stream.hxx>
33 : #include <vector>
34 : #include <ucbhelper/content.hxx>
35 : #include <xmloff/attrlist.hxx>
36 :
37 : #include "LotusWordProImportFilter.hxx"
38 : #include "lwpfilter.hxx"
39 :
40 : using namespace com::sun::star;
41 : using com::sun::star::uno::Sequence;
42 : using com::sun::star::lang::XComponent;
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::io::XInputStream;
49 : using com::sun::star::lang::XMultiServiceFactory;
50 : using com::sun::star::beans::PropertyValue;
51 : using com::sun::star::document::XFilter;
52 : using com::sun::star::document::XExtendedFilterDetection;
53 : using com::sun::star::ucb::XCommandEnvironment;
54 :
55 : using com::sun::star::document::XImporter;
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 : // W o r d P r o
61 : static const sal_Int8 header[] = { 0x57, 0x6f, 0x72, 0x64, 0x50, 0x72, 0x6f };
62 :
63 12 : bool SAL_CALL LotusWordProImportFilter::importImpl( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
64 : throw (RuntimeException)
65 : {
66 :
67 12 : sal_Int32 nLength = aDescriptor.getLength();
68 12 : const PropertyValue * pValue = aDescriptor.getConstArray();
69 12 : OUString sURL;
70 24 : for ( sal_Int32 i = 0 ; i < nLength; i++)
71 : {
72 : //Note, we should attempt to use InputStream here first!
73 12 : if ( pValue[i].Name == "URL" )
74 12 : pValue[i].Value >>= sURL;
75 : }
76 :
77 24 : SvFileStream inputStream( sURL, STREAM_READ );
78 12 : if ( inputStream.IsEof() || ( inputStream.GetError() != SVSTREAM_OK ) )
79 0 : return false;
80 :
81 : // An XML import service: what we push sax messages to..
82 24 : OUString sXMLImportService ( "com.sun.star.comp.Writer.XMLImporter" );
83 :
84 24 : uno::Reference< XDocumentHandler > xInternalHandler( mxContext->getServiceManager()->createInstanceWithContext( sXMLImportService, mxContext ), UNO_QUERY );
85 24 : uno::Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
86 12 : if (xImporter.is())
87 0 : xImporter->setTargetDocument(mxDoc);
88 :
89 24 : return ( ReadWordproFile( inputStream, xInternalHandler) == 0 );
90 :
91 : }
92 :
93 12 : sal_Bool SAL_CALL LotusWordProImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
94 : throw (RuntimeException, std::exception)
95 : {
96 12 : return importImpl ( aDescriptor );
97 : }
98 0 : void SAL_CALL LotusWordProImportFilter::cancel( )
99 : throw (RuntimeException, std::exception)
100 : {
101 0 : }
102 :
103 : // XImporter
104 0 : void SAL_CALL LotusWordProImportFilter::setTargetDocument( const uno::Reference< ::com::sun::star::lang::XComponent >& xDoc )
105 : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
106 : {
107 0 : mxDoc = xDoc;
108 0 : }
109 :
110 : // XExtendedFilterDetection
111 0 : OUString SAL_CALL LotusWordProImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
112 : throw( com::sun::star::uno::RuntimeException, std::exception )
113 : {
114 :
115 0 : OUString sTypeName( "writer_LotusWordPro_Document" );
116 0 : sal_Int32 nLength = Descriptor.getLength();
117 0 : OUString sURL;
118 0 : const PropertyValue * pValue = Descriptor.getConstArray();
119 0 : uno::Reference < XInputStream > xInputStream;
120 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
121 : {
122 0 : if ( pValue[i].Name == "TypeName" )
123 0 : pValue[i].Value >>= sTypeName;
124 0 : else if ( pValue[i].Name == "InputStream" )
125 0 : pValue[i].Value >>= xInputStream;
126 0 : else if ( pValue[i].Name == "URL" )
127 0 : pValue[i].Value >>= sURL;
128 : }
129 :
130 0 : uno::Reference< com::sun::star::ucb::XCommandEnvironment > xEnv;
131 0 : if (!xInputStream.is())
132 : {
133 : try
134 : {
135 0 : ::ucbhelper::Content aContent(sURL, xEnv, mxContext);
136 0 : xInputStream = aContent.openStream();
137 : }
138 0 : catch ( Exception& )
139 : {
140 0 : return OUString();
141 : }
142 :
143 0 : if (!xInputStream.is())
144 0 : return OUString();
145 : }
146 :
147 0 : Sequence< ::sal_Int8 > aData;
148 0 : sal_Int32 nLen = SAL_N_ELEMENTS( header );
149 0 : if ( !( ( nLen == xInputStream->readBytes( aData, nLen ) )
150 0 : && ( memcmp( ( void* )header, (void*) aData.getConstArray(), nLen ) == 0 ) ) )
151 0 : sTypeName = OUString();
152 :
153 0 : return sTypeName;
154 : }
155 :
156 : // XInitialization
157 0 : void SAL_CALL LotusWordProImportFilter::initialize( const Sequence< Any >& aArguments )
158 : throw (Exception, RuntimeException, std::exception)
159 : {
160 0 : Sequence < PropertyValue > aAnySeq;
161 0 : sal_Int32 nLength = aArguments.getLength();
162 0 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
163 : {
164 0 : const PropertyValue * pValue = aAnySeq.getConstArray();
165 0 : nLength = aAnySeq.getLength();
166 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
167 : {
168 0 : if ( pValue[i].Name == "Type" )
169 : {
170 0 : pValue[i].Value >>= msFilterName;
171 0 : break;
172 : }
173 : }
174 0 : }
175 0 : }
176 :
177 2 : OUString LotusWordProImportFilter_getImplementationName ()
178 : throw (RuntimeException)
179 : {
180 2 : return OUString ( "com.sun.star.comp.Writer.LotusWordProImportFilter" );
181 : }
182 :
183 : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
184 : #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
185 :
186 2 : Sequence< OUString > SAL_CALL LotusWordProImportFilter_getSupportedServiceNames( )
187 : throw (RuntimeException)
188 : {
189 2 : Sequence < OUString > aRet(2);
190 2 : OUString* pArray = aRet.getArray();
191 2 : pArray[0] = OUString ( SERVICE_NAME1 );
192 2 : pArray[1] = OUString ( SERVICE_NAME2 );
193 2 : return aRet;
194 : }
195 :
196 : #undef SERVICE_NAME2
197 : #undef SERVICE_NAME1
198 :
199 2 : uno::Reference< XInterface > SAL_CALL LotusWordProImportFilter_createInstance( const uno::Reference< XMultiServiceFactory > & rSMgr)
200 : throw( Exception )
201 : {
202 2 : return (cppu::OWeakObject*) new LotusWordProImportFilter( comphelper::getComponentContext(rSMgr) );
203 : }
204 :
205 : // XServiceInfo
206 0 : OUString SAL_CALL LotusWordProImportFilter::getImplementationName( )
207 : throw (RuntimeException, std::exception)
208 : {
209 0 : return LotusWordProImportFilter_getImplementationName();
210 : }
211 0 : sal_Bool SAL_CALL LotusWordProImportFilter::supportsService( const OUString& rServiceName )
212 : throw (RuntimeException, std::exception)
213 : {
214 0 : return cppu::supportsService(this, rServiceName);
215 : }
216 0 : Sequence< OUString > SAL_CALL LotusWordProImportFilter::getSupportedServiceNames( )
217 : throw (RuntimeException, std::exception)
218 : {
219 0 : return LotusWordProImportFilter_getSupportedServiceNames();
220 : }
221 :
222 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|