bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / text / XMLSectionImportContext.cxx
blob2574fce93dc5c09df6b39904762c4a2644445228
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 <xmloff/xmlictxt.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include <xmloff/txtimp.hxx>
26 #include <xmloff/nmspmap.hxx>
27 #include <xmloff/xmlnmspe.hxx>
28 #include <xmloff/xmltoken.hxx>
29 #include <xmloff/prstylei.hxx>
30 #include <sax/tools/converter.hxx>
31 #include <com/sun/star/container/XNamed.hpp>
32 #include <com/sun/star/uno/Reference.h>
33 #include <com/sun/star/text/XTextContent.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/text/ControlCharacter.hpp>
39 using ::com::sun::star::beans::XPropertySet;
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::xml::sax::XAttributeList;
42 using ::com::sun::star::lang::XMultiServiceFactory;
43 using ::com::sun::star::container::XNamed;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::text;
47 using namespace ::xmloff::token;
50 TYPEINIT1( XMLSectionImportContext, SvXMLImportContext );
52 const sal_Char sAPI_TextSection[] = "com.sun.star.text.TextSection";
53 const sal_Char sAPI_IndexHeaderSection[] = "com.sun.star.text.IndexHeaderSection";
54 const sal_Char sAPI_IsProtected[] = "IsProtected";
55 const sal_Char sAPI_Condition[] = "Condition";
56 const sal_Char sAPI_IsVisible[] = "IsVisible";
57 const sal_Char sAPI_IsCurrentlyVisible[] = "IsCurrentlyVisible";
58 const sal_Char sAPI_ProtectionKey[] = "ProtectionKey";
60 enum XMLSectionToken
62 XML_TOK_SECTION_XMLID,
63 XML_TOK_SECTION_STYLE_NAME,
64 XML_TOK_SECTION_NAME,
65 XML_TOK_SECTION_CONDITION,
66 XML_TOK_SECTION_DISPLAY,
67 XML_TOK_SECTION_PROTECT,
68 XML_TOK_SECTION_PROTECTION_KEY,
69 XML_TOK_SECTION_IS_HIDDEN
72 static const SvXMLTokenMapEntry aSectionTokenMap[] =
74 { XML_NAMESPACE_XML , XML_ID, XML_TOK_SECTION_XMLID },
75 { XML_NAMESPACE_TEXT, XML_STYLE_NAME, XML_TOK_SECTION_STYLE_NAME },
76 { XML_NAMESPACE_TEXT, XML_NAME, XML_TOK_SECTION_NAME },
77 { XML_NAMESPACE_TEXT, XML_CONDITION, XML_TOK_SECTION_CONDITION },
78 { XML_NAMESPACE_TEXT, XML_DISPLAY, XML_TOK_SECTION_DISPLAY },
79 { XML_NAMESPACE_TEXT, XML_PROTECTED, XML_TOK_SECTION_PROTECT },
80 { XML_NAMESPACE_TEXT, XML_PROTECTION_KEY, XML_TOK_SECTION_PROTECTION_KEY},
81 { XML_NAMESPACE_TEXT, XML_IS_HIDDEN, XML_TOK_SECTION_IS_HIDDEN },
82 // compatibility with SRC629 (or earlier) versions
83 { XML_NAMESPACE_TEXT, XML_PROTECT, XML_TOK_SECTION_PROTECT },
84 XML_TOKEN_MAP_END
88 // section import: This one is fairly tricky due to a variety of
89 // limits of the core or the API. The main problem is that if you
90 // insert a section within another section, you can't move the cursor
91 // between the ends of the inner and the enclosing section. To avoid
92 // these problems, additional markers are first inserted and later deleted.
93 XMLSectionImportContext::XMLSectionImportContext(
94 SvXMLImport& rImport,
95 sal_uInt16 nPrfx,
96 const OUString& rLocalName )
97 : SvXMLImportContext(rImport, nPrfx, rLocalName)
98 , sTextSection(sAPI_TextSection)
99 , sIndexHeaderSection(sAPI_IndexHeaderSection)
100 , sCondition(sAPI_Condition)
101 , sIsVisible(sAPI_IsVisible)
102 , sProtectionKey(sAPI_ProtectionKey)
103 , sIsProtected(sAPI_IsProtected)
104 , sIsCurrentlyVisible(sAPI_IsCurrentlyVisible)
105 , bProtect(false)
106 , bCondOK(false)
107 , bIsVisible(true)
108 , bValid(false)
109 , bSequenceOK(false)
110 , bIsCurrentlyVisible(true)
111 , bIsCurrentlyVisibleOK(false)
112 , bHasContent(false)
116 XMLSectionImportContext::~XMLSectionImportContext()
120 void XMLSectionImportContext::StartElement(
121 const Reference<XAttributeList> & xAttrList)
123 // process attributes
124 ProcessAttributes(xAttrList);
126 // process index headers:
127 bool bIsIndexHeader = IsXMLToken( GetLocalName(), XML_INDEX_TITLE );
128 if (bIsIndexHeader)
130 bValid = true;
133 rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
135 // valid?
136 if (bValid)
138 // create text section (as XPropertySet)
139 Reference<XMultiServiceFactory> xFactory(
140 GetImport().GetModel(),UNO_QUERY);
141 if (xFactory.is())
143 Reference<XInterface> xIfc =
144 xFactory->createInstance( bIsIndexHeader ? sIndexHeaderSection
145 : sTextSection );
146 if (xIfc.is())
148 Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
150 // save PropertySet (for CreateChildContext)
151 xSectionPropertySet = xPropSet;
153 // name
154 Reference<XNamed> xNamed(xPropSet, UNO_QUERY);
155 xNamed->setName(sName);
157 // stylename?
158 if (!sStyleName.isEmpty())
160 XMLPropStyleContext* pStyle = rHelper->
161 FindSectionStyle(sStyleName);
163 if (pStyle != NULL)
165 pStyle->FillPropertySet( xPropSet );
169 // IsVisible and condition (not for index headers)
170 if (! bIsIndexHeader)
172 Any aAny;
173 aAny.setValue( &bIsVisible, cppu::UnoType<bool>::get() );
174 xPropSet->setPropertyValue( sIsVisible, aAny );
176 // #97450# hidden sections must be hidden on reload
177 // For backwards compatibility, set flag only if it is
178 // present
179 if( bIsCurrentlyVisibleOK )
181 aAny.setValue( &bIsCurrentlyVisible,
182 cppu::UnoType<bool>::get() );
183 xPropSet->setPropertyValue( sIsCurrentlyVisible, aAny);
186 if (bCondOK)
188 aAny <<= sCond;
189 xPropSet->setPropertyValue( sCondition, aAny );
193 // password (only for regular sections)
194 if ( bSequenceOK &&
195 IsXMLToken(GetLocalName(), XML_SECTION) )
197 Any aAny;
198 aAny <<= aSequence;
199 xPropSet->setPropertyValue(sProtectionKey, aAny);
202 // protection
203 Any aAny;
204 aAny.setValue( &bProtect, cppu::UnoType<bool>::get() );
205 xPropSet->setPropertyValue( sIsProtected, aAny );
207 // insert marker, <paragraph>, marker; then insert
208 // section over the first marker character, and delete the
209 // last paragraph (and marker) when closing a section.
210 Reference<XTextRange> xStart =
211 rHelper->GetCursor()->getStart();
212 #ifndef DBG_UTIL
213 static const sal_Char sMarker[] = " ";
214 #else
215 static const sal_Char sMarker[] = "X";
216 #endif
217 OUString sMarkerString(sMarker);
218 rHelper->InsertString(sMarkerString);
219 rHelper->InsertControlCharacter(
220 ControlCharacter::APPEND_PARAGRAPH );
221 rHelper->InsertString(sMarkerString);
223 // select first marker
224 rHelper->GetCursor()->gotoRange(xStart, sal_False);
225 rHelper->GetCursor()->goRight(1, sal_True);
227 // convert section to XTextContent
228 Reference<XTextContent> xTextContent(xSectionPropertySet,
229 UNO_QUERY);
231 // and insert (over marker)
232 rHelper->GetText()->insertTextContent(
233 rHelper->GetCursorAsRange(), xTextContent, sal_True );
235 // and delete first marker (in section)
236 rHelper->GetText()->insertString(
237 rHelper->GetCursorAsRange(), sEmpty, sal_True);
239 // finally, check for redlines that should start at
240 // the section start node
241 rHelper->RedlineAdjustStartNodeCursor(true); // start ???
243 // xml:id for RDF metadata
244 GetImport().SetXmlId(xIfc, sXmlId);
250 void XMLSectionImportContext::ProcessAttributes(
251 const Reference<XAttributeList> & xAttrList )
253 SvXMLTokenMap aTokenMap(aSectionTokenMap);
255 sal_Int16 nLength = xAttrList->getLength();
256 for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
258 OUString sLocalName;
259 sal_uInt16 nNamePrefix = GetImport().GetNamespaceMap().
260 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
261 &sLocalName );
262 OUString sAttr = xAttrList->getValueByIndex(nAttr);
264 switch (aTokenMap.Get(nNamePrefix, sLocalName))
266 case XML_TOK_SECTION_XMLID:
267 sXmlId = sAttr;
268 break;
269 case XML_TOK_SECTION_STYLE_NAME:
270 sStyleName = sAttr;
271 break;
272 case XML_TOK_SECTION_NAME:
273 sName = sAttr;
274 bValid = true;
275 break;
276 case XML_TOK_SECTION_CONDITION:
278 OUString sTmp;
279 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
280 _GetKeyByAttrName( sAttr, &sTmp, false );
281 if( XML_NAMESPACE_OOOW == nPrefix )
283 sCond = sTmp;
284 bCondOK = true;
286 else
287 sCond = sAttr;
289 break;
290 case XML_TOK_SECTION_DISPLAY:
291 if (IsXMLToken(sAttr, XML_TRUE))
293 bIsVisible = true;
295 else if ( IsXMLToken(sAttr, XML_NONE) ||
296 IsXMLToken(sAttr, XML_CONDITION) )
298 bIsVisible = false;
300 // else: ignore
301 break;
302 case XML_TOK_SECTION_IS_HIDDEN:
304 bool bTmp(false);
305 if (::sax::Converter::convertBool(bTmp, sAttr))
307 bIsCurrentlyVisible = !bTmp;
308 bIsCurrentlyVisibleOK = true;
311 break;
312 case XML_TOK_SECTION_PROTECTION_KEY:
313 ::sax::Converter::decodeBase64(aSequence, sAttr);
314 bSequenceOK = true;
315 break;
316 case XML_TOK_SECTION_PROTECT:
318 bool bTmp(false);
319 if (::sax::Converter::convertBool(bTmp, sAttr))
321 bProtect = bTmp;
323 break;
325 default:
326 ; // ignore
327 break;
332 void XMLSectionImportContext::EndElement()
334 // get rid of last paragraph
335 // (unless it's the only paragraph in the section)
336 rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
337 rHelper->GetCursor()->goRight(1, sal_False);
338 if (bHasContent)
340 rHelper->GetCursor()->goLeft(1, sal_True);
341 rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
342 sEmpty, sal_True);
345 // and delete second marker
346 rHelper->GetCursor()->goRight(1, sal_True);
347 rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
348 sEmpty, sal_True);
350 // check for redlines to our endnode
351 rHelper->RedlineAdjustStartNodeCursor(false);
354 SvXMLImportContext* XMLSectionImportContext::CreateChildContext(
355 sal_uInt16 nPrefix,
356 const OUString& rLocalName,
357 const Reference<XAttributeList> & xAttrList )
359 SvXMLImportContext* pContext = NULL;
361 // section-source (-dde) elements
362 if ( (XML_NAMESPACE_TEXT == nPrefix) &&
363 IsXMLToken(rLocalName, XML_SECTION_SOURCE) )
365 pContext = new XMLSectionSourceImportContext(GetImport(),
366 nPrefix, rLocalName,
367 xSectionPropertySet);
369 else if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
370 IsXMLToken(rLocalName, XML_DDE_SOURCE) )
372 pContext = new XMLSectionSourceDDEImportContext(GetImport(),
373 nPrefix, rLocalName,
374 xSectionPropertySet);
376 else
378 // otherwise: text context
379 pContext = GetImport().GetTextImport()->CreateTextChildContext(
380 GetImport(), nPrefix, rLocalName, xAttrList,
381 XML_TEXT_TYPE_SECTION );
383 // if that fails, default context
384 if (NULL == pContext)
386 pContext = new SvXMLImportContext( GetImport(),
387 nPrefix, rLocalName );
389 else
390 bHasContent = true;
393 return pContext;
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */