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