Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
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 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef _HWPREADER_HXX_
21 : #define _HWPREADER_HXX_
22 : #include <errno.h>
23 : #include <stdio.h>
24 : #include <string.h>
25 : #include <sal/alloca.h>
26 :
27 : #include <com/sun/star/lang/XServiceInfo.hpp>
28 : #include <com/sun/star/lang/XComponent.hpp>
29 : #include <com/sun/star/io/XInputStream.hpp>
30 : #include <com/sun/star/document/XFilter.hpp>
31 : #include <com/sun/star/document/XImporter.hpp>
32 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
33 :
34 : #include <cppuhelper/implbase2.hxx>
35 : #include <com/sun/star/io/XActiveDataSink.hpp>
36 : #include <com/sun/star/io/XActiveDataControl.hpp>
37 : #include <com/sun/star/io/XStreamListener.hpp>
38 : #include <com/sun/star/document/XExtendedFilterDetection.hpp>
39 :
40 : #include <cppuhelper/factory.hxx>
41 : #include <cppuhelper/weak.hxx>
42 : #include <cppuhelper/implbase1.hxx>
43 : #include <cppuhelper/implbase4.hxx>
44 : #include <cppuhelper/servicefactory.hxx>
45 :
46 : using namespace ::rtl;
47 : using namespace ::cppu;
48 : using namespace ::com::sun::star::lang;
49 : using namespace ::com::sun::star::uno;
50 : using namespace ::com::sun::star::io;
51 : using namespace ::com::sun::star::registry;
52 : using namespace ::com::sun::star::document;
53 : using namespace ::com::sun::star::beans;
54 : using namespace ::com::sun::star::xml::sax;
55 : using namespace ::com::sun::star::document;
56 :
57 : #include <assert.h>
58 :
59 : #include <comphelper/mediadescriptor.hxx>
60 :
61 : #include "hwpfile.h"
62 : #include "hcode.h"
63 : #include "hbox.h"
64 : #include "htags.h"
65 : #include "hstream.h"
66 : #include "drawdef.h"
67 : #include "attributes.hxx"
68 :
69 : #define IMPLEMENTATION_NAME "com.sun.comp.hwpimport.HwpImportFilter"
70 : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
71 : #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
72 : #define WRITER_IMPORTER_NAME "com.sun.star.comp.Writer.XMLImporter"
73 :
74 : struct HwpReaderPrivate;
75 : /**
76 : * This class implements the external Parser interface
77 : */
78 : class HwpReader : public WeakImplHelper1<XFilter>
79 : {
80 :
81 : public:
82 : HwpReader();
83 : ~HwpReader();
84 :
85 : public:
86 : /**
87 : * parseStream does Parser-startup initializations
88 : */
89 : virtual sal_Bool SAL_CALL filter(const Sequence< PropertyValue >& aDescriptor) throw (RuntimeException);
90 0 : virtual void SAL_CALL cancel() throw(RuntimeException) {}
91 2 : virtual void SAL_CALL setDocumentHandler(Reference< XDocumentHandler > xHandler)
92 : {
93 2 : m_rxDocumentHandler = xHandler;
94 2 : }
95 : private:
96 : Reference< XDocumentHandler > m_rxDocumentHandler;
97 : Reference< XAttributeList > rList;
98 : AttributeListImpl *pList;
99 : HWPFile hwpfile;
100 : HwpReaderPrivate *d;
101 : private:
102 : /* -------- Document Parsing --------- */
103 : void makeMeta();
104 : void makeStyles();
105 : void makeDrawMiscStyle(HWPDrawingObject *);
106 : void makeAutoStyles();
107 : void makeMasterStyles();
108 : void makeBody();
109 :
110 : void makeTextDecls();
111 :
112 : /* -------- Paragraph Parsing --------- */
113 : void parsePara(HWPPara *para, sal_Bool bParaStart = sal_False);
114 : void make_text_p0(HWPPara *para, sal_Bool bParaStart = sal_False);
115 : void make_text_p1(HWPPara *para, sal_Bool bParaStart = sal_False);
116 : void make_text_p3(HWPPara *para, sal_Bool bParaStart = sal_False);
117 :
118 : /* -------- rDocument->characters(x) --------- */
119 : void makeChars(hchar_string & rStr);
120 :
121 : /* -------- Special Char Parsing --------- */
122 : void makeFieldCode(hchar_string & rStr, FieldCode *hbox); //6
123 : void makeBookmark(Bookmark *hbox); //6
124 : void makeDateFormat(DateCode *hbox); //7
125 : void makeDateCode(DateCode *hbox); //8
126 : void makeTab(Tab *hbox); //9
127 : void makeTable(TxtBox *hbox);
128 : void makeTextBox(TxtBox *hbox);
129 : void makeFormula(TxtBox *hbox);
130 : void makeHyperText(TxtBox *hbox);
131 : void makePicture(Picture *hbox);
132 : void makePictureDRAW(HWPDrawingObject *drawobj, Picture *hbox);
133 : void makeLine(Line *hbox);
134 : void makeHidden(Hidden *hbox);
135 : void makeFootnote(Footnote *hbox);
136 : void makeAutoNum(AutoNum *hbox);
137 : void makeShowPageNum();
138 : void makeMailMerge(MailMerge *hbox);
139 : void makeTocMark(TocMark *hbox);
140 : void makeIndexMark(IndexMark *hbox);
141 : void makeOutline(Outline *hbox);
142 :
143 : /* --------- Styles Parsing ------------ */
144 : void makePageStyle();
145 : void makeColumns(ColumnDef *);
146 : void makeTStyle(CharShape *);
147 : void makePStyle(ParaShape *);
148 : void makeFStyle(FBoxStyle *);
149 : void makeCaptionStyle(FBoxStyle *);
150 : void makeDrawStyle(HWPDrawingObject *,FBoxStyle *);
151 : void makeTableStyle(Table *);
152 : void parseCharShape(CharShape *);
153 : void parseParaShape(ParaShape *);
154 : char* getTStyleName(int, char *);
155 : char* getPStyleName(int, char *);
156 : };
157 :
158 : class HwpImportFilter : public WeakImplHelper4< XFilter, XImporter, XServiceInfo, XExtendedFilterDetection >
159 : {
160 : public:
161 : HwpImportFilter( const Reference< XMultiServiceFactory > xFact );
162 : ~HwpImportFilter();
163 :
164 : public:
165 : static Sequence< OUString > getSupportedServiceNames_Static() throw();
166 : static OUString getImplementationName_Static() throw();
167 :
168 : public:
169 : // XFilter
170 : virtual sal_Bool SAL_CALL filter( const Sequence< PropertyValue >& aDescriptor )
171 : throw( RuntimeException );
172 : virtual void SAL_CALL cancel() throw(RuntimeException);
173 :
174 : // XImporter
175 : virtual void SAL_CALL setTargetDocument( const Reference< XComponent >& xDoc)
176 : throw( IllegalArgumentException, RuntimeException );
177 :
178 : // XServiceInfo
179 : OUString SAL_CALL getImplementationName() throw (RuntimeException);
180 : Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw (::com::sun::star::uno::RuntimeException);
181 : sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw (::com::sun::star::uno::RuntimeException);
182 :
183 : //XExtendedFilterDetection
184 : virtual OUString SAL_CALL detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rDescriptor ) throw (::com::sun::star::uno::RuntimeException);
185 :
186 : public:
187 : Reference< XFilter > rFilter;
188 : Reference< XImporter > rImporter;
189 : };
190 :
191 2 : Reference< XInterface > HwpImportFilter_CreateInstance(
192 : const Reference< XMultiServiceFactory >& rSMgr ) throw( Exception )
193 : {
194 2 : HwpImportFilter *p = new HwpImportFilter( rSMgr );
195 :
196 2 : return Reference< XInterface > ( (OWeakObject* )p );
197 : }
198 :
199 2 : Sequence< OUString > HwpImportFilter::getSupportedServiceNames_Static() throw ()
200 : {
201 2 : Sequence< OUString > aRet(1);
202 2 : aRet.getArray()[0] = HwpImportFilter::getImplementationName_Static();
203 2 : return aRet;
204 : }
205 :
206 2 : HwpImportFilter::HwpImportFilter( const Reference< XMultiServiceFactory > xFact )
207 : {
208 2 : OUString sService( WRITER_IMPORTER_NAME );
209 : try {
210 : Reference< XDocumentHandler >
211 2 : xHandler( xFact->createInstance( sService ), UNO_QUERY );
212 :
213 2 : HwpReader *p = new HwpReader;
214 2 : p->setDocumentHandler( xHandler );
215 :
216 2 : Reference< XImporter > xImporter = Reference< XImporter >( xHandler, UNO_QUERY );
217 2 : rImporter = xImporter;
218 2 : Reference< XFilter > xFilter = Reference< XFilter >( p );
219 2 : rFilter = xFilter;
220 : }
221 0 : catch( Exception & )
222 : {
223 0 : printf(" fail to instanciate %s\n", WRITER_IMPORTER_NAME );
224 0 : exit( 1 );
225 2 : }
226 2 : }
227 :
228 4 : HwpImportFilter::~HwpImportFilter()
229 : {
230 4 : }
231 :
232 4 : sal_Bool HwpImportFilter::filter( const Sequence< PropertyValue >& aDescriptor )
233 : throw( RuntimeException )
234 : {
235 : // delegate to IchitaroImpoter
236 4 : return rFilter->filter( aDescriptor );
237 : }
238 :
239 0 : void HwpImportFilter::cancel() throw(::com::sun::star::uno::RuntimeException)
240 : {
241 0 : rFilter->cancel();
242 0 : }
243 :
244 0 : void HwpImportFilter::setTargetDocument( const Reference< XComponent >& xDoc )
245 : throw( IllegalArgumentException, RuntimeException )
246 : {
247 : // delegate
248 0 : rImporter->setTargetDocument( xDoc );
249 0 : }
250 :
251 2 : OUString HwpImportFilter::getImplementationName_Static() throw()
252 : {
253 2 : return OUString( IMPLEMENTATION_NAME );
254 : }
255 :
256 0 : OUString HwpImportFilter::getImplementationName() throw(::com::sun::star::uno::RuntimeException)
257 : {
258 0 : return OUString( IMPLEMENTATION_NAME );
259 : }
260 :
261 0 : sal_Bool HwpImportFilter::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
262 : {
263 0 : Sequence< OUString > aSNL = getSupportedServiceNames();
264 0 : const OUString *pArray = aSNL.getConstArray();
265 :
266 0 : for ( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
267 0 : if ( pArray[i] == ServiceName )
268 0 : return sal_True;
269 :
270 0 : return sal_False;
271 : }
272 :
273 : //XExtendedFilterDetection
274 0 : OUString HwpImportFilter::detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rDescriptor ) throw (::com::sun::star::uno::RuntimeException)
275 : {
276 0 : rtl::OUString sTypeName;
277 :
278 0 : comphelper::MediaDescriptor aDescriptor(rDescriptor);
279 0 : aDescriptor.addInputStream();
280 :
281 : Reference< XInputStream > xInputStream(
282 0 : aDescriptor[comphelper::MediaDescriptor::PROP_INPUTSTREAM()], UNO_QUERY);
283 :
284 0 : if (xInputStream.is())
285 : {
286 0 : Sequence< sal_Int8 > aData;
287 0 : sal_Int32 nLen = HWPIDLen;
288 0 : if (
289 0 : nLen == xInputStream->readBytes(aData, nLen) &&
290 0 : detect_hwp_version(reinterpret_cast<const char*>(aData.getConstArray()))
291 : )
292 : {
293 0 : sTypeName = OUString("writer_MIZI_Hwp_97");
294 0 : }
295 : }
296 :
297 0 : return sTypeName;
298 : }
299 :
300 0 : Sequence< OUString> HwpImportFilter::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)
301 : {
302 0 : Sequence < OUString > aRet(2);
303 0 : OUString* pArray = aRet.getArray();
304 0 : pArray[0] = OUString(SERVICE_NAME1);
305 0 : pArray[1] = OUString(SERVICE_NAME2);
306 0 : return aRet;
307 : }
308 :
309 : extern "C"
310 : {
311 2 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL hwp_component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * )
312 : {
313 2 : void * pRet = 0;
314 :
315 2 : if (pServiceManager )
316 : {
317 2 : Reference< XSingleServiceFactory > xRet;
318 2 : Reference< XMultiServiceFactory > xSMgr = reinterpret_cast< XMultiServiceFactory * > ( pServiceManager );
319 :
320 2 : OUString aImplementationName = OUString::createFromAscii( pImplName );
321 :
322 2 : if (aImplementationName == IMPLEMENTATION_NAME )
323 : {
324 : xRet = createSingleFactory( xSMgr, aImplementationName,
325 : HwpImportFilter_CreateInstance,
326 2 : HwpImportFilter::getSupportedServiceNames_Static() );
327 : }
328 2 : if (xRet.is())
329 : {
330 2 : xRet->acquire();
331 2 : pRet = xRet.get();
332 2 : }
333 : }
334 :
335 2 : return pRet;
336 : }
337 : }
338 :
339 : #endif
340 :
341 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|