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 <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
;
55 XML_TOK_SECTION_XMLID
,
56 XML_TOK_SECTION_STYLE_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
},
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(
91 const OUString
& rLocalName
)
92 : SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
98 , bIsCurrentlyVisible(true)
99 , bIsCurrentlyVisibleOK(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
);
121 rtl::Reference
<XMLTextImportHelper
> rHelper
= GetImport().GetTextImport();
127 // create text section (as XPropertySet)
128 Reference
<XMultiServiceFactory
> xFactory(
129 GetImport().GetModel(),UNO_QUERY
);
133 Reference
<XInterface
> xIfc
=
134 xFactory
->createInstance( bIsIndexHeader
? OUString("com.sun.star.text.IndexHeaderSection")
135 : OUString("com.sun.star.text.TextSection") );
139 Reference
<XPropertySet
> xPropSet(xIfc
, UNO_QUERY
);
141 // save PropertySet (for CreateChildContext)
142 xSectionPropertySet
= xPropSet
;
145 Reference
<XNamed
> xNamed(xPropSet
, UNO_QUERY
);
146 xNamed
->setName(sName
);
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
168 if( bIsCurrentlyVisibleOK
)
170 xPropSet
->setPropertyValue( "IsCurrentlyVisible", Any(bIsCurrentlyVisible
));
175 xPropSet
->setPropertyValue( "Condition", Any(sCond
) );
179 // password (only for regular sections)
181 IsXMLToken(GetLocalName(), XML_SECTION
) )
183 xPropSet
->setPropertyValue("ProtectionKey", Any(aSequence
));
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();
195 OUString
sMarkerString(" ");
197 OUString
sMarkerString("X");
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
,
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
++)
237 sal_uInt16 nNamePrefix
= GetImport().GetNamespaceMap().
238 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
240 OUString sAttr
= xAttrList
->getValueByIndex(nAttr
);
242 switch (aTokenMap
.Get(nNamePrefix
, sLocalName
))
244 case XML_TOK_SECTION_XMLID
:
247 case XML_TOK_SECTION_STYLE_NAME
:
250 case XML_TOK_SECTION_NAME
:
254 case XML_TOK_SECTION_CONDITION
:
257 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
258 GetKeyByAttrValueQName(sAttr
, &sTmp
);
259 if( XML_NAMESPACE_OOOW
== nPrefix
)
268 case XML_TOK_SECTION_DISPLAY
:
269 if (IsXMLToken(sAttr
, XML_TRUE
))
273 else if ( IsXMLToken(sAttr
, XML_NONE
) ||
274 IsXMLToken(sAttr
, XML_CONDITION
) )
280 case XML_TOK_SECTION_IS_HIDDEN
:
283 if (::sax::Converter::convertBool(bTmp
, sAttr
))
285 bIsCurrentlyVisible
= !bTmp
;
286 bIsCurrentlyVisibleOK
= true;
290 case XML_TOK_SECTION_PROTECTION_KEY
:
291 ::comphelper::Base64::decode(aSequence
, sAttr
);
294 case XML_TOK_SECTION_PROTECT
:
297 if (::sax::Converter::convertBool(bTmp
, sAttr
))
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);
318 rHelper
->GetCursor()->goLeft(1, true);
319 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
323 // and delete second marker
324 rHelper
->GetCursor()->goRight(1, true);
325 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
328 // check for redlines to our endnode
329 rHelper
->RedlineAdjustStartNodeCursor();
332 SvXMLImportContextRef
XMLSectionImportContext::CreateChildContext(
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(),
345 xSectionPropertySet
);
347 else if ( (XML_NAMESPACE_OFFICE
== nPrefix
) &&
348 IsXMLToken(rLocalName
, XML_DDE_SOURCE
) )
350 pContext
= new XMLSectionSourceDDEImportContext(GetImport(),
352 xSectionPropertySet
);
356 // otherwise: text context
357 pContext
= GetImport().GetTextImport()->CreateTextChildContext(
358 GetImport(), nPrefix
, rLocalName
, xAttrList
,
359 XMLTextType::Section
);
361 // if that fails, default context
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */