Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /* WPGImportFilter: Sets up the filter, and calls OdgExporter
3 : : * to do the actual filtering
4 : : *
5 : : * This file is part of the LibreOffice project.
6 : : *
7 : : * This Source Code Form is subject to the terms of the Mozilla Public
8 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
9 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 : : */
11 : :
12 : : /* "This product is not manufactured, approved, or supported by
13 : : * Corel Corporation or Corel Corporation Limited."
14 : : */
15 : :
16 : : #include <osl/diagnose.h>
17 : : #include <rtl/tencinfo.h>
18 : :
19 : : #include <com/sun/star/io/XInputStream.hpp>
20 : : #include <com/sun/star/xml/sax/XAttributeList.hpp>
21 : : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
22 : : #include <com/sun/star/xml/sax/InputSource.hpp>
23 : : #include <com/sun/star/xml/sax/XParser.hpp>
24 : : #include <com/sun/star/io/XSeekable.hpp>
25 : : #include <com/sun/star/uno/Reference.h>
26 : :
27 : : #include <comphelper/componentcontext.hxx>
28 : : #include <xmloff/attrlist.hxx>
29 : :
30 : : #include "filter/DocumentHandler.hxx"
31 : : #include "filter/OdgGenerator.hxx"
32 : : #include "WPGImportFilter.hxx"
33 : : #include "stream/WPXSvStream.h"
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 namespace ::rtl;
43 : : using rtl::OString;
44 : : using rtl::OUString;
45 : : using com::sun::star::uno::Sequence;
46 : : using com::sun::star::uno::Reference;
47 : : using com::sun::star::uno::Any;
48 : : using com::sun::star::uno::UNO_QUERY;
49 : : using com::sun::star::uno::XInterface;
50 : : using com::sun::star::uno::Exception;
51 : : using com::sun::star::uno::RuntimeException;
52 : : using com::sun::star::beans::PropertyValue;
53 : : using com::sun::star::document::XFilter;
54 : : using com::sun::star::document::XExtendedFilterDetection;
55 : :
56 : : using com::sun::star::io::XInputStream;
57 : : using com::sun::star::document::XImporter;
58 : : using com::sun::star::xml::sax::InputSource;
59 : : using com::sun::star::xml::sax::XAttributeList;
60 : : using com::sun::star::xml::sax::XDocumentHandler;
61 : : using com::sun::star::xml::sax::XParser;
62 : :
63 : :
64 : 0 : sal_Bool SAL_CALL WPGImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
65 : : throw (RuntimeException)
66 : : {
67 : : #ifdef DEBUG
68 : : std::cerr << "WPGImportFilter::filter" << std::endl;
69 : : #endif
70 : 0 : sal_Int32 nLength = aDescriptor.getLength();
71 : 0 : const PropertyValue *pValue = aDescriptor.getConstArray();
72 : 0 : OUString sURL;
73 : 0 : Reference < XInputStream > xInputStream;
74 [ # # ]: 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
75 : : {
76 [ # # ]: 0 : if ( pValue[i].Name == "InputStream" )
77 [ # # ]: 0 : pValue[i].Value >>= xInputStream;
78 [ # # ]: 0 : else if ( pValue[i].Name == "URL" )
79 : 0 : pValue[i].Value >>= sURL;
80 : : }
81 [ # # ]: 0 : if ( !xInputStream.is() )
82 : : {
83 : : OSL_ASSERT( 0 );
84 : 0 : return sal_False;
85 : : }
86 : 0 : OString sFileName;
87 [ # # ]: 0 : sFileName = OUStringToOString(sURL, RTL_TEXTENCODING_INFO_ASCII);
88 : :
89 : : // An XML import service: what we push sax messages to..
90 : 0 : OUString sXMLImportService ( "com.sun.star.comp.Draw.XMLOasisImporter" );
91 [ # # ][ # # ]: 0 : Reference < XDocumentHandler > xInternalHandler( comphelper::ComponentContext( mxContext ).createComponent( sXMLImportService ), UNO_QUERY );
[ # # ][ # # ]
92 : :
93 : : // The XImporter sets up an empty target document for XDocumentHandler to write to..
94 [ # # ]: 0 : Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
95 [ # # ][ # # ]: 0 : xImporter->setTargetDocument( mxDoc );
96 : :
97 : : // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
98 : : // writes to in-memory target doc
99 [ # # ]: 0 : DocumentHandler xHandler(xInternalHandler);
100 : :
101 [ # # ]: 0 : WPXSvInputStream input( xInputStream );
102 : :
103 [ # # ]: 0 : OdgGenerator exporter(&xHandler, ODF_FLAT_XML);
104 [ # # ]: 0 : bool tmpParseResult = libwpg::WPGraphics::parse(&input, &exporter);
105 [ # # ][ # # ]: 0 : return tmpParseResult;
106 : : }
107 : :
108 : 0 : void SAL_CALL WPGImportFilter::cancel( )
109 : : throw (RuntimeException)
110 : : {
111 : : #ifdef DEBUG
112 : : std::cerr << "WPGImportFilter::cancel" << std::endl;
113 : : #endif
114 : 0 : }
115 : :
116 : : // XImporter
117 : 0 : void SAL_CALL WPGImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
118 : : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException)
119 : : {
120 : : #ifdef DEBUG
121 : : std::cerr << "WPGImportFilter::setTargetDocument" << std::endl;
122 : : #endif
123 : 0 : mxDoc = xDoc;
124 : 0 : }
125 : :
126 : : // XExtendedFilterDetection
127 : 10 : OUString SAL_CALL WPGImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
128 : : throw( com::sun::star::uno::RuntimeException )
129 : : {
130 : : #ifdef DEBUG
131 : : std::cerr << "WPGImportFilter::detect" << std::endl;
132 : : #endif
133 : 10 : OUString sTypeName;
134 : 10 : sal_Int32 nLength = Descriptor.getLength();
135 : 10 : sal_Int32 location = nLength;
136 : 10 : const PropertyValue *pValue = Descriptor.getConstArray();
137 : 10 : Reference < XInputStream > xInputStream;
138 [ + + ]: 80 : for ( sal_Int32 i = 0 ; i < nLength; i++)
139 : : {
140 [ - + ]: 70 : if ( pValue[i].Name == "TypeName" )
141 : 0 : location=i;
142 [ + + ]: 70 : else if ( pValue[i].Name == "InputStream" )
143 [ + - ]: 10 : pValue[i].Value >>= xInputStream;
144 : : }
145 : :
146 [ + - ]: 10 : WPXSvInputStream input( xInputStream );
147 : :
148 [ - + ][ + - ]: 10 : if (libwpg::WPGraphics::isSupported(&input))
149 : 0 : sTypeName = OUString( "draw_WordPerfect_Graphics" );
150 : :
151 [ - + ]: 10 : if (!sTypeName.isEmpty())
152 : : {
153 [ # # ]: 0 : if ( location == Descriptor.getLength() )
154 : : {
155 [ # # ]: 0 : Descriptor.realloc(nLength+1);
156 [ # # ]: 0 : Descriptor[location].Name = ::rtl::OUString("TypeName");
157 : : }
158 : :
159 [ # # ][ # # ]: 0 : Descriptor[location].Value <<=sTypeName;
160 : : }
161 [ + - ]: 10 : return sTypeName;
162 : : }
163 : :
164 : :
165 : : // XInitialization
166 : 0 : void SAL_CALL WPGImportFilter::initialize( const Sequence< Any >& aArguments )
167 : : throw (Exception, RuntimeException)
168 : : {
169 : : #ifdef DEBUG
170 : : std::cerr << "WPGImportFilter::initialize" << std::endl;
171 : : #endif
172 [ # # ]: 0 : Sequence < PropertyValue > aAnySeq;
173 : 0 : sal_Int32 nLength = aArguments.getLength();
174 [ # # ][ # # ]: 0 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
[ # # ][ # # ]
175 : : {
176 : 0 : const PropertyValue *pValue = aAnySeq.getConstArray();
177 : 0 : nLength = aAnySeq.getLength();
178 [ # # ]: 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
179 : : {
180 [ # # ]: 0 : if ( pValue[i].Name == "Type" )
181 : : {
182 : 0 : pValue[i].Value >>= msFilterName;
183 : 0 : break;
184 : : }
185 : : }
186 [ # # ]: 0 : }
187 : 0 : }
188 : 30 : OUString WPGImportFilter_getImplementationName ()
189 : : throw (RuntimeException)
190 : : {
191 : : #ifdef DEBUG
192 : : std::cerr << "WPGImportFilter_getImplementationName" << std::endl;
193 : : #endif
194 : 30 : return OUString ( "com.sun.star.comp.Draw.WPGImportFilter" );
195 : : }
196 : :
197 : : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
198 : : #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
199 : 0 : sal_Bool SAL_CALL WPGImportFilter_supportsService( const OUString &ServiceName )
200 : : throw (RuntimeException)
201 : : {
202 : : #ifdef DEBUG
203 : : std::cerr << "WPGImportFilter_supportsService" << std::endl;
204 : : #endif
205 [ # # ][ # # ]: 0 : return ( ServiceName == SERVICE_NAME1 || ServiceName == SERVICE_NAME2 );
206 : : }
207 : 6 : Sequence< OUString > SAL_CALL WPGImportFilter_getSupportedServiceNames( )
208 : : throw (RuntimeException)
209 : : {
210 : : #ifdef DEBUG
211 : : std::cerr << "WPGImportFilter_getSupportedServiceNames" << std::endl;
212 : : #endif
213 : 6 : Sequence < OUString > aRet(2);
214 [ + - ]: 6 : OUString *pArray = aRet.getArray();
215 : 6 : pArray[0] = OUString ( SERVICE_NAME1 );
216 : 6 : pArray[1] = OUString ( SERVICE_NAME2 );
217 : 6 : return aRet;
218 : : }
219 : : #undef SERVICE_NAME2
220 : : #undef SERVICE_NAME1
221 : :
222 : 10 : Reference< XInterface > SAL_CALL WPGImportFilter_createInstance( const Reference< XComponentContext > & rContext)
223 : : throw( Exception )
224 : : {
225 : : #ifdef DEBUG
226 : : std::cerr << "WPGImportFilter_createInstance" << std::endl;
227 : : #endif
228 [ + - ]: 10 : return (cppu::OWeakObject *) new WPGImportFilter( rContext );
229 : : }
230 : :
231 : : // XServiceInfo
232 : 0 : OUString SAL_CALL WPGImportFilter::getImplementationName( )
233 : : throw (RuntimeException)
234 : : {
235 : : #ifdef DEBUG
236 : : std::cerr << "WPGImportFilter::getImplementationName" << std::endl;
237 : : #endif
238 : 0 : return WPGImportFilter_getImplementationName();
239 : : }
240 : 0 : sal_Bool SAL_CALL WPGImportFilter::supportsService( const OUString &rServiceName )
241 : : throw (RuntimeException)
242 : : {
243 : : #ifdef DEBUG
244 : : std::cerr << "WPGImportFilter::supportsService" << std::endl;
245 : : #endif
246 : 0 : return WPGImportFilter_supportsService( rServiceName );
247 : : }
248 : 0 : Sequence< OUString > SAL_CALL WPGImportFilter::getSupportedServiceNames( )
249 : : throw (RuntimeException)
250 : : {
251 : : #ifdef DEBUG
252 : : std::cerr << "WPGImportFilter::getSupportedServiceNames" << std::endl;
253 : : #endif
254 : 0 : return WPGImportFilter_getSupportedServiceNames();
255 [ + - ][ + - ]: 18 : }
256 : :
257 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|