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