1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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";
62 XML_TOK_SECTION_XMLID
,
63 XML_TOK_SECTION_STYLE_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
},
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(
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
)
110 , bIsCurrentlyVisible(true)
111 , bIsCurrentlyVisibleOK(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
);
133 rtl::Reference
<XMLTextImportHelper
> rHelper
= GetImport().GetTextImport();
138 // create text section (as XPropertySet)
139 Reference
<XMultiServiceFactory
> xFactory(
140 GetImport().GetModel(),UNO_QUERY
);
143 Reference
<XInterface
> xIfc
=
144 xFactory
->createInstance( bIsIndexHeader
? sIndexHeaderSection
148 Reference
<XPropertySet
> xPropSet(xIfc
, UNO_QUERY
);
150 // save PropertySet (for CreateChildContext)
151 xSectionPropertySet
= xPropSet
;
154 Reference
<XNamed
> xNamed(xPropSet
, UNO_QUERY
);
155 xNamed
->setName(sName
);
158 if (!sStyleName
.isEmpty())
160 XMLPropStyleContext
* pStyle
= rHelper
->
161 FindSectionStyle(sStyleName
);
165 pStyle
->FillPropertySet( xPropSet
);
169 // IsVisible and condition (not for index headers)
170 if (! bIsIndexHeader
)
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
179 if( bIsCurrentlyVisibleOK
)
181 aAny
.setValue( &bIsCurrentlyVisible
,
182 cppu::UnoType
<bool>::get() );
183 xPropSet
->setPropertyValue( sIsCurrentlyVisible
, aAny
);
189 xPropSet
->setPropertyValue( sCondition
, aAny
);
193 // password (only for regular sections)
195 IsXMLToken(GetLocalName(), XML_SECTION
) )
199 xPropSet
->setPropertyValue(sProtectionKey
, 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();
213 static const sal_Char sMarker
[] = " ";
215 static const sal_Char sMarker
[] = "X";
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
,
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
++)
259 sal_uInt16 nNamePrefix
= GetImport().GetNamespaceMap().
260 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
262 OUString sAttr
= xAttrList
->getValueByIndex(nAttr
);
264 switch (aTokenMap
.Get(nNamePrefix
, sLocalName
))
266 case XML_TOK_SECTION_XMLID
:
269 case XML_TOK_SECTION_STYLE_NAME
:
272 case XML_TOK_SECTION_NAME
:
276 case XML_TOK_SECTION_CONDITION
:
279 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
280 _GetKeyByAttrName( sAttr
, &sTmp
, false );
281 if( XML_NAMESPACE_OOOW
== nPrefix
)
290 case XML_TOK_SECTION_DISPLAY
:
291 if (IsXMLToken(sAttr
, XML_TRUE
))
295 else if ( IsXMLToken(sAttr
, XML_NONE
) ||
296 IsXMLToken(sAttr
, XML_CONDITION
) )
302 case XML_TOK_SECTION_IS_HIDDEN
:
305 if (::sax::Converter::convertBool(bTmp
, sAttr
))
307 bIsCurrentlyVisible
= !bTmp
;
308 bIsCurrentlyVisibleOK
= true;
312 case XML_TOK_SECTION_PROTECTION_KEY
:
313 ::sax::Converter::decodeBase64(aSequence
, sAttr
);
316 case XML_TOK_SECTION_PROTECT
:
319 if (::sax::Converter::convertBool(bTmp
, sAttr
))
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
);
340 rHelper
->GetCursor()->goLeft(1, sal_True
);
341 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
345 // and delete second marker
346 rHelper
->GetCursor()->goRight(1, sal_True
);
347 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
350 // check for redlines to our endnode
351 rHelper
->RedlineAdjustStartNodeCursor(false);
354 SvXMLImportContext
* XMLSectionImportContext::CreateChildContext(
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(),
367 xSectionPropertySet
);
369 else if ( (XML_NAMESPACE_OFFICE
== nPrefix
) &&
370 IsXMLToken(rLocalName
, XML_DDE_SOURCE
) )
372 pContext
= new XMLSectionSourceDDEImportContext(GetImport(),
374 xSectionPropertySet
);
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
);
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */