LibreOffice Module xmloff (master)  1
XMLTextNumRuleInfo.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 
21 #include <osl/diagnose.h>
22 #include <sal/log.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/container/XIndexReplace.hpp>
26 #include <com/sun/star/style/NumberingType.hpp>
27 #include <com/sun/star/text/XTextContent.hpp>
28 #include <com/sun/star/container/XNamed.hpp>
29 #include "XMLTextNumRuleInfo.hxx"
31 
32 
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::beans;
35 using namespace ::com::sun::star::container;
36 using namespace ::com::sun::star::style;
37 
38 // Complete refactoring of the class and enhancement of the class for lists.
40  : mxNumRules()
41  , msNumRulesName()
42  , msListId()
43  , mnListStartValue( -1 )
44  , mnListLevel( 0 )
45  , mbIsNumbered( false )
46  , mbIsRestart( false )
47  , mnListLevelStartValue( -1 )
48  , mbOutlineStyleAsNormalListStyle( false )
49 {
50  Reset();
51 }
52 
53 // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
55  const css::uno::Reference < css::text::XTextContent > & xTextContent,
56  const bool bOutlineStyleAsNormalListStyle,
57  const XMLTextListAutoStylePool& rListAutoPool,
58  const bool bExportTextNumberElement )
59 {
60  Reset();
61  // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
62  mbOutlineStyleAsNormalListStyle = bOutlineStyleAsNormalListStyle;
63 
64  Reference< XPropertySet > xPropSet( xTextContent, UNO_QUERY );
65  Reference< XPropertySetInfo > xPropSetInfo = xPropSet->getPropertySetInfo();
66 
67  // check if this paragraph supports a numbering
68  if( !xPropSetInfo->hasPropertyByName( "NumberingLevel" ) )
69  return;
70 
71  if( xPropSet->getPropertyValue( "NumberingLevel" ) >>= mnListLevel )
72  {
73  if( xPropSetInfo->hasPropertyByName( "NumberingRules" ) )
74  {
75  xPropSet->getPropertyValue( "NumberingRules" ) >>= mxNumRules;
76  }
77  }
78  else
79  {
80  // in applications using the outliner we always have a numbering rule,
81  // so a void property no numbering
82  mnListLevel = 0;
83  }
84 
85  // Assertion saving writer document (#i97312#)
86  if ( mxNumRules.is() && mxNumRules->getCount() < 1 )
87  {
88  SAL_WARN("xmloff",
89  "<XMLTextNumRuleInfo::Set(..)> - numbering rules instance does not contain any numbering rule" );
90  Reset();
91  return;
92  }
93 
94  if ( mnListLevel < 0 )
95  {
96  SAL_WARN("xmloff",
97  "<XMLTextNumRuleInfo::Set(..)> - unexpected numbering level" );
98  Reset();
99  return;
100  }
101 
102  // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
103  bool bSuppressListStyle( false );
104  if ( mxNumRules.is() )
105  {
107  {
108  Reference<XPropertySet> xNumRulesProps(mxNumRules, UNO_QUERY);
109  if ( xNumRulesProps.is() &&
110  xNumRulesProps->getPropertySetInfo()->
111  hasPropertyByName( "NumberingIsOutline" ) )
112  {
113  bool bIsOutline = false;
114  xNumRulesProps->getPropertyValue( "NumberingIsOutline" ) >>= bIsOutline;
115  bSuppressListStyle = bIsOutline;
116  }
117  }
118  }
119 
120  if( mxNumRules.is() && !bSuppressListStyle )
121  {
122  // First try to find the numbering rules in the list auto style pool.
123  // If not found, the numbering rules instance has to be named.
124  msNumRulesName = rListAutoPool.Find( mxNumRules );
125  if ( msNumRulesName.isEmpty() )
126  {
127  Reference < XNamed > xNamed( mxNumRules, UNO_QUERY );
128  SAL_WARN_IF( !xNamed.is(), "xmloff",
129  "<XMLTextNumRuleInfo::Set(..)> - numbering rules instance have to be named. Serious defect." );
130  if( xNamed.is() )
131  {
132  msNumRulesName = xNamed->getName();
133  }
134  }
135  SAL_WARN_IF( msNumRulesName.isEmpty(), "xmloff",
136  "<XMLTextNumRuleInfo::Set(..)> - no name found for numbering rules instance. Serious defect." );
137 
138  if( xPropSetInfo->hasPropertyByName( "ListId" ) )
139  {
140  xPropSet->getPropertyValue( "ListId" ) >>= msListId;
141  }
142 
144  if( xPropSetInfo->hasPropertyByName( "ContinueingPreviousSubTree" ) )
145  {
146  xPropSet->getPropertyValue( "ContinueingPreviousSubTree" ) >>= mbContinueingPreviousSubTree;
147  }
148 
149  mbIsNumbered = true;
150  if( xPropSetInfo->hasPropertyByName( "NumberingIsNumber" ) )
151  {
152  if( !(xPropSet->getPropertyValue( "NumberingIsNumber" ) >>= mbIsNumbered ) )
153  {
154  OSL_FAIL( "numbered paragraph without number info" );
155  mbIsNumbered = false;
156  }
157  }
158 
159  if( mbIsNumbered )
160  {
161  if( xPropSetInfo->hasPropertyByName( "ParaIsNumberingRestart" ) )
162  {
163  xPropSet->getPropertyValue( "ParaIsNumberingRestart" ) >>= mbIsRestart;
164  }
165  if( xPropSetInfo->hasPropertyByName( "NumberingStartValue" ) )
166  {
167  xPropSet->getPropertyValue( "NumberingStartValue" ) >>= mnListStartValue;
168  }
169  }
170 
171  OSL_ENSURE( mnListLevel < mxNumRules->getCount(), "wrong num rule level" );
172  if( mnListLevel >= mxNumRules->getCount() )
173  {
174  Reset();
175  return;
176  }
177 
178  Sequence<PropertyValue> aProps;
179  mxNumRules->getByIndex( mnListLevel ) >>= aProps;
180 
181  auto pProp = std::find_if(aProps.begin(), aProps.end(),
182  [](const PropertyValue& rProp) { return rProp.Name == "StartWith"; });
183  if (pProp != aProps.end())
184  {
185  pProp->Value >>= mnListLevelStartValue;
186  }
187 
188  msListLabelString.clear();
189  if ( bExportTextNumberElement &&
190  xPropSetInfo->hasPropertyByName( "ListLabelString" ) )
191  {
192  xPropSet->getPropertyValue( "ListLabelString" ) >>= msListLabelString;
193  }
194 
195  // paragraph's list level range is [0..9] representing list levels [1..10]
196  ++mnListLevel;
197  }
198  else
199  {
200  mnListLevel = 0;
201  }
202 }
203 
205 {
206  bool bRet( true );
207  // Currently only the text documents support <ListId>.
208  if ( !rCmp.msListId.isEmpty() || !msListId.isEmpty() )
209  {
210  bRet = rCmp.msListId == msListId;
211  }
212  else
213  {
214  bRet = rCmp.msNumRulesName == msNumRulesName;
215  }
216 
217  return bRet;
218 }
219 
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool BelongsToSameList(const XMLTextNumRuleInfo &rCmp) const
css::uno::Reference< css::container::XIndexReplace > mxNumRules
information about list and list style for a certain paragraph
void Set(const css::uno::Reference< css::text::XTextContent > &rTextContent, const bool bOutlineStyleAsNormalListStyle, const XMLTextListAutoStylePool &rListAutoPool, const bool bExportTextNumberElement)
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
SAL_DLLPRIVATE sal_uInt32 Find(const XMLTextListAutoStylePoolEntry_Impl *pEntry) const