1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include <osl/diagnose.h>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
27 #include <com/sun/star/io/XInputStream.hpp>
28 #include <comphelper/mediadescriptor.hxx>
29 #include <oox/core/filterdetect.hxx>
30 #include <dmapper/DomainMapper.hxx>
31 #include <WriterFilter.hxx>
32 #include <doctok/WW8Document.hxx>
33 #include <ooxml/OOXMLDocument.hxx>
34 #ifdef DEBUG_IMPORT
35 #include <iostream>
36 #include <osl/process.h>
37 #endif
38
39 #include <resourcemodel/TagLogger.hxx>
40 using namespace ::rtl;
41 using namespace ::com::sun::star;
42 using ::comphelper::MediaDescriptor;
43
44 /*-- 09.06.2006 10:15:20---------------------------------------------------
45
46 -----------------------------------------------------------------------*/
filter(const uno::Sequence<beans::PropertyValue> & aDescriptor)47 sal_Bool WriterFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
48 throw (uno::RuntimeException)
49 {
50 if( m_xSrcDoc.is() )
51 {
52 uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
53 uno::Reference< uno::XInterface > xIfc( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.DocxExport" ))), uno::UNO_QUERY_THROW);
54 if (!xIfc.is())
55 return sal_False;
56 uno::Reference< document::XExporter > xExprtr(xIfc, uno::UNO_QUERY_THROW);
57 uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
58 if (!xExprtr.is() || !xFltr.is())
59 return sal_False;
60 xExprtr->setSourceDocument(m_xSrcDoc);
61 return xFltr->filter(aDescriptor);
62 }
63 else if (m_xDstDoc.is())
64 {
65 MediaDescriptor aMediaDesc( aDescriptor );
66 OUString sFilterName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_FILTERNAME(), OUString() );
67
68 uno::Reference< io::XInputStream > xInputStream;
69 try
70 {
71 // use the oox.core.FilterDetect implementation to extract the decrypted ZIP package
72 ::oox::core::FilterDetect aDetector( m_xContext );
73 xInputStream = aDetector.extractUnencryptedPackage( aMediaDesc );
74 }
75 catch( uno::Exception& e)
76 {
77 (void) e;
78 }
79
80 if ( !xInputStream.is() )
81 {
82 return sal_False;
83 }
84
85 #ifdef DEBUG_IMPORT
86 OUString sURL = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() );
87 ::std::string sURLc = OUStringToOString(sURL, RTL_TEXTENCODING_ASCII_US).getStr();
88
89 writerfilter::TagLogger::Pointer_t debugLogger
90 (writerfilter::TagLogger::getInstance("DEBUG"));
91 debugLogger->setFileName(sURLc);
92 debugLogger->startDocument();
93
94 writerfilter::TagLogger::Pointer_t dmapperLogger
95 (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
96 dmapperLogger->setFileName(sURLc);
97 dmapperLogger->startDocument();
98 #endif
99
100 writerfilter::dmapper::SourceDocumentType eType =
101 (m_sFilterName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_2007" ) ) ||
102 m_sFilterName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_2007_Template" ) )) ?
103 writerfilter::dmapper::DOCUMENT_OOXML : writerfilter::dmapper::DOCUMENT_DOC;
104 writerfilter::Stream::Pointer_t pStream(new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, eType));
105 //create the tokenizer and domain mapper
106 if( eType == writerfilter::dmapper::DOCUMENT_OOXML )
107 {
108 writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream = writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext, xInputStream);
109 writerfilter::ooxml::OOXMLDocument::Pointer_t pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream));
110
111 uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
112 pDocument->setModel(xModel);
113
114 uno::Reference<drawing::XDrawPageSupplier> xDrawings
115 (m_xDstDoc, uno::UNO_QUERY_THROW);
116 uno::Reference<drawing::XDrawPage> xDrawPage
117 (xDrawings->getDrawPage(), uno::UNO_SET_THROW);
118 pDocument->setDrawPage(xDrawPage);
119
120 pDocument->resolve(*pStream);
121 }
122 else
123 {
124 writerfilter::doctok::WW8Stream::Pointer_t pDocStream = writerfilter::doctok::WW8DocumentFactory::createStream(m_xContext, xInputStream);
125 writerfilter::doctok::WW8Document::Pointer_t pDocument(writerfilter::doctok::WW8DocumentFactory::createDocument(pDocStream));
126
127 pDocument->resolve(*pStream);
128 }
129
130 #ifdef DEBUG_IMPORT
131 writerfilter::TagLogger::dump("DOMAINMAPPER");
132 dmapperLogger->endDocument();
133 writerfilter::TagLogger::dump("DEBUG");
134 debugLogger->endDocument();
135 #endif
136
137 return sal_True;
138 }
139 return sal_False;
140 }
141 /*-- 09.06.2006 10:15:20---------------------------------------------------
142
143 -----------------------------------------------------------------------*/
cancel()144 void WriterFilter::cancel( ) throw (uno::RuntimeException)
145 {
146 }
147
148 /*-- 09.06.2006 10:15:20---------------------------------------------------
149
150 -----------------------------------------------------------------------*/
setTargetDocument(const uno::Reference<lang::XComponent> & xDoc)151 void WriterFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
152 throw (lang::IllegalArgumentException, uno::RuntimeException)
153 {
154 m_xDstDoc = xDoc;
155 }
156
setSourceDocument(const uno::Reference<lang::XComponent> & xDoc)157 void WriterFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
158 throw (lang::IllegalArgumentException, uno::RuntimeException)
159 {
160 m_xSrcDoc = xDoc;
161 }
162
163 /*-- 09.06.2006 10:15:20---------------------------------------------------
164
165 -----------------------------------------------------------------------*/
initialize(const uno::Sequence<uno::Any> & aArguments)166 void WriterFilter::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException)
167 {
168 uno::Sequence < beans::PropertyValue > aAnySeq;
169 sal_Int32 nLength = aArguments.getLength();
170 if ( nLength && ( aArguments[0] >>= aAnySeq ) )
171 {
172 const beans::PropertyValue * pValue = aAnySeq.getConstArray();
173 nLength = aAnySeq.getLength();
174 for ( sal_Int32 i = 0 ; i < nLength; i++)
175 {
176 if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) )
177 {
178 pValue[i].Value >>= m_sFilterName;
179 break;
180 }
181 }
182 }
183 }
184 /*-- 09.06.2006 10:15:20---------------------------------------------------
185
186 -----------------------------------------------------------------------*/
WriterFilter_getImplementationName()187 OUString WriterFilter_getImplementationName () throw (uno::RuntimeException)
188 {
189 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.WriterFilter" ) );
190 }
191
192 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
193 #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
194 /*-- 09.06.2006 10:15:20---------------------------------------------------
195
196 -----------------------------------------------------------------------*/
WriterFilter_supportsService(const OUString & ServiceName)197 sal_Bool WriterFilter_supportsService( const OUString& ServiceName ) throw (uno::RuntimeException)
198 {
199 return (ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ) ||
200 ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ));
201 }
202 /*-- 09.06.2006 10:15:20---------------------------------------------------
203
204 -----------------------------------------------------------------------*/
WriterFilter_getSupportedServiceNames()205 uno::Sequence< OUString > WriterFilter_getSupportedServiceNames( ) throw (uno::RuntimeException)
206 {
207 uno::Sequence < OUString > aRet(2);
208 OUString* pArray = aRet.getArray();
209 pArray[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1 ) );
210 pArray[1] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME2 ) );
211 return aRet;
212 }
213 #undef SERVICE_NAME1
214 #undef SERVICE_NAME2
215
216 /*-- 09.06.2006 10:15:20---------------------------------------------------
217
218 -----------------------------------------------------------------------*/
WriterFilter_createInstance(const uno::Reference<uno::XComponentContext> & xContext)219 uno::Reference< uno::XInterface > WriterFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
220 throw( uno::Exception )
221 {
222 return (cppu::OWeakObject*) new WriterFilter( xContext );
223 }
224
225 /*-- 09.06.2006 10:15:20---------------------------------------------------
226
227 -----------------------------------------------------------------------*/
getImplementationName()228 OUString WriterFilter::getImplementationName( ) throw (uno::RuntimeException)
229 {
230 return WriterFilter_getImplementationName();
231 }
232 /*-- 09.06.2006 10:15:20---------------------------------------------------
233
234 -----------------------------------------------------------------------*/
supportsService(const OUString & rServiceName)235 sal_Bool WriterFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
236 {
237 return WriterFilter_supportsService( rServiceName );
238 }
239 /*-- 09.06.2006 10:15:20---------------------------------------------------
240
241 -----------------------------------------------------------------------*/
getSupportedServiceNames()242 uno::Sequence< OUString > WriterFilter::getSupportedServiceNames( ) throw (uno::RuntimeException)
243 {
244 return WriterFilter_getSupportedServiceNames();
245 }
246
247