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 * Copyright (C) 2000 by Sun Microsystems, Inc.
6 * Copyright (C) 2002-2004 William Lachance (wlach@interlog.com)
7 * Copyright (C) 2004 Net Integration Technologies (http://www.net-itech.com)
8 * Copyright (C) 2004-2006 Fridrich Strba <fridrich.strba@bluewin.ch>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * Contributor(s): Martin Gallwey (gallwey@sun.com)
29 /* "This product is not manufactured, approved, or supported by
30 * Corel Corporation or Corel Corporation Limited."
33 #include <osl/diagnose.h>
34 #include <rtl/tencinfo.h>
36 #include <com/sun/star/io/XInputStream.hpp>
37 #include <com/sun/star/xml/sax/XAttributeList.hpp>
38 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
39 #include <com/sun/star/xml/sax/InputSource.hpp>
40 #include <com/sun/star/xml/sax/XParser.hpp>
41 #include <com/sun/star/io/XSeekable.hpp>
42 #include <com/sun/star/uno/Reference.h>
44 #include <comphelper/componentcontext.hxx>
45 #include <xmloff/attrlist.hxx>
47 #include <libcdr/libcdr.h>
48 #include "filter/DocumentHandler.hxx"
49 #include "filter/OdgGenerator.hxx"
50 #include "CMXImportFilter.hxx"
51 #include "stream/WPXSvStream.h"
55 using namespace ::com::sun::star::uno
;
56 using com::sun::star::uno::Reference
;
57 using com::sun::star::io::XInputStream
;
58 using com::sun::star::io::XSeekable
;
59 using com::sun::star::uno::Sequence
;
60 using namespace ::rtl
;
63 using com::sun::star::uno::Sequence
;
64 using com::sun::star::uno::Reference
;
65 using com::sun::star::uno::Any
;
66 using com::sun::star::uno::UNO_QUERY
;
67 using com::sun::star::uno::XInterface
;
68 using com::sun::star::uno::Exception
;
69 using com::sun::star::uno::RuntimeException
;
70 using com::sun::star::beans::PropertyValue
;
71 using com::sun::star::document::XFilter
;
72 using com::sun::star::document::XExtendedFilterDetection
;
74 using com::sun::star::io::XInputStream
;
75 using com::sun::star::document::XImporter
;
76 using com::sun::star::xml::sax::InputSource
;
77 using com::sun::star::xml::sax::XAttributeList
;
78 using com::sun::star::xml::sax::XDocumentHandler
;
79 using com::sun::star::xml::sax::XParser
;
82 sal_Bool SAL_CALL
CMXImportFilter::filter( const Sequence
< ::com::sun::star::beans::PropertyValue
>& aDescriptor
)
83 throw (RuntimeException
)
86 std::cerr
<< "CMXImportFilter::filter" << std::endl
;
88 sal_Int32 nLength
= aDescriptor
.getLength();
89 const PropertyValue
*pValue
= aDescriptor
.getConstArray();
91 Reference
< XInputStream
> xInputStream
;
92 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
94 if ( pValue
[i
].Name
== "InputStream" )
95 pValue
[i
].Value
>>= xInputStream
;
96 else if ( pValue
[i
].Name
== "URL" )
97 pValue
[i
].Value
>>= sURL
;
99 if ( !xInputStream
.is() )
105 sFileName
= OUStringToOString(sURL
, RTL_TEXTENCODING_INFO_ASCII
);
107 // An XML import service: what we push sax messages to..
108 OUString
sXMLImportService ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Draw.XMLOasisImporter" ) );
109 Reference
< XDocumentHandler
> xInternalHandler( comphelper::ComponentContext( mxContext
).createComponent( sXMLImportService
), UNO_QUERY
);
111 // The XImporter sets up an empty target document for XDocumentHandler to write to..
112 Reference
< XImporter
> xImporter(xInternalHandler
, UNO_QUERY
);
113 xImporter
->setTargetDocument( mxDoc
);
115 // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
116 // writes to in-memory target doc
117 DocumentHandler
xHandler(xInternalHandler
);
119 WPXSvInputStream
input( xInputStream
);
121 OdgGenerator
exporter(&xHandler
, ODF_FLAT_XML
);
122 bool tmpParseResult
= libcdr::CMXDocument::parse(&input
, &exporter
);
123 return tmpParseResult
;
126 void SAL_CALL
CMXImportFilter::cancel( )
127 throw (RuntimeException
)
130 std::cerr
<< "CMXImportFilter::cancel" << std::endl
;
135 void SAL_CALL
CMXImportFilter::setTargetDocument( const Reference
< ::com::sun::star::lang::XComponent
>& xDoc
)
136 throw (::com::sun::star::lang::IllegalArgumentException
, RuntimeException
)
139 std::cerr
<< "CMXImportFilter::setTargetDocument" << std::endl
;
144 // XExtendedFilterDetection
145 OUString SAL_CALL
CMXImportFilter::detect( com::sun::star::uno::Sequence
< PropertyValue
>& Descriptor
)
146 throw( com::sun::star::uno::RuntimeException
)
149 std::cerr
<< "CMXImportFilter::detect" << std::endl
;
152 sal_Int32 nLength
= Descriptor
.getLength();
153 sal_Int32 location
= nLength
;
154 const PropertyValue
*pValue
= Descriptor
.getConstArray();
155 Reference
< XInputStream
> xInputStream
;
156 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
158 if ( pValue
[i
].Name
== "TypeName" )
160 else if ( pValue
[i
].Name
== "InputStream" )
161 pValue
[i
].Value
>>= xInputStream
;
164 WPXSvInputStream
input( xInputStream
);
166 if (libcdr::CMXDocument::isSupported(&input
))
167 sTypeName
= OUString( RTL_CONSTASCII_USTRINGPARAM ( "draw_Corel_Presentation_Exchange" ) );
169 if (sTypeName
.getLength())
171 if ( location
== Descriptor
.getLength() )
173 Descriptor
.realloc(nLength
+1);
174 Descriptor
[location
].Name
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TypeName"));
177 Descriptor
[location
].Value
<<=sTypeName
;
184 void SAL_CALL
CMXImportFilter::initialize( const Sequence
< Any
>& aArguments
)
185 throw (Exception
, RuntimeException
)
188 std::cerr
<< "CMXImportFilter::initialize" << std::endl
;
190 Sequence
< PropertyValue
> aAnySeq
;
191 sal_Int32 nLength
= aArguments
.getLength();
192 if ( nLength
&& ( aArguments
[0] >>= aAnySeq
) )
194 const PropertyValue
*pValue
= aAnySeq
.getConstArray();
195 nLength
= aAnySeq
.getLength();
196 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
198 if ( pValue
[i
].Name
== "Type" )
200 pValue
[i
].Value
>>= msFilterName
;
206 OUString
CMXImportFilter_getImplementationName ()
207 throw (RuntimeException
)
210 std::cerr
<< "CMXImportFilter_getImplementationName" << std::endl
;
212 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Draw.CMXImportFilter" ) );
215 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
216 #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
217 sal_Bool SAL_CALL
CMXImportFilter_supportsService( const OUString
&ServiceName
)
218 throw (RuntimeException
)
221 std::cerr
<< "CMXImportFilter_supportsService" << std::endl
;
223 return ( ServiceName
== SERVICE_NAME1
|| ServiceName
== SERVICE_NAME2
);
225 Sequence
< OUString
> SAL_CALL
CMXImportFilter_getSupportedServiceNames( )
226 throw (RuntimeException
)
229 std::cerr
<< "CMXImportFilter_getSupportedServiceNames" << std::endl
;
231 Sequence
< OUString
> aRet(2);
232 OUString
*pArray
= aRet
.getArray();
233 pArray
[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1
) );
234 pArray
[1] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME2
) );
240 Reference
< XInterface
> SAL_CALL
CMXImportFilter_createInstance( const Reference
< XComponentContext
> & rContext
)
244 std::cerr
<< "CMXImportFilter_createInstance" << std::endl
;
246 return (cppu::OWeakObject
*) new CMXImportFilter( rContext
);
250 OUString SAL_CALL
CMXImportFilter::getImplementationName( )
251 throw (RuntimeException
)
254 std::cerr
<< "CMXImportFilter::getImplementationName" << std::endl
;
256 return CMXImportFilter_getImplementationName();
258 sal_Bool SAL_CALL
CMXImportFilter::supportsService( const OUString
&rServiceName
)
259 throw (RuntimeException
)
262 std::cerr
<< "CMXImportFilter::supportsService" << std::endl
;
264 return CMXImportFilter_supportsService( rServiceName
);
266 Sequence
< OUString
> SAL_CALL
CMXImportFilter::getSupportedServiceNames( )
267 throw (RuntimeException
)
270 std::cerr
<< "CMXImportFilter::getSupportedServiceNames" << std::endl
;
272 return CMXImportFilter_getSupportedServiceNames();
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */