1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
26 #include "com/sun/star/uno/Reference.hxx"
27 #include "com/sun/star/uno/RuntimeException.hpp"
28 #include "com/sun/star/uno/XInterface.hpp"
29 #include "rtl/ustring.h"
30 #include "rtl/ustring.hxx"
31 #include "sal/types.h"
34 #include "partial.hxx"
41 OUString
const & path
, sal_Int32
* index
, OUString
* segment
)
44 index
!= 0 && *index
>= 0 && *index
<= path
.getLength() &&
46 if (path
[(*index
)++] == '/') {
49 OUString templateName
;
50 *index
= Data::parseSegment(
51 path
, *index
, &name
, &setElement
, &templateName
);
53 *segment
= Data::createSegment(templateName
, name
);
54 return *index
== path
.getLength();
57 throw css::uno::RuntimeException(
58 OUString("bad path ") + path
,
59 css::uno::Reference
< css::uno::XInterface
>());
65 std::set
< OUString
> const & includedPaths
,
66 std::set
< OUString
> const & excludedPaths
)
68 // The Partial::Node tree built up here encodes the following information:
69 // * Inner node, startInclude: an include starts here that contains excluded
71 // * Inner node, !startInclude: contains in-/excluded sub-trees
72 // * Leaf node, startInclude: an include starts here
73 // * Leaf node, !startInclude: an exclude starts here
74 for (std::set
< OUString
>::const_iterator
i(includedPaths
.begin());
75 i
!= includedPaths
.end(); ++i
)
78 for (Node
* p
= &root_
;;) {
80 bool end
= parseSegment(*i
, &n
, &seg
);
81 p
= &p
->children
[seg
];
82 if (p
->startInclude
) {
87 p
->startInclude
= true;
92 for (std::set
< OUString
>::const_iterator
i(excludedPaths
.begin());
93 i
!= excludedPaths
.end(); ++i
)
96 for (Node
* p
= &root_
;;) {
98 bool end
= parseSegment(*i
, &n
, &seg
);
100 p
->children
[seg
].clear();
103 Node::Children::iterator
j(p
->children
.find(seg
));
104 if (j
== p
->children
.end()) {
112 Partial::~Partial() {}
114 Partial::Containment
Partial::contains(Path
const & path
) const {
115 //TODO: For set elements, the segment names recorded in the node tree need
116 // not match the corresponding path segments, so this function can fail.
118 // * If path ends at a leaf node or goes past a leaf node:
119 // ** If that leaf node is startInclude: => CONTAINS_NODE
120 // ** If that leaf node is !startInclude: => CONTAINS_NOT
121 // * If path ends at inner node:
122 // ** If there is some startInclude along its trace: => CONTAINS_NODE
123 // ** If there is no startInclude along its trace: => CONTAINS_SUBNODES
124 Node
const * p
= &root_
;
125 bool includes
= false;
126 for (Path::const_iterator
i(path
.begin()); i
!= path
.end(); ++i
) {
127 Node::Children::const_iterator
j(p
->children
.find(*i
));
128 if (j
== p
->children
.end()) {
129 return p
->startInclude
? CONTAINS_NODE
: CONTAINS_NOT
;
132 includes
|= p
->startInclude
;
134 return p
->children
.empty() && !p
->startInclude
136 : includes
? CONTAINS_NODE
: CONTAINS_SUBNODES
;
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */