merged tag ooo/DEV300_m102
[LibreOffice.git] / hwpfilter / source / hwpreader.hxx
blob239a1ff225d6279b2e70bfd36e7b35730064c0d0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #ifndef _HWPREADER_HXX_
29 #define _HWPREADER_HXX_
30 #include <errno.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <sal/alloca.h>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/lang/XComponent.hpp>
37 #include <com/sun/star/io/XInputStream.hpp>
38 #include <com/sun/star/document/XFilter.hpp>
39 #include <com/sun/star/document/XImporter.hpp>
40 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
42 #include <com/sun/star/ucb/XContentIdentifierFactory.hpp>
43 #include <com/sun/star/ucb/XContentProvider.hpp>
44 #include <com/sun/star/ucb/XContentIdentifier.hpp>
45 #include <com/sun/star/ucb/XContent.hpp>
46 #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
47 #include <com/sun/star/ucb/OpenMode.hpp>
48 #include <com/sun/star/ucb/XCommandProcessor.hpp>
49 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
50 #include <cppuhelper/implbase2.hxx>
51 #include <com/sun/star/io/XActiveDataSink.hpp>
52 #include <com/sun/star/io/XActiveDataControl.hpp>
53 #include <com/sun/star/io/XStreamListener.hpp>
55 #include <cppuhelper/factory.hxx>
56 #include <cppuhelper/weak.hxx>
57 #include <cppuhelper/implbase1.hxx>
58 #include <cppuhelper/implbase3.hxx>
59 #include <cppuhelper/servicefactory.hxx>
61 using namespace ::rtl;
62 using namespace ::cppu;
63 using namespace ::com::sun::star::ucb;
64 using namespace ::com::sun::star::lang;
65 using namespace ::com::sun::star::uno;
66 using namespace ::com::sun::star::io;
67 using namespace ::com::sun::star::registry;
68 using namespace ::com::sun::star::document;
69 using namespace ::com::sun::star::beans;
70 using namespace ::com::sun::star::xml::sax;
72 #include <assert.h>
74 #include "hwpfile.h"
75 #include "hcode.h"
76 #include "hbox.h"
77 #include "htags.h"
78 #include "hstream.h"
79 #include "drawdef.h"
80 #include "attributes.hxx"
83 #define IMPLEMENTATION_NAME "com.sun.comp.hwpimport.HwpImportFilter"
84 #define SERVICE_NAME "com.sun.star.document.ImportFilter"
85 #define WRITER_IMPORTER_NAME "com.sun.star.comp.Writer.XMLImporter"
87 class MyDataSink : public ::cppu::WeakImplHelper2< XActiveDataControl, XActiveDataSink >
89 Reference < XInputStream > m_xInputStream;
90 public:
92 // XActiveDataControl.
93 virtual void SAL_CALL addListener ( const Reference<XStreamListener> &)
94 throw(RuntimeException) {}
95 virtual void SAL_CALL removeListener ( const Reference<XStreamListener> &)
96 throw(RuntimeException) {}
97 virtual void SAL_CALL start (void) throw(RuntimeException) {}
98 virtual void SAL_CALL terminate (void) throw(RuntimeException) {}
100 // XActiveDataSink.
101 virtual void SAL_CALL setInputStream ( const Reference<XInputStream> &rxInputStream)
102 throw(RuntimeException);
103 virtual Reference<XInputStream> SAL_CALL getInputStream (void)
104 throw(RuntimeException);
107 void SAL_CALL MyDataSink::setInputStream ( const Reference<XInputStream> &rxInputStream)
108 throw(RuntimeException )
110 m_xInputStream = rxInputStream;
113 Reference < XInputStream > SAL_CALL MyDataSink::getInputStream (void)
114 throw(RuntimeException)
116 return m_xInputStream;
119 struct HwpReaderPrivate;
121 * This class implements the external Parser interface
123 class HwpReader : public WeakImplHelper1<XFilter>
126 public:
127 HwpReader();
128 ~HwpReader();
130 public:
132 * parseStream does Parser-startup initializations
134 virtual sal_Bool SAL_CALL filter(const Sequence< PropertyValue >& aDescriptor) throw (RuntimeException);
135 virtual void SAL_CALL cancel() throw(RuntimeException) {}
136 virtual void SAL_CALL setDocumentHandler(Reference< XDocumentHandler > xHandler)
138 rDocumentHandler = xHandler;
140 void setUCB( Reference< XInterface > xUCB ){
141 rUCB = xUCB;
143 private:
144 Reference< XDocumentHandler > rDocumentHandler;
145 Reference< XInterface > rUCB;
146 Reference< XAttributeList > rList;
147 AttributeListImpl *pList;
148 HWPFile hwpfile;
149 HwpReaderPrivate *d;
151 private:
152 /* -------- Document Parsing --------- */
153 void makeMeta();
154 void makeStyles();
155 void makeDrawMiscStyle(HWPDrawingObject *);
156 void makeAutoStyles();
157 void makeMasterStyles();
158 void makeBody();
160 void makeTextDecls();
162 /* -------- Paragraph Parsing --------- */
163 void parsePara(HWPPara *para, sal_Bool bParaStart = sal_False);
164 void make_text_p0(HWPPara *para, sal_Bool bParaStart = sal_False);
165 void make_text_p1(HWPPara *para, sal_Bool bParaStart = sal_False);
166 void make_text_p3(HWPPara *para, sal_Bool bParaStart = sal_False);
168 /* -------- rDocument->characters(x) --------- */
169 void makeChars(hchar *, int);
171 /* -------- Special Char Parsing --------- */
172 void makeFieldCode(FieldCode *hbox); //6
173 void makeBookmark(Bookmark *hbox); //6
174 void makeDateFormat(DateCode *hbox); //7
175 void makeDateCode(DateCode *hbox); //8
176 void makeTab(Tab *hbox); //9
177 void makeTable(TxtBox *hbox);
178 void makeTextBox(TxtBox *hbox);
179 void makeFormula(TxtBox *hbox);
180 void makeHyperText(TxtBox *hbox);
181 void makePicture(Picture *hbox);
182 void makePictureDRAW(HWPDrawingObject *drawobj, Picture *hbox);
183 void makeLine(Line *hbox);
184 void makeHidden(Hidden *hbox);
185 void makeFootnote(Footnote *hbox);
186 void makeAutoNum(AutoNum *hbox);
187 void makeShowPageNum();
188 void makeMailMerge(MailMerge *hbox);
189 void makeTocMark(TocMark *hbox);
190 void makeIndexMark(IndexMark *hbox);
191 void makeOutline(Outline *hbox);
193 /* --------- Styles Parsing ------------ */
194 void makePageStyle();
195 void makeColumns(ColumnDef *);
196 void makeTStyle(CharShape *);
197 void makePStyle(ParaShape *);
198 void makeFStyle(FBoxStyle *);
199 void makeCaptionStyle(FBoxStyle *);
200 void makeDrawStyle(HWPDrawingObject *,FBoxStyle *);
201 void makeTableStyle(Table *);
202 void parseCharShape(CharShape *);
203 void parseParaShape(ParaShape *);
204 char* getTStyleName(int, char *);
205 char* getPStyleName(int, char *);
208 class HwpImportFilter : public WeakImplHelper3< XFilter, XImporter, XServiceInfo >
210 public:
211 HwpImportFilter( const Reference< XMultiServiceFactory > xFact );
212 ~HwpImportFilter();
214 public:
215 static Sequence< OUString > getSupportedServiceNames_Static( void ) throw();
216 static OUString getImplementationName_Static() throw();
218 public:
219 // XFilter
220 virtual sal_Bool SAL_CALL filter( const Sequence< PropertyValue >& aDescriptor )
221 throw( RuntimeException );
222 virtual void SAL_CALL cancel() throw(RuntimeException);
223 // XImporter
224 virtual void SAL_CALL setTargetDocument( const Reference< XComponent >& xDoc)
225 throw( IllegalArgumentException, RuntimeException );
226 // XServiceInfo
227 OUString SAL_CALL getImplementationName() throw (RuntimeException);
228 Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw (::com::sun::star::uno::RuntimeException);
229 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw (::com::sun::star::uno::RuntimeException);
231 public:
232 Reference< XFilter > rFilter;
233 Reference< XImporter > rImporter;
236 Reference< XInterface > HwpImportFilter_CreateInstance(
237 const Reference< XMultiServiceFactory >& rSMgr ) throw( Exception )
239 HwpImportFilter *p = new HwpImportFilter( rSMgr );
241 return Reference< XInterface > ( (OWeakObject* )p );
244 Sequence< OUString > HwpImportFilter::getSupportedServiceNames_Static( void ) throw ()
246 Sequence< OUString > aRet(1);
247 aRet.getArray()[0] = HwpImportFilter::getImplementationName_Static();
248 return aRet;
250 HwpImportFilter::HwpImportFilter( const Reference< XMultiServiceFactory > xFact )
252 OUString sService = OUString::createFromAscii( WRITER_IMPORTER_NAME );
253 try {
254 Reference< XDocumentHandler >
255 xHandler( xFact->createInstance( sService ), UNO_QUERY );
257 HwpReader *p = new HwpReader;
258 p->setDocumentHandler( xHandler );
260 Sequence< Any > aArgs( 2 );
261 aArgs[0] <<= OUString::createFromAscii( "Local" );
262 aArgs[1] <<= OUString::createFromAscii( "Office" );
263 Reference< XInterface > xUCB
264 ( xFact->createInstanceWithArguments
265 (OUString::createFromAscii("com.sun.star.ucb.UniversalContentBroker"),
266 aArgs));
267 p->setUCB( xUCB );
270 Reference< XImporter > xImporter = Reference< XImporter >( xHandler, UNO_QUERY );
271 rImporter = xImporter;
272 Reference< XFilter > xFilter = Reference< XFilter >( p );
273 rFilter = xFilter;
275 catch( Exception & )
277 printf(" fail to instanciate %s\n", WRITER_IMPORTER_NAME );
278 exit( 1 );
282 HwpImportFilter::~HwpImportFilter()
286 sal_Bool HwpImportFilter::filter( const Sequence< PropertyValue >& aDescriptor )
287 throw( RuntimeException )
289 // delegate to IchitaroImpoter
290 rFilter->filter( aDescriptor );
292 return sal_True;
295 void HwpImportFilter::cancel() throw(::com::sun::star::uno::RuntimeException)
297 rFilter->cancel();
300 void HwpImportFilter::setTargetDocument( const Reference< XComponent >& xDoc )
301 throw( IllegalArgumentException, RuntimeException )
303 // delegate
304 rImporter->setTargetDocument( xDoc );
307 OUString HwpImportFilter::getImplementationName_Static() throw()
309 return OUString::createFromAscii( IMPLEMENTATION_NAME );
312 OUString HwpImportFilter::getImplementationName() throw(::com::sun::star::uno::RuntimeException)
314 return OUString::createFromAscii( IMPLEMENTATION_NAME );
316 sal_Bool HwpImportFilter::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
318 Sequence< OUString > aSNL = getSupportedServiceNames();
319 const OUString *pArray = aSNL.getConstArray();
321 for ( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
322 if ( pArray[i] == ServiceName )
323 return sal_True;
325 return sal_False;
328 Sequence< OUString> HwpImportFilter::getSupportedServiceNames( void ) throw(::com::sun::star::uno::RuntimeException)
330 Sequence< OUString > seq(1);
331 seq.getArray()[0] = OUString::createFromAscii( SERVICE_NAME );
332 return seq;
335 /////////////////////////////////////////////////////////////////////////////////////
336 // The below three C functions are nessesary for this shared library is treaded as
337 // UNO component library.
338 /////////////////////////////////////////////////////////////////////////////////////
339 extern "C"
342 void SAL_CALL component_getImplementationEnvironment(
343 const sal_Char ** ppEnvTypeName, uno_Environment ** )
345 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
348 void * SAL_CALL component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * )
350 void * pRet = 0;
352 if (pServiceManager )
354 Reference< XSingleServiceFactory > xRet;
355 Reference< XMultiServiceFactory > xSMgr = reinterpret_cast< XMultiServiceFactory * > ( pServiceManager );
357 OUString aImplementationName = OUString::createFromAscii( pImplName );
359 if (aImplementationName == OUString::createFromAscii( IMPLEMENTATION_NAME ) )
361 xRet = createSingleFactory( xSMgr, aImplementationName,
362 HwpImportFilter_CreateInstance,
363 HwpImportFilter::getSupportedServiceNames_Static() );
365 if (xRet.is())
367 xRet->acquire();
368 pRet = xRet.get();
372 return pRet;
376 #endif