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