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 buf.append(templateName);
89 //TODO: verify template name contains no bad chars?
90 buf.append("['");
91 for (sal_Int32 i = 0; i < name.getLength(); ++i) {
92 sal_Unicode c = name[i];
93 switch (c) {
94 case '&':
95 buf.append("&amp;");
96 break;
97 case '"':
98 buf.append("&quot;");
99 break;
100 case '\'':
101 buf.append("&apos;");
102 break;
103 default:
104 buf.append(c);
105 break;
106 }
107 }
108 buf.append("']");
109 return buf.makeStringAndClear();
110}
111
113 OUString const & path, sal_Int32 index, OUString * name,
114 bool * setElement, OUString * templateName)
115{
116 assert(
117 index >= 0 && index <= path.getLength() && name != nullptr &&
118 setElement != nullptr);
119 sal_Int32 i = index;
120 while (i < path.getLength() && path[i] != '/' && path[i] != '[') {
121 ++i;
122 }
123 if (i == path.getLength() || path[i] == '/') {
124 *name = path.copy(index, i - index);
125 *setElement = false;
126 return i;
127 }
128 if (templateName != nullptr) {
129 if (i - index == 1 && path[index] == '*') {
130 templateName->clear();
131 } else {
132 *templateName = path.copy(index, i - index);
133 }
134 }
135 if (++i == path.getLength()) {
136 return -1;
137 }
138 sal_Unicode del = path[i++];
139 if (del != '\'' && del != '"') {
140 return -1;
141 }
142 sal_Int32 j = path.indexOf(del, i);
143 if (j == -1 || j + 1 == path.getLength() || path[j + 1] != ']' ||
144 !decode(path, i, j, name))
145 {
146 return -1;
147 }
148 *setElement = true;
149 return j + 2;
150}
151
153 std::u16string_view component, std::u16string_view name)
154{
155 if (component.find(':') != std::u16string_view::npos || name.find(':') != std::u16string_view::npos) {
156 throw css::uno::RuntimeException(
157 OUString::Concat("bad component/name pair containing colon ") + component + "/" +
158 name);
159 }
160 return OUString::Concat(component) + ":" + name;
161}
162
164 OUString const & shortName, OUString const & longName)
165{
166 if (shortName.indexOf(':') == -1) {
167 sal_Int32 i = longName.indexOf(':') + 1;
168 assert(i > 0);
169 return
170 rtl_ustr_compare_WithLength(
171 shortName.getStr(), shortName.getLength(),
172 longName.getStr() + i, longName.getLength() - i) ==
173 0;
174 } else {
175 return shortName == longName;
176 }
177}
178
180
182 OUString const & pathRepresentation,
183 OUString * canonicRepresentation, std::vector<OUString> * path, int * finalizedLayer)
184 const
185{
186 if (pathRepresentation.isEmpty() || pathRepresentation[0] != '/') {
187 throw css::uno::RuntimeException(
188 "bad path " + pathRepresentation);
189 }
190 if (path != nullptr) {
191 path->clear();
192 }
193 if (pathRepresentation == "/") {
194 if (canonicRepresentation != nullptr) {
195 *canonicRepresentation = pathRepresentation;
196 }
197 if (finalizedLayer != nullptr) {
198 *finalizedLayer = NO_LAYER;
199 }
200 return root_;
201 }
202 OUString seg;
203 bool setElement;
204 OUString templateName;
205 sal_Int32 n = parseSegment(pathRepresentation, 1, &seg, &setElement, nullptr);
206 if (n == -1 || setElement)
207 {
208 throw css::uno::RuntimeException(
209 "bad path " + pathRepresentation);
210 }
211 NodeMap const & components = getComponents();
212 NodeMap::const_iterator i(components.find(seg));
213 OUStringBuffer canonic(128);
215 int finalized = NO_LAYER;
216 for (rtl::Reference< Node > p(i == components.end() ? nullptr : i->second);;) {
217 if (!p.is()) {
218 return p;
219 }
220 if (canonicRepresentation != nullptr) {
221 canonic.append('/');
222 canonic.append(createSegment(templateName, seg));
223 }
224 if (path != nullptr) {
225 path->push_back(seg);
226 }
227 finalized = std::min(finalized, p->getFinalized());
228 if (n != pathRepresentation.getLength() &&
229 pathRepresentation[n++] != '/')
230 {
231 throw css::uno::RuntimeException(
232 "bad path " + pathRepresentation);
233 }
234 // for backwards compatibility, ignore a final slash
235 if (n == pathRepresentation.getLength()) {
236 if (canonicRepresentation != nullptr) {
237 *canonicRepresentation = canonic.makeStringAndClear();
238 }
239 if (finalizedLayer != nullptr) {
240 *finalizedLayer = finalized;
241 }
242 return p;
243 }
244 parent = p;
245 templateName.clear();
246 n = parseSegment(
247 pathRepresentation, n, &seg, &setElement, &templateName);
248 if (n == -1) {
249 throw css::uno::RuntimeException(
250 "bad path " + pathRepresentation);
251 }
252 // For backwards compatibility, allow set members to be accessed with
253 // simple path segments, like group members:
254 p = p->getMember(seg);
255 if (setElement) {
256 switch (parent->kind()) {
258 if (!templateName.isEmpty()) {
259 throw css::uno::RuntimeException(
260 "bad path " + pathRepresentation);
261 }
262 break;
263 case Node::KIND_SET:
264 if (!templateName.isEmpty() &&
265 !static_cast< SetNode * >(parent.get())->isValidTemplate(
266 templateName))
267 {
268 throw css::uno::RuntimeException(
269 "bad path " + pathRepresentation);
270 }
271 break;
272 default:
273 throw css::uno::RuntimeException(
274 "bad path " + pathRepresentation);
275 }
276 if (!templateName.isEmpty() && p != nullptr) {
277 assert(!p->getTemplateName().isEmpty());
278 if (!equalTemplateNames(templateName, p->getTemplateName())) {
279 throw css::uno::RuntimeException(
280 "bad path " + pathRepresentation);
281 }
282 }
283 }
284 }
285}
286
288 int layer, OUString const & fullName) const
289{
290 return templates.findNode(layer, fullName);
291}
292
294 return root_->getMembers();
295}
296
298 OUString const & url, int layer)
299{
301 ExtensionXcuAdditions::iterator i(
303 url, rtl::Reference< ExtensionXcu >()).first);
304 if (i->second.is()) {
305 throw css::uno::RuntimeException(
306 "already added extension xcu " + url);
307 }
308 i->second = item;
309 item->layer = layer;
310 return &item->additions;
311}
312
314 OUString const & url)
315{
316 ExtensionXcuAdditions::iterator i(extensionXcuAdditions_.find(url));
317 if (i == extensionXcuAdditions_.end()) {
318 // This can happen, as migration of pre OOo 3.3 UserInstallation
319 // extensions in dp_registry::backend::configuration::BackendImpl::
320 // PackageImpl::processPackage_ can cause just-in-time creation of
321 // extension xcu files that are never added via addExtensionXcuAdditions
322 // (also, there might be url spelling differences between calls to
323 // addExtensionXcuAdditions and removeExtensionXcuAdditions?):
324 SAL_INFO(
325 "configmgr",
326 "unknown Data::removeExtensionXcuAdditions(" << url << ")");
328 }
329 rtl::Reference< ExtensionXcu > item(i->second);
331 return item;
332}
333
334}
335
336/* 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:313
NodeMap templates
Definition: data.hxx:49
static OUString fullTemplateName(std::u16string_view component, std::u16string_view name)
Definition: data.cxx:152
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:112
rtl::Reference< Node > resolvePathRepresentation(OUString const &pathRepresentation, OUString *canonicRepresentation, std::vector< OUString > *path, int *finalizedLayer) const
Definition: data.cxx:181
NodeMap & getComponents() const
Definition: data.cxx:293
static bool equalTemplateNames(OUString const &shortName, OUString const &longName)
Definition: data.cxx:163
Additions * addExtensionXcuAdditions(OUString const &url, int layer)
Definition: data.cxx:297
rtl::Reference< Node > getTemplate(int layer, OUString const &fullName) const
Definition: data.cxx:287
static OUString createSegment(std::u16string_view templateName, OUString const &name)
Definition: data.cxx:81
sal_uInt16 sal_Unicode