Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* FreehandImportFilter: 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 : #include <cppuhelper/supportsservice.hxx>
21 :
22 : #include <xmloff/attrlist.hxx>
23 :
24 : #include <libfreehand/libfreehand.h>
25 : #include <libodfgen/libodfgen.hxx>
26 :
27 : #include "common/DocumentHandler.hxx"
28 : #include "common/WPXSvStream.hxx"
29 : #include "FreehandImportFilter.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 FreehandImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
54 : throw (RuntimeException, std::exception)
55 : {
56 : SAL_INFO("writerperfect", "FreehandImportFilter::filter");
57 0 : sal_Int32 nLength = aDescriptor.getLength();
58 0 : const PropertyValue *pValue = aDescriptor.getConstArray();
59 0 : Reference < XInputStream > xInputStream;
60 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
61 : {
62 0 : if ( pValue[i].Name == "InputStream" )
63 0 : pValue[i].Value >>= xInputStream;
64 : }
65 0 : if ( !xInputStream.is() )
66 : {
67 : OSL_ASSERT( false );
68 0 : return sal_False;
69 : }
70 :
71 : // An XML import service: what we push sax messages to..
72 : Reference < XDocumentHandler > xInternalHandler(
73 0 : mxContext->getServiceManager()->createInstanceWithContext(
74 0 : "com.sun.star.comp.Draw.XMLOasisImporter", mxContext),
75 0 : css::uno::UNO_QUERY_THROW);
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 = libfreehand::FreeHandDocument::parse(&input, &exporter);
89 0 : return tmpParseResult;
90 : }
91 :
92 0 : void SAL_CALL FreehandImportFilter::cancel( )
93 : throw (RuntimeException, std::exception)
94 : {
95 : SAL_INFO("writerperfect", "FreehandImportFilter::cancel");
96 0 : }
97 :
98 : // XImporter
99 0 : void SAL_CALL FreehandImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
100 : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
101 : {
102 : SAL_INFO("writerperfect", "FreehandImportFilter::setTargetDocument");
103 0 : mxDoc = xDoc;
104 0 : }
105 :
106 : // XExtendedFilterDetection
107 0 : OUString SAL_CALL FreehandImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
108 : throw( com::sun::star::uno::RuntimeException, std::exception )
109 : {
110 : SAL_INFO("writerperfect", "FreehandImportFilter::detect");
111 0 : OUString sTypeName;
112 0 : sal_Int32 nLength = Descriptor.getLength();
113 0 : sal_Int32 location = nLength;
114 0 : const PropertyValue *pValue = Descriptor.getConstArray();
115 0 : Reference < XInputStream > xInputStream;
116 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
117 : {
118 0 : if ( pValue[i].Name == "TypeName" )
119 0 : location=i;
120 0 : else if ( pValue[i].Name == "InputStream" )
121 0 : pValue[i].Value >>= xInputStream;
122 : }
123 :
124 0 : if (!xInputStream.is())
125 0 : return OUString();
126 :
127 0 : WPXSvInputStream input( xInputStream );
128 :
129 0 : if (libfreehand::FreeHandDocument::isSupported(&input))
130 0 : sTypeName = "draw_Freehand_Document";
131 :
132 0 : if (!sTypeName.isEmpty())
133 : {
134 0 : if ( location == nLength )
135 : {
136 0 : Descriptor.realloc(nLength+1);
137 0 : Descriptor[location].Name = "TypeName";
138 : }
139 :
140 0 : Descriptor[location].Value <<=sTypeName;
141 : }
142 0 : return sTypeName;
143 : }
144 :
145 :
146 : // XInitialization
147 0 : void SAL_CALL FreehandImportFilter::initialize( const Sequence< Any >& aArguments )
148 : throw (Exception, RuntimeException, std::exception)
149 : {
150 : SAL_INFO("writerperfect", "FreehandImportFilter::initialize");
151 0 : Sequence < PropertyValue > aAnySeq;
152 0 : sal_Int32 nLength = aArguments.getLength();
153 0 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
154 : {
155 0 : const PropertyValue *pValue = aAnySeq.getConstArray();
156 0 : nLength = aAnySeq.getLength();
157 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
158 : {
159 0 : if ( pValue[i].Name == "Type" )
160 : {
161 0 : pValue[i].Value >>= msFilterName;
162 0 : break;
163 : }
164 : }
165 0 : }
166 0 : }
167 0 : OUString FreehandImportFilter_getImplementationName ()
168 : throw (RuntimeException)
169 : {
170 : SAL_INFO("writerperfect", "FreehandImportFilter_getImplementationName");
171 0 : return OUString ( "com.sun.star.comp.Draw.FreehandImportFilter" );
172 : }
173 :
174 0 : Sequence< OUString > SAL_CALL FreehandImportFilter_getSupportedServiceNames( )
175 : throw (RuntimeException)
176 : {
177 : SAL_INFO("writerperfect", "FreehandImportFilter_getSupportedServiceNames");
178 0 : Sequence < OUString > aRet(2);
179 0 : OUString *pArray = aRet.getArray();
180 0 : pArray[0] = "com.sun.star.document.ImportFilter";
181 0 : pArray[1] = "com.sun.star.document.ExtendedTypeDetection";
182 0 : return aRet;
183 : }
184 :
185 0 : Reference< XInterface > SAL_CALL FreehandImportFilter_createInstance( const Reference< XComponentContext > & rContext)
186 : throw( Exception )
187 : {
188 : SAL_INFO("writerperfect", "FreehandImportFilter_createInstance");
189 0 : return (cppu::OWeakObject *) new FreehandImportFilter( rContext );
190 : }
191 :
192 : // XServiceInfo
193 0 : OUString SAL_CALL FreehandImportFilter::getImplementationName( )
194 : throw (RuntimeException, std::exception)
195 : {
196 : SAL_INFO("writerperfect", "FreehandImportFilter::getImplementationName");
197 0 : return FreehandImportFilter_getImplementationName();
198 : }
199 0 : sal_Bool SAL_CALL FreehandImportFilter::supportsService( const OUString &rServiceName )
200 : throw (RuntimeException, std::exception)
201 : {
202 : SAL_INFO("writerperfect", "FreehandImportFilter::supportsService");
203 0 : return cppu::supportsService( this, rServiceName );
204 : }
205 0 : Sequence< OUString > SAL_CALL FreehandImportFilter::getSupportedServiceNames( )
206 : throw (RuntimeException, std::exception)
207 : {
208 : SAL_INFO("writerperfect", "FreehandImportFilter::getSupportedServiceNames");
209 0 : return FreehandImportFilter_getSupportedServiceNames();
210 0 : }
211 :
212 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|