1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_HWPFILTER_SOURCE_HWPREADER_HXX
21 #define INCLUDED_HWPFILTER_SOURCE_HWPREADER_HXX
26 #include <sal/alloca.h>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/io/XInputStream.hpp>
33 #include <com/sun/star/document/XFilter.hpp>
34 #include <com/sun/star/document/XImporter.hpp>
35 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
37 #include <com/sun/star/io/XActiveDataSink.hpp>
38 #include <com/sun/star/io/XActiveDataControl.hpp>
39 #include <com/sun/star/io/XStreamListener.hpp>
40 #include <com/sun/star/document/XExtendedFilterDetection.hpp>
42 #include <cppuhelper/factory.hxx>
43 #include <cppuhelper/implbase.hxx>
44 #include <cppuhelper/supportsservice.hxx>
45 #include <cppuhelper/weak.hxx>
48 using namespace ::cppu
;
49 using namespace ::com::sun::star::lang
;
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::io
;
52 using namespace ::com::sun::star::registry
;
53 using namespace ::com::sun::star::document
;
54 using namespace ::com::sun::star::beans
;
55 using namespace ::com::sun::star::xml::sax
;
59 #include <unotools/mediadescriptor.hxx>
65 #include "hstream.hxx"
67 #include "attributes.hxx"
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"
74 struct HwpReaderPrivate
;
76 * This class implements the external Parser interface
78 class HwpReader
: public WeakImplHelper
<XFilter
>
83 virtual ~HwpReader() override
;
87 * parseStream does Parser-startup initializations
89 virtual sal_Bool SAL_CALL
filter(const Sequence
< PropertyValue
>& aDescriptor
) override
;
90 virtual void SAL_CALL
cancel() override
{}
91 void setDocumentHandler(Reference
< XDocumentHandler
> const & xHandler
)
93 m_rxDocumentHandler
= xHandler
;
96 Reference
< XDocumentHandler
> m_rxDocumentHandler
;
97 rtl::Reference
<AttributeListImpl
> mxList
;
99 std::unique_ptr
<HwpReaderPrivate
> d
;
101 /* -------- Document Parsing --------- */
104 void makeDrawMiscStyle(HWPDrawingObject
*);
105 void makeAutoStyles();
106 void makeMasterStyles();
109 void makeTextDecls();
111 /* -------- Paragraph Parsing --------- */
112 void parsePara(HWPPara
*para
);
113 void make_text_p0(HWPPara
*para
, bool bParaStart
);
114 void make_text_p1(HWPPara
*para
, bool bParaStart
);
115 void make_text_p3(HWPPara
*para
, bool bParaStart
);
117 /* -------- rDocument->characters(x) --------- */
118 void makeChars(hchar_string
& rStr
);
120 /* -------- Special Char Parsing --------- */
121 void makeFieldCode(hchar_string
const & rStr
, FieldCode
const *hbox
); //6
122 void makeBookmark(Bookmark
const *hbox
); //6
123 void makeDateFormat(DateCode
*hbox
); //7
124 void makeDateCode(DateCode
*hbox
); //8
126 void makeTable(TxtBox
*hbox
);
127 void makeTextBox(TxtBox
*hbox
);
128 void makeFormula(TxtBox
*hbox
);
129 void makeHyperText(TxtBox
*hbox
);
130 void makePicture(Picture
*hbox
);
131 void makePictureDRAW(HWPDrawingObject
*drawobj
, Picture
*hbox
);
133 void makeHidden(Hidden
*hbox
);
134 void makeFootnote(Footnote
*hbox
);
135 void makeAutoNum(AutoNum
const *hbox
);
136 void makeShowPageNum();
137 void makeMailMerge(MailMerge
*hbox
);
138 void makeOutline(Outline
const *hbox
);
140 /* --------- Styles Parsing ------------ */
141 void makePageStyle();
142 void makeColumns(ColumnDef
const *);
143 void makeTStyle(CharShape
const *);
144 void makePStyle(ParaShape
const *);
145 void makeFStyle(FBoxStyle
*);
146 void makeCaptionStyle(FBoxStyle
*);
147 void makeDrawStyle(HWPDrawingObject
*,FBoxStyle
*);
148 void makeTableStyle(Table
*);
149 void parseCharShape(CharShape
const *);
150 void parseParaShape(ParaShape
const *);
151 static char* getTStyleName(int, char *);
152 static char* getPStyleName(int, char *);
155 class HwpImportFilter
: public WeakImplHelper
< XFilter
, XImporter
, XServiceInfo
, XExtendedFilterDetection
>
158 explicit HwpImportFilter(const Reference
< XMultiServiceFactory
>& rFact
);
159 virtual ~HwpImportFilter() override
;
162 static Sequence
< OUString
> getSupportedServiceNames_Static() throw();
163 static OUString
getImplementationName_Static() throw();
167 virtual sal_Bool SAL_CALL
filter( const Sequence
< PropertyValue
>& aDescriptor
) override
;
168 virtual void SAL_CALL
cancel() override
;
171 virtual void SAL_CALL
setTargetDocument( const Reference
< XComponent
>& xDoc
) override
;
174 OUString SAL_CALL
getImplementationName() override
;
175 Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
176 sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
178 //XExtendedFilterDetection
179 virtual OUString SAL_CALL
detect( css::uno::Sequence
< css::beans::PropertyValue
>& rDescriptor
) override
;
182 Reference
< XFilter
> rFilter
;
183 Reference
< XImporter
> rImporter
;
186 /// @throws Exception
187 Reference
< XInterface
> HwpImportFilter_CreateInstance(
188 const Reference
< XMultiServiceFactory
>& rSMgr
)
190 HwpImportFilter
*p
= new HwpImportFilter( rSMgr
);
192 return Reference
< XInterface
> ( static_cast<OWeakObject
*>(p
) );
195 Sequence
< OUString
> HwpImportFilter::getSupportedServiceNames_Static() throw ()
197 return { HwpImportFilter::getImplementationName_Static() };
200 HwpImportFilter::HwpImportFilter(const Reference
< XMultiServiceFactory
>& rFact
)
202 OUString
sService( WRITER_IMPORTER_NAME
);
204 Reference
< XDocumentHandler
> xHandler( rFact
->createInstance( sService
), UNO_QUERY
);
206 HwpReader
*p
= new HwpReader
;
207 p
->setDocumentHandler( xHandler
);
209 Reference
< XImporter
> xImporter( xHandler
, UNO_QUERY
);
210 rImporter
= xImporter
;
211 Reference
< XFilter
> xFilter( p
);
216 printf(" fail to instantiate %s\n", WRITER_IMPORTER_NAME
);
221 HwpImportFilter::~HwpImportFilter()
225 sal_Bool
HwpImportFilter::filter( const Sequence
< PropertyValue
>& aDescriptor
)
227 // delegate to IchitaroImpoter
228 return rFilter
->filter( aDescriptor
);
231 void HwpImportFilter::cancel()
236 void HwpImportFilter::setTargetDocument( const Reference
< XComponent
>& xDoc
)
239 rImporter
->setTargetDocument( xDoc
);
242 OUString
HwpImportFilter::getImplementationName_Static() throw()
244 return IMPLEMENTATION_NAME
;
247 OUString
HwpImportFilter::getImplementationName()
249 return IMPLEMENTATION_NAME
;
252 sal_Bool
HwpImportFilter::supportsService( const OUString
& ServiceName
)
254 return cppu::supportsService(this, ServiceName
);
257 //XExtendedFilterDetection
258 OUString
HwpImportFilter::detect( css::uno::Sequence
< css::beans::PropertyValue
>& rDescriptor
)
262 utl::MediaDescriptor
aDescriptor(rDescriptor
);
263 aDescriptor
.addInputStream();
265 Reference
< XInputStream
> xInputStream(
266 aDescriptor
[utl::MediaDescriptor::PROP_INPUTSTREAM()], UNO_QUERY
);
268 if (xInputStream
.is())
270 Sequence
< sal_Int8
> aData
;
271 sal_Int32 nLen
= HWPIDLen
;
273 nLen
== xInputStream
->readBytes(aData
, nLen
) &&
274 detect_hwp_version(reinterpret_cast<const char*>(aData
.getConstArray()))
277 sTypeName
= "writer_MIZI_Hwp_97";
284 Sequence
< OUString
> HwpImportFilter::getSupportedServiceNames()
286 return { SERVICE_NAME1
, SERVICE_NAME2
};
291 SAL_DLLPUBLIC_EXPORT
void * hwp_component_getFactory( const char * pImplName
, void * pServiceManager
, void * )
293 void * pRet
= nullptr;
295 if (pServiceManager
)
297 Reference
< XSingleServiceFactory
> xRet
;
298 Reference
< XMultiServiceFactory
> xSMgr
= static_cast< XMultiServiceFactory
* > ( pServiceManager
);
300 OUString aImplementationName
= OUString::createFromAscii( pImplName
);
302 if (aImplementationName
== IMPLEMENTATION_NAME
)
304 xRet
= createSingleFactory( xSMgr
, aImplementationName
,
305 HwpImportFilter_CreateInstance
,
306 HwpImportFilter::getSupportedServiceNames_Static() );
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */