Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/configmgr/source/xcdparser.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 <sal/config.h>
21
22
#include <cassert>
23
#include <climits>
24
#include <set>
25
26
#include <com/sun/star/uno/RuntimeException.hpp>
27
#include <rtl/ustring.hxx>
28
#include <xmlreader/span.hxx>
29
#include <xmlreader/xmlreader.hxx>
30
31
#include "parsemanager.hxx"
32
#include "xcdparser.hxx"
33
#include "xcsparser.hxx"
34
#include "xcuparser.hxx"
35
#include "xmldata.hxx"
36
37
namespace configmgr {
38
39
XcdParser::XcdParser(
40
    int layer, std::set< OUString > const & processedDependencies, Data & data):
41
0
    layer_(layer), processedDependencies_(processedDependencies), data_(data),
42
0
    state_(STATE_START), dependencyOptional_(), nesting_()
43
0
{}
44
45
0
XcdParser::~XcdParser() {}
46
47
0
xmlreader::XmlReader::Text XcdParser::getTextMode() {
48
0
    return nestedParser_.is()
49
0
        ? nestedParser_->getTextMode() : xmlreader::XmlReader::Text::NONE;
50
0
}
51
52
bool XcdParser::startElement(
53
    xmlreader::XmlReader & reader, int nsId, xmlreader::Span const & name,
54
    std::set< OUString > const * existingDependencies)
55
0
{
56
0
    if (nestedParser_.is()) {
57
0
        assert(nesting_ != LONG_MAX);
58
0
        ++nesting_;
59
0
        return nestedParser_->startElement(
60
0
            reader, nsId, name, existingDependencies);
61
0
    }
62
0
    switch (state_) {
63
0
    case STATE_START:
64
0
        if (nsId == ParseManager::NAMESPACE_OOR && name == "data") {
65
0
            state_ = STATE_DEPENDENCIES;
66
0
            return true;
67
0
        }
68
0
        break;
69
0
    case STATE_DEPENDENCIES:
70
0
        if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
71
0
            name == "dependency")
72
0
        {
73
0
            if (dependencyFile_.isEmpty()) {
74
0
                dependencyOptional_ = false;
75
0
                xmlreader::Span attrFile;
76
0
                for (;;) {
77
0
                    int attrNsId;
78
0
                    xmlreader::Span attrLn;
79
0
                    if (!reader.nextAttribute(&attrNsId, &attrLn)) {
80
0
                        break;
81
0
                    }
82
0
                    if (attrNsId == xmlreader::XmlReader::NAMESPACE_NONE &&
83
                            //TODO: _OOR
84
0
                        attrLn == "file")
85
0
                    {
86
0
                        attrFile = reader.getAttributeValue(false);
87
0
                    } else if ((attrNsId ==
88
0
                                xmlreader::XmlReader::NAMESPACE_NONE) &&
89
0
                               attrLn == "optional")
90
0
                    {
91
0
                        dependencyOptional_ = xmldata::parseBoolean(
92
0
                            reader.getAttributeValue(true));
93
0
                    }
94
0
                }
95
0
                if (!attrFile.is()) {
96
0
                    throw css::uno::RuntimeException(
97
0
                        "no dependency file attribute in " + reader.getUrl());
98
0
                }
99
0
                dependencyFile_ = attrFile.convertFromUtf8();
100
0
                if (dependencyFile_.isEmpty()) {
101
0
                    throw css::uno::RuntimeException(
102
0
                        "bad dependency file attribute in " + reader.getUrl());
103
0
                }
104
0
            }
105
0
            if ((processedDependencies_.find(dependencyFile_) ==
106
0
                 processedDependencies_.end()) &&
107
0
                (!dependencyOptional_ || existingDependencies == nullptr ||
108
0
                 (existingDependencies->find(dependencyFile_) !=
109
0
                  existingDependencies->end())))
110
0
            {
111
0
                return false;
112
0
            }
113
0
            state_ = STATE_DEPENDENCY;
114
0
            dependencyFile_.clear();
115
0
            return true;
116
0
        }
117
0
        state_ = STATE_COMPONENTS;
118
0
        [[fallthrough]];
119
0
    case STATE_COMPONENTS:
120
0
        if (nsId == ParseManager::NAMESPACE_OOR &&
121
0
            name == "component-schema")
122
0
        {
123
0
            nestedParser_ = new XcsParser(layer_, data_);
124
0
            nesting_ = 1;
125
0
            return nestedParser_->startElement(
126
0
                reader, nsId, name, existingDependencies);
127
0
        }
128
0
        if (nsId == ParseManager::NAMESPACE_OOR &&
129
0
            (name == "component-data" || name == "items"))
130
0
        {
131
0
            nestedParser_ = new XcuParser(layer_ + 1, data_, nullptr, nullptr, nullptr);
132
0
            nesting_ = 1;
133
0
            return nestedParser_->startElement(
134
0
                reader, nsId, name, existingDependencies);
135
0
        }
136
0
        break;
137
0
    default: // STATE_DEPENDENCY
138
0
        assert(false); // this cannot happen
139
0
        break;
140
0
    }
141
0
    throw css::uno::RuntimeException(
142
0
        "bad member <" + name.convertFromUtf8() + "> in " + reader.getUrl());
143
0
}
144
145
0
void XcdParser::endElement(xmlreader::XmlReader const & reader) {
146
0
    if (nestedParser_.is()) {
147
0
        nestedParser_->endElement(reader);
148
0
        if (--nesting_ == 0) {
149
0
            nestedParser_.clear();
150
0
        }
151
0
    } else {
152
0
        switch (state_) {
153
0
        case STATE_DEPENDENCY:
154
0
            state_ = STATE_DEPENDENCIES;
155
0
            break;
156
0
        case STATE_DEPENDENCIES:
157
0
        case STATE_COMPONENTS:
158
0
            break;
159
0
        default:
160
0
            assert(false); // this cannot happen
161
0
            break;
162
0
        }
163
0
    }
164
0
}
165
166
0
void XcdParser::characters(xmlreader::Span const & text) {
167
0
    if (nestedParser_.is()) {
168
0
        nestedParser_->characters(text);
169
0
    }
170
0
}
171
172
}
173
174
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */