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