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/XMLIndexTOCStylesContext.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
#include "XMLIndexTOCStylesContext.hxx"
21
22
#include "XMLIndexSourceBaseContext.hxx"
23
#include <com/sun/star/beans/XPropertySet.hpp>
24
#include <com/sun/star/container/XIndexReplace.hpp>
25
#include <xmloff/xmlictxt.hxx>
26
#include <xmloff/xmlimp.hxx>
27
#include <xmloff/txtimp.hxx>
28
#include <xmloff/xmlnamespace.hxx>
29
#include <xmloff/xmltoken.hxx>
30
#include <sax/tools/converter.hxx>
31
#include <com/sun/star/uno/Sequence.hxx>
32
#include <rtl/ustring.hxx>
33
34
35
using namespace ::xmloff::token;
36
37
using ::com::sun::star::beans::XPropertySet;
38
using ::com::sun::star::uno::Reference;
39
using ::com::sun::star::uno::Sequence;
40
using ::com::sun::star::uno::Any;
41
using ::com::sun::star::container::XIndexReplace;
42
43
44
45
XMLIndexTOCStylesContext::XMLIndexTOCStylesContext(
46
    SvXMLImport& rImport, Reference<XPropertySet> & rPropSet)
47
0
    : SvXMLImportContext(rImport)
48
0
    , rTOCPropertySet(rPropSet)
49
0
    , nOutlineLevel(0)
50
0
{
51
0
}
52
53
XMLIndexTOCStylesContext::~XMLIndexTOCStylesContext()
54
0
{
55
0
}
56
57
void XMLIndexTOCStylesContext::startFastElement(
58
    sal_Int32 /*nElement*/,
59
    const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
60
0
{
61
    // find text:outline-level attribute
62
0
    for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
63
0
    {
64
0
        if ( aIter.getToken() == XML_ELEMENT(TEXT, XML_OUTLINE_LEVEL) )
65
0
        {
66
0
            sal_Int32 nTmp;
67
0
            if (::sax::Converter::convertNumber(
68
0
                    nTmp, aIter.toView(), 1,
69
0
                    GetImport().GetTextImport()->GetChapterNumbering()->
70
0
                                                                getCount()))
71
0
            {
72
                // API numbers 0..9, we number 1..10
73
0
                nOutlineLevel = nTmp-1;
74
0
            }
75
0
            break;
76
0
        }
77
0
    }
78
0
}
79
80
void XMLIndexTOCStylesContext::endFastElement(sal_Int32 )
81
0
{
82
    // if valid...
83
0
    if (nOutlineLevel < 0)
84
0
        return;
85
86
    // copy vector into sequence
87
0
    const sal_Int32 nCount = aStyleNames.size();
88
0
    Sequence<OUString> aStyleNamesSequence(nCount);
89
0
    auto aStyleNamesSequenceRange = asNonConstRange(aStyleNamesSequence);
90
0
    for(sal_Int32 i = 0; i < nCount; i++)
91
0
    {
92
0
        aStyleNamesSequenceRange[i] = GetImport().GetStyleDisplayName(
93
0
                        XmlStyleFamily::TEXT_PARAGRAPH,
94
0
                           aStyleNames[i] );
95
0
    }
96
97
    // get index replace
98
0
    Any aAny = rTOCPropertySet->getPropertyValue(u"LevelParagraphStyles"_ustr);
99
0
    Reference<XIndexReplace> xIndexReplace;
100
0
    aAny >>= xIndexReplace;
101
102
    // set style names
103
0
    xIndexReplace->replaceByIndex(nOutlineLevel, Any(aStyleNamesSequence));
104
0
}
105
106
namespace xmloff {
107
108
OUString GetIndexSourceStyleName(
109
        css::uno::Reference<css::xml::sax::XFastAttributeList> const& xAttrList)
110
0
{
111
0
    for (auto& rIter : sax_fastparser::castToFastAttributeList(xAttrList))
112
0
    {
113
0
        if (rIter.getToken() == XML_ELEMENT(TEXT, XML_STYLE_NAME))
114
0
        {
115
0
            return rIter.toString();
116
0
        }
117
0
    }
118
0
    return OUString();
119
0
}
120
121
} // namespace xmloff
122
123
css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexTOCStylesContext::createFastChildContext(
124
    sal_Int32 nElement,
125
    const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
126
0
{
127
    // check for index-source-style
128
0
    if ( nElement == XML_ELEMENT(TEXT, XML_INDEX_SOURCE_STYLE) )
129
0
    {
130
        // find text:style-name attribute and record in aStyleNames
131
0
        OUString const styleName(xmloff::GetIndexSourceStyleName(xAttrList));
132
0
        if (!styleName.isEmpty())
133
0
        {
134
0
            aStyleNames.emplace_back(styleName);
135
0
        }
136
0
    }
137
138
    // always return default context; we already got the interesting info
139
0
    return new SvXMLImportContext(GetImport());
140
0
}
141
142
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */