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 
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::xml::sax;
31 using namespace ::com::sun::star::text;
32 using 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  const OUString sShareContentFirst( "FirstIsShared" );
50  // NOTE: if this ever handles XML_DISPLAY attr then beware of fdo#72850 !
51  if( !(bLeft || bFirst) )
52  return;
53 
54  Any aAny = xPropSet->getPropertyValue( sOn );
55  bool bOn = *o3tl::doAccess<bool>(aAny);
56 
57  if( bOn )
58  {
59  if (bLeft)
60  {
61  aAny = xPropSet->getPropertyValue( sShareContent );
62  bool bShared = bool();
63  if (!(aAny >>= bShared))
64  assert(false); // should return a value!
65  if( bShared )
66  {
67  // Don't share headers any longer
68  xPropSet->setPropertyValue( sShareContent, Any(false) );
69  }
70  }
71  if (bFirst)
72  {
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 
97  sal_uInt16 nPrefix,
98  const OUString& rLocalName,
99  const uno::Reference< xml::sax::XAttributeList > & xAttrList )
100 {
101  SvXMLImportContext *pContext = nullptr;
102  if( bInsertContent )
103  {
104  if( !xOldTextCursor.is() )
105  {
106  bool bRemoveContent = true;
107  Any aAny;
108  if( bLeft || bFirst )
109  {
110  // Headers and footers are switched on already,
111  // and they aren't shared.
112  if (bLeft)
113  aAny = xPropSet->getPropertyValue( sTextLeft );
114  else
115  aAny = xPropSet->getPropertyValue( sTextFirst );
116  }
117  else
118  {
119  aAny = xPropSet->getPropertyValue( sOn );
120  bool bOn = *o3tl::doAccess<bool>(aAny);
121 
122  if( !bOn )
123  {
124  // Switch header on
125  xPropSet->setPropertyValue( sOn, Any(true) );
126 
127  // The content has not to be removed, because the header
128  // or footer is empty already.
129  bRemoveContent = false;
130  }
131 
132  // If a header or footer is not shared, share it now.
133  aAny = xPropSet->getPropertyValue( sShareContent );
134  bool bShared = *o3tl::doAccess<bool>(aAny);
135  if( !bShared )
136  {
137  xPropSet->setPropertyValue( sShareContent, Any(true) );
138  }
139 
140  aAny = xPropSet->getPropertyValue( sText );
141  }
142 
143  Reference < XText > xText;
144  aAny >>= xText;
145 
146  if( bRemoveContent )
147  {
148  xText->setString(OUString());
149  // fdo#82165 shapes anchored at the beginning or end survive
150  // setString("") - kill them the hard way: SwDoc::DelFullPara()
151  uno::Reference<text::XParagraphAppend> const xAppend(
152  xText, uno::UNO_QUERY_THROW);
153  uno::Reference<lang::XComponent> const xPara(
154  xAppend->finishParagraph(
155  uno::Sequence<beans::PropertyValue>()),
156  uno::UNO_QUERY_THROW);
157  xPara->dispose();
158  }
159 
162 
163  xOldTextCursor = xTxtImport->GetCursor();
164  xTxtImport->SetCursor( xText->createTextCursor() );
165  }
166 
167  pContext =
168  GetImport().GetTextImport()->CreateTextChildContext(
169  GetImport(), nPrefix, rLocalName, xAttrList,
171  }
172 
173  return pContext;
174 }
175 
177 {
178  if( xOldTextCursor.is() )
179  {
180  GetImport().GetTextImport()->DeleteParagraph();
181  GetImport().GetTextImport()->SetCursor( xOldTextCursor );
182  }
183  else if( !bLeft )
184  {
185  // If no content has been inserted into the header or footer,
186  // switch it off.
187  xPropSet->setPropertyValue( sOn, Any(false) );
188  }
189 }
190 
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:600
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
css::uno::Reference< css::text::XTextCursor > xOldTextCursor
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
css::uno::Reference< css::beans::XPropertySet > xPropSet
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
XMLTextHeaderFooterContext(SvXMLImport &rImport, const css::uno::Reference< css::beans::XPropertySet > &rPageStylePropSet, bool bFooter, bool bLft, bool bFrst)