LibreOffice Module lotuswordpro (master) 1
LotusWordProImportFilter.cxx
Go to the documentation of this file.
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/xml/sax/XDocumentHandler.hpp>
22#include <com/sun/star/ucb/XCommandEnvironment.hpp>
23#include <com/sun/star/uno/Reference.hxx>
24#include <com/sun/star/uno/XComponentContext.hpp>
26#include <sal/macros.h>
27#include <tools/stream.hxx>
28#include <ucbhelper/content.hxx>
29
31#include "lwpfilter.hxx"
32
33#if OSL_DEBUG_LEVEL > 0
34#include <memory>
35
36#include <com/sun/star/io/XOutputStream.hpp>
37#include <com/sun/star/xml/sax/Writer.hpp>
38
40
41#include <cppuhelper/weak.hxx>
42
43#include <osl/file.hxx>
44#include <utility>
45#endif
46
47using namespace com::sun::star;
49using com::sun::star::uno::Any;
50using com::sun::star::uno::UNO_QUERY;
51using com::sun::star::uno::Exception;
52using com::sun::star::io::XInputStream;
53using com::sun::star::beans::PropertyValue;
54using com::sun::star::ucb::XCommandEnvironment;
55using com::sun::star::document::XImporter;
56using com::sun::star::xml::sax::XDocumentHandler;
57
58#if OSL_DEBUG_LEVEL > 0
59namespace
60{
61
62class DebugDocumentHandler final : public cppu::WeakImplHelper< XDocumentHandler >
63{
64public:
65 DebugDocumentHandler(uno::Reference< XDocumentHandler > handler, uno::Reference< XDocumentHandler > debug)
66 : m_handler(std::move(handler))
67 , m_debug(std::move(debug))
68 {
69 }
70
71 // XDocumentHandler
72
73 virtual void SAL_CALL
74 startDocument() override
75 {
76 m_handler->startDocument();
77 m_debug->startDocument();
78 }
79
80 virtual void SAL_CALL
81 endDocument() override
82 {
83 m_handler->endDocument();
84 m_debug->endDocument();
85 }
86
87 virtual void SAL_CALL
88 startElement(
89 const OUString& aName,
90 const uno::Reference< xml::sax::XAttributeList > & xAttribs) override
91 {
92 m_handler->startElement(aName, xAttribs);
93 m_debug->ignorableWhitespace(" "); // NOTE: needed for pretty-printing
94 m_debug->startElement(aName, xAttribs);
95 }
96
97 virtual void SAL_CALL
98 endElement(const OUString& aName) override
99 {
100 m_handler->endElement(aName);
101 m_debug->ignorableWhitespace(" "); // NOTE: needed for pretty-printing
102 m_debug->endElement(aName);
103 }
104
105 virtual void SAL_CALL
106 characters(const OUString& aChars) override
107 {
108 m_handler->characters(aChars);
109 m_debug->characters(aChars);
110 }
111
112 virtual void SAL_CALL
113 ignorableWhitespace(const OUString& aWhitespaces) override
114 {
115 m_handler->ignorableWhitespace(aWhitespaces);
116 m_debug->ignorableWhitespace(aWhitespaces);
117 }
118
119 virtual void SAL_CALL
120 processingInstruction(const OUString& aTarget, const OUString& aData) override
121 {
122 m_handler->processingInstruction(aTarget, aData);
123 m_debug->processingInstruction(aTarget, aData);
124 }
125
126 virtual void SAL_CALL
127 setDocumentLocator(const uno::Reference< xml::sax::XLocator >& xLocator) override
128 {
129 m_handler->setDocumentLocator(xLocator);
130 m_debug->setDocumentLocator(xLocator);
131 }
132
133 // XInterface
134
135 virtual uno::Any SAL_CALL queryInterface(const uno::Type & rType) override
136 {
137 uno::Any aIface = cppu::WeakImplHelper< XDocumentHandler >::queryInterface(rType);
138 // delegate all queries we cannot satisfy ourselves to the real handler
139 if (!aIface.has< uno::Reference< uno::XInterface > >())
140 aIface = m_handler->queryInterface(rType);
141 return aIface;
142 }
143
144private:
145 uno::Reference< XDocumentHandler > m_handler;
146 uno::Reference< XDocumentHandler > m_debug;
147};
148
149}
150#endif
151
152// W o r d P r o
153constexpr sal_Int8 header[] = { 0x57, 0x6f, 0x72, 0x64, 0x50, 0x72, 0x6f };
154
156{
157 OUString sURL;
158 for (const PropertyValue& rValue : aDescriptor)
159 {
160 //Note, we should attempt to use InputStream here first!
161 if ( rValue.Name == "URL" )
162 rValue.Value >>= sURL;
163 }
164
165 SvFileStream inputStream( sURL, StreamMode::READ );
166 if (!inputStream.good())
167 return false;
168
169 // An XML import service: what we push sax messages to...
170 uno::Reference< XDocumentHandler > xInternalHandler(
171 mxContext->getServiceManager()->createInstanceWithContext( "com.sun.star.comp.Writer.XMLImporter", mxContext ), UNO_QUERY );
172
173#if OSL_DEBUG_LEVEL > 0
174 std::unique_ptr<osl::File> pDebugFile;
175 const char* pDir = getenv("DBG_LWPIMPORT_DIR");
176 if (pDir)
177 {
178 OUString aStr(OStringToOUString(pDir, RTL_TEXTENCODING_UTF8));
179 OUString aFileURL;
180 osl_getFileURLFromSystemPath(aStr.pData, &aFileURL.pData);
181 pDebugFile = std::make_unique<osl::File>(aFileURL + "/lwpimport.xml");
182 if (pDebugFile->open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Create) != osl::File::E_None)
183 {
184 pDebugFile->open(osl_File_OpenFlag_Write);
185 pDebugFile->setSize(0);
186 }
187
188 uno::Reference< xml::sax::XWriter > xDebugWriter = xml::sax::Writer::create(mxContext);
189 uno::Reference< io::XOutputStream > xOutputStream(new comphelper::OSLOutputStreamWrapper(*pDebugFile));
190 xDebugWriter->setOutputStream(xOutputStream);
191
192 xInternalHandler.set(new DebugDocumentHandler(xInternalHandler, xDebugWriter));
193 }
194#endif
195
196 uno::Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
197 if (xImporter.is())
198 xImporter->setTargetDocument(mxDoc);
199
200 return ( ReadWordproFile( inputStream, xInternalHandler) == 0 );
201
202}
203
204extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportLWP(SvStream &rStream)
205{
206 uno::Reference< XDocumentHandler > xHandler;
207 return ReadWordproFile(rStream, xHandler) == 0;
208}
209
211{
212 return importImpl ( aDescriptor );
213}
215{
216}
217
218// XImporter
219void SAL_CALL LotusWordProImportFilter::setTargetDocument( const uno::Reference< css::lang::XComponent >& xDoc )
220{
221 mxDoc = xDoc;
222}
223
224// XExtendedFilterDetection
225OUString SAL_CALL LotusWordProImportFilter::detect( css::uno::Sequence< PropertyValue >& Descriptor )
226{
227 OUString sTypeName( "writer_LotusWordPro_Document" );
228 OUString sURL;
229 uno::Reference < XInputStream > xInputStream;
230 for (const PropertyValue& rValue : std::as_const(Descriptor))
231 {
232 if ( rValue.Name == "TypeName" )
233 rValue.Value >>= sTypeName;
234 else if ( rValue.Name == "InputStream" )
235 rValue.Value >>= xInputStream;
236 else if ( rValue.Name == "URL" )
237 rValue.Value >>= sURL;
238 }
239
240 uno::Reference< css::ucb::XCommandEnvironment > xEnv;
241 if (!xInputStream.is())
242 {
243 try
244 {
245 ::ucbhelper::Content aContent(sURL, xEnv, mxContext);
246 xInputStream = aContent.openStream();
247 }
248 catch ( Exception& )
249 {
250 return OUString();
251 }
252
253 if (!xInputStream.is())
254 return OUString();
255 }
256
258 constexpr sal_Int32 nLen = SAL_N_ELEMENTS( header );
259 if ( ( nLen != xInputStream->readBytes( aData, nLen ) )
260 || ( memcmp( static_cast<void const *>(header), static_cast<void const *>(aData.getConstArray()), nLen ) != 0 ) )
261 sTypeName.clear();
262
263 return sTypeName;
264}
265
266// XInitialization
267void SAL_CALL LotusWordProImportFilter::initialize( const Sequence< Any >& /*aArguments*/ )
268{
269}
270
271// XServiceInfo
273{
274 return "com.sun.star.comp.Writer.LotusWordProImportFilter";
275}
276
277sal_Bool SAL_CALL LotusWordProImportFilter::supportsService(const OUString& rServiceName)
278{
279 return cppu::supportsService(this, rServiceName);
280}
281
283{
284 return { "com.sun.star.document.ImportFilter", "com.sun.star.document.ExtendedTypeDetection" };
285}
286
287extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
289 css::uno::XComponentContext *context,
290 css::uno::Sequence<css::uno::Any> const &)
291{
292 return cppu::acquire(new LotusWordProImportFilter(context));
293}
294
295/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * LotusWordProImportFilter_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
constexpr sal_Int8 header[]
SAL_DLLPUBLIC_EXPORT bool TestImportLWP(SvStream &rStream)
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL setTargetDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
virtual OUString SAL_CALL detect(css::uno::Sequence< css::beans::PropertyValue > &Descriptor) override
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
bool importImpl(const css::uno::Sequence< css::beans::PropertyValue > &aDescriptor)
virtual sal_Bool SAL_CALL filter(const css::uno::Sequence< css::beans::PropertyValue > &aDescriptor) override
css::uno::Reference< css::lang::XComponent > mxDoc
virtual void SAL_CALL cancel() override
bool good() const
css::uno::Reference< css::io::XInputStream > openStream()
int ReadWordproFile(SvStream &rStream, uno::Reference< css::xml::sax::XDocumentHandler > const &xHandler)
Definition: lwpfilter.cxx:170
#define SAL_N_ELEMENTS(arr)
aStr
constexpr OUStringLiteral aData
@ Exception
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool
signed char sal_Int8