nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / text / XMLSectionImportContext.cxx
blobc4ca75ba06d680e19e51da7f721eb2cb224e7c18
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 "XMLSectionImportContext.hxx"
21 #include "XMLSectionSourceImportContext.hxx"
22 #include "XMLSectionSourceDDEImportContext.hxx"
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>
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;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::text;
49 using namespace ::xmloff::token;
51 namespace {
53 enum XMLSectionToken
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
67 const SvXMLTokenMapEntry aSectionTokenMap[] =
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 },
79 XML_TOKEN_MAP_END
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.
88 XMLSectionImportContext::XMLSectionImportContext(
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)
104 XMLSectionImportContext::~XMLSectionImportContext()
108 void XMLSectionImportContext::StartElement(
109 const Reference<XAttributeList> & xAttrList)
111 // process attributes
112 ProcessAttributes(xAttrList);
114 // process index headers:
115 bool bIsIndexHeader = IsXMLToken( GetLocalName(), XML_INDEX_TITLE );
116 if (bIsIndexHeader)
118 bValid = true;
121 rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
123 // valid?
124 if (!bValid)
125 return;
127 // create text section (as XPropertySet)
128 Reference<XMultiServiceFactory> xFactory(
129 GetImport().GetModel(),UNO_QUERY);
130 if (!xFactory.is())
131 return;
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;
139 Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
141 // save PropertySet (for CreateChildContext)
142 xSectionPropertySet = xPropSet;
144 // name
145 Reference<XNamed> xNamed(xPropSet, UNO_QUERY);
146 xNamed->setName(sName);
148 // stylename?
149 if (!sStyleName.isEmpty())
151 XMLPropStyleContext* pStyle = rHelper->
152 FindSectionStyle(sStyleName);
154 if (pStyle != nullptr)
156 pStyle->FillPropertySet( xPropSet );
160 // IsVisible and condition (not for index headers)
161 if (! bIsIndexHeader)
163 xPropSet->setPropertyValue( "IsVisible", Any(bIsVisible) );
165 // #97450# hidden sections must be hidden on reload
166 // For backwards compatibility, set flag only if it is
167 // present
168 if( bIsCurrentlyVisibleOK )
170 xPropSet->setPropertyValue( "IsCurrentlyVisible", Any(bIsCurrentlyVisible));
173 if (bCondOK)
175 xPropSet->setPropertyValue( "Condition", Any(sCond) );
179 // password (only for regular sections)
180 if ( bSequenceOK &&
181 IsXMLToken(GetLocalName(), XML_SECTION) )
183 xPropSet->setPropertyValue("ProtectionKey", Any(aSequence));
186 // protection
187 xPropSet->setPropertyValue( "IsProtected", Any(bProtect) );
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);
204 // select first marker
205 rHelper->GetCursor()->gotoRange(xStart, false);
206 rHelper->GetCursor()->goRight(1, true);
208 // convert section to XTextContent
209 Reference<XTextContent> xTextContent(xSectionPropertySet,
210 UNO_QUERY);
212 // and insert (over marker)
213 rHelper->GetText()->insertTextContent(
214 rHelper->GetCursorAsRange(), xTextContent, true );
216 // and delete first marker (in section)
217 rHelper->GetText()->insertString(
218 rHelper->GetCursorAsRange(), "", true);
220 // finally, check for redlines that should start at
221 // the section start node
222 rHelper->RedlineAdjustStartNodeCursor(); // start ???
224 // xml:id for RDF metadata
225 GetImport().SetXmlId(xIfc, sXmlId);
228 void XMLSectionImportContext::ProcessAttributes(
229 const Reference<XAttributeList> & xAttrList )
231 static const SvXMLTokenMap aTokenMap(aSectionTokenMap);
233 sal_Int16 nLength = xAttrList->getLength();
234 for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
236 OUString sLocalName;
237 sal_uInt16 nNamePrefix = GetImport().GetNamespaceMap().
238 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
239 &sLocalName );
240 OUString sAttr = xAttrList->getValueByIndex(nAttr);
242 switch (aTokenMap.Get(nNamePrefix, sLocalName))
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:
256 OUString sTmp;
257 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
258 GetKeyByAttrValueQName(sAttr, &sTmp);
259 if( XML_NAMESPACE_OOOW == nPrefix )
261 sCond = sTmp;
262 bCondOK = true;
264 else
265 sCond = sAttr;
267 break;
268 case XML_TOK_SECTION_DISPLAY:
269 if (IsXMLToken(sAttr, XML_TRUE))
271 bIsVisible = true;
273 else if ( IsXMLToken(sAttr, XML_NONE) ||
274 IsXMLToken(sAttr, XML_CONDITION) )
276 bIsVisible = false;
278 // else: ignore
279 break;
280 case XML_TOK_SECTION_IS_HIDDEN:
282 bool bTmp(false);
283 if (::sax::Converter::convertBool(bTmp, sAttr))
285 bIsCurrentlyVisible = !bTmp;
286 bIsCurrentlyVisibleOK = true;
289 break;
290 case XML_TOK_SECTION_PROTECTION_KEY:
291 ::comphelper::Base64::decode(aSequence, sAttr);
292 bSequenceOK = true;
293 break;
294 case XML_TOK_SECTION_PROTECT:
296 bool bTmp(false);
297 if (::sax::Converter::convertBool(bTmp, sAttr))
299 bProtect = bTmp;
301 break;
303 default:
304 ; // ignore
305 break;
310 void XMLSectionImportContext::endFastElement(sal_Int32 )
312 // get rid of last paragraph
313 // (unless it's the only paragraph in the section)
314 rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
315 rHelper->GetCursor()->goRight(1, false);
316 if (bHasContent)
318 rHelper->GetCursor()->goLeft(1, true);
319 rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
320 "", true);
323 // and delete second marker
324 rHelper->GetCursor()->goRight(1, true);
325 rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
326 "", true);
328 // check for redlines to our endnode
329 rHelper->RedlineAdjustStartNodeCursor();
332 SvXMLImportContextRef XMLSectionImportContext::CreateChildContext(
333 sal_uInt16 nPrefix,
334 const OUString& rLocalName,
335 const Reference<XAttributeList> & xAttrList )
337 SvXMLImportContext* pContext = nullptr;
339 // section-source (-dde) elements
340 if ( (XML_NAMESPACE_TEXT == nPrefix) &&
341 IsXMLToken(rLocalName, XML_SECTION_SOURCE) )
343 pContext = new XMLSectionSourceImportContext(GetImport(),
344 nPrefix, rLocalName,
345 xSectionPropertySet);
347 else if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
348 IsXMLToken(rLocalName, XML_DDE_SOURCE) )
350 pContext = new XMLSectionSourceDDEImportContext(GetImport(),
351 nPrefix, rLocalName,
352 xSectionPropertySet);
354 else
356 // otherwise: text context
357 pContext = GetImport().GetTextImport()->CreateTextChildContext(
358 GetImport(), nPrefix, rLocalName, xAttrList,
359 XMLTextType::Section );
361 // if that fails, default context
362 if (pContext)
363 bHasContent = true;
366 return pContext;
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */