| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ConfigurationNodePointer |
|
| 2.0;2 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.configuration.tree.xpath; | |
| 18 | ||
| 19 | import java.util.List; | |
| 20 | import java.util.Locale; | |
| 21 | ||
| 22 | import org.apache.commons.configuration.tree.ConfigurationNode; | |
| 23 | import org.apache.commons.jxpath.ri.Compiler; | |
| 24 | import org.apache.commons.jxpath.ri.QName; | |
| 25 | import org.apache.commons.jxpath.ri.compiler.NodeTest; | |
| 26 | import org.apache.commons.jxpath.ri.compiler.NodeTypeTest; | |
| 27 | import org.apache.commons.jxpath.ri.model.NodeIterator; | |
| 28 | import org.apache.commons.jxpath.ri.model.NodePointer; | |
| 29 | ||
| 30 | /** | |
| 31 | * <p> | |
| 32 | * A specific {@code NodePointer} implementation for configuration nodes. | |
| 33 | * </p> | |
| 34 | * <p> | |
| 35 | * This is needed for queries using JXPath. | |
| 36 | * </p> | |
| 37 | * | |
| 38 | * @since 1.3 | |
| 39 | * @author <a | |
| 40 | * href="http://commons.apache.org/configuration/team-list.html">Commons | |
| 41 | * Configuration team</a> | |
| 42 | * @version $Id: ConfigurationNodePointer.java 1206496 2011-11-26 17:01:14Z oheger $ | |
| 43 | */ | |
| 44 | class ConfigurationNodePointer extends NodePointer | |
| 45 | { | |
| 46 | /** | |
| 47 | * The serial version UID. | |
| 48 | */ | |
| 49 | private static final long serialVersionUID = -1087475639680007713L; | |
| 50 | ||
| 51 | /** Stores the associated configuration node. */ | |
| 52 | private ConfigurationNode node; | |
| 53 | ||
| 54 | /** | |
| 55 | * Creates a new instance of {@code ConfigurationNodePointer}. | |
| 56 | * | |
| 57 | * @param node the node | |
| 58 | * @param locale the locale | |
| 59 | */ | |
| 60 | public ConfigurationNodePointer(ConfigurationNode node, Locale locale) | |
| 61 | { | |
| 62 | 81946 | super(null, locale); |
| 63 | 81946 | this.node = node; |
| 64 | 81946 | } |
| 65 | ||
| 66 | /** | |
| 67 | * Creates a new instance of {@code ConfigurationNodePointer} and | |
| 68 | * initializes it with its parent pointer. | |
| 69 | * | |
| 70 | * @param parent the parent pointer | |
| 71 | * @param node the associated node | |
| 72 | */ | |
| 73 | public ConfigurationNodePointer(NodePointer parent, ConfigurationNode node) | |
| 74 | { | |
| 75 | 415874 | super(parent); |
| 76 | 415874 | this.node = node; |
| 77 | 415874 | } |
| 78 | ||
| 79 | /** | |
| 80 | * Returns a flag whether this node is a leaf. This is the case if there are | |
| 81 | * no child nodes. | |
| 82 | * | |
| 83 | * @return a flag if this node is a leaf | |
| 84 | */ | |
| 85 | @Override | |
| 86 | public boolean isLeaf() | |
| 87 | { | |
| 88 | 2349 | return node.getChildrenCount() < 1; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Returns a flag if this node is a collection. This is not the case. | |
| 93 | * | |
| 94 | * @return the collection flag | |
| 95 | */ | |
| 96 | @Override | |
| 97 | public boolean isCollection() | |
| 98 | { | |
| 99 | 0 | return false; |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * Returns this node's length. This is always 1. | |
| 104 | * | |
| 105 | * @return the node's length | |
| 106 | */ | |
| 107 | @Override | |
| 108 | public int getLength() | |
| 109 | { | |
| 110 | 398554 | return 1; |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * Checks whether this node pointer refers to an attribute node. This method | |
| 115 | * checks the attribute flag of the associated configuration node. | |
| 116 | * | |
| 117 | * @return the attribute flag | |
| 118 | */ | |
| 119 | @Override | |
| 120 | public boolean isAttribute() | |
| 121 | { | |
| 122 | 782 | return node.isAttribute(); |
| 123 | } | |
| 124 | ||
| 125 | /** | |
| 126 | * Returns this node's name. | |
| 127 | * | |
| 128 | * @return the name | |
| 129 | */ | |
| 130 | @Override | |
| 131 | public QName getName() | |
| 132 | { | |
| 133 | 3 | return new QName(null, node.getName()); |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * Returns this node's base value. This is the associated configuration | |
| 138 | * node. | |
| 139 | * | |
| 140 | * @return the base value | |
| 141 | */ | |
| 142 | @Override | |
| 143 | public Object getBaseValue() | |
| 144 | { | |
| 145 | 760 | return node; |
| 146 | } | |
| 147 | ||
| 148 | /** | |
| 149 | * Returns the immediate node. This is the associated configuration node. | |
| 150 | * | |
| 151 | * @return the immediate node | |
| 152 | */ | |
| 153 | @Override | |
| 154 | public Object getImmediateNode() | |
| 155 | { | |
| 156 | 361838 | return node; |
| 157 | } | |
| 158 | ||
| 159 | /** | |
| 160 | * Returns the value of this node. | |
| 161 | * | |
| 162 | * @return the represented node's value | |
| 163 | */ | |
| 164 | @Override | |
| 165 | public Object getValue() | |
| 166 | { | |
| 167 | 138434 | return node.getValue(); |
| 168 | } | |
| 169 | ||
| 170 | /** | |
| 171 | * Sets the value of this node. | |
| 172 | * | |
| 173 | * @param value the new value | |
| 174 | */ | |
| 175 | @Override | |
| 176 | public void setValue(Object value) | |
| 177 | { | |
| 178 | 0 | node.setValue(value); |
| 179 | 0 | } |
| 180 | ||
| 181 | /** | |
| 182 | * Compares two child node pointers. | |
| 183 | * | |
| 184 | * @param pointer1 one pointer | |
| 185 | * @param pointer2 another pointer | |
| 186 | * @return a flag, which pointer should be sorted first | |
| 187 | */ | |
| 188 | @Override | |
| 189 | public int compareChildNodePointers(NodePointer pointer1, | |
| 190 | NodePointer pointer2) | |
| 191 | { | |
| 192 | 380 | ConfigurationNode node1 = (ConfigurationNode) pointer1.getBaseValue(); |
| 193 | 380 | ConfigurationNode node2 = (ConfigurationNode) pointer2.getBaseValue(); |
| 194 | ||
| 195 | // attributes will be sorted before child nodes | |
| 196 | 380 | if (node1.isAttribute() && !node2.isAttribute()) |
| 197 | { | |
| 198 | 1 | return -1; |
| 199 | } | |
| 200 | 379 | else if (node2.isAttribute() && !node1.isAttribute()) |
| 201 | { | |
| 202 | 1 | return 1; |
| 203 | } | |
| 204 | ||
| 205 | else | |
| 206 | { | |
| 207 | // sort based on the occurrence in the sub node list | |
| 208 | 378 | List<ConfigurationNode> subNodes = node1.isAttribute() ? node.getAttributes() : node |
| 209 | .getChildren(); | |
| 210 | 378 | for (ConfigurationNode child : subNodes) |
| 211 | { | |
| 212 | 946 | if (child == node1) |
| 213 | { | |
| 214 | 374 | return -1; |
| 215 | } | |
| 216 | 572 | else if (child == node2) |
| 217 | { | |
| 218 | 2 | return 1; |
| 219 | } | |
| 220 | } | |
| 221 | 2 | return 0; // should not happen |
| 222 | } | |
| 223 | } | |
| 224 | ||
| 225 | /** | |
| 226 | * Returns an iterator for the attributes that match the given name. | |
| 227 | * | |
| 228 | * @param name the attribute name | |
| 229 | * @return the iterator for the attributes | |
| 230 | */ | |
| 231 | @Override | |
| 232 | public NodeIterator attributeIterator(QName name) | |
| 233 | { | |
| 234 | 145489 | return new ConfigurationNodeIteratorAttribute(this, name); |
| 235 | } | |
| 236 | ||
| 237 | /** | |
| 238 | * Returns an iterator for the children of this pointer that match the given | |
| 239 | * test object. | |
| 240 | * | |
| 241 | * @param test the test object | |
| 242 | * @param reverse the reverse flag | |
| 243 | * @param startWith the start value of the iteration | |
| 244 | */ | |
| 245 | @Override | |
| 246 | public NodeIterator childIterator(NodeTest test, boolean reverse, | |
| 247 | NodePointer startWith) | |
| 248 | { | |
| 249 | 161135 | return new ConfigurationNodeIteratorChildren(this, test, reverse, |
| 250 | startWith); | |
| 251 | } | |
| 252 | ||
| 253 | /** | |
| 254 | * Tests if this node matches the given test. Configuration nodes are text | |
| 255 | * nodes, too because they can contain a value. | |
| 256 | * | |
| 257 | * @param test the test object | |
| 258 | * @return a flag if this node corresponds to the test | |
| 259 | */ | |
| 260 | @Override | |
| 261 | public boolean testNode(NodeTest test) | |
| 262 | { | |
| 263 | 2346 | if (test instanceof NodeTypeTest |
| 264 | && ((NodeTypeTest) test).getNodeType() == Compiler.NODE_TYPE_TEXT) | |
| 265 | { | |
| 266 | 0 | return true; |
| 267 | } | |
| 268 | 2346 | return super.testNode(test); |
| 269 | } | |
| 270 | } |