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>
27 #include <xmloff/namespacemap.hxx>
28 #include <xmloff/xmlnamespace.hxx>
29 #include <xmloff/xmltoken.hxx>
30 #include <xmloff/prstylei.hxx>
31 #include <sax/tools/converter.hxx>
32 #include <com/sun/star/container/XNamed.hpp>
33 #include <com/sun/star/frame/XModel.hpp>
34 #include <com/sun/star/uno/Reference.h>
35 #include <com/sun/star/text/XTextContent.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/text/ControlCharacter.hpp>
39 
40 
41 using ::com::sun::star::beans::XPropertySet;
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::xml::sax::XAttributeList;
44 using ::com::sun::star::lang::XMultiServiceFactory;
45 using ::com::sun::star::container::XNamed;
46 
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::text;
49 using namespace ::xmloff::token;
50 
51 namespace {
52 
54 {
55  XML_TOK_SECTION_XMLID,
56  XML_TOK_SECTION_STYLE_NAME,
57  XML_TOK_SECTION_NAME,
58  XML_TOK_SECTION_CONDITION,
59  XML_TOK_SECTION_DISPLAY,
60  XML_TOK_SECTION_PROTECT,
61  XML_TOK_SECTION_PROTECTION_KEY,
62  XML_TOK_SECTION_IS_HIDDEN
63 };
64 
65 }
66 
68 {
69  { XML_NAMESPACE_XML , XML_ID, XML_TOK_SECTION_XMLID },
70  { XML_NAMESPACE_TEXT, XML_STYLE_NAME, XML_TOK_SECTION_STYLE_NAME },
71  { XML_NAMESPACE_TEXT, XML_NAME, XML_TOK_SECTION_NAME },
72  { XML_NAMESPACE_TEXT, XML_CONDITION, XML_TOK_SECTION_CONDITION },
73  { XML_NAMESPACE_TEXT, XML_DISPLAY, XML_TOK_SECTION_DISPLAY },
74  { XML_NAMESPACE_TEXT, XML_PROTECTED, XML_TOK_SECTION_PROTECT },
75  { XML_NAMESPACE_TEXT, XML_PROTECTION_KEY, XML_TOK_SECTION_PROTECTION_KEY},
76  { XML_NAMESPACE_TEXT, XML_IS_HIDDEN, XML_TOK_SECTION_IS_HIDDEN },
77  // compatibility with SRC629 (or earlier) versions
78  { XML_NAMESPACE_TEXT, XML_PROTECT, XML_TOK_SECTION_PROTECT },
80 };
81 
82 
83 // section import: This one is fairly tricky due to a variety of
84 // limits of the core or the API. The main problem is that if you
85 // insert a section within another section, you can't move the cursor
86 // between the ends of the inner and the enclosing section. To avoid
87 // these problems, additional markers are first inserted and later deleted.
89  SvXMLImport& rImport,
90  sal_uInt16 nPrfx,
91  const OUString& rLocalName )
92 : SvXMLImportContext(rImport, nPrfx, rLocalName)
93 , bProtect(false)
94 , bCondOK(false)
95 , bIsVisible(true)
96 , bValid(false)
97 , bSequenceOK(false)
98 , bIsCurrentlyVisible(true)
99 , bIsCurrentlyVisibleOK(false)
100 , bHasContent(false)
101 {
102 }
103 
105 {
106 }
107 
109  const Reference<XAttributeList> & xAttrList)
110 {
111  // process attributes
112  ProcessAttributes(xAttrList);
113 
114  // process index headers:
115  bool bIsIndexHeader = IsXMLToken( GetLocalName(), XML_INDEX_TITLE );
116  if (bIsIndexHeader)
117  {
118  bValid = true;
119  }
120 
122 
123  // valid?
124  if (!bValid)
125  return;
126 
127  // create text section (as XPropertySet)
128  Reference<XMultiServiceFactory> xFactory(
129  GetImport().GetModel(),UNO_QUERY);
130  if (!xFactory.is())
131  return;
132 
133  Reference<XInterface> xIfc =
134  xFactory->createInstance( bIsIndexHeader ? OUString("com.sun.star.text.IndexHeaderSection")
135  : OUString("com.sun.star.text.TextSection") );
136  if (!xIfc.is())
137  return;
138 
139  Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
140 
141  // save PropertySet (for CreateChildContext)
142  xSectionPropertySet = xPropSet;
143 
144  // name
145  Reference<XNamed> xNamed(xPropSet, UNO_QUERY);
146  xNamed->setName(sName);
147 
148  // stylename?
149  if (!sStyleName.isEmpty())
150  {
151  XMLPropStyleContext* pStyle = rHelper->
152  FindSectionStyle(sStyleName);
153 
154  if (pStyle != nullptr)
155  {
156  pStyle->FillPropertySet( xPropSet );
157  }
158  }
159 
160  // IsVisible and condition (not for index headers)
161  if (! bIsIndexHeader)
162  {
163  xPropSet->setPropertyValue( "IsVisible", Any(bIsVisible) );
164 
165  // #97450# hidden sections must be hidden on reload
166  // For backwards compatibility, set flag only if it is
167  // present
169  {
170  xPropSet->setPropertyValue( "IsCurrentlyVisible", Any(bIsCurrentlyVisible));
171  }
172 
173  if (bCondOK)
174  {
175  xPropSet->setPropertyValue( "Condition", Any(sCond) );
176  }
177  }
178 
179  // password (only for regular sections)
180  if ( bSequenceOK &&
182  {
183  xPropSet->setPropertyValue("ProtectionKey", Any(aSequence));
184  }
185 
186  // protection
187  xPropSet->setPropertyValue( "IsProtected", Any(bProtect) );
188 
189  // insert marker, <paragraph>, marker; then insert
190  // section over the first marker character, and delete the
191  // last paragraph (and marker) when closing a section.
192  Reference<XTextRange> xStart =
193  rHelper->GetCursor()->getStart();
194 #ifndef DBG_UTIL
195  OUString sMarkerString(" ");
196 #else
197  OUString sMarkerString("X");
198 #endif
199  rHelper->InsertString(sMarkerString);
200  rHelper->InsertControlCharacter(
201  ControlCharacter::APPEND_PARAGRAPH );
202  rHelper->InsertString(sMarkerString);
203 
204  // select first marker
205  rHelper->GetCursor()->gotoRange(xStart, false);
206  rHelper->GetCursor()->goRight(1, true);
207 
208  // convert section to XTextContent
209  Reference<XTextContent> xTextContent(xSectionPropertySet,
210  UNO_QUERY);
211 
212  // and insert (over marker)
213  rHelper->GetText()->insertTextContent(
214  rHelper->GetCursorAsRange(), xTextContent, true );
215 
216  // and delete first marker (in section)
217  rHelper->GetText()->insertString(
218  rHelper->GetCursorAsRange(), "", true);
219 
220  // finally, check for redlines that should start at
221  // the section start node
222  rHelper->RedlineAdjustStartNodeCursor(); // start ???
223 
224  // xml:id for RDF metadata
225  GetImport().SetXmlId(xIfc, sXmlId);
226 }
227 
229  const Reference<XAttributeList> & xAttrList )
230 {
231  static const SvXMLTokenMap aTokenMap(aSectionTokenMap);
232 
233  sal_Int16 nLength = xAttrList->getLength();
234  for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
235  {
236  OUString sLocalName;
237  sal_uInt16 nNamePrefix = GetImport().GetNamespaceMap().
238  GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
239  &sLocalName );
240  OUString sAttr = xAttrList->getValueByIndex(nAttr);
241 
242  switch (aTokenMap.Get(nNamePrefix, sLocalName))
243  {
244  case XML_TOK_SECTION_XMLID:
245  sXmlId = sAttr;
246  break;
247  case XML_TOK_SECTION_STYLE_NAME:
248  sStyleName = sAttr;
249  break;
250  case XML_TOK_SECTION_NAME:
251  sName = sAttr;
252  bValid = true;
253  break;
254  case XML_TOK_SECTION_CONDITION:
255  {
256  OUString sTmp;
257  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
258  GetKeyByAttrValueQName(sAttr, &sTmp);
259  if( XML_NAMESPACE_OOOW == nPrefix )
260  {
261  sCond = sTmp;
262  bCondOK = true;
263  }
264  else
265  sCond = sAttr;
266  }
267  break;
268  case XML_TOK_SECTION_DISPLAY:
269  if (IsXMLToken(sAttr, XML_TRUE))
270  {
271  bIsVisible = true;
272  }
273  else if ( IsXMLToken(sAttr, XML_NONE) ||
274  IsXMLToken(sAttr, XML_CONDITION) )
275  {
276  bIsVisible = false;
277  }
278  // else: ignore
279  break;
280  case XML_TOK_SECTION_IS_HIDDEN:
281  {
282  bool bTmp(false);
283  if (::sax::Converter::convertBool(bTmp, sAttr))
284  {
285  bIsCurrentlyVisible = !bTmp;
286  bIsCurrentlyVisibleOK = true;
287  }
288  }
289  break;
290  case XML_TOK_SECTION_PROTECTION_KEY:
292  bSequenceOK = true;
293  break;
294  case XML_TOK_SECTION_PROTECT:
295  {
296  bool bTmp(false);
297  if (::sax::Converter::convertBool(bTmp, sAttr))
298  {
299  bProtect = bTmp;
300  }
301  break;
302  }
303  default:
304  ; // ignore
305  break;
306  }
307  }
308 }
309 
311 {
312  // get rid of last paragraph
313  // (unless it's the only paragraph in the section)
315  rHelper->GetCursor()->goRight(1, false);
316  if (bHasContent)
317  {
318  rHelper->GetCursor()->goLeft(1, true);
319  rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
320  "", true);
321  }
322 
323  // and delete second marker
324  rHelper->GetCursor()->goRight(1, true);
325  rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
326  "", true);
327 
328  // check for redlines to our endnode
329  rHelper->RedlineAdjustStartNodeCursor();
330 }
331 
333  sal_uInt16 nPrefix,
334  const OUString& rLocalName,
335  const Reference<XAttributeList> & xAttrList )
336 {
337  SvXMLImportContext* pContext = nullptr;
338 
339  // section-source (-dde) elements
340  if ( (XML_NAMESPACE_TEXT == nPrefix) &&
341  IsXMLToken(rLocalName, XML_SECTION_SOURCE) )
342  {
343  pContext = new XMLSectionSourceImportContext(GetImport(),
344  nPrefix, rLocalName,
346  }
347  else if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
348  IsXMLToken(rLocalName, XML_DDE_SOURCE) )
349  {
351  nPrefix, rLocalName,
353  }
354  else
355  {
356  // otherwise: text context
357  pContext = GetImport().GetTextImport()->CreateTextChildContext(
358  GetImport(), nPrefix, rLocalName, xAttrList,
360 
361  // if that fails, default context
362  if (pContext)
363  bHasContent = true;
364  }
365 
366  return pContext;
367 }
368 
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
XMLTokenEnum const aTokenMap[]
Definition: Oasis2OOo.cxx:1145
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
static bool convertBool(bool &rBool, const OUString &rString)
css::uno::Sequence< sal_Int8 > aSequence
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:394
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:592
#define XML_TOKEN_MAP_END
Definition: xmltkmap.hxx:33
const OUString & GetLocalName() const
Definition: xmlictxt.hxx:69
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
StartElement is called after a context has been constructed and before an elements context is parsed...
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...
constexpr sal_uInt16 XML_NAMESPACE_XML
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: prstylei.cxx:223
constexpr sal_uInt16 XML_NAMESPACE_TEXT
static void decode(css::uno::Sequence< sal_Int8 > &aPass, const OUString &sBuffer)
XMLSectionImportContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLocalName)
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
Definition: xmltkmap.cxx:99
css::uno::Reference< css::beans::XPropertySet > xSectionPropertySet
TextSection (as XPropertySet) for passing down to data source elements.
void SetXmlId(css::uno::Reference< css::uno::XInterface > const &i_xIfc, OUString const &i_rXmlId)
set the XmlId attribute of given UNO object (for RDF metadata)
Definition: xmlimp.cxx:1939
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
Handling of tokens in XML:
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
void ProcessAttributes(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList)
const SvXMLTokenMapEntry aSectionTokenMap[]
Reference< XSingleServiceFactory > xFactory
sal_Int32 nLength
Definition: xmltoken.cxx:36
constexpr sal_uInt16 XML_NAMESPACE_OOOW
virtual ~XMLSectionImportContext() override