1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLSectionImportContext.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include "XMLSectionImportContext.hxx"
34 #include "XMLSectionSourceImportContext.hxx"
35 #include "XMLSectionSourceDDEImportContext.hxx"
36 #include <xmloff/xmlictxt.hxx>
37 #include <xmloff/xmlimp.hxx>
38 #include <xmloff/txtimp.hxx>
39 #include <xmloff/nmspmap.hxx>
40 #include "xmlnmspe.hxx"
41 #include <xmloff/xmltoken.hxx>
42 #include <xmloff/xmluconv.hxx>
43 #include <xmloff/prstylei.hxx>
44 #include <com/sun/star/container/XNamed.hpp>
45 #include <com/sun/star/uno/Reference.h>
46 #include <com/sun/star/text/XTextContent.hpp>
47 #include <com/sun/star/beans/XPropertySet.hpp>
48 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
49 #include <com/sun/star/text/ControlCharacter.hpp>
52 using ::rtl::OUString
;
53 using ::com::sun::star::beans::XPropertySet
;
54 using ::com::sun::star::uno::Reference
;
55 using ::com::sun::star::xml::sax::XAttributeList
;
56 using ::com::sun::star::lang::XMultiServiceFactory
;
57 using ::com::sun::star::container::XNamed
;
59 using namespace ::com::sun::star::uno
;
60 using namespace ::com::sun::star::text
;
61 using namespace ::xmloff::token
;
64 TYPEINIT1( XMLSectionImportContext
, SvXMLImportContext
);
66 const sal_Char sAPI_TextSection
[] = "com.sun.star.text.TextSection";
67 const sal_Char sAPI_IndexHeaderSection
[] = "com.sun.star.text.IndexHeaderSection";
68 const sal_Char sAPI_IsProtected
[] = "IsProtected";
69 const sal_Char sAPI_Condition
[] = "Condition";
70 const sal_Char sAPI_IsVisible
[] = "IsVisible";
71 const sal_Char sAPI_IsCurrentlyVisible
[] = "IsCurrentlyVisible";
72 const sal_Char sAPI_ProtectionKey
[] = "ProtectionKey";
76 XML_TOK_SECTION_XMLID
,
77 XML_TOK_SECTION_STYLE_NAME
,
79 XML_TOK_SECTION_CONDITION
,
80 XML_TOK_SECTION_DISPLAY
,
81 XML_TOK_SECTION_PROTECT
,
82 XML_TOK_SECTION_PROTECTION_KEY
,
83 XML_TOK_SECTION_IS_HIDDEN
86 static __FAR_DATA SvXMLTokenMapEntry aSectionTokenMap
[] =
88 { XML_NAMESPACE_XML
, XML_ID
, XML_TOK_SECTION_XMLID
},
89 { XML_NAMESPACE_TEXT
, XML_STYLE_NAME
, XML_TOK_SECTION_STYLE_NAME
},
90 { XML_NAMESPACE_TEXT
, XML_NAME
, XML_TOK_SECTION_NAME
},
91 { XML_NAMESPACE_TEXT
, XML_CONDITION
, XML_TOK_SECTION_CONDITION
},
92 { XML_NAMESPACE_TEXT
, XML_DISPLAY
, XML_TOK_SECTION_DISPLAY
},
93 { XML_NAMESPACE_TEXT
, XML_PROTECTED
, XML_TOK_SECTION_PROTECT
},
94 { XML_NAMESPACE_TEXT
, XML_PROTECTION_KEY
, XML_TOK_SECTION_PROTECTION_KEY
},
95 { XML_NAMESPACE_TEXT
, XML_IS_HIDDEN
, XML_TOK_SECTION_IS_HIDDEN
},
96 // compatibility with SRC629 (or earlier) versions
97 { XML_NAMESPACE_TEXT
, XML_PROTECT
, XML_TOK_SECTION_PROTECT
},
102 // section import: This one is fairly tricky due to a variety of
103 // limits of the core or the API. The main problem is that if you
104 // insert a section within another section, you can't move the cursor
105 // between the ends of the inner and the enclosing section. To avoid
106 // these problems, additional markers are first inserted and later deleted.
107 XMLSectionImportContext::XMLSectionImportContext(
108 SvXMLImport
& rImport
,
110 const OUString
& rLocalName
)
111 : SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
112 , sTextSection(RTL_CONSTASCII_USTRINGPARAM(sAPI_TextSection
))
113 , sIndexHeaderSection(RTL_CONSTASCII_USTRINGPARAM(sAPI_IndexHeaderSection
))
114 , sCondition(RTL_CONSTASCII_USTRINGPARAM(sAPI_Condition
))
115 , sIsVisible(RTL_CONSTASCII_USTRINGPARAM(sAPI_IsVisible
))
116 , sProtectionKey(RTL_CONSTASCII_USTRINGPARAM(sAPI_ProtectionKey
))
117 , sIsProtected(RTL_CONSTASCII_USTRINGPARAM(sAPI_IsProtected
))
118 , sIsCurrentlyVisible(RTL_CONSTASCII_USTRINGPARAM(sAPI_IsCurrentlyVisible
))
119 , bProtect(sal_False
)
121 , bIsVisible(sal_True
)
123 , bSequenceOK(sal_False
)
124 , bIsCurrentlyVisible(sal_True
)
125 , bIsCurrentlyVisibleOK(sal_False
)
126 , bHasContent(sal_False
)
130 XMLSectionImportContext::~XMLSectionImportContext()
134 void XMLSectionImportContext::StartElement(
135 const Reference
<XAttributeList
> & xAttrList
)
137 // process attributes
138 ProcessAttributes(xAttrList
);
140 // process index headers:
141 sal_Bool bIsIndexHeader
= IsXMLToken( GetLocalName(), XML_INDEX_TITLE
);
147 UniReference
<XMLTextImportHelper
> rHelper
= GetImport().GetTextImport();
152 // create text section (as XPropertySet)
153 Reference
<XMultiServiceFactory
> xFactory(
154 GetImport().GetModel(),UNO_QUERY
);
157 Reference
<XInterface
> xIfc
=
158 xFactory
->createInstance( bIsIndexHeader
? sIndexHeaderSection
162 Reference
<XPropertySet
> xPropSet(xIfc
, UNO_QUERY
);
164 // save PropertySet (for CreateChildContext)
165 xSectionPropertySet
= xPropSet
;
167 // xml:id for RDF metadata
168 GetImport().SetXmlId(xIfc
, sXmlId
);
171 Reference
<XNamed
> xNamed(xPropSet
, UNO_QUERY
);
172 xNamed
->setName(sName
);
175 if (sStyleName
.getLength() > 0)
177 XMLPropStyleContext
* pStyle
= rHelper
->
178 FindSectionStyle(sStyleName
);
182 pStyle
->FillPropertySet( xPropSet
);
186 // IsVisible and condition (not for index headers)
187 if (! bIsIndexHeader
)
190 aAny
.setValue( &bIsVisible
, ::getBooleanCppuType() );
191 xPropSet
->setPropertyValue( sIsVisible
, aAny
);
193 // #97450# hidden sections must be hidden on reload
194 // For backwards compatibilty, set flag only if it is
196 if( bIsCurrentlyVisibleOK
)
198 aAny
.setValue( &bIsCurrentlyVisible
,
199 ::getBooleanCppuType() );
200 xPropSet
->setPropertyValue( sIsCurrentlyVisible
, aAny
);
206 xPropSet
->setPropertyValue( sCondition
, aAny
);
210 // password (only for regular sections)
212 IsXMLToken(GetLocalName(), XML_SECTION
) )
216 xPropSet
->setPropertyValue(sProtectionKey
, aAny
);
221 aAny
.setValue( &bProtect
, ::getBooleanCppuType() );
222 xPropSet
->setPropertyValue( sIsProtected
, aAny
);
224 // insert marker, <paragraph>, marker; then insert
225 // section over the first marker character, and delete the
226 // last paragraph (and marker) when closing a section.
227 Reference
<XTextRange
> xStart
=
228 rHelper
->GetCursor()->getStart();
230 static const sal_Char sMarker
[] = " ";
232 static const sal_Char sMarker
[] = "X";
234 OUString
sMarkerString(RTL_CONSTASCII_USTRINGPARAM(sMarker
));
235 rHelper
->InsertString(sMarkerString
);
236 rHelper
->InsertControlCharacter(
237 ControlCharacter::APPEND_PARAGRAPH
);
238 rHelper
->InsertString(sMarkerString
);
240 // select first marker
241 rHelper
->GetCursor()->gotoRange(xStart
, sal_False
);
242 rHelper
->GetCursor()->goRight(1, sal_True
);
244 // convert section to XTextContent
245 Reference
<XTextContent
> xTextContent(xSectionPropertySet
,
248 // and insert (over marker)
249 rHelper
->GetText()->insertTextContent(
250 rHelper
->GetCursorAsRange(), xTextContent
, sal_True
);
252 // and delete first marker (in section)
253 rHelper
->GetText()->insertString(
254 rHelper
->GetCursorAsRange(), sEmpty
, sal_True
);
256 // finally, check for redlines that should start at
257 // the section start node
258 rHelper
->RedlineAdjustStartNodeCursor(sal_True
); // start ???
264 void XMLSectionImportContext::ProcessAttributes(
265 const Reference
<XAttributeList
> & xAttrList
)
267 SvXMLTokenMap
aTokenMap(aSectionTokenMap
);
269 sal_Int16 nLength
= xAttrList
->getLength();
270 for(sal_Int16 nAttr
= 0; nAttr
< nLength
; nAttr
++)
273 sal_uInt16 nNamePrefix
= GetImport().GetNamespaceMap().
274 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
276 OUString sAttr
= xAttrList
->getValueByIndex(nAttr
);
278 switch (aTokenMap
.Get(nNamePrefix
, sLocalName
))
280 case XML_TOK_SECTION_XMLID
:
283 case XML_TOK_SECTION_STYLE_NAME
:
286 case XML_TOK_SECTION_NAME
:
290 case XML_TOK_SECTION_CONDITION
:
293 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
294 _GetKeyByAttrName( sAttr
, &sTmp
, sal_False
);
295 if( XML_NAMESPACE_OOOW
== nPrefix
)
304 case XML_TOK_SECTION_DISPLAY
:
305 if (IsXMLToken(sAttr
, XML_TRUE
))
307 bIsVisible
= sal_True
;
309 else if ( IsXMLToken(sAttr
, XML_NONE
) ||
310 IsXMLToken(sAttr
, XML_CONDITION
) )
312 bIsVisible
= sal_False
;
316 case XML_TOK_SECTION_IS_HIDDEN
:
319 if (SvXMLUnitConverter::convertBool(bTmp
, sAttr
))
321 bIsCurrentlyVisible
= !bTmp
;
322 bIsCurrentlyVisibleOK
= sal_True
;
326 case XML_TOK_SECTION_PROTECTION_KEY
:
327 SvXMLUnitConverter::decodeBase64(aSequence
, sAttr
);
328 bSequenceOK
= sal_True
;
330 case XML_TOK_SECTION_PROTECT
:
333 if (SvXMLUnitConverter::convertBool(bTmp
, sAttr
))
346 void XMLSectionImportContext::EndElement()
348 // get rid of last paragraph
349 // (unless it's the only paragraph in the section)
350 UniReference
<XMLTextImportHelper
> rHelper
= GetImport().GetTextImport();
351 rHelper
->GetCursor()->goRight(1, sal_False
);
354 rHelper
->GetCursor()->goLeft(1, sal_True
);
355 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
359 // and delete second marker
360 rHelper
->GetCursor()->goRight(1, sal_True
);
361 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
364 // check for redlines to our endnode
365 rHelper
->RedlineAdjustStartNodeCursor(sal_False
);
368 SvXMLImportContext
* XMLSectionImportContext::CreateChildContext(
370 const OUString
& rLocalName
,
371 const Reference
<XAttributeList
> & xAttrList
)
373 SvXMLImportContext
* pContext
= NULL
;
375 // section-source (-dde) elements
376 if ( (XML_NAMESPACE_TEXT
== nPrefix
) &&
377 IsXMLToken(rLocalName
, XML_SECTION_SOURCE
) )
379 pContext
= new XMLSectionSourceImportContext(GetImport(),
381 xSectionPropertySet
);
383 else if ( (XML_NAMESPACE_OFFICE
== nPrefix
) &&
384 IsXMLToken(rLocalName
, XML_DDE_SOURCE
) )
386 pContext
= new XMLSectionSourceDDEImportContext(GetImport(),
388 xSectionPropertySet
);
392 // otherwise: text context
393 pContext
= GetImport().GetTextImport()->CreateTextChildContext(
394 GetImport(), nPrefix
, rLocalName
, xAttrList
,
395 XML_TEXT_TYPE_SECTION
);
397 // if that fails, default context
398 if (NULL
== pContext
)
400 pContext
= new SvXMLImportContext( GetImport(),
401 nPrefix
, rLocalName
);
404 bHasContent
= sal_True
;