LibreOffice Module xmloff (master) 1
XMLSectionImportContext.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
23#include <comphelper/base64.hxx>
24#include <xmloff/xmlictxt.hxx>
25#include <xmloff/xmlimp.hxx>
26#include <xmloff/txtimp.hxx>
29#include <xmloff/xmltoken.hxx>
30#include <xmloff/prstylei.hxx>
31#include <sal/log.hxx>
33#include <com/sun/star/container/XNamed.hpp>
34#include <com/sun/star/frame/XModel.hpp>
35#include <com/sun/star/uno/Reference.h>
36#include <com/sun/star/text/XTextContent.hpp>
37#include <com/sun/star/beans/XPropertySet.hpp>
38#include <com/sun/star/lang/XMultiServiceFactory.hpp>
39#include <com/sun/star/text/ControlCharacter.hpp>
40
41
42using ::com::sun::star::beans::XPropertySet;
43using ::com::sun::star::uno::Reference;
44using ::com::sun::star::xml::sax::XFastAttributeList;
45using ::com::sun::star::lang::XMultiServiceFactory;
46using ::com::sun::star::container::XNamed;
47
48using namespace ::com::sun::star::uno;
49using namespace ::com::sun::star::text;
50using namespace ::xmloff::token;
51
52
53// section import: This one is fairly tricky due to a variety of
54// limits of the core or the API. The main problem is that if you
55// insert a section within another section, you can't move the cursor
56// between the ends of the inner and the enclosing section. To avoid
57// these problems, additional markers are first inserted and later deleted.
59: SvXMLImportContext(rImport)
60, bProtect(false)
61, bCondOK(false)
62, bIsVisible(true)
63, bValid(false)
64, bSequenceOK(false)
65, bIsCurrentlyVisible(true)
66, bIsCurrentlyVisibleOK(false)
67, bHasContent(false)
68{
69}
70
72{
73}
74
76 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
77{
78 // process attributes
79 ProcessAttributes(xAttrList);
80
81 // process index headers:
82 bool bIsIndexHeader = (nElement & TOKEN_MASK) == XML_INDEX_TITLE;
83 if (bIsIndexHeader)
84 {
85 bValid = true;
86 }
87
88 rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
89
90 // valid?
91 if (!bValid)
92 return;
93
94 // create text section (as XPropertySet)
95 Reference<XMultiServiceFactory> xFactory(
96 GetImport().GetModel(),UNO_QUERY);
97 if (!xFactory.is())
98 return;
99
100 Reference<XInterface> xIfc =
101 xFactory->createInstance( bIsIndexHeader ? OUString("com.sun.star.text.IndexHeaderSection")
102 : OUString("com.sun.star.text.TextSection") );
103 if (!xIfc.is())
104 return;
105
106 Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
107
108 // save PropertySet (for CreateChildContext)
109 xSectionPropertySet = xPropSet;
110
111 // name
112 Reference<XNamed> xNamed(xPropSet, UNO_QUERY);
113 xNamed->setName(sName);
114
115 // stylename?
116 if (!sStyleName.isEmpty())
117 {
118 XMLPropStyleContext* pStyle = rHelper->
119 FindSectionStyle(sStyleName);
120
121 if (pStyle != nullptr)
122 {
123 pStyle->FillPropertySet( xPropSet );
124 }
125 }
126
127 // IsVisible and condition (not for index headers)
128 if (! bIsIndexHeader)
129 {
130 xPropSet->setPropertyValue( "IsVisible", Any(bIsVisible) );
131
132 // #97450# hidden sections must be hidden on reload
133 // For backwards compatibility, set flag only if it is
134 // present
136 {
137 xPropSet->setPropertyValue( "IsCurrentlyVisible", Any(bIsCurrentlyVisible));
138 }
139
140 if (bCondOK)
141 {
142 xPropSet->setPropertyValue( "Condition", Any(sCond) );
143 }
144 }
145
146 // password (only for regular sections)
147 if ( bSequenceOK &&
148 (nElement & TOKEN_MASK) == XML_SECTION )
149 {
150 xPropSet->setPropertyValue("ProtectionKey", Any(aSequence));
151 }
152
153 // protection
154 xPropSet->setPropertyValue( "IsProtected", Any(bProtect) );
155
156 // insert marker, <paragraph>, marker; then insert
157 // section over the first marker character, and delete the
158 // last paragraph (and marker) when closing a section.
159 Reference<XTextRange> xStart =
160 rHelper->GetCursor()->getStart();
161#ifndef DBG_UTIL
162 OUString sMarkerString(" ");
163#else
164 OUString sMarkerString("X");
165#endif
166 rHelper->InsertString(sMarkerString);
167 rHelper->InsertControlCharacter(
168 ControlCharacter::APPEND_PARAGRAPH );
169 rHelper->InsertString(sMarkerString);
170
171 // select first marker
172 rHelper->GetCursor()->gotoRange(xStart, false);
173 rHelper->GetCursor()->goRight(1, true);
174
175 // convert section to XTextContent
176 Reference<XTextContent> xTextContent(xSectionPropertySet,
177 UNO_QUERY);
178
179 // and insert (over marker)
180 rHelper->GetText()->insertTextContent(
181 rHelper->GetCursorAsRange(), xTextContent, true );
182
183 // and delete first marker (in section)
184 rHelper->GetText()->insertString(
185 rHelper->GetCursorAsRange(), "", true);
186
187 // finally, check for redlines that should start at
188 // the section start node
189 rHelper->RedlineAdjustStartNodeCursor(); // start ???
190
191 // xml:id for RDF metadata
192 GetImport().SetXmlId(xIfc, sXmlId);
193}
194
196 const Reference<XFastAttributeList> & xAttrList )
197{
198 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
199 {
200 switch (aIter.getToken())
201 {
202 case XML_ELEMENT(XML, XML_ID):
203 sXmlId = aIter.toString();
204 break;
206 sStyleName = aIter.toString();
207 break;
209 sName = aIter.toString();
210 bValid = true;
211 break;
213 {
214 OUString sValue = aIter.toString();
215 OUString sTmp;
216 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
217 GetKeyByAttrValueQName(sValue, &sTmp);
218 if( XML_NAMESPACE_OOOW == nPrefix )
219 {
220 sCond = sTmp;
221 bCondOK = true;
222 }
223 else
224 sCond = sValue;
225 }
226 break;
228 if (IsXMLToken(aIter, XML_TRUE))
229 {
230 bIsVisible = true;
231 }
232 else if ( IsXMLToken(aIter, XML_NONE) ||
233 IsXMLToken(aIter, XML_CONDITION) )
234 {
235 bIsVisible = false;
236 }
237 // else: ignore
238 break;
240 {
241 bool bTmp(false);
242 if (::sax::Converter::convertBool(bTmp, aIter.toView()))
243 {
244 bIsCurrentlyVisible = !bTmp;
246 }
247 }
248 break;
250 ::comphelper::Base64::decode(aSequence, aIter.toString());
251 bSequenceOK = true;
252 break;
254 // compatibility with SRC629 (or earlier) versions
256 {
257 bool bTmp(false);
258 if (::sax::Converter::convertBool(bTmp, aIter.toView()))
259 {
260 bProtect = bTmp;
261 }
262 break;
263 }
264 default:
265 // ignore
266 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
267 break;
268 }
269 }
270}
271
273{
274 // get rid of last paragraph
275 // (unless it's the only paragraph in the section)
276 rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
277 rHelper->GetCursor()->goRight(1, false);
278 if (bHasContent)
279 {
280 rHelper->GetCursor()->goLeft(1, true);
281 rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
282 "", true);
283 }
284
285 // and delete second marker
286 rHelper->GetCursor()->goRight(1, true);
287 rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
288 "", true);
289
290 // check for redlines to our endnode
291 rHelper->RedlineAdjustStartNodeCursor();
292}
293
294css::uno::Reference< css::xml::sax::XFastContextHandler > XMLSectionImportContext::createFastChildContext(
295 sal_Int32 nElement,
296 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
297{
298 // section-source (-dde) elements
299 if ( nElement == XML_ELEMENT(TEXT, XML_SECTION_SOURCE) )
300 {
303 }
304 else if ( nElement == XML_ELEMENT(OFFICE, XML_DDE_SOURCE) )
305 {
308 }
309 else
310 {
311 // otherwise: text context
312 auto pContext = GetImport().GetTextImport()->CreateTextChildContext(
313 GetImport(), nElement, xAttrList, XMLTextType::Section );
314
315 // if that fails, default context
316 if (pContext)
317 bHasContent = true;
318 else
319 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
320 return pContext;
321 }
322}
323
324/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: prstylei.cxx:222
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
css::uno::Reference< css::beans::XPropertySet > xSectionPropertySet
TextSection (as XPropertySet) for passing down to data source elements.
XMLSectionImportContext(SvXMLImport &rImport)
css::uno::Sequence< sal_Int8 > aSequence
virtual ~XMLSectionImportContext() override
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
void ProcessAttributes(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
static void decode(css::uno::Sequence< sal_Int8 > &aPass, std::u16string_view sBuffer)
static bool convertBool(bool &rBool, std::u16string_view rString)
Reference< XSingleServiceFactory > xFactory
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3597
TEXT
@ Section
Definition: txtimp.hxx:75
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:120
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
constexpr sal_Int32 TOKEN_MASK
Definition: xmlimp.hxx:94
constexpr sal_uInt16 XML_NAMESPACE_OOOW