1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include "precompiled_configmgr.hxx" 29 #include "sal/config.h" 30 31 #include <map> 32 #include <set> 33 34 #include "com/sun/star/uno/Reference.hxx" 35 #include "com/sun/star/uno/RuntimeException.hpp" 36 #include "com/sun/star/uno/XInterface.hpp" 37 #include "osl/diagnose.h" 38 #include "rtl/ustring.h" 39 #include "rtl/ustring.hxx" 40 #include "sal/types.h" 41 42 #include "data.hxx" 43 #include "partial.hxx" 44 45 namespace configmgr { 46 47 namespace { 48 49 namespace css = com::sun::star; 50 51 bool parseSegment( 52 rtl::OUString const & path, sal_Int32 * index, rtl::OUString * segment) 53 { 54 OSL_ASSERT( 55 index != 0 && *index >= 0 && *index <= path.getLength() && 56 segment != 0); 57 if (path[(*index)++] == '/') { 58 rtl::OUString name; 59 bool setElement; 60 rtl::OUString templateName; 61 *index = Data::parseSegment( 62 path, *index, &name, &setElement, &templateName); 63 if (*index != -1) { 64 *segment = Data::createSegment(templateName, name); 65 return *index == path.getLength(); 66 } 67 } 68 throw css::uno::RuntimeException( 69 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bad path ")) + path, 70 css::uno::Reference< css::uno::XInterface >()); 71 } 72 73 } 74 75 Partial::Partial( 76 std::set< rtl::OUString > const & includedPaths, 77 std::set< rtl::OUString > const & excludedPaths) 78 { 79 for (std::set< rtl::OUString >::const_iterator i(includedPaths.begin()); 80 i != includedPaths.end(); ++i) 81 { 82 sal_Int32 n = 0; 83 for (Node * p = &root_;;) { 84 rtl::OUString seg; 85 bool end = parseSegment(*i, &n, &seg); 86 p = &p->children[seg]; 87 if (p->startInclude) { 88 break; 89 } 90 if (end) { 91 p->children.clear(); 92 p->startInclude = true; 93 break; 94 } 95 } 96 } 97 for (std::set< rtl::OUString >::const_iterator i(excludedPaths.begin()); 98 i != excludedPaths.end(); ++i) 99 { 100 sal_Int32 n = 0; 101 for (Node * p = &root_;;) { 102 rtl::OUString seg; 103 bool end = parseSegment(*i, &n, &seg); 104 if (end) { 105 p->children[seg] = Node(); 106 break; 107 } 108 Node::Children::iterator j(p->children.find(seg)); 109 if (j == p->children.end()) { 110 break; 111 } 112 p = &j->second; 113 } 114 } 115 } 116 117 Partial::~Partial() {} 118 119 Partial::Containment Partial::contains(Path const & path) const { 120 //TODO: For set elements, the segment names recorded in the node tree need 121 // not match the corresponding path segments, so this function can fail. 122 Node const * p = &root_; 123 bool includes = false; 124 for (Path::const_iterator i(path.begin()); i != path.end(); ++i) { 125 Node::Children::const_iterator j(p->children.find(*i)); 126 if (j == p->children.end()) { 127 break; 128 } 129 p = &j->second; 130 includes |= p->startInclude; 131 } 132 return p->children.empty() && !p->startInclude 133 ? CONTAINS_NOT 134 : includes ? CONTAINS_NODE : CONTAINS_SUBNODES; 135 } 136 137 } 138