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 css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
58  sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
59 };
60 
61 class SwXMLBlockContext : public SvXMLImportContext
62 {
63 public:
64  SwXMLBlockContext( SwXMLBlockListImport& rImport,
65  const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList );
66 };
67 
68 class SwXMLTextBlockDocumentContext : public SvXMLImportContext
69 {
70 private:
71  SwXMLTextBlockImport & rLocalRef;
72 
73 public:
74  SwXMLTextBlockDocumentContext( SwXMLTextBlockImport& rImport );
75 
76  virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
77  sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
78 };
79 
80 class SwXMLTextBlockBodyContext : public SvXMLImportContext
81 {
82 private:
83  SwXMLTextBlockImport & rLocalRef;
84 
85 public:
86  SwXMLTextBlockBodyContext( SwXMLTextBlockImport& rImport );
87 
88  virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
89  sal_Int32, const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
90 };
91 
92 class SwXMLTextBlockTextContext : public SvXMLImportContext
93 {
94 private:
95  SwXMLTextBlockImport & rLocalRef;
96 
97 public:
98  SwXMLTextBlockTextContext( SwXMLTextBlockImport& rImport );
99 
100  virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
101  sal_Int32 Element,
102  const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
103 };
104 
105 class SwXMLTextBlockParContext : public SvXMLImportContext
106 {
107 private:
108  SwXMLTextBlockImport & rLocalRef;
109 
110 public:
111  SwXMLTextBlockParContext( SwXMLTextBlockImport & rImport );
112 
113  virtual void SAL_CALL characters( const OUString & aChars ) override;
114 
115  virtual ~SwXMLTextBlockParContext() override;
116 };
117 
118 }
119 
121 {
122 }
123 
125 {
126 }
127 
128 sal_Int32 SAL_CALL SwXMLTextBlockTokenHandler::getTokenFromUTF8( const Sequence< sal_Int8 >& Identifier )
129 {
130  return getTokenDirect( reinterpret_cast< const char* >( Identifier.getConstArray() ), Identifier.getLength() );
131 }
132 
133 Sequence< sal_Int8 > SAL_CALL SwXMLTextBlockTokenHandler::getUTF8Identifier( sal_Int32 )
134 {
135  return Sequence< sal_Int8 >();
136 }
137 
138 sal_Int32 SwXMLTextBlockTokenHandler::getTokenDirect( const char *pTag, sal_Int32 nLength ) const
139 {
140  if( !nLength )
141  nLength = strlen( pTag );
142  const struct xmltoken* pToken = TextBlockTokens::in_word_set( pTag, nLength );
143  return pToken ? pToken->nToken : XML_TOKEN_INVALID;
144 }
145 
147 {
148 }
149 
151 {
152 }
153 
154 sal_Int32 SAL_CALL SwXMLBlockListTokenHandler::getTokenFromUTF8( const Sequence< sal_Int8 >& Identifier )
155 {
156  return getTokenDirect( reinterpret_cast< const char* >( Identifier.getConstArray() ), Identifier.getLength() );
157 }
158 
159 Sequence< sal_Int8 > SAL_CALL SwXMLBlockListTokenHandler::getUTF8Identifier( sal_Int32 )
160 {
161  return Sequence< sal_Int8 >();
162 }
163 
164 sal_Int32 SwXMLBlockListTokenHandler::getTokenDirect( const char *pTag, sal_Int32 nLength ) const
165 {
166  if( !nLength )
167  nLength = strlen( pTag );
168  const struct xmltoken* pToken = BlockListTokens::in_word_set( pTag, nLength );
169  return pToken ? pToken->nToken : XML_TOKEN_INVALID;
170 }
171 
172 SwXMLBlockListContext::SwXMLBlockListContext(
173  SwXMLBlockListImport& rImport,
174  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) :
175  SvXMLImportContext( rImport ),
176  rLocalRef( rImport )
177 {
178  if( xAttrList.is() && xAttrList->hasAttribute( SwXMLBlockListToken::LIST_NAME ) )
179  rImport.getBlockList().SetName( xAttrList->getValue( SwXMLBlockListToken::LIST_NAME ) );
180 }
181 
182 uno::Reference< ::xml::sax::XFastContextHandler > SAL_CALL
183 SwXMLBlockListContext::createFastChildContext( sal_Int32 Element,
184  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
185 {
186  if ( Element == SwXMLBlockListToken::BLOCK )
187  return new SwXMLBlockContext( rLocalRef, xAttrList );
188  return nullptr;
189 }
190 
191 SwXMLBlockContext::SwXMLBlockContext(
192  SwXMLBlockListImport& rImport,
193  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) :
194  SvXMLImportContext( rImport )
195 {
196  static const CharClass & rCC = GetAppCharClass();
197  OUString aShort, aLong, aPackageName;
198  bool bTextOnly = false;
199  if( xAttrList.is() )
200  {
201  if( xAttrList->hasAttribute( SwXMLBlockListToken::ABBREVIATED_NAME ) )
202  aShort = rCC.uppercase( xAttrList->getValue( SwXMLBlockListToken::ABBREVIATED_NAME ) );
203  if( xAttrList->hasAttribute( SwXMLBlockListToken::NAME ) )
204  aLong = xAttrList->getValue( SwXMLBlockListToken::NAME );
205  if( xAttrList->hasAttribute( SwXMLBlockListToken::PACKAGE_NAME ) )
206  aPackageName = xAttrList->getValue( SwXMLBlockListToken::PACKAGE_NAME );
207  if( xAttrList->hasAttribute( SwXMLBlockListToken::UNFORMATTED_TEXT ) )
208  {
209  OUString rAttrValue( xAttrList->getValue( SwXMLBlockListToken::UNFORMATTED_TEXT ) );
210  if( IsXMLToken( rAttrValue, XML_TRUE ) )
211  bTextOnly = true;
212  }
213  }
214  if (aShort.isEmpty() || aLong.isEmpty() || aPackageName.isEmpty())
215  return;
216  rImport.getBlockList().AddName( aShort, aLong, aPackageName, bTextOnly);
217 }
218 
219 SwXMLTextBlockDocumentContext::SwXMLTextBlockDocumentContext(
220  SwXMLTextBlockImport& rImport ) :
221  SvXMLImportContext( rImport ),
222  rLocalRef(rImport)
223 {
224 }
225 
226 uno::Reference< ::xml::sax::XFastContextHandler > SAL_CALL
227 SwXMLTextBlockDocumentContext::createFastChildContext( sal_Int32 Element,
228  const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
229 {
230  if ( Element == SwXMLTextBlockToken::OFFICE_BODY )
231  return new SwXMLTextBlockBodyContext( rLocalRef );
232  return nullptr;
233 }
234 
235 SwXMLTextBlockTextContext::SwXMLTextBlockTextContext(
236  SwXMLTextBlockImport& rImport) :
237  SvXMLImportContext ( rImport ),
238  rLocalRef( rImport )
239 {
240 }
241 
242 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
243 SwXMLTextBlockTextContext::createFastChildContext( sal_Int32 Element,
244  const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
245 {
246  if ( Element == SwXMLTextBlockToken::TEXT_P )
247  return new SwXMLTextBlockParContext( rLocalRef );
248  return nullptr;
249 }
250 
251 SwXMLTextBlockBodyContext::SwXMLTextBlockBodyContext(
252  SwXMLTextBlockImport& rImport ) :
253  SvXMLImportContext( rImport ),
254  rLocalRef(rImport)
255 {
256 }
257 
258 uno::Reference < xml::sax::XFastContextHandler > SAL_CALL
259 SwXMLTextBlockBodyContext::createFastChildContext( sal_Int32 Element,
260  const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
261 {
262  if( Element == SwXMLTextBlockToken::OFFICE_TEXT )
263  return new SwXMLTextBlockTextContext( rLocalRef );
264  else if( Element == SwXMLTextBlockToken::TEXT_P )
265  return new SwXMLTextBlockParContext( rLocalRef );
266  return nullptr;
267 }
268 
269 SwXMLTextBlockParContext::SwXMLTextBlockParContext(
270  SwXMLTextBlockImport& rImport ) :
271  SvXMLImportContext( rImport ),
272  rLocalRef( rImport )
273 {
274 }
275 
276 void SAL_CALL SwXMLTextBlockParContext::characters( const OUString & aChars )
277 {
278  rLocalRef.m_rText += aChars;
279 }
280 
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 //////////////////////////////
294  const uno::Reference< uno::XComponentContext >& rContext,
295  SwXMLTextBlocks &rBlocks )
296 : SvXMLImport( rContext, "", SvXMLImportFlags::NONE ),
297  rBlockList (rBlocks)
298 {
299 }
300 
302  throw ()
303 {
304 }
305 
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 
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 
326  throw()
327 {
328 }
329 
331  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
332 {
333  if( Element == SwXMLTextBlockToken::OFFICE_DOCUMENT ||
335  return new SwXMLTextBlockDocumentContext( *this );
336  else
337  return SvXMLImport::CreateFastContext( Element, xAttrList );
338 }
339 
341 {
342 }
343 
344 /* 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)
virtual SvXMLImportContext * CreateFastContext(sal_Int32 Element, const ::css::uno::Reference< ::css::xml::sax::XFastAttributeList > &xAttrList)
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