LibreOffice Module xmloff (master) 1
XMLTextHeaderFooterContext.cxx
Go to the documentation of this file.
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 <com/sun/star/text/XText.hpp>
21#include <com/sun/star/text/XParagraphAppend.hpp>
22#include <com/sun/star/beans/XPropertySet.hpp>
23#include <o3tl/any.hxx>
25#include <xmloff/xmlimp.hxx>
26
27
28using namespace ::com::sun::star;
29using namespace ::com::sun::star::uno;
30using namespace ::com::sun::star::xml::sax;
31using namespace ::com::sun::star::text;
32using namespace ::com::sun::star::beans;
33
34
36 const Reference < XPropertySet > & rPageStylePropSet,
37 bool bFooter, bool bLft, bool bFrst ) :
38 SvXMLImportContext( rImport ),
39 xPropSet( rPageStylePropSet ),
40 sOn( bFooter ? OUString("FooterIsOn") : OUString("HeaderIsOn") ),
41 sShareContent( bFooter ? OUString("FooterIsShared") : OUString("HeaderIsShared") ),
42 sText( bFooter ? OUString("FooterText") : OUString("HeaderText") ),
43 sTextFirst(bFooter ? OUString("FooterTextFirst") : OUString("HeaderTextFirst")),
44 sTextLeft( bFooter ? OUString("FooterTextLeft") : OUString("HeaderTextLeft") ),
45 bInsertContent( true ),
46 bLeft( bLft ),
47 bFirst( bFrst )
48{
49 // NOTE: if this ever handles XML_DISPLAY attr then beware of fdo#72850 !
50 if( !(bLeft || bFirst) )
51 return;
52
53 Any aAny = xPropSet->getPropertyValue( sOn );
54 bool bOn = *o3tl::doAccess<bool>(aAny);
55
56 if( bOn )
57 {
58 if (bLeft)
59 {
60 aAny = xPropSet->getPropertyValue( sShareContent );
61 bool bShared = bool();
62 if (!(aAny >>= bShared))
63 assert(false); // should return a value!
64 if( bShared )
65 {
66 // Don't share headers any longer
67 xPropSet->setPropertyValue( sShareContent, Any(false) );
68 }
69 }
70 if (bFirst)
71 {
72 static const OUStringLiteral sShareContentFirst( u"FirstIsShared" );
73 aAny = xPropSet->getPropertyValue( sShareContentFirst );
74 bool bSharedFirst = bool();
75 if (!(aAny >>= bSharedFirst))
76 assert(false); // should return a value!
77 if( bSharedFirst )
78 {
79 // Don't share first/right headers any longer
80 xPropSet->setPropertyValue( sShareContentFirst, Any(false) );
81 }
82 }
83 }
84 else
85 {
86 // If headers or footers are switched off, no content must be
87 // inserted.
88 bInsertContent = false;
89 }
90}
91
93{
94}
95
96css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTextHeaderFooterContext::createFastChildContext(
97 sal_Int32 nElement,
98 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
99{
100 SvXMLImportContext *pContext = nullptr;
101 if( bInsertContent )
102 {
103 if( !xOldTextCursor.is() )
104 {
105 bool bRemoveContent = true;
106 Any aAny;
107 if( bLeft || bFirst )
108 {
109 // Headers and footers are switched on already,
110 // and they aren't shared.
111 if (bLeft)
112 aAny = xPropSet->getPropertyValue( sTextLeft );
113 else
114 aAny = xPropSet->getPropertyValue( sTextFirst );
115 }
116 else
117 {
118 aAny = xPropSet->getPropertyValue( sOn );
119 bool bOn = *o3tl::doAccess<bool>(aAny);
120
121 if( !bOn )
122 {
123 // Switch header on
124 xPropSet->setPropertyValue( sOn, Any(true) );
125
126 // The content has not to be removed, because the header
127 // or footer is empty already.
128 bRemoveContent = false;
129 }
130
131 // If a header or footer is not shared, share it now.
132 aAny = xPropSet->getPropertyValue( sShareContent );
133 bool bShared = *o3tl::doAccess<bool>(aAny);
134 if( !bShared )
135 {
136 xPropSet->setPropertyValue( sShareContent, Any(true) );
137 }
138
139 aAny = xPropSet->getPropertyValue( sText );
140 }
141
142 Reference < XText > xText;
143 aAny >>= xText;
144
145 if( bRemoveContent )
146 {
147 xText->setString(OUString());
148 // fdo#82165 shapes anchored at the beginning or end survive
149 // setString("") - kill them the hard way: SwDoc::DelFullPara()
150 uno::Reference<text::XParagraphAppend> const xAppend(
151 xText, uno::UNO_QUERY_THROW);
152 uno::Reference<lang::XComponent> const xPara(
153 xAppend->finishParagraph(
154 uno::Sequence<beans::PropertyValue>()),
155 uno::UNO_QUERY_THROW);
156 xPara->dispose();
157 }
158
161
162 xOldTextCursor = xTxtImport->GetCursor();
163 xTxtImport->SetCursor( xText->createTextCursor() );
164 }
165
166 pContext =
167 GetImport().GetTextImport()->CreateTextChildContext(
168 GetImport(), nElement, xAttrList,
170 }
171
172 return pContext;
173}
174
176{
177 if( xOldTextCursor.is() )
178 {
179 GetImport().GetTextImport()->DeleteParagraph();
180 GetImport().GetTextImport()->SetCursor( xOldTextCursor );
181 }
182 else if( !bLeft )
183 {
184 // If no content has been inserted into the header or footer,
185 // switch it off.
186 xPropSet->setPropertyValue( sOn, Any(false) );
187 }
188}
189
190/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:602
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
css::uno::Reference< css::beans::XPropertySet > xPropSet
XMLTextHeaderFooterContext(SvXMLImport &rImport, const css::uno::Reference< css::beans::XPropertySet > &rPageStylePropSet, bool bFooter, bool bLft, bool bFrst)
css::uno::Reference< css::text::XTextCursor > xOldTextCursor
float u
@ HeaderFooter
Definition: txtimp.hxx:74