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 #include <SwXMLBlockImport.hxx>
21 #include <SwXMLTextBlocks.hxx>
22 #include <xmloff/xmlictxt.hxx>
23 #include <unotools/charclass.hxx>
24 #include <swtypes.hxx>
25 
26 #if defined __clang__
27 #if __has_warning("-Wdeprecated-register")
28 #pragma GCC diagnostic push
29 #pragma GCC diagnostic ignored "-Wdeprecated-register"
30 #endif
31 #endif
32 #include <tokens.cxx>
33 #if defined __clang__
34 #if __has_warning("-Wdeprecated-register")
35 #pragma GCC diagnostic pop
36 #endif
37 #endif
38 
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star;
41 using namespace css::xml::sax;
42 
43 class SwXMLBlockListImport;
44 class SwXMLTextBlockImport;
45 
46 class SwXMLBlockListContext : public SvXMLImportContext
47 {
48 private:
49     SwXMLBlockListImport & rLocalRef;
50 
51 public:
52     SwXMLBlockListContext( SwXMLBlockListImport& rImport,
53         const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList );
54 
55     virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
56         sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
57 };
58 
59 class SwXMLBlockContext : public SvXMLImportContext
60 {
61 public:
62     SwXMLBlockContext( SwXMLBlockListImport& rImport,
63         const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList );
64 };
65 
66 class SwXMLTextBlockDocumentContext : public SvXMLImportContext
67 {
68 private:
69     SwXMLTextBlockImport & rLocalRef;
70 
71 public:
72     SwXMLTextBlockDocumentContext( SwXMLTextBlockImport& rImport );
73 
74     virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
75         sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
76 };
77 
78 class SwXMLTextBlockBodyContext : public SvXMLImportContext
79 {
80 private:
81     SwXMLTextBlockImport & rLocalRef;
82 
83 public:
84     SwXMLTextBlockBodyContext( SwXMLTextBlockImport& rImport );
85 
86     virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
87         sal_Int32, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
88 };
89 
90 class SwXMLTextBlockTextContext : public SvXMLImportContext
91 {
92 private:
93     SwXMLTextBlockImport & rLocalRef;
94 
95 public:
96     SwXMLTextBlockTextContext( SwXMLTextBlockImport& rImport );
97 
98     virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
99         sal_Int32 Element,
100         const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
101 };
102 
103 class SwXMLTextBlockParContext : public SvXMLImportContext
104 {
105 private:
106     SwXMLTextBlockImport & rLocalRef;
107 
108 public:
109     SwXMLTextBlockParContext( SwXMLTextBlockImport & rImport );
110 
111     virtual void SAL_CALL characters( const OUString & aChars ) override;
112 
113     virtual ~SwXMLTextBlockParContext() override;
114 };
115 
SwXMLTextBlockTokenHandler()116 SwXMLTextBlockTokenHandler::SwXMLTextBlockTokenHandler()
117 {
118 }
119 
~SwXMLTextBlockTokenHandler()120 SwXMLTextBlockTokenHandler::~SwXMLTextBlockTokenHandler()
121 {
122 }
123 
getTokenFromUTF8(const Sequence<sal_Int8> & Identifier)124 sal_Int32 SAL_CALL SwXMLTextBlockTokenHandler::getTokenFromUTF8( const Sequence< sal_Int8 >& Identifier )
125 {
126     return getTokenDirect( reinterpret_cast< const char* >( Identifier.getConstArray() ), Identifier.getLength() );
127 }
128 
getUTF8Identifier(sal_Int32)129 Sequence< sal_Int8 > SAL_CALL SwXMLTextBlockTokenHandler::getUTF8Identifier( sal_Int32 )
130 {
131     return Sequence< sal_Int8 >();
132 }
133 
getTokenDirect(const char * pTag,sal_Int32 nLength) const134 sal_Int32 SwXMLTextBlockTokenHandler::getTokenDirect( const char *pTag, sal_Int32 nLength ) const
135 {
136     if( !nLength )
137         nLength = strlen( pTag );
138     const struct xmltoken* pToken = TextBlockTokens::in_word_set( pTag, nLength );
139     return pToken ? pToken->nToken : XML_TOKEN_INVALID;
140 }
141 
SwXMLBlockListTokenHandler()142 SwXMLBlockListTokenHandler::SwXMLBlockListTokenHandler()
143 {
144 }
145 
~SwXMLBlockListTokenHandler()146 SwXMLBlockListTokenHandler::~SwXMLBlockListTokenHandler()
147 {
148 }
149 
getTokenFromUTF8(const Sequence<sal_Int8> & Identifier)150 sal_Int32 SAL_CALL SwXMLBlockListTokenHandler::getTokenFromUTF8( const Sequence< sal_Int8 >& Identifier )
151 {
152     return getTokenDirect( reinterpret_cast< const char* >( Identifier.getConstArray() ), Identifier.getLength() );
153 }
154 
getUTF8Identifier(sal_Int32)155 Sequence< sal_Int8 > SAL_CALL SwXMLBlockListTokenHandler::getUTF8Identifier( sal_Int32 )
156 {
157     return Sequence< sal_Int8 >();
158 }
159 
getTokenDirect(const char * pTag,sal_Int32 nLength) const160 sal_Int32 SwXMLBlockListTokenHandler::getTokenDirect( const char *pTag, sal_Int32 nLength ) const
161 {
162     if( !nLength )
163         nLength = strlen( pTag );
164     const struct xmltoken* pToken = BlockListTokens::in_word_set( pTag, nLength );
165     return pToken ? pToken->nToken : XML_TOKEN_INVALID;
166 }
167 
SwXMLBlockListContext(SwXMLBlockListImport & rImport,const uno::Reference<xml::sax::XFastAttributeList> & xAttrList)168 SwXMLBlockListContext::SwXMLBlockListContext(
169     SwXMLBlockListImport& rImport,
170     const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) :
171     SvXMLImportContext( rImport ),
172     rLocalRef( rImport )
173 {
174     if( xAttrList.is() && xAttrList->hasAttribute( SwXMLBlockListToken::LIST_NAME ) )
175         rImport.getBlockList().SetName( xAttrList->getValue( SwXMLBlockListToken::LIST_NAME ) );
176 }
177 
178 uno::Reference< ::xml::sax::XFastContextHandler > SAL_CALL
createFastChildContext(sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> & xAttrList)179 SwXMLBlockListContext::createFastChildContext( sal_Int32 Element,
180     const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
181 {
182     if ( Element == SwXMLBlockListToken::BLOCK )
183         return new SwXMLBlockContext( rLocalRef, xAttrList );
184     else
185         return new SvXMLImportContext( rLocalRef );
186 }
187 
SwXMLBlockContext(SwXMLBlockListImport & rImport,const uno::Reference<xml::sax::XFastAttributeList> & xAttrList)188 SwXMLBlockContext::SwXMLBlockContext(
189     SwXMLBlockListImport& rImport,
190     const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) :
191     SvXMLImportContext( rImport )
192 {
193     static const CharClass & rCC = GetAppCharClass();
194     OUString aShort, aLong, aPackageName;
195     bool bTextOnly = false;
196     if( xAttrList.is() )
197     {
198         if( xAttrList->hasAttribute( SwXMLBlockListToken::ABBREVIATED_NAME ) )
199             aShort = rCC.uppercase( xAttrList->getValue( SwXMLBlockListToken::ABBREVIATED_NAME ) );
200         if( xAttrList->hasAttribute( SwXMLBlockListToken::NAME ) )
201             aLong = xAttrList->getValue( SwXMLBlockListToken::NAME );
202         if( xAttrList->hasAttribute( SwXMLBlockListToken::PACKAGE_NAME ) )
203             aPackageName = xAttrList->getValue( SwXMLBlockListToken::PACKAGE_NAME );
204         if( xAttrList->hasAttribute( SwXMLBlockListToken::UNFORMATTED_TEXT ) )
205         {
206             OUString rAttrValue( xAttrList->getValue( SwXMLBlockListToken::UNFORMATTED_TEXT ) );
207             if( IsXMLToken( rAttrValue, XML_TRUE ) )
208                 bTextOnly = true;
209         }
210     }
211     if (aShort.isEmpty() || aLong.isEmpty() || aPackageName.isEmpty())
212         return;
213     rImport.getBlockList().AddName( aShort, aLong, aPackageName, bTextOnly);
214 }
215 
SwXMLTextBlockDocumentContext(SwXMLTextBlockImport & rImport)216 SwXMLTextBlockDocumentContext::SwXMLTextBlockDocumentContext(
217     SwXMLTextBlockImport& rImport ) :
218     SvXMLImportContext( rImport ),
219     rLocalRef(rImport)
220 {
221 }
222 
223 uno::Reference< ::xml::sax::XFastContextHandler > SAL_CALL
createFastChildContext(sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> &)224 SwXMLTextBlockDocumentContext::createFastChildContext( sal_Int32 Element,
225     const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
226 {
227     if ( Element == SwXMLTextBlockToken::OFFICE_BODY )
228         return new SwXMLTextBlockBodyContext( rLocalRef );
229     else
230         return new SvXMLImportContext( rLocalRef );
231 }
232 
SwXMLTextBlockTextContext(SwXMLTextBlockImport & rImport)233 SwXMLTextBlockTextContext::SwXMLTextBlockTextContext(
234     SwXMLTextBlockImport& rImport) :
235     SvXMLImportContext ( rImport ),
236     rLocalRef( rImport )
237 {
238 }
239 
240 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
createFastChildContext(sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> &)241 SwXMLTextBlockTextContext::createFastChildContext( sal_Int32 Element,
242     const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
243 {
244     if ( Element == SwXMLTextBlockToken::TEXT_P )
245         return new SwXMLTextBlockParContext( rLocalRef );
246     else
247         return new SvXMLImportContext( rLocalRef );
248 }
249 
SwXMLTextBlockBodyContext(SwXMLTextBlockImport & rImport)250 SwXMLTextBlockBodyContext::SwXMLTextBlockBodyContext(
251     SwXMLTextBlockImport& rImport ) :
252     SvXMLImportContext( rImport ),
253     rLocalRef(rImport)
254 {
255 }
256 
257 uno::Reference < xml::sax::XFastContextHandler > SAL_CALL
createFastChildContext(sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> &)258 SwXMLTextBlockBodyContext::createFastChildContext( sal_Int32 Element,
259     const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
260 {
261     if( Element == SwXMLTextBlockToken::OFFICE_TEXT )
262         return new SwXMLTextBlockTextContext( rLocalRef );
263     else if( Element == SwXMLTextBlockToken::TEXT_P )
264         return new SwXMLTextBlockParContext( rLocalRef );
265     else
266         return new SvXMLImportContext( rLocalRef );
267 }
268 
SwXMLTextBlockParContext(SwXMLTextBlockImport & rImport)269 SwXMLTextBlockParContext::SwXMLTextBlockParContext(
270     SwXMLTextBlockImport& rImport ) :
271     SvXMLImportContext( rImport ),
272     rLocalRef( rImport )
273 {
274 }
275 
characters(const OUString & aChars)276 void SAL_CALL SwXMLTextBlockParContext::characters( const OUString & aChars )
277 {
278     rLocalRef.m_rText += aChars;
279 }
280 
~SwXMLTextBlockParContext()281 SwXMLTextBlockParContext::~SwXMLTextBlockParContext()
282 {
283     if (rLocalRef.bTextOnly)
284         rLocalRef.m_rText += "\015";
285     else
286     {
287         if (!rLocalRef.m_rText.endsWith( " " ))
288             rLocalRef.m_rText += " ";
289     }
290 }
291 
292 // SwXMLBlockListImport //////////////////////////////
SwXMLBlockListImport(const uno::Reference<uno::XComponentContext> & rContext,SwXMLTextBlocks & rBlocks)293 SwXMLBlockListImport::SwXMLBlockListImport(
294     const uno::Reference< uno::XComponentContext >& rContext,
295     SwXMLTextBlocks &rBlocks )
296 :   SvXMLImport( rContext, "", SvXMLImportFlags::NONE ),
297     rBlockList (rBlocks)
298 {
299 }
300 
~SwXMLBlockListImport()301 SwXMLBlockListImport::~SwXMLBlockListImport()
302     throw ()
303 {
304 }
305 
CreateFastContext(sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> & xAttrList)306 SvXMLImportContext* SwXMLBlockListImport::CreateFastContext( sal_Int32 Element,
307     const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
308 {
309     if( Element == SwXMLBlockListToken::BLOCK_LIST )
310         return new SwXMLBlockListContext( *this, xAttrList );
311     else
312         return SvXMLImport::CreateFastContext( Element, xAttrList );
313 }
314 
SwXMLTextBlockImport(const uno::Reference<uno::XComponentContext> & rContext,OUString & rNewText,bool bNewTextOnly)315 SwXMLTextBlockImport::SwXMLTextBlockImport(
316     const uno::Reference< uno::XComponentContext >& rContext,
317     OUString & rNewText,
318     bool bNewTextOnly )
319 :   SvXMLImport(rContext, "", SvXMLImportFlags::ALL ),
320     bTextOnly ( bNewTextOnly ),
321     m_rText ( rNewText )
322 {
323 }
324 
~SwXMLTextBlockImport()325 SwXMLTextBlockImport::~SwXMLTextBlockImport()
326     throw()
327 {
328 }
329 
CreateFastContext(sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> & xAttrList)330 SvXMLImportContext* SwXMLTextBlockImport::CreateFastContext( sal_Int32 Element,
331     const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
332 {
333     if( Element == SwXMLTextBlockToken::OFFICE_DOCUMENT ||
334         Element == SwXMLTextBlockToken::OFFICE_DOCUMENT_CONTENT )
335         return new SwXMLTextBlockDocumentContext( *this );
336     else
337         return SvXMLImport::CreateFastContext( Element, xAttrList );
338 }
339 
endDocument()340 void SAL_CALL SwXMLTextBlockImport::endDocument()
341 {
342 }
343 
344 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
345