1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ImportFilter.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/diagnose.h>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
34 #include <com/sun/star/io/XInputStream.hpp>
35 #include <comphelper/mediadescriptor.hxx>
36 #include <oox/core/filterdetect.hxx>
37 #include <dmapper/DomainMapper.hxx>
38 #include <WriterFilter.hxx>
39 #include <doctok/WW8Document.hxx>
40 #include <ooxml/OOXMLDocument.hxx>
43 #include <osl/process.h>
46 #include <resourcemodel/TagLogger.hxx>
47 using namespace ::rtl
;
48 using namespace ::com::sun::star
;
49 using ::comphelper::MediaDescriptor
;
51 /*-- 09.06.2006 10:15:20---------------------------------------------------
53 -----------------------------------------------------------------------*/
54 sal_Bool
WriterFilter::filter( const uno::Sequence
< beans::PropertyValue
>& aDescriptor
)
55 throw (uno::RuntimeException
)
59 uno::Reference
< lang::XMultiServiceFactory
> xMSF(m_xContext
->getServiceManager(), uno::UNO_QUERY_THROW
);
60 uno::Reference
< uno::XInterface
> xIfc( xMSF
->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.DocxExport" ))), uno::UNO_QUERY_THROW
);
63 uno::Reference
< document::XExporter
> xExprtr(xIfc
, uno::UNO_QUERY_THROW
);
64 uno::Reference
< document::XFilter
> xFltr(xIfc
, uno::UNO_QUERY_THROW
);
65 if (!xExprtr
.is() || !xFltr
.is())
67 xExprtr
->setSourceDocument(m_xSrcDoc
);
68 return xFltr
->filter(aDescriptor
);
70 else if (m_xDstDoc
.is())
72 MediaDescriptor
aMediaDesc( aDescriptor
);
73 OUString sFilterName
= aMediaDesc
.getUnpackedValueOrDefault( MediaDescriptor::PROP_FILTERNAME(), OUString() );
75 uno::Reference
< io::XInputStream
> xInputStream
;
78 // use the oox.core.FilterDetect implementation to extract the decrypted ZIP package
79 uno::Reference
< lang::XMultiServiceFactory
> xFactory( m_xContext
->getServiceManager(), uno::UNO_QUERY_THROW
);
80 ::oox::core::FilterDetect
aDetector( xFactory
);
81 xInputStream
= aDetector
.extractUnencryptedPackage( aMediaDesc
);
83 catch( uno::Exception
& )
87 if ( !xInputStream
.is() )
93 writerfilter::TagLogger::Pointer_t debugLogger
94 (writerfilter::TagLogger::getInstance("DEBUG"));
95 debugLogger
->startDocument();
97 writerfilter::TagLogger::Pointer_t dmapperLogger
98 (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
99 dmapperLogger
->startDocument();
102 writerfilter::dmapper::SourceDocumentType eType
=
103 (m_sFilterName
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_2007" ) ) ||
104 m_sFilterName
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_2007_Template" ) )) ?
105 writerfilter::dmapper::DOCUMENT_OOXML
: writerfilter::dmapper::DOCUMENT_DOC
;
106 writerfilter::Stream::Pointer_t
pStream(new writerfilter::dmapper::DomainMapper(m_xContext
, xInputStream
, m_xDstDoc
, eType
));
107 //create the tokenizer and domain mapper
108 if( eType
== writerfilter::dmapper::DOCUMENT_OOXML
)
110 writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream
= writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext
, xInputStream
);
111 writerfilter::ooxml::OOXMLDocument::Pointer_t
pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream
));
113 uno::Reference
<frame::XModel
> xModel(m_xDstDoc
, uno::UNO_QUERY_THROW
);
114 pDocument
->setModel(xModel
);
116 uno::Reference
<drawing::XDrawPageSupplier
> xDrawings
117 (m_xDstDoc
, uno::UNO_QUERY_THROW
);
118 uno::Reference
<drawing::XDrawPage
> xDrawPage
119 (xDrawings
->getDrawPage(), uno::UNO_SET_THROW
);
120 pDocument
->setDrawPage(xDrawPage
);
122 pDocument
->resolve(*pStream
);
126 writerfilter::doctok::WW8Stream::Pointer_t pDocStream
= writerfilter::doctok::WW8DocumentFactory::createStream(m_xContext
, xInputStream
);
127 writerfilter::doctok::WW8Document::Pointer_t
pDocument(writerfilter::doctok::WW8DocumentFactory::createDocument(pDocStream
));
129 pDocument
->resolve(*pStream
);
133 writerfilter::TagLogger::dump("DEBUG");
134 debugLogger
->endDocument();
135 writerfilter::TagLogger::dump("DOMAINMAPPER");
136 dmapperLogger
->endDocument();
143 /*-- 09.06.2006 10:15:20---------------------------------------------------
145 -----------------------------------------------------------------------*/
146 void WriterFilter::cancel( ) throw (uno::RuntimeException
)
150 /*-- 09.06.2006 10:15:20---------------------------------------------------
152 -----------------------------------------------------------------------*/
153 void WriterFilter::setTargetDocument( const uno::Reference
< lang::XComponent
>& xDoc
)
154 throw (lang::IllegalArgumentException
, uno::RuntimeException
)
159 void WriterFilter::setSourceDocument( const uno::Reference
< lang::XComponent
>& xDoc
)
160 throw (lang::IllegalArgumentException
, uno::RuntimeException
)
165 /*-- 09.06.2006 10:15:20---------------------------------------------------
167 -----------------------------------------------------------------------*/
168 void WriterFilter::initialize( const uno::Sequence
< uno::Any
>& aArguments
) throw (uno::Exception
, uno::RuntimeException
)
170 uno::Sequence
< beans::PropertyValue
> aAnySeq
;
171 sal_Int32 nLength
= aArguments
.getLength();
172 if ( nLength
&& ( aArguments
[0] >>= aAnySeq
) )
174 const beans::PropertyValue
* pValue
= aAnySeq
.getConstArray();
175 nLength
= aAnySeq
.getLength();
176 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
178 if ( pValue
[i
].Name
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) )
180 pValue
[i
].Value
>>= m_sFilterName
;
186 /*-- 09.06.2006 10:15:20---------------------------------------------------
188 -----------------------------------------------------------------------*/
189 OUString
WriterFilter_getImplementationName () throw (uno::RuntimeException
)
191 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.WriterFilter" ) );
194 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
195 #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
196 /*-- 09.06.2006 10:15:20---------------------------------------------------
198 -----------------------------------------------------------------------*/
199 sal_Bool
WriterFilter_supportsService( const OUString
& ServiceName
) throw (uno::RuntimeException
)
201 return (ServiceName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1
) ) ||
202 ServiceName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1
) ));
204 /*-- 09.06.2006 10:15:20---------------------------------------------------
206 -----------------------------------------------------------------------*/
207 uno::Sequence
< OUString
> WriterFilter_getSupportedServiceNames( ) throw (uno::RuntimeException
)
209 uno::Sequence
< OUString
> aRet(2);
210 OUString
* pArray
= aRet
.getArray();
211 pArray
[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1
) );
212 pArray
[1] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME2
) );
218 /*-- 09.06.2006 10:15:20---------------------------------------------------
220 -----------------------------------------------------------------------*/
221 uno::Reference
< uno::XInterface
> WriterFilter_createInstance( const uno::Reference
< uno::XComponentContext
>& xContext
)
222 throw( uno::Exception
)
224 return (cppu::OWeakObject
*) new WriterFilter( xContext
);
227 /*-- 09.06.2006 10:15:20---------------------------------------------------
229 -----------------------------------------------------------------------*/
230 OUString
WriterFilter::getImplementationName( ) throw (uno::RuntimeException
)
232 return WriterFilter_getImplementationName();
234 /*-- 09.06.2006 10:15:20---------------------------------------------------
236 -----------------------------------------------------------------------*/
237 sal_Bool
WriterFilter::supportsService( const OUString
& rServiceName
) throw (uno::RuntimeException
)
239 return WriterFilter_supportsService( rServiceName
);
241 /*-- 09.06.2006 10:15:20---------------------------------------------------
243 -----------------------------------------------------------------------*/
244 uno::Sequence
< OUString
> WriterFilter::getSupportedServiceNames( ) throw (uno::RuntimeException
)
246 return WriterFilter_getSupportedServiceNames();