xref: /trunk/main/configmgr/source/partial.cxx (revision 8235712b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_configmgr.hxx"
25 #include "sal/config.h"
26 
27 #include <map>
28 #include <set>
29 
30 #include "com/sun/star/uno/Reference.hxx"
31 #include "com/sun/star/uno/RuntimeException.hpp"
32 #include "com/sun/star/uno/XInterface.hpp"
33 #include "osl/diagnose.h"
34 #include "rtl/ustring.h"
35 #include "rtl/ustring.hxx"
36 #include "sal/types.h"
37 
38 #include "data.hxx"
39 #include "partial.hxx"
40 
41 namespace configmgr {
42 
43 namespace {
44 
45 namespace css = com::sun::star;
46 
parseSegment(rtl::OUString const & path,sal_Int32 * index,rtl::OUString * segment)47 bool parseSegment(
48     rtl::OUString const & path, sal_Int32 * index, rtl::OUString * segment)
49 {
50     OSL_ASSERT(
51         index != 0 && *index >= 0 && *index <= path.getLength() &&
52         segment != 0);
53     if (path[(*index)++] == '/') {
54         rtl::OUString name;
55         bool setElement;
56         rtl::OUString templateName;
57         *index = Data::parseSegment(
58             path, *index, &name, &setElement, &templateName);
59         if (*index != -1) {
60             *segment = Data::createSegment(templateName, name);
61             return *index == path.getLength();
62         }
63     }
64     throw css::uno::RuntimeException(
65         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bad path ")) + path,
66         css::uno::Reference< css::uno::XInterface >());
67 }
68 
69 }
70 
Partial(std::set<rtl::OUString> const & includedPaths,std::set<rtl::OUString> const & excludedPaths)71 Partial::Partial(
72     std::set< rtl::OUString > const & includedPaths,
73     std::set< rtl::OUString > const & excludedPaths)
74 {
75     for (std::set< rtl::OUString >::const_iterator i(includedPaths.begin());
76          i != includedPaths.end(); ++i)
77     {
78         sal_Int32 n = 0;
79         for (Node * p = &root_;;) {
80             rtl::OUString seg;
81             bool end = parseSegment(*i, &n, &seg);
82             p = &p->children[seg];
83             if (p->startInclude) {
84                 break;
85             }
86             if (end) {
87                 p->children.clear();
88                 p->startInclude = true;
89                 break;
90             }
91         }
92     }
93     for (std::set< rtl::OUString >::const_iterator i(excludedPaths.begin());
94          i != excludedPaths.end(); ++i)
95     {
96         sal_Int32 n = 0;
97         for (Node * p = &root_;;) {
98             rtl::OUString seg;
99             bool end = parseSegment(*i, &n, &seg);
100             if (end) {
101                 p->children[seg].clear();
102                 break;
103             }
104             Node::Children::iterator j(p->children.find(seg));
105             if (j == p->children.end()) {
106                 break;
107             }
108             p = &j->second;
109         }
110     }
111 }
112 
~Partial()113 Partial::~Partial() {}
114 
contains(Path const & path) const115 Partial::Containment Partial::contains(Path const & path) const {
116     //TODO: For set elements, the segment names recorded in the node tree need
117     // not match the corresponding path segments, so this function can fail.
118     Node const * p = &root_;
119     bool includes = false;
120     for (Path::const_iterator i(path.begin()); i != path.end(); ++i) {
121         Node::Children::const_iterator j(p->children.find(*i));
122         if ( j == p->children.end() )
123         {
124             break;
125         }
126         p = &j->second;
127         includes |= p->startInclude;
128     }
129     return ( ( p->children.empty() || p == &root_ )
130              && !p->startInclude )
131            ? CONTAINS_NOT
132            : ( includes ? CONTAINS_NODE : CONTAINS_SUBNODES );
133 }
134 
135 }
136