Coverage Report

Created: 2025-12-31 10:39

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