1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hwpreader.hxx,v $
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_
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
;
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
;
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
) {}
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
>
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
){
147 Reference
< XDocumentHandler
> rDocumentHandler
;
148 Reference
< XInterface
> rUCB
;
149 Reference
< XAttributeList
> rList
;
150 AttributeListImpl
*pList
;
155 /* -------- Document Parsing --------- */
158 void makeDrawMiscStyle(HWPDrawingObject
*);
159 void makeAutoStyles();
160 void makeMasterStyles();
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
>
214 HwpImportFilter( const Reference
< XMultiServiceFactory
> xFact
);
218 static Sequence
< OUString
> getSupportedServiceNames_Static( void ) throw();
219 static OUString
getImplementationName_Static() throw();
223 virtual sal_Bool SAL_CALL
filter( const Sequence
< PropertyValue
>& aDescriptor
)
224 throw( RuntimeException
);
225 virtual void SAL_CALL
cancel() throw(RuntimeException
);
227 virtual void SAL_CALL
setTargetDocument( const Reference
< XComponent
>& xDoc
)
228 throw( IllegalArgumentException
, RuntimeException
);
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
);
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();
253 HwpImportFilter::HwpImportFilter( const Reference
< XMultiServiceFactory
> xFact
)
255 OUString sService
= OUString::createFromAscii( WRITER_IMPORTER_NAME
);
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"),
273 Reference
< XImporter
> xImporter
= Reference
< XImporter
>( xHandler
, UNO_QUERY
);
274 rImporter
= xImporter
;
275 Reference
< XFilter
> xFilter
= Reference
< XFilter
>( p
);
280 printf(" fail to instanciate %s\n", WRITER_IMPORTER_NAME
);
285 HwpImportFilter::~HwpImportFilter()
289 sal_Bool
HwpImportFilter::filter( const Sequence
< PropertyValue
>& aDescriptor
)
290 throw( RuntimeException
)
292 // delegate to IchitaroImpoter
293 rFilter
->filter( aDescriptor
);
298 void HwpImportFilter::cancel() throw(::com::sun::star::uno::RuntimeException
)
303 void HwpImportFilter::setTargetDocument( const Reference
< XComponent
>& xDoc
)
304 throw( IllegalArgumentException
, RuntimeException
)
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
)
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
);
338 /////////////////////////////////////////////////////////////////////////////////////
339 // The below three C functions are nessesary for this shared library is treaded as
340 // UNO component library.
341 /////////////////////////////////////////////////////////////////////////////////////
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
)
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
) );
367 catch (InvalidRegistryException
&)
369 OSL_ENSURE( sal_False
, "### InvalidRegistryException!" );
375 void * SAL_CALL
component_getFactory( const sal_Char
* pImplName
, void * pServiceManager
, void * )
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() );