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
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/.
10 #include <osl/diagnose.h>
11 #include <rtl/tencinfo.h>
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>
21 #include <comphelper/componentcontext.hxx>
22 #include <xmloff/attrlist.hxx>
24 #include <libvisio/libvisio.h>
25 #include <libodfgen/libodfgen.hxx>
27 #include "common/DocumentHandler.hxx"
28 #include "common/WPXSvStream.hxx"
29 #include "VisioImportFilter.hxx"
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
;
53 sal_Bool SAL_CALL
VisioImportFilter::filter( const Sequence
< ::com::sun::star::beans::PropertyValue
>& aDescriptor
)
54 throw (RuntimeException
)
57 std::cerr
<< "VisioImportFilter::filter" << std::endl
;
59 sal_Int32 nLength
= aDescriptor
.getLength();
60 const PropertyValue
*pValue
= aDescriptor
.getConstArray();
61 Reference
< XInputStream
> xInputStream
;
62 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
64 if ( pValue
[i
].Name
== "InputStream" )
65 pValue
[i
].Value
>>= xInputStream
;
67 if ( !xInputStream
.is() )
73 // An XML import service: what we push sax messages to..
74 OUString
sXMLImportService ( "com.sun.star.comp.Draw.XMLOasisImporter" );
75 Reference
< XDocumentHandler
> xInternalHandler( comphelper::ComponentContext( mxContext
).createComponent( sXMLImportService
), UNO_QUERY
);
77 // The XImporter sets up an empty target document for XDocumentHandler to write to..
78 Reference
< XImporter
> xImporter(xInternalHandler
, UNO_QUERY
);
79 xImporter
->setTargetDocument( mxDoc
);
81 // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
82 // writes to in-memory target doc
83 DocumentHandler
xHandler(xInternalHandler
);
85 WPXSvInputStream
input( xInputStream
);
87 OdgGenerator
exporter(&xHandler
, ODF_FLAT_XML
);
88 bool tmpParseResult
= libvisio::VisioDocument::parse(&input
, &exporter
);
89 return tmpParseResult
;
92 void SAL_CALL
VisioImportFilter::cancel( )
93 throw (RuntimeException
)
96 std::cerr
<< "VisioImportFilter::cancel" << std::endl
;
101 void SAL_CALL
VisioImportFilter::setTargetDocument( const Reference
< ::com::sun::star::lang::XComponent
>& xDoc
)
102 throw (::com::sun::star::lang::IllegalArgumentException
, RuntimeException
)
105 std::cerr
<< "VisioImportFilter::setTargetDocument" << std::endl
;
110 // XExtendedFilterDetection
111 OUString SAL_CALL
VisioImportFilter::detect( com::sun::star::uno::Sequence
< PropertyValue
>& Descriptor
)
112 throw( com::sun::star::uno::RuntimeException
)
115 std::cerr
<< "VisioImportFilter::detect" << std::endl
;
118 sal_Int32 nLength
= Descriptor
.getLength();
119 sal_Int32 location
= nLength
;
120 const PropertyValue
*pValue
= Descriptor
.getConstArray();
121 Reference
< XInputStream
> xInputStream
;
122 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
124 if ( pValue
[i
].Name
== "TypeName" )
126 else if ( pValue
[i
].Name
== "InputStream" )
127 pValue
[i
].Value
>>= xInputStream
;
130 if (!xInputStream
.is())
133 WPXSvInputStream
input( xInputStream
);
135 if (libvisio::VisioDocument::isSupported(&input
))
136 sTypeName
= "draw_Visio_Document";
138 if (!sTypeName
.isEmpty())
140 if ( location
== nLength
)
142 Descriptor
.realloc(nLength
+1);
143 Descriptor
[location
].Name
= "TypeName";
146 Descriptor
[location
].Value
<<=sTypeName
;
153 void SAL_CALL
VisioImportFilter::initialize( const Sequence
< Any
>& aArguments
)
154 throw (Exception
, RuntimeException
)
157 std::cerr
<< "VisioImportFilter::initialize" << std::endl
;
159 Sequence
< PropertyValue
> aAnySeq
;
160 sal_Int32 nLength
= aArguments
.getLength();
161 if ( nLength
&& ( aArguments
[0] >>= aAnySeq
) )
163 const PropertyValue
*pValue
= aAnySeq
.getConstArray();
164 nLength
= aAnySeq
.getLength();
165 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
167 if ( pValue
[i
].Name
== "Type" )
169 pValue
[i
].Value
>>= msFilterName
;
175 OUString
VisioImportFilter_getImplementationName ()
176 throw (RuntimeException
)
179 std::cerr
<< "VisioImportFilter_getImplementationName" << std::endl
;
181 return OUString ( "com.sun.star.comp.Draw.VisioImportFilter" );
184 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
185 #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
186 sal_Bool SAL_CALL
VisioImportFilter_supportsService( const OUString
&ServiceName
)
187 throw (RuntimeException
)
190 std::cerr
<< "VisioImportFilter_supportsService" << std::endl
;
192 return ( ServiceName
== SERVICE_NAME1
|| ServiceName
== SERVICE_NAME2
);
194 Sequence
< OUString
> SAL_CALL
VisioImportFilter_getSupportedServiceNames( )
195 throw (RuntimeException
)
198 std::cerr
<< "VisioImportFilter_getSupportedServiceNames" << std::endl
;
200 Sequence
< OUString
> aRet(2);
201 OUString
*pArray
= aRet
.getArray();
202 pArray
[0] = OUString ( SERVICE_NAME1
);
203 pArray
[1] = OUString ( SERVICE_NAME2
);
209 Reference
< XInterface
> SAL_CALL
VisioImportFilter_createInstance( const Reference
< XComponentContext
> & rContext
)
213 std::cerr
<< "VisioImportFilter_createInstance" << std::endl
;
215 return (cppu::OWeakObject
*) new VisioImportFilter( rContext
);
219 OUString SAL_CALL
VisioImportFilter::getImplementationName( )
220 throw (RuntimeException
)
223 std::cerr
<< "VisioImportFilter::getImplementationName" << std::endl
;
225 return VisioImportFilter_getImplementationName();
227 sal_Bool SAL_CALL
VisioImportFilter::supportsService( const OUString
&rServiceName
)
228 throw (RuntimeException
)
231 std::cerr
<< "VisioImportFilter::supportsService" << std::endl
;
233 return VisioImportFilter_supportsService( rServiceName
);
235 Sequence
< OUString
> SAL_CALL
VisioImportFilter::getSupportedServiceNames( )
236 throw (RuntimeException
)
239 std::cerr
<< "VisioImportFilter::getSupportedServiceNames" << std::endl
;
241 return VisioImportFilter_getSupportedServiceNames();
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */