merge the formfield patch from ooo-build
[ooovba.git] / hwpfilter / source / hwpreader.hxx
blob73ea49ebead39f9a92e76fe7e24e401860c93ad6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hwpreader.hxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _HWPREADER_HXX_
32 #define _HWPREADER_HXX_
33 #include <errno.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sal/alloca.h>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/io/XInputStream.hpp>
41 #include <com/sun/star/document/XFilter.hpp>
42 #include <com/sun/star/document/XImporter.hpp>
43 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
45 #include <com/sun/star/ucb/XContentIdentifierFactory.hpp>
46 #include <com/sun/star/ucb/XContentProvider.hpp>
47 #include <com/sun/star/ucb/XContentIdentifier.hpp>
48 #include <com/sun/star/ucb/XContent.hpp>
49 #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
50 #include <com/sun/star/ucb/OpenMode.hpp>
51 #include <com/sun/star/ucb/XCommandProcessor.hpp>
52 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
53 #include <cppuhelper/implbase2.hxx>
54 #include <com/sun/star/io/XActiveDataSink.hpp>
55 #include <com/sun/star/io/XActiveDataControl.hpp>
56 #include <com/sun/star/io/XStreamListener.hpp>
58 #include <cppuhelper/factory.hxx>
59 #include <cppuhelper/weak.hxx>
60 #include <cppuhelper/implbase1.hxx>
61 #include <cppuhelper/implbase3.hxx>
62 #include <cppuhelper/servicefactory.hxx>
64 using namespace ::rtl;
65 using namespace ::cppu;
66 using namespace ::com::sun::star::ucb;
67 using namespace ::com::sun::star::lang;
68 using namespace ::com::sun::star::uno;
69 using namespace ::com::sun::star::io;
70 using namespace ::com::sun::star::registry;
71 using namespace ::com::sun::star::document;
72 using namespace ::com::sun::star::beans;
73 using namespace ::com::sun::star::xml::sax;
75 #include <assert.h>
77 #include "hwpfile.h"
78 #include "hcode.h"
79 #include "hbox.h"
80 #include "htags.h"
81 #include "hstream.h"
82 #include "drawdef.h"
83 #include "attributes.hxx"
86 #define IMPLEMENTATION_NAME "com.sun.comp.hwpimport.HwpImportFilter"
87 #define SERVICE_NAME "com.sun.star.document.ImportFilter"
88 #define WRITER_IMPORTER_NAME "com.sun.star.comp.Writer.XMLImporter"
90 class MyDataSink : public ::cppu::WeakImplHelper2< XActiveDataControl, XActiveDataSink >
92 Reference < XInputStream > m_xInputStream;
93 public:
95 // XActiveDataControl.
96 virtual void SAL_CALL addListener ( const Reference<XStreamListener> &)
97 throw(RuntimeException) {}
98 virtual void SAL_CALL removeListener ( const Reference<XStreamListener> &)
99 throw(RuntimeException) {}
100 virtual void SAL_CALL start (void) throw(RuntimeException) {}
101 virtual void SAL_CALL terminate (void) throw(RuntimeException) {}
103 // XActiveDataSink.
104 virtual void SAL_CALL setInputStream ( const Reference<XInputStream> &rxInputStream)
105 throw(RuntimeException);
106 virtual Reference<XInputStream> SAL_CALL getInputStream (void)
107 throw(RuntimeException);
110 void SAL_CALL MyDataSink::setInputStream ( const Reference<XInputStream> &rxInputStream)
111 throw(RuntimeException )
113 m_xInputStream = rxInputStream;
116 Reference < XInputStream > SAL_CALL MyDataSink::getInputStream (void)
117 throw(RuntimeException)
119 return m_xInputStream;
122 struct HwpReaderPrivate;
124 * This class implements the external Parser interface
126 class HwpReader : public WeakImplHelper1<XFilter>
129 public:
130 HwpReader();
131 ~HwpReader();
133 public:
135 * parseStream does Parser-startup initializations
137 virtual sal_Bool SAL_CALL filter(const Sequence< PropertyValue >& aDescriptor) throw (RuntimeException);
138 virtual void SAL_CALL cancel() throw(RuntimeException) {}
139 virtual void SAL_CALL setDocumentHandler(Reference< XDocumentHandler > xHandler)
141 rDocumentHandler = xHandler;
143 void setUCB( Reference< XInterface > xUCB ){
144 rUCB = xUCB;
146 private:
147 Reference< XDocumentHandler > rDocumentHandler;
148 Reference< XInterface > rUCB;
149 Reference< XAttributeList > rList;
150 AttributeListImpl *pList;
151 HWPFile hwpfile;
152 HwpReaderPrivate *d;
154 private:
155 /* -------- Document Parsing --------- */
156 void makeMeta();
157 void makeStyles();
158 void makeDrawMiscStyle(HWPDrawingObject *);
159 void makeAutoStyles();
160 void makeMasterStyles();
161 void makeBody();
163 void makeTextDecls();
165 /* -------- Paragraph Parsing --------- */
166 void parsePara(HWPPara *para, sal_Bool bParaStart = sal_False);
167 void make_text_p0(HWPPara *para, sal_Bool bParaStart = sal_False);
168 void make_text_p1(HWPPara *para, sal_Bool bParaStart = sal_False);
169 void make_text_p3(HWPPara *para, sal_Bool bParaStart = sal_False);
171 /* -------- rDocument->characters(x) --------- */
172 void makeChars(hchar *, int);
174 /* -------- Special Char Parsing --------- */
175 void makeFieldCode(FieldCode *hbox); //6
176 void makeBookmark(Bookmark *hbox); //6
177 void makeDateFormat(DateCode *hbox); //7
178 void makeDateCode(DateCode *hbox); //8
179 void makeTab(Tab *hbox); //9
180 void makeTable(TxtBox *hbox);
181 void makeTextBox(TxtBox *hbox);
182 void makeFormula(TxtBox *hbox);
183 void makeHyperText(TxtBox *hbox);
184 void makePicture(Picture *hbox);
185 void makePictureDRAW(HWPDrawingObject *drawobj, Picture *hbox);
186 void makeLine(Line *hbox);
187 void makeHidden(Hidden *hbox);
188 void makeFootnote(Footnote *hbox);
189 void makeAutoNum(AutoNum *hbox);
190 void makeShowPageNum();
191 void makeMailMerge(MailMerge *hbox);
192 void makeTocMark(TocMark *hbox);
193 void makeIndexMark(IndexMark *hbox);
194 void makeOutline(Outline *hbox);
196 /* --------- Styles Parsing ------------ */
197 void makePageStyle();
198 void makeColumns(ColumnDef *);
199 void makeTStyle(CharShape *);
200 void makePStyle(ParaShape *);
201 void makeFStyle(FBoxStyle *);
202 void makeCaptionStyle(FBoxStyle *);
203 void makeDrawStyle(HWPDrawingObject *,FBoxStyle *);
204 void makeTableStyle(Table *);
205 void parseCharShape(CharShape *);
206 void parseParaShape(ParaShape *);
207 char* getTStyleName(int, char *);
208 char* getPStyleName(int, char *);
211 class HwpImportFilter : public WeakImplHelper3< XFilter, XImporter, XServiceInfo >
213 public:
214 HwpImportFilter( const Reference< XMultiServiceFactory > xFact );
215 ~HwpImportFilter();
217 public:
218 static Sequence< OUString > getSupportedServiceNames_Static( void ) throw();
219 static OUString getImplementationName_Static() throw();
221 public:
222 // XFilter
223 virtual sal_Bool SAL_CALL filter( const Sequence< PropertyValue >& aDescriptor )
224 throw( RuntimeException );
225 virtual void SAL_CALL cancel() throw(RuntimeException);
226 // XImporter
227 virtual void SAL_CALL setTargetDocument( const Reference< XComponent >& xDoc)
228 throw( IllegalArgumentException, RuntimeException );
229 // XServiceInfo
230 OUString SAL_CALL getImplementationName() throw (RuntimeException);
231 Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw (::com::sun::star::uno::RuntimeException);
232 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw (::com::sun::star::uno::RuntimeException);
234 public:
235 Reference< XFilter > rFilter;
236 Reference< XImporter > rImporter;
239 Reference< XInterface > HwpImportFilter_CreateInstance(
240 const Reference< XMultiServiceFactory >& rSMgr ) throw( Exception )
242 HwpImportFilter *p = new HwpImportFilter( rSMgr );
244 return Reference< XInterface > ( (OWeakObject* )p );
247 Sequence< OUString > HwpImportFilter::getSupportedServiceNames_Static( void ) throw ()
249 Sequence< OUString > aRet(1);
250 aRet.getArray()[0] = HwpImportFilter::getImplementationName_Static();
251 return aRet;
253 HwpImportFilter::HwpImportFilter( const Reference< XMultiServiceFactory > xFact )
255 OUString sService = OUString::createFromAscii( WRITER_IMPORTER_NAME );
256 try {
257 Reference< XDocumentHandler >
258 xHandler( xFact->createInstance( sService ), UNO_QUERY );
260 HwpReader *p = new HwpReader;
261 p->setDocumentHandler( xHandler );
263 Sequence< Any > aArgs( 2 );
264 aArgs[0] <<= OUString::createFromAscii( "Local" );
265 aArgs[1] <<= OUString::createFromAscii( "Office" );
266 Reference< XInterface > xUCB
267 ( xFact->createInstanceWithArguments
268 (OUString::createFromAscii("com.sun.star.ucb.UniversalContentBroker"),
269 aArgs));
270 p->setUCB( xUCB );
273 Reference< XImporter > xImporter = Reference< XImporter >( xHandler, UNO_QUERY );
274 rImporter = xImporter;
275 Reference< XFilter > xFilter = Reference< XFilter >( p );
276 rFilter = xFilter;
278 catch( Exception & )
280 printf(" fail to instanciate %s\n", WRITER_IMPORTER_NAME );
281 exit( 1 );
285 HwpImportFilter::~HwpImportFilter()
289 sal_Bool HwpImportFilter::filter( const Sequence< PropertyValue >& aDescriptor )
290 throw( RuntimeException )
292 // delegate to IchitaroImpoter
293 rFilter->filter( aDescriptor );
295 return sal_True;
298 void HwpImportFilter::cancel() throw(::com::sun::star::uno::RuntimeException)
300 rFilter->cancel();
303 void HwpImportFilter::setTargetDocument( const Reference< XComponent >& xDoc )
304 throw( IllegalArgumentException, RuntimeException )
306 // delegate
307 rImporter->setTargetDocument( xDoc );
310 OUString HwpImportFilter::getImplementationName_Static() throw()
312 return OUString::createFromAscii( IMPLEMENTATION_NAME );
315 OUString HwpImportFilter::getImplementationName() throw(::com::sun::star::uno::RuntimeException)
317 return OUString::createFromAscii( IMPLEMENTATION_NAME );
319 sal_Bool HwpImportFilter::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
321 Sequence< OUString > aSNL = getSupportedServiceNames();
322 const OUString *pArray = aSNL.getConstArray();
324 for ( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
325 if ( pArray[i] == ServiceName )
326 return sal_True;
328 return sal_False;
331 Sequence< OUString> HwpImportFilter::getSupportedServiceNames( void ) throw(::com::sun::star::uno::RuntimeException)
333 Sequence< OUString > seq(1);
334 seq.getArray()[0] = OUString::createFromAscii( SERVICE_NAME );
335 return seq;
338 /////////////////////////////////////////////////////////////////////////////////////
339 // The below three C functions are nessesary for this shared library is treaded as
340 // UNO component library.
341 /////////////////////////////////////////////////////////////////////////////////////
342 extern "C"
345 void SAL_CALL component_getImplementationEnvironment(
346 const sal_Char ** ppEnvTypeName, uno_Environment ** )
348 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
352 sal_Bool SAL_CALL component_writeInfo(
353 void * , void * pRegistryKey )
355 if (pRegistryKey)
359 Reference< XRegistryKey > xKey( reinterpret_cast< XRegistryKey * >( pRegistryKey ) );
361 Reference< XRegistryKey > xNewKey = xKey->createKey(
362 OUString::createFromAscii( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) );
363 xNewKey->createKey( OUString::createFromAscii( SERVICE_NAME ) );
365 return sal_True;
367 catch (InvalidRegistryException &)
369 OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
372 return sal_False;
375 void * SAL_CALL component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * )
377 void * pRet = 0;
379 if (pServiceManager )
381 Reference< XSingleServiceFactory > xRet;
382 Reference< XMultiServiceFactory > xSMgr = reinterpret_cast< XMultiServiceFactory * > ( pServiceManager );
384 OUString aImplementationName = OUString::createFromAscii( pImplName );
386 if (aImplementationName == OUString::createFromAscii( IMPLEMENTATION_NAME ) )
388 xRet = createSingleFactory( xSMgr, aImplementationName,
389 HwpImportFilter_CreateInstance,
390 HwpImportFilter::getSupportedServiceNames_Static() );
392 if (xRet.is())
394 xRet->acquire();
395 pRet = xRet.get();
399 return pRet;
403 #endif