Line data Source code
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
4 : *
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/.
8 : */
9 :
10 : /* "This product is not manufactured, approved, or supported by
11 : * Corel Corporation or Corel Corporation Limited."
12 : */
13 :
14 : #include <osl/diagnose.h>
15 : #include <rtl/tencinfo.h>
16 :
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>
24 :
25 : #include <comphelper/componentcontext.hxx>
26 : #include <xmloff/attrlist.hxx>
27 :
28 : #include <libcdr/libcdr.h>
29 : #include <libodfgen/libodfgen.hxx>
30 :
31 : #include "common/DocumentHandler.hxx"
32 : #include "common/WPXSvStream.hxx"
33 : #include "CMXImportFilter.hxx"
34 :
35 : #include <iostream>
36 :
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;
55 :
56 :
57 0 : sal_Bool SAL_CALL CMXImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
58 : throw (RuntimeException)
59 : {
60 : #ifdef DEBUG
61 : std::cerr << "CMXImportFilter::filter" << std::endl;
62 : #endif
63 0 : sal_Int32 nLength = aDescriptor.getLength();
64 0 : const PropertyValue *pValue = aDescriptor.getConstArray();
65 0 : Reference < XInputStream > xInputStream;
66 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
67 : {
68 0 : if ( pValue[i].Name == "InputStream" )
69 0 : pValue[i].Value >>= xInputStream;
70 : }
71 0 : if ( !xInputStream.is() )
72 : {
73 : OSL_ASSERT( 0 );
74 0 : return sal_False;
75 : }
76 :
77 : // An XML import service: what we push sax messages to..
78 0 : OUString sXMLImportService ( "com.sun.star.comp.Draw.XMLOasisImporter" );
79 0 : Reference < XDocumentHandler > xInternalHandler( comphelper::ComponentContext( mxContext ).createComponent( sXMLImportService ), UNO_QUERY );
80 :
81 : // The XImporter sets up an empty target document for XDocumentHandler to write to..
82 0 : Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
83 0 : xImporter->setTargetDocument( mxDoc );
84 :
85 : // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
86 : // writes to in-memory target doc
87 0 : DocumentHandler xHandler(xInternalHandler);
88 :
89 0 : WPXSvInputStream input( xInputStream );
90 :
91 0 : OdgGenerator exporter(&xHandler, ODF_FLAT_XML);
92 0 : bool tmpParseResult = libcdr::CMXDocument::parse(&input, &exporter);
93 0 : return tmpParseResult;
94 : }
95 :
96 0 : void SAL_CALL CMXImportFilter::cancel( )
97 : throw (RuntimeException)
98 : {
99 : #ifdef DEBUG
100 : std::cerr << "CMXImportFilter::cancel" << std::endl;
101 : #endif
102 0 : }
103 :
104 : // XImporter
105 0 : void SAL_CALL CMXImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
106 : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException)
107 : {
108 : #ifdef DEBUG
109 : std::cerr << "CMXImportFilter::setTargetDocument" << std::endl;
110 : #endif
111 0 : mxDoc = xDoc;
112 0 : }
113 :
114 : // XExtendedFilterDetection
115 0 : OUString SAL_CALL CMXImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
116 : throw( com::sun::star::uno::RuntimeException )
117 : {
118 : #ifdef DEBUG
119 : std::cerr << "CMXImportFilter::detect" << std::endl;
120 : #endif
121 0 : OUString sTypeName;
122 0 : sal_Int32 nLength = Descriptor.getLength();
123 0 : sal_Int32 location = nLength;
124 0 : const PropertyValue *pValue = Descriptor.getConstArray();
125 0 : Reference < XInputStream > xInputStream;
126 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
127 : {
128 0 : if ( pValue[i].Name == "TypeName" )
129 0 : location=i;
130 0 : else if ( pValue[i].Name == "InputStream" )
131 0 : pValue[i].Value >>= xInputStream;
132 : }
133 :
134 0 : if (!xInputStream.is())
135 0 : return OUString();
136 :
137 0 : WPXSvInputStream input( xInputStream );
138 :
139 0 : if (libcdr::CMXDocument::isSupported(&input))
140 0 : sTypeName = "draw_Corel_Presentation_Exchange";
141 :
142 0 : if (!sTypeName.isEmpty())
143 : {
144 0 : if ( location == nLength )
145 : {
146 0 : Descriptor.realloc(nLength+1);
147 0 : Descriptor[location].Name = "TypeName";
148 : }
149 :
150 0 : Descriptor[location].Value <<=sTypeName;
151 : }
152 0 : return sTypeName;
153 : }
154 :
155 :
156 : // XInitialization
157 0 : void SAL_CALL CMXImportFilter::initialize( const Sequence< Any >& aArguments )
158 : throw (Exception, RuntimeException)
159 : {
160 : #ifdef DEBUG
161 : std::cerr << "CMXImportFilter::initialize" << std::endl;
162 : #endif
163 0 : Sequence < PropertyValue > aAnySeq;
164 0 : sal_Int32 nLength = aArguments.getLength();
165 0 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
166 : {
167 0 : const PropertyValue *pValue = aAnySeq.getConstArray();
168 0 : nLength = aAnySeq.getLength();
169 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
170 : {
171 0 : if ( pValue[i].Name == "Type" )
172 : {
173 0 : pValue[i].Value >>= msFilterName;
174 0 : break;
175 : }
176 : }
177 0 : }
178 0 : }
179 0 : OUString CMXImportFilter_getImplementationName ()
180 : throw (RuntimeException)
181 : {
182 : #ifdef DEBUG
183 : std::cerr << "CMXImportFilter_getImplementationName" << std::endl;
184 : #endif
185 0 : return OUString ( "com.sun.star.comp.Draw.CMXImportFilter" );
186 : }
187 :
188 : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
189 : #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
190 0 : sal_Bool SAL_CALL CMXImportFilter_supportsService( const OUString &ServiceName )
191 : throw (RuntimeException)
192 : {
193 : #ifdef DEBUG
194 : std::cerr << "CMXImportFilter_supportsService" << std::endl;
195 : #endif
196 0 : return ( ServiceName == SERVICE_NAME1 || ServiceName == SERVICE_NAME2 );
197 : }
198 0 : Sequence< OUString > SAL_CALL CMXImportFilter_getSupportedServiceNames( )
199 : throw (RuntimeException)
200 : {
201 : #ifdef DEBUG
202 : std::cerr << "CMXImportFilter_getSupportedServiceNames" << std::endl;
203 : #endif
204 0 : Sequence < OUString > aRet(2);
205 0 : OUString *pArray = aRet.getArray();
206 0 : pArray[0] = OUString ( SERVICE_NAME1 );
207 0 : pArray[1] = OUString ( SERVICE_NAME2 );
208 0 : return aRet;
209 : }
210 : #undef SERVICE_NAME2
211 : #undef SERVICE_NAME1
212 :
213 0 : Reference< XInterface > SAL_CALL CMXImportFilter_createInstance( const Reference< XComponentContext > & rContext)
214 : throw( Exception )
215 : {
216 : #ifdef DEBUG
217 : std::cerr << "CMXImportFilter_createInstance" << std::endl;
218 : #endif
219 0 : return (cppu::OWeakObject *) new CMXImportFilter( rContext );
220 : }
221 :
222 : // XServiceInfo
223 0 : OUString SAL_CALL CMXImportFilter::getImplementationName( )
224 : throw (RuntimeException)
225 : {
226 : #ifdef DEBUG
227 : std::cerr << "CMXImportFilter::getImplementationName" << std::endl;
228 : #endif
229 0 : return CMXImportFilter_getImplementationName();
230 : }
231 0 : sal_Bool SAL_CALL CMXImportFilter::supportsService( const OUString &rServiceName )
232 : throw (RuntimeException)
233 : {
234 : #ifdef DEBUG
235 : std::cerr << "CMXImportFilter::supportsService" << std::endl;
236 : #endif
237 0 : return CMXImportFilter_supportsService( rServiceName );
238 : }
239 0 : Sequence< OUString > SAL_CALL CMXImportFilter::getSupportedServiceNames( )
240 : throw (RuntimeException)
241 : {
242 : #ifdef DEBUG
243 : std::cerr << "CMXImportFilter::getSupportedServiceNames" << std::endl;
244 : #endif
245 0 : return CMXImportFilter_getSupportedServiceNames();
246 0 : }
247 :
248 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|