1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* CMXImportFilter: Sets up the filter, and calls OdgExporter
3 * to do the actual filtering
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/.
10 /* "This product is not manufactured, approved, or supported by
11 * Corel Corporation or Corel Corporation Limited."
14 #include <osl/diagnose.h>
15 #include <rtl/tencinfo.h>
17 #include <com/sun/star/io/XInputStream.hpp>
18 #include <com/sun/star/xml/sax/XAttributeList.hpp>
19 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
20 #include <com/sun/star/xml/sax/InputSource.hpp>
21 #include <com/sun/star/xml/sax/XParser.hpp>
22 #include <com/sun/star/io/XSeekable.hpp>
23 #include <com/sun/star/uno/Reference.h>
25 #include <comphelper/componentcontext.hxx>
26 #include <xmloff/attrlist.hxx>
28 #include <libcdr/libcdr.h>
29 #include <libodfgen/libodfgen.hxx>
31 #include "common/DocumentHandler.hxx"
32 #include "common/WPXSvStream.hxx"
33 #include "CMXImportFilter.hxx"
37 using namespace ::com::sun::star::uno
;
38 using com::sun::star::uno::Reference
;
39 using com::sun::star::io::XInputStream
;
40 using com::sun::star::io::XSeekable
;
41 using com::sun::star::uno::Sequence
;
42 using com::sun::star::uno::Any
;
43 using com::sun::star::uno::UNO_QUERY
;
44 using com::sun::star::uno::XInterface
;
45 using com::sun::star::uno::Exception
;
46 using com::sun::star::uno::RuntimeException
;
47 using com::sun::star::beans::PropertyValue
;
48 using com::sun::star::document::XFilter
;
49 using com::sun::star::document::XExtendedFilterDetection
;
50 using com::sun::star::document::XImporter
;
51 using com::sun::star::xml::sax::InputSource
;
52 using com::sun::star::xml::sax::XAttributeList
;
53 using com::sun::star::xml::sax::XDocumentHandler
;
54 using com::sun::star::xml::sax::XParser
;
57 sal_Bool SAL_CALL
CMXImportFilter::filter( const Sequence
< ::com::sun::star::beans::PropertyValue
>& aDescriptor
)
58 throw (RuntimeException
)
61 std::cerr
<< "CMXImportFilter::filter" << std::endl
;
63 sal_Int32 nLength
= aDescriptor
.getLength();
64 const PropertyValue
*pValue
= aDescriptor
.getConstArray();
65 Reference
< XInputStream
> xInputStream
;
66 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
68 if ( pValue
[i
].Name
== "InputStream" )
69 pValue
[i
].Value
>>= xInputStream
;
71 if ( !xInputStream
.is() )
77 // An XML import service: what we push sax messages to..
78 OUString
sXMLImportService ( "com.sun.star.comp.Draw.XMLOasisImporter" );
79 Reference
< XDocumentHandler
> xInternalHandler( comphelper::ComponentContext( mxContext
).createComponent( sXMLImportService
), UNO_QUERY
);
81 // The XImporter sets up an empty target document for XDocumentHandler to write to..
82 Reference
< XImporter
> xImporter(xInternalHandler
, UNO_QUERY
);
83 xImporter
->setTargetDocument( mxDoc
);
85 // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
86 // writes to in-memory target doc
87 DocumentHandler
xHandler(xInternalHandler
);
89 WPXSvInputStream
input( xInputStream
);
91 OdgGenerator
exporter(&xHandler
, ODF_FLAT_XML
);
92 bool tmpParseResult
= libcdr::CMXDocument::parse(&input
, &exporter
);
93 return tmpParseResult
;
96 void SAL_CALL
CMXImportFilter::cancel( )
97 throw (RuntimeException
)
100 std::cerr
<< "CMXImportFilter::cancel" << std::endl
;
105 void SAL_CALL
CMXImportFilter::setTargetDocument( const Reference
< ::com::sun::star::lang::XComponent
>& xDoc
)
106 throw (::com::sun::star::lang::IllegalArgumentException
, RuntimeException
)
109 std::cerr
<< "CMXImportFilter::setTargetDocument" << std::endl
;
114 // XExtendedFilterDetection
115 OUString SAL_CALL
CMXImportFilter::detect( com::sun::star::uno::Sequence
< PropertyValue
>& Descriptor
)
116 throw( com::sun::star::uno::RuntimeException
)
119 std::cerr
<< "CMXImportFilter::detect" << std::endl
;
122 sal_Int32 nLength
= Descriptor
.getLength();
123 sal_Int32 location
= nLength
;
124 const PropertyValue
*pValue
= Descriptor
.getConstArray();
125 Reference
< XInputStream
> xInputStream
;
126 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
128 if ( pValue
[i
].Name
== "TypeName" )
130 else if ( pValue
[i
].Name
== "InputStream" )
131 pValue
[i
].Value
>>= xInputStream
;
134 if (!xInputStream
.is())
137 WPXSvInputStream
input( xInputStream
);
139 if (libcdr::CMXDocument::isSupported(&input
))
140 sTypeName
= "draw_Corel_Presentation_Exchange";
142 if (!sTypeName
.isEmpty())
144 if ( location
== nLength
)
146 Descriptor
.realloc(nLength
+1);
147 Descriptor
[location
].Name
= "TypeName";
150 Descriptor
[location
].Value
<<=sTypeName
;
157 void SAL_CALL
CMXImportFilter::initialize( const Sequence
< Any
>& aArguments
)
158 throw (Exception
, RuntimeException
)
161 std::cerr
<< "CMXImportFilter::initialize" << std::endl
;
163 Sequence
< PropertyValue
> aAnySeq
;
164 sal_Int32 nLength
= aArguments
.getLength();
165 if ( nLength
&& ( aArguments
[0] >>= aAnySeq
) )
167 const PropertyValue
*pValue
= aAnySeq
.getConstArray();
168 nLength
= aAnySeq
.getLength();
169 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
171 if ( pValue
[i
].Name
== "Type" )
173 pValue
[i
].Value
>>= msFilterName
;
179 OUString
CMXImportFilter_getImplementationName ()
180 throw (RuntimeException
)
183 std::cerr
<< "CMXImportFilter_getImplementationName" << std::endl
;
185 return OUString ( "com.sun.star.comp.Draw.CMXImportFilter" );
188 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
189 #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
190 sal_Bool SAL_CALL
CMXImportFilter_supportsService( const OUString
&ServiceName
)
191 throw (RuntimeException
)
194 std::cerr
<< "CMXImportFilter_supportsService" << std::endl
;
196 return ( ServiceName
== SERVICE_NAME1
|| ServiceName
== SERVICE_NAME2
);
198 Sequence
< OUString
> SAL_CALL
CMXImportFilter_getSupportedServiceNames( )
199 throw (RuntimeException
)
202 std::cerr
<< "CMXImportFilter_getSupportedServiceNames" << std::endl
;
204 Sequence
< OUString
> aRet(2);
205 OUString
*pArray
= aRet
.getArray();
206 pArray
[0] = OUString ( SERVICE_NAME1
);
207 pArray
[1] = OUString ( SERVICE_NAME2
);
213 Reference
< XInterface
> SAL_CALL
CMXImportFilter_createInstance( const Reference
< XComponentContext
> & rContext
)
217 std::cerr
<< "CMXImportFilter_createInstance" << std::endl
;
219 return (cppu::OWeakObject
*) new CMXImportFilter( rContext
);
223 OUString SAL_CALL
CMXImportFilter::getImplementationName( )
224 throw (RuntimeException
)
227 std::cerr
<< "CMXImportFilter::getImplementationName" << std::endl
;
229 return CMXImportFilter_getImplementationName();
231 sal_Bool SAL_CALL
CMXImportFilter::supportsService( const OUString
&rServiceName
)
232 throw (RuntimeException
)
235 std::cerr
<< "CMXImportFilter::supportsService" << std::endl
;
237 return CMXImportFilter_supportsService( rServiceName
);
239 Sequence
< OUString
> SAL_CALL
CMXImportFilter::getSupportedServiceNames( )
240 throw (RuntimeException
)
243 std::cerr
<< "CMXImportFilter::getSupportedServiceNames" << std::endl
;
245 return CMXImportFilter_getSupportedServiceNames();
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */