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