Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / writerperfect / source / draw / CMXImportFilter.cxx
blobe1cf06cdebb1fd5cc5ddf96b3b16b749ef3a4122
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
23 * 02111-1307, USA.
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"
53 #include <iostream>
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;
61 using rtl::OString;
62 using rtl::OUString;
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)
85 #ifdef DEBUG
86 std::cerr << "CMXImportFilter::filter" << std::endl;
87 #endif
88 sal_Int32 nLength = aDescriptor.getLength();
89 const PropertyValue *pValue = aDescriptor.getConstArray();
90 OUString sURL;
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() )
101 OSL_ASSERT( 0 );
102 return sal_False;
104 OString sFileName;
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)
129 #ifdef DEBUG
130 std::cerr << "CMXImportFilter::cancel" << std::endl;
131 #endif
134 // XImporter
135 void SAL_CALL CMXImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
136 throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException)
138 #ifdef DEBUG
139 std::cerr << "CMXImportFilter::setTargetDocument" << std::endl;
140 #endif
141 mxDoc = xDoc;
144 // XExtendedFilterDetection
145 OUString SAL_CALL CMXImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
146 throw( com::sun::star::uno::RuntimeException )
148 #ifdef DEBUG
149 std::cerr << "CMXImportFilter::detect" << std::endl;
150 #endif
151 OUString sTypeName;
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" )
159 location=i;
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;
179 return sTypeName;
183 // XInitialization
184 void SAL_CALL CMXImportFilter::initialize( const Sequence< Any >& aArguments )
185 throw (Exception, RuntimeException)
187 #ifdef DEBUG
188 std::cerr << "CMXImportFilter::initialize" << std::endl;
189 #endif
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;
201 break;
206 OUString CMXImportFilter_getImplementationName ()
207 throw (RuntimeException)
209 #ifdef DEBUG
210 std::cerr << "CMXImportFilter_getImplementationName" << std::endl;
211 #endif
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)
220 #ifdef DEBUG
221 std::cerr << "CMXImportFilter_supportsService" << std::endl;
222 #endif
223 return ( ServiceName == SERVICE_NAME1 || ServiceName == SERVICE_NAME2 );
225 Sequence< OUString > SAL_CALL CMXImportFilter_getSupportedServiceNames( )
226 throw (RuntimeException)
228 #ifdef DEBUG
229 std::cerr << "CMXImportFilter_getSupportedServiceNames" << std::endl;
230 #endif
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 ) );
235 return aRet;
237 #undef SERVICE_NAME2
238 #undef SERVICE_NAME1
240 Reference< XInterface > SAL_CALL CMXImportFilter_createInstance( const Reference< XComponentContext > & rContext)
241 throw( Exception )
243 #ifdef DEBUG
244 std::cerr << "CMXImportFilter_createInstance" << std::endl;
245 #endif
246 return (cppu::OWeakObject *) new CMXImportFilter( rContext );
249 // XServiceInfo
250 OUString SAL_CALL CMXImportFilter::getImplementationName( )
251 throw (RuntimeException)
253 #ifdef DEBUG
254 std::cerr << "CMXImportFilter::getImplementationName" << std::endl;
255 #endif
256 return CMXImportFilter_getImplementationName();
258 sal_Bool SAL_CALL CMXImportFilter::supportsService( const OUString &rServiceName )
259 throw (RuntimeException)
261 #ifdef DEBUG
262 std::cerr << "CMXImportFilter::supportsService" << std::endl;
263 #endif
264 return CMXImportFilter_supportsService( rServiceName );
266 Sequence< OUString > SAL_CALL CMXImportFilter::getSupportedServiceNames( )
267 throw (RuntimeException)
269 #ifdef DEBUG
270 std::cerr << "CMXImportFilter::getSupportedServiceNames" << std::endl;
271 #endif
272 return CMXImportFilter_getSupportedServiceNames();
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */