xref: /trunk/main/configmgr/source/partial.cxx (revision e4e2125aa62e0f7eb93feb1a28a0a374d6afbf8c)
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 
getOrCreateChild(Node * parent,rtl::OUString const & name)71 Partial::Node * Partial::getOrCreateChild(
72     Node * parent, rtl::OUString const & name)
73 {
74     Node::Children::iterator i(parent->children.find(name));
75     if (i == parent->children.end()) {
76         i = parent->children.insert(
77             Node::Children::value_type(name, Node::NodePtr(new Node))).first;
78     }
79     return i->second.get();
80 }
81 
Partial(std::set<rtl::OUString> const & includedPaths,std::set<rtl::OUString> const & excludedPaths)82 Partial::Partial(
83     std::set< rtl::OUString > const & includedPaths,
84     std::set< rtl::OUString > const & excludedPaths)
85 {
86     for (std::set< rtl::OUString >::const_iterator i(includedPaths.begin());
87          i != includedPaths.end(); ++i)
88     {
89         sal_Int32 n = 0;
90         for (Node * p = &root_;;) {
91             rtl::OUString seg;
92             bool end = parseSegment(*i, &n, &seg);
93             p = getOrCreateChild(p, seg);
94             if (p->startInclude) {
95                 break;
96             }
97             if (end) {
98                 p->children.clear();
99                 p->startInclude = true;
100                 break;
101             }
102         }
103     }
104     for (std::set< rtl::OUString >::const_iterator i(excludedPaths.begin());
105          i != excludedPaths.end(); ++i)
106     {
107         sal_Int32 n = 0;
108         for (Node * p = &root_;;) {
109             rtl::OUString seg;
110             bool end = parseSegment(*i, &n, &seg);
111             if (end) {
112                 getOrCreateChild(p, seg)->clear();
113                 break;
114             }
115             Node::Children::iterator j(p->children.find(seg));
116             if (j == p->children.end()) {
117                 break;
118             }
119             p = j->second.get();
120         }
121     }
122 }
123 
~Partial()124 Partial::~Partial() {}
125 
contains(Path const & path) const126 Partial::Containment Partial::contains(Path const & path) const {
127     //TODO: For set elements, the segment names recorded in the node tree need
128     // not match the corresponding path segments, so this function can fail.
129     Node const * p = &root_;
130     bool includes = false;
131     for (Path::const_iterator i(path.begin()); i != path.end(); ++i) {
132         Node::Children::const_iterator j(p->children.find(*i));
133         if ( j == p->children.end() )
134         {
135             break;
136         }
137         p = j->second.get();
138         includes |= p->startInclude;
139     }
140     return ( ( p->children.empty() || p == &root_ )
141              && !p->startInclude )
142            ? CONTAINS_NOT
143            : ( includes ? CONTAINS_NODE : CONTAINS_SUBNODES );
144 }
145 
146 }
147