Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
37 :
38 :
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;
44 :
45 : using namespace ::com::sun::star::uno;
46 : using namespace ::com::sun::star::text;
47 : using namespace ::xmloff::token;
48 :
49 :
50 0 : TYPEINIT1( XMLSectionImportContext, SvXMLImportContext );
51 :
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";
59 :
60 : enum XMLSectionToken
61 : {
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
70 : };
71 :
72 : static const SvXMLTokenMapEntry aSectionTokenMap[] =
73 : {
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
85 : };
86 :
87 :
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 0 : 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(sal_False)
106 : , bCondOK(sal_False)
107 : , bIsVisible(sal_True)
108 : , bValid(sal_False)
109 : , bSequenceOK(sal_False)
110 : , bIsCurrentlyVisible(sal_True)
111 : , bIsCurrentlyVisibleOK(sal_False)
112 0 : , bHasContent(sal_False)
113 : {
114 0 : }
115 :
116 0 : XMLSectionImportContext::~XMLSectionImportContext()
117 : {
118 0 : }
119 :
120 0 : void XMLSectionImportContext::StartElement(
121 : const Reference<XAttributeList> & xAttrList)
122 : {
123 : // process attributes
124 0 : ProcessAttributes(xAttrList);
125 :
126 : // process index headers:
127 0 : sal_Bool bIsIndexHeader = IsXMLToken( GetLocalName(), XML_INDEX_TITLE );
128 0 : if (bIsIndexHeader)
129 : {
130 0 : bValid = sal_True;
131 : }
132 :
133 0 : UniReference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
134 :
135 : // valid?
136 0 : if (bValid)
137 : {
138 : // create text section (as XPropertySet)
139 : Reference<XMultiServiceFactory> xFactory(
140 0 : GetImport().GetModel(),UNO_QUERY);
141 0 : if (xFactory.is())
142 : {
143 : Reference<XInterface> xIfc =
144 0 : xFactory->createInstance( bIsIndexHeader ? sIndexHeaderSection
145 0 : : sTextSection );
146 0 : if (xIfc.is())
147 : {
148 0 : Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
149 :
150 : // save PropertySet (for CreateChildContext)
151 0 : xSectionPropertySet = xPropSet;
152 :
153 : // name
154 0 : Reference<XNamed> xNamed(xPropSet, UNO_QUERY);
155 0 : xNamed->setName(sName);
156 :
157 : // stylename?
158 0 : if (!sStyleName.isEmpty())
159 : {
160 : XMLPropStyleContext* pStyle = rHelper->
161 0 : FindSectionStyle(sStyleName);
162 :
163 0 : if (pStyle != NULL)
164 : {
165 0 : pStyle->FillPropertySet( xPropSet );
166 : }
167 : }
168 :
169 : // IsVisible and condition (not for index headers)
170 0 : if (! bIsIndexHeader)
171 : {
172 0 : Any aAny;
173 0 : aAny.setValue( &bIsVisible, ::getBooleanCppuType() );
174 0 : xPropSet->setPropertyValue( sIsVisible, aAny );
175 :
176 : // #97450# hidden sections must be hidden on reload
177 : // For backwards compatibilty, set flag only if it is
178 : // present
179 0 : if( bIsCurrentlyVisibleOK )
180 : {
181 : aAny.setValue( &bIsCurrentlyVisible,
182 0 : ::getBooleanCppuType() );
183 0 : xPropSet->setPropertyValue( sIsCurrentlyVisible, aAny);
184 : }
185 :
186 0 : if (bCondOK)
187 : {
188 0 : aAny <<= sCond;
189 0 : xPropSet->setPropertyValue( sCondition, aAny );
190 0 : }
191 : }
192 :
193 : // password (only for regular sections)
194 0 : if ( bSequenceOK &&
195 0 : IsXMLToken(GetLocalName(), XML_SECTION) )
196 : {
197 0 : Any aAny;
198 0 : aAny <<= aSequence;
199 0 : xPropSet->setPropertyValue(sProtectionKey, aAny);
200 : }
201 :
202 : // protection
203 0 : Any aAny;
204 0 : aAny.setValue( &bProtect, ::getBooleanCppuType() );
205 0 : xPropSet->setPropertyValue( sIsProtected, aAny );
206 :
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 0 : 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 0 : OUString sMarkerString(sMarker);
218 0 : rHelper->InsertString(sMarkerString);
219 : rHelper->InsertControlCharacter(
220 0 : ControlCharacter::APPEND_PARAGRAPH );
221 0 : rHelper->InsertString(sMarkerString);
222 :
223 : // select first marker
224 0 : rHelper->GetCursor()->gotoRange(xStart, sal_False);
225 0 : rHelper->GetCursor()->goRight(1, sal_True);
226 :
227 : // convert section to XTextContent
228 : Reference<XTextContent> xTextContent(xSectionPropertySet,
229 0 : UNO_QUERY);
230 :
231 : // and insert (over marker)
232 0 : rHelper->GetText()->insertTextContent(
233 0 : rHelper->GetCursorAsRange(), xTextContent, sal_True );
234 :
235 : // and delete first marker (in section)
236 0 : rHelper->GetText()->insertString(
237 0 : rHelper->GetCursorAsRange(), sEmpty, sal_True);
238 :
239 : // finally, check for redlines that should start at
240 : // the section start node
241 0 : rHelper->RedlineAdjustStartNodeCursor(true); // start ???
242 :
243 : // xml:id for RDF metadata
244 0 : GetImport().SetXmlId(xIfc, sXmlId);
245 0 : }
246 0 : }
247 0 : }
248 0 : }
249 :
250 0 : void XMLSectionImportContext::ProcessAttributes(
251 : const Reference<XAttributeList> & xAttrList )
252 : {
253 0 : SvXMLTokenMap aTokenMap(aSectionTokenMap);
254 :
255 0 : sal_Int16 nLength = xAttrList->getLength();
256 0 : for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
257 : {
258 0 : OUString sLocalName;
259 0 : sal_uInt16 nNamePrefix = GetImport().GetNamespaceMap().
260 0 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
261 0 : &sLocalName );
262 0 : OUString sAttr = xAttrList->getValueByIndex(nAttr);
263 :
264 0 : switch (aTokenMap.Get(nNamePrefix, sLocalName))
265 : {
266 : case XML_TOK_SECTION_XMLID:
267 0 : sXmlId = sAttr;
268 0 : break;
269 : case XML_TOK_SECTION_STYLE_NAME:
270 0 : sStyleName = sAttr;
271 0 : break;
272 : case XML_TOK_SECTION_NAME:
273 0 : sName = sAttr;
274 0 : bValid = sal_True;
275 0 : break;
276 : case XML_TOK_SECTION_CONDITION:
277 : {
278 0 : OUString sTmp;
279 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
280 0 : _GetKeyByAttrName( sAttr, &sTmp, false );
281 0 : if( XML_NAMESPACE_OOOW == nPrefix )
282 : {
283 0 : sCond = sTmp;
284 0 : bCondOK = sal_True;
285 : }
286 : else
287 0 : sCond = sAttr;
288 : }
289 0 : break;
290 : case XML_TOK_SECTION_DISPLAY:
291 0 : if (IsXMLToken(sAttr, XML_TRUE))
292 : {
293 0 : bIsVisible = sal_True;
294 : }
295 0 : else if ( IsXMLToken(sAttr, XML_NONE) ||
296 0 : IsXMLToken(sAttr, XML_CONDITION) )
297 : {
298 0 : bIsVisible = sal_False;
299 : }
300 : // else: ignore
301 0 : break;
302 : case XML_TOK_SECTION_IS_HIDDEN:
303 : {
304 0 : bool bTmp(false);
305 0 : if (::sax::Converter::convertBool(bTmp, sAttr))
306 : {
307 0 : bIsCurrentlyVisible = !bTmp;
308 0 : bIsCurrentlyVisibleOK = sal_True;
309 : }
310 : }
311 0 : break;
312 : case XML_TOK_SECTION_PROTECTION_KEY:
313 0 : ::sax::Converter::decodeBase64(aSequence, sAttr);
314 0 : bSequenceOK = sal_True;
315 0 : break;
316 : case XML_TOK_SECTION_PROTECT:
317 : {
318 0 : bool bTmp(false);
319 0 : if (::sax::Converter::convertBool(bTmp, sAttr))
320 : {
321 0 : bProtect = bTmp;
322 : }
323 0 : break;
324 : }
325 : default:
326 : ; // ignore
327 0 : break;
328 : }
329 0 : }
330 0 : }
331 :
332 0 : void XMLSectionImportContext::EndElement()
333 : {
334 : // get rid of last paragraph
335 : // (unless it's the only paragraph in the section)
336 0 : UniReference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
337 0 : rHelper->GetCursor()->goRight(1, sal_False);
338 0 : if (bHasContent)
339 : {
340 0 : rHelper->GetCursor()->goLeft(1, sal_True);
341 0 : rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
342 0 : sEmpty, sal_True);
343 : }
344 :
345 : // and delete second marker
346 0 : rHelper->GetCursor()->goRight(1, sal_True);
347 0 : rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
348 0 : sEmpty, sal_True);
349 :
350 : // check for redlines to our endnode
351 0 : rHelper->RedlineAdjustStartNodeCursor(false);
352 0 : }
353 :
354 0 : SvXMLImportContext* XMLSectionImportContext::CreateChildContext(
355 : sal_uInt16 nPrefix,
356 : const OUString& rLocalName,
357 : const Reference<XAttributeList> & xAttrList )
358 : {
359 0 : SvXMLImportContext* pContext = NULL;
360 :
361 : // section-source (-dde) elements
362 0 : if ( (XML_NAMESPACE_TEXT == nPrefix) &&
363 0 : IsXMLToken(rLocalName, XML_SECTION_SOURCE) )
364 : {
365 0 : pContext = new XMLSectionSourceImportContext(GetImport(),
366 : nPrefix, rLocalName,
367 0 : xSectionPropertySet);
368 : }
369 0 : else if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
370 0 : IsXMLToken(rLocalName, XML_DDE_SOURCE) )
371 : {
372 0 : pContext = new XMLSectionSourceDDEImportContext(GetImport(),
373 : nPrefix, rLocalName,
374 0 : xSectionPropertySet);
375 : }
376 : else
377 : {
378 : // otherwise: text context
379 0 : pContext = GetImport().GetTextImport()->CreateTextChildContext(
380 0 : GetImport(), nPrefix, rLocalName, xAttrList,
381 0 : XML_TEXT_TYPE_SECTION );
382 :
383 : // if that fails, default context
384 0 : if (NULL == pContext)
385 : {
386 0 : pContext = new SvXMLImportContext( GetImport(),
387 0 : nPrefix, rLocalName );
388 : }
389 : else
390 0 : bHasContent = sal_True;
391 : }
392 :
393 0 : return pContext;
394 : }
395 :
396 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|