LibreOffice Module configmgr (master) 1
data.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 <sal/config.h>
21
22#include <algorithm>
23#include <cassert>
24#include <cstddef>
25
26#include <com/sun/star/uno/RuntimeException.hpp>
27#include <rtl/ref.hxx>
28#include <rtl/string.h>
29#include <rtl/ustrbuf.hxx>
30#include <rtl/ustring.h>
31#include <rtl/ustring.hxx>
32#include <sal/log.hxx>
33#include <sal/types.h>
34#include <o3tl/string_view.hxx>
35
36#include "additions.hxx"
37#include "data.hxx"
38#include "node.hxx"
39#include "nodemap.hxx"
40#include "rootnode.hxx"
41#include "setnode.hxx"
42
43namespace configmgr {
44
45namespace {
46
47bool decode(
48 std::u16string_view encoded, std::size_t begin, std::size_t end,
49 OUString * decoded)
50{
51 assert(
52 begin <= end && end <= encoded.size() &&
53 decoded != nullptr);
54 OUStringBuffer buf(end - begin);
55 while (begin != end) {
56 sal_Unicode c = encoded[begin++];
57 if (c == '&') {
58 if (o3tl::starts_with(encoded.substr(begin), u"amp;")) {
59 buf.append('&');
60 begin += RTL_CONSTASCII_LENGTH("amp;");
61 } else if (o3tl::starts_with(encoded.substr(begin), u"quot;")) {
62 buf.append('"');
63 begin += RTL_CONSTASCII_LENGTH("quot;");
64 } else if (o3tl::starts_with(encoded.substr(begin), u"apos;")) {
65 buf.append('\'');
66 begin += RTL_CONSTASCII_LENGTH("apos;");
67 } else {
68 return false;
69 }
70 assert(begin <= end);
71 } else {
72 buf.append(c);
73 }
74 }
75 *decoded = buf.makeStringAndClear();
76 return true;
77}
78
79}
80
82 std::u16string_view templateName, OUString const & name)
83{
84 if (templateName.empty()) {
85 return name;
86 }
87 OUStringBuffer buf(128);
88 //TODO: verify template name contains no bad chars?
89 buf.append(OUString::Concat(templateName) + "['");
90 for (sal_Int32 i = 0; i < name.getLength(); ++i) {
91 sal_Unicode c = name[i];
92 switch (c) {
93 case '&':
94 buf.append("&amp;");
95 break;
96 case '"':
97 buf.append("&quot;");
98 break;
99 case '\'':
100 buf.append("&apos;");
101 break;
102 default:
103 buf.append(c);
104 break;
105 }
106 }
107 buf.append("']");
108 return buf.makeStringAndClear();
109}
110
112 OUString const & path, sal_Int32 index, OUString * name,
113 bool * setElement, OUString * templateName)
114{
115 assert(
116 index >= 0 && index <= path.getLength() && name != nullptr &&
117 setElement != nullptr);
118 sal_Int32 i = index;
119 while (i < path.getLength() && path[i] != '/' && path[i] != '[') {
120 ++i;
121 }
122 if (i == path.getLength() || path[i] == '/') {
123 *name = path.copy(index, i - index);
124 *setElement = false;
125 return i;
126 }
127 if (i - index == 1 && path[index] == '*') {
128 *setElement = true;
129 if (templateName != nullptr) {
130 templateName->clear();
131 }
132 } else {
133 *setElement = i != index;
134 if (templateName != nullptr) {
135 *templateName = path.copy(index, i - index);
136 }
137 }
138 if (++i == path.getLength()) {
139 return -1;
140 }
141 sal_Unicode del = path[i++];
142 if (del != '\'' && del != '"') {
143 return -1;
144 }
145 sal_Int32 j = path.indexOf(del, i);
146 if (j == -1 || j + 1 == path.getLength() || path[j + 1] != ']' ||
147 !decode(path, i, j, name))
148 {
149 return -1;
150 }
151 return j + 2;
152}
153
155 std::u16string_view component, std::u16string_view name)
156{
157 if (component.find(':') != std::u16string_view::npos || name.find(':') != std::u16string_view::npos) {
158 throw css::uno::RuntimeException(
159 OUString::Concat("bad component/name pair containing colon ") + component + "/" +
160 name);
161 }
162 return OUString::Concat(component) + ":" + name;
163}
164
166 OUString const & shortName, OUString const & longName)
167{
168 if (shortName.indexOf(':') == -1) {
169 sal_Int32 i = longName.indexOf(':') + 1;
170 assert(i > 0);
171 return
172 rtl_ustr_compare_WithLength(
173 shortName.getStr(), shortName.getLength(),
174 longName.getStr() + i, longName.getLength() - i) ==
175 0;
176 } else {
177 return shortName == longName;
178 }
179}
180
182
184 OUString const & pathRepresentation,
185 OUString * canonicRepresentation, std::vector<OUString> * path, int * finalizedLayer)
186 const
187{
188 if (pathRepresentation.isEmpty() || pathRepresentation[0] != '/') {
189 throw css::uno::RuntimeException(
190 "bad path " + pathRepresentation);
191 }
192 if (path != nullptr) {
193 path->clear();
194 }
195 if (pathRepresentation == "/") {
196 if (canonicRepresentation != nullptr) {
197 *canonicRepresentation = pathRepresentation;
198 }
199 if (finalizedLayer != nullptr) {
200 *finalizedLayer = NO_LAYER;
201 }
202 return root_;
203 }
204 OUString seg;
205 bool setElement;
206 OUString templateName;
207 sal_Int32 n = parseSegment(pathRepresentation, 1, &seg, &setElement, nullptr);
208 if (n == -1 || setElement)
209 {
210 throw css::uno::RuntimeException(
211 "bad path " + pathRepresentation);
212 }
213 NodeMap const & components = getComponents();
214 NodeMap::const_iterator i(components.find(seg));
215 OUStringBuffer canonic(128);
217 int finalized = NO_LAYER;
218 for (rtl::Reference< Node > p(i == components.end() ? nullptr : i->second);;) {
219 if (!p.is()) {
220 return p;
221 }
222 if (canonicRepresentation != nullptr) {
223 canonic.append("/" + createSegment(templateName, seg));
224 }
225 if (path != nullptr) {
226 path->push_back(seg);
227 }
228 finalized = std::min(finalized, p->getFinalized());
229 if (n != pathRepresentation.getLength() &&
230 pathRepresentation[n++] != '/')
231 {
232 throw css::uno::RuntimeException(
233 "bad path " + pathRepresentation);
234 }
235 // for backwards compatibility, ignore a final slash
236 if (n == pathRepresentation.getLength()) {
237 if (canonicRepresentation != nullptr) {
238 *canonicRepresentation = canonic.makeStringAndClear();
239 }
240 if (finalizedLayer != nullptr) {
241 *finalizedLayer = finalized;
242 }
243 return p;
244 }
245 parent = p;
246 templateName.clear();
247 n = parseSegment(
248 pathRepresentation, n, &seg, &setElement, &templateName);
249 if (n == -1) {
250 throw css::uno::RuntimeException(
251 "bad path " + pathRepresentation);
252 }
253 // For backwards compatibility, allow set members to be accessed with
254 // simple path segments, like group members:
255 p = p->getMember(seg);
256 if (setElement) {
257 switch (parent->kind()) {
259 if (!templateName.isEmpty()) {
260 throw css::uno::RuntimeException(
261 "bad path " + pathRepresentation);
262 }
263 break;
264 case Node::KIND_SET:
265 if (!templateName.isEmpty() &&
266 !static_cast< SetNode * >(parent.get())->isValidTemplate(
267 templateName))
268 {
269 throw css::uno::RuntimeException(
270 "bad path " + pathRepresentation);
271 }
272 break;
273 default:
274 throw css::uno::RuntimeException(
275 "bad path " + pathRepresentation);
276 }
277 if (!templateName.isEmpty() && p != nullptr) {
278 assert(!p->getTemplateName().isEmpty());
279 if (!equalTemplateNames(templateName, p->getTemplateName())) {
280 throw css::uno::RuntimeException(
281 "bad path " + pathRepresentation);
282 }
283 }
284 }
285 }
286}
287
289 int layer, OUString const & fullName) const
290{
291 return templates.findNode(layer, fullName);
292}
293
295 return root_->getMembers();
296}
297
299 OUString const & url, int layer)
300{
302 ExtensionXcuAdditions::iterator i(
304 url, rtl::Reference< ExtensionXcu >()).first);
305 if (i->second.is()) {
306 throw css::uno::RuntimeException(
307 "already added extension xcu " + url);
308 }
309 i->second = item;
310 item->layer = layer;
311 return &item->additions;
312}
313
315 OUString const & url)
316{
317 ExtensionXcuAdditions::iterator i(extensionXcuAdditions_.find(url));
318 if (i == extensionXcuAdditions_.end()) {
319 // This can happen, as migration of pre OOo 3.3 UserInstallation
320 // extensions in dp_registry::backend::configuration::BackendImpl::
321 // PackageImpl::processPackage_ can cause just-in-time creation of
322 // extension xcu files that are never added via addExtensionXcuAdditions
323 // (also, there might be url spelling differences between calls to
324 // addExtensionXcuAdditions and removeExtensionXcuAdditions?):
325 SAL_INFO(
326 "configmgr",
327 "unknown Data::removeExtensionXcuAdditions(" << url << ")");
329 }
330 rtl::Reference< ExtensionXcu > item(i->second);
332 return item;
333}
334
335}
336
337/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
NodeMapImpl::const_iterator const_iterator
Definition: nodemap.hxx:38
iterator end()
Definition: nodemap.hxx:49
rtl::Reference< Node > findNode(int layer, OUString const &name) const
Definition: nodemap.cxx:43
iterator find(const OUString &aStr)
Definition: nodemap.hxx:43
@ KIND_LOCALIZED_PROPERTY
Definition: node.hxx:35
bool isValidTemplate(OUString const &templateName) const
Definition: setnode.cxx:63
OUString name
Definition: components.cxx:85
void * p
sal_Int64 n
#define SAL_INFO(area, stream)
std::vector< std::vector< OUString > > Additions
Definition: additions.hxx:31
int i
index
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
enumrange< T >::Iterator begin(enumrange< T >)
rtl::Reference< RootAccess > root_
rtl::Reference< ExtensionXcu > removeExtensionXcuAdditions(OUString const &url)
Definition: data.cxx:314
NodeMap templates
Definition: data.hxx:49
static OUString fullTemplateName(std::u16string_view component, std::u16string_view name)
Definition: data.cxx:154
ExtensionXcuAdditions extensionXcuAdditions_
Definition: data.hxx:94
rtl::Reference< Node > root_
Definition: data.hxx:92
static sal_Int32 parseSegment(OUString const &path, sal_Int32 index, OUString *name, bool *setElement, OUString *templateName)
Definition: data.cxx:111
rtl::Reference< Node > resolvePathRepresentation(OUString const &pathRepresentation, OUString *canonicRepresentation, std::vector< OUString > *path, int *finalizedLayer) const
Definition: data.cxx:183
NodeMap & getComponents() const
Definition: data.cxx:294
static bool equalTemplateNames(OUString const &shortName, OUString const &longName)
Definition: data.cxx:165
Additions * addExtensionXcuAdditions(OUString const &url, int layer)
Definition: data.cxx:298
rtl::Reference< Node > getTemplate(int layer, OUString const &fullName) const
Definition: data.cxx:288
static OUString createSegment(std::u16string_view templateName, OUString const &name)
Definition: data.cxx:81
sal_uInt16 sal_Unicode