docthemes: Save themes def. to a file when added to ColorSets
[LibreOffice.git] / sw / source / core / swg / SwXMLBlockImport.cxx
blob4f19f72510f312864bd2bff33fd868effb0c9f89
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <SwXMLBlockImport.hxx>
21 #include <SwXMLTextBlocks.hxx>
22 #include <xmloff/xmlictxt.hxx>
23 #include <unotools/charclass.hxx>
24 #include <swtypes.hxx>
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
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star;
41 using namespace css::xml::sax;
43 class SwXMLBlockListImport;
44 class SwXMLTextBlockImport;
46 namespace {
48 class SwXMLBlockListContext : public SvXMLImportContext
50 private:
51 SwXMLBlockListImport& m_rLocalRef;
53 public:
54 SwXMLBlockListContext( SwXMLBlockListImport& rImport,
55 const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList );
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;
61 class SwXMLBlockContext : public SvXMLImportContext
63 public:
64 SwXMLBlockContext( SwXMLBlockListImport& rImport,
65 const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList );
68 class SwXMLTextBlockDocumentContext : public SvXMLImportContext
70 private:
71 SwXMLTextBlockImport& m_rLocalRef;
73 public:
74 SwXMLTextBlockDocumentContext( SwXMLTextBlockImport& rImport );
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;
80 class SwXMLTextBlockBodyContext : public SvXMLImportContext
82 private:
83 SwXMLTextBlockImport& m_rLocalRef;
85 public:
86 SwXMLTextBlockBodyContext( SwXMLTextBlockImport& rImport );
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;
92 class SwXMLTextBlockTextContext : public SvXMLImportContext
94 private:
95 SwXMLTextBlockImport& m_rLocalRef;
97 public:
98 SwXMLTextBlockTextContext( SwXMLTextBlockImport& rImport );
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;
105 class SwXMLTextBlockParContext : public SvXMLImportContext
107 private:
108 SwXMLTextBlockImport& m_rLocalRef;
110 public:
111 SwXMLTextBlockParContext( SwXMLTextBlockImport & rImport );
113 virtual void SAL_CALL characters( const OUString & aChars ) override;
115 virtual ~SwXMLTextBlockParContext() override;
120 SwXMLTextBlockTokenHandler::SwXMLTextBlockTokenHandler()
124 SwXMLTextBlockTokenHandler::~SwXMLTextBlockTokenHandler()
128 sal_Int32 SAL_CALL SwXMLTextBlockTokenHandler::getTokenFromUTF8( const Sequence< sal_Int8 >& Identifier )
130 return getTokenDirect( std::string_view(reinterpret_cast< const char* >( Identifier.getConstArray() ), Identifier.getLength()) );
133 Sequence< sal_Int8 > SAL_CALL SwXMLTextBlockTokenHandler::getUTF8Identifier( sal_Int32 )
135 return Sequence< sal_Int8 >();
138 sal_Int32 SwXMLTextBlockTokenHandler::getTokenDirect(std::string_view token) const
140 const struct xmltoken* pToken = TextBlockTokens::in_word_set(token.data(), token.size());
141 return pToken ? pToken->nToken : XML_TOKEN_INVALID;
144 SwXMLBlockListTokenHandler::SwXMLBlockListTokenHandler()
148 SwXMLBlockListTokenHandler::~SwXMLBlockListTokenHandler()
152 sal_Int32 SAL_CALL SwXMLBlockListTokenHandler::getTokenFromUTF8( const Sequence< sal_Int8 >& Identifier )
154 return getTokenDirect( std::string_view(reinterpret_cast< const char* >( Identifier.getConstArray() ), Identifier.getLength()) );
157 Sequence< sal_Int8 > SAL_CALL SwXMLBlockListTokenHandler::getUTF8Identifier( sal_Int32 )
159 return Sequence< sal_Int8 >();
162 sal_Int32 SwXMLBlockListTokenHandler::getTokenDirect(std::string_view token) const
164 const struct xmltoken* pToken = BlockListTokens::in_word_set(token.data(), token.size());
165 return pToken ? pToken->nToken : XML_TOKEN_INVALID;
168 SwXMLBlockListContext::SwXMLBlockListContext(
169 SwXMLBlockListImport& rImport, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
170 : SvXMLImportContext(rImport)
171 , m_rLocalRef(rImport)
173 if( xAttrList.is() && xAttrList->hasAttribute( SwXMLBlockListToken::LIST_NAME ) )
174 rImport.getBlockList().SetName( xAttrList->getValue( SwXMLBlockListToken::LIST_NAME ) );
177 uno::Reference< ::xml::sax::XFastContextHandler > SAL_CALL
178 SwXMLBlockListContext::createFastChildContext( sal_Int32 Element,
179 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
181 if ( Element == SwXMLBlockListToken::BLOCK )
182 return new SwXMLBlockContext(m_rLocalRef, xAttrList);
183 return nullptr;
186 SwXMLBlockContext::SwXMLBlockContext(
187 SwXMLBlockListImport& rImport,
188 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) :
189 SvXMLImportContext( rImport )
191 static const CharClass & rCC = GetAppCharClass();
192 OUString aShort, aLong, aPackageName;
193 bool bTextOnly = false;
194 if( xAttrList.is() )
196 if( xAttrList->hasAttribute( SwXMLBlockListToken::ABBREVIATED_NAME ) )
197 aShort = rCC.uppercase( xAttrList->getValue( SwXMLBlockListToken::ABBREVIATED_NAME ) );
198 if( xAttrList->hasAttribute( SwXMLBlockListToken::NAME ) )
199 aLong = xAttrList->getValue( SwXMLBlockListToken::NAME );
200 if( xAttrList->hasAttribute( SwXMLBlockListToken::PACKAGE_NAME ) )
201 aPackageName = xAttrList->getValue( SwXMLBlockListToken::PACKAGE_NAME );
202 if( xAttrList->hasAttribute( SwXMLBlockListToken::UNFORMATTED_TEXT ) )
204 OUString rAttrValue( xAttrList->getValue( SwXMLBlockListToken::UNFORMATTED_TEXT ) );
205 if( IsXMLToken( rAttrValue, XML_TRUE ) )
206 bTextOnly = true;
209 if (aShort.isEmpty() || aLong.isEmpty() || aPackageName.isEmpty())
210 return;
211 rImport.getBlockList().AddName( aShort, aLong, aPackageName, bTextOnly);
214 SwXMLTextBlockDocumentContext::SwXMLTextBlockDocumentContext(SwXMLTextBlockImport& rImport)
215 : SvXMLImportContext(rImport)
216 , m_rLocalRef(rImport)
220 uno::Reference< ::xml::sax::XFastContextHandler > SAL_CALL
221 SwXMLTextBlockDocumentContext::createFastChildContext( sal_Int32 Element,
222 const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
224 if ( Element == SwXMLTextBlockToken::OFFICE_BODY )
225 return new SwXMLTextBlockBodyContext(m_rLocalRef);
226 return nullptr;
229 SwXMLTextBlockTextContext::SwXMLTextBlockTextContext(SwXMLTextBlockImport& rImport)
230 : SvXMLImportContext(rImport)
231 , m_rLocalRef(rImport)
235 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
236 SwXMLTextBlockTextContext::createFastChildContext( sal_Int32 Element,
237 const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
239 if ( Element == SwXMLTextBlockToken::TEXT_P )
240 return new SwXMLTextBlockParContext(m_rLocalRef);
241 return nullptr;
244 SwXMLTextBlockBodyContext::SwXMLTextBlockBodyContext(SwXMLTextBlockImport& rImport)
245 : SvXMLImportContext(rImport)
246 , m_rLocalRef(rImport)
250 uno::Reference < xml::sax::XFastContextHandler > SAL_CALL
251 SwXMLTextBlockBodyContext::createFastChildContext( sal_Int32 Element,
252 const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
254 if( Element == SwXMLTextBlockToken::OFFICE_TEXT )
255 return new SwXMLTextBlockTextContext(m_rLocalRef);
256 else if( Element == SwXMLTextBlockToken::TEXT_P )
257 return new SwXMLTextBlockParContext(m_rLocalRef);
258 return nullptr;
261 SwXMLTextBlockParContext::SwXMLTextBlockParContext(SwXMLTextBlockImport& rImport)
262 : SvXMLImportContext(rImport)
263 , m_rLocalRef(rImport)
267 void SAL_CALL SwXMLTextBlockParContext::characters( const OUString & aChars )
269 m_rLocalRef.m_rText += aChars;
272 SwXMLTextBlockParContext::~SwXMLTextBlockParContext()
274 if (m_rLocalRef.m_bTextOnly)
275 m_rLocalRef.m_rText += "\015";
276 else
278 if (!m_rLocalRef.m_rText.endsWith(" "))
279 m_rLocalRef.m_rText += " ";
283 // SwXMLBlockListImport //////////////////////////////
284 SwXMLBlockListImport::SwXMLBlockListImport(
285 const uno::Reference< uno::XComponentContext >& rContext,
286 SwXMLTextBlocks &rBlocks )
287 : SvXMLImport( rContext, u""_ustr, SvXMLImportFlags::NONE ),
288 m_rBlockList (rBlocks)
292 SwXMLBlockListImport::~SwXMLBlockListImport()
293 noexcept
297 SvXMLImportContext* SwXMLBlockListImport::CreateFastContext( sal_Int32 Element,
298 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
300 if( Element == SwXMLBlockListToken::BLOCK_LIST )
301 return new SwXMLBlockListContext( *this, xAttrList );
302 return nullptr;
305 SwXMLTextBlockImport::SwXMLTextBlockImport(
306 const uno::Reference< uno::XComponentContext >& rContext,
307 OUString & rNewText,
308 bool bNewTextOnly )
309 : SvXMLImport(rContext, u""_ustr, SvXMLImportFlags::ALL ),
310 m_bTextOnly ( bNewTextOnly ),
311 m_rText ( rNewText )
315 SwXMLTextBlockImport::~SwXMLTextBlockImport()
316 noexcept
320 SvXMLImportContext* SwXMLTextBlockImport::CreateFastContext( sal_Int32 Element,
321 const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
323 if( Element == SwXMLTextBlockToken::OFFICE_DOCUMENT ||
324 Element == SwXMLTextBlockToken::OFFICE_DOCUMENT_CONTENT )
325 return new SwXMLTextBlockDocumentContext( *this );
326 return nullptr;
329 void SAL_CALL SwXMLTextBlockImport::endDocument()
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */