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.configuration2.tree.xpath; 18 19 import java.util.Locale; 20 21 import org.apache.commons.configuration2.tree.NodeHandler; 22 import org.apache.commons.jxpath.ri.QName; 23 import org.apache.commons.jxpath.ri.model.NodePointer; 24 import org.apache.commons.jxpath.ri.model.NodePointerFactory; 25 26 /** 27 * <p> 28 * Implementation of the {@code NodePointerFactory} interface for configuration nodes. 29 * </p> 30 * <p> 31 * This class is able to create {@code NodePointer}s for the nodes of hierarchical configurations. Because there is no 32 * common base class for configuration nodes (any specific configuration implementation can use its own node class) a 33 * trick is needed for activating this factory for a concrete JXPath query: The {@code wrapNode()} method has to be 34 * called with the node object and its corresponding {@code NodeHandler}. This creates a wrapper object containing all 35 * information required by the factory for processing a query. Then this wrapper object has to be passed to the query 36 * methods of the JXPath context. 37 * </p> 38 * 39 * @since 1.3 40 */ 41 public class ConfigurationNodePointerFactory implements NodePointerFactory { 42 /** Constant for the order of this factory. */ 43 public static final int CONFIGURATION_NODE_POINTER_FACTORY_ORDER = 200; 44 45 /** 46 * Gets the order of this factory between other factories. 47 * 48 * @return this order's factory 49 */ 50 @Override 51 public int getOrder() { 52 return CONFIGURATION_NODE_POINTER_FACTORY_ORDER; 53 } 54 55 /** 56 * Creates a node pointer for the specified bean. If the bean is a configuration node (indicated by a wrapper object), a 57 * corresponding pointer is returned. 58 * 59 * @param name the name of the node 60 * @param bean the bean 61 * @param locale the locale 62 * @return a pointer for a configuration node if the bean is such a node 63 */ 64 @Override 65 @SuppressWarnings("unchecked") 66 /* 67 * Type casts are safe here; because of the way the NodeWrapper was constructed the node handler must be compatible with 68 * the node. 69 */ 70 public NodePointer createNodePointer(final QName name, final Object bean, final Locale locale) { 71 if (bean instanceof NodeWrapper) { 72 final NodeWrapper<Object> wrapper = (NodeWrapper<Object>) bean; 73 return new ConfigurationNodePointer<>(wrapper.getNode(), locale, wrapper.getNodeHandler()); 74 } 75 return null; 76 } 77 78 /** 79 * Creates a node pointer for the specified bean. If the bean is a configuration node, a corresponding pointer is 80 * returned. 81 * 82 * @param parent the parent node 83 * @param name the name 84 * @param bean the bean 85 * @return a pointer for a configuration node if the bean is such a node 86 */ 87 @Override 88 @SuppressWarnings("unchecked") 89 /* 90 * Type casts are safe here, see above. Also, the hierarchy of node pointers is consistent, so a parent is compatible to 91 * a child. 92 */ 93 public NodePointer createNodePointer(final NodePointer parent, final QName name, final Object bean) { 94 if (bean instanceof NodeWrapper) { 95 final NodeWrapper<Object> wrapper = (NodeWrapper<Object>) bean; 96 return new ConfigurationNodePointer<>((ConfigurationNodePointer<Object>) parent, wrapper.getNode(), wrapper.getNodeHandler()); 97 } 98 return null; 99 } 100 101 /** 102 * Creates a node wrapper for the specified node and its handler. This wrapper has to be passed to the JXPath context 103 * instead of the original node. 104 * 105 * @param <T> the type of the node 106 * @param node the node 107 * @param handler the corresponding node handler 108 * @return a wrapper for this node 109 */ 110 public static <T> Object wrapNode(final T node, final NodeHandler<T> handler) { 111 return new NodeWrapper<>(node, handler); 112 } 113 114 /** 115 * An internally used wrapper class that holds all information for processing a query for a specific node. 116 * 117 * @param <T> the type of the nodes this class deals with 118 */ 119 static class NodeWrapper<T> { 120 /** Stores the node. */ 121 private final T node; 122 123 /** Stores the corresponding node handler. */ 124 private final NodeHandler<T> nodeHandler; 125 126 /** 127 * Creates a new instance of {@code NodeWrapper} and initializes it. 128 * 129 * @param nd the node 130 * @param handler the node handler 131 */ 132 public NodeWrapper(final T nd, final NodeHandler<T> handler) { 133 node = nd; 134 nodeHandler = handler; 135 } 136 137 /** 138 * Gets the wrapped node. 139 * 140 * @return the node 141 */ 142 public T getNode() { 143 return node; 144 } 145 146 /** 147 * Gets the node handler for the wrapped node. 148 * 149 * @return the node handler 150 */ 151 public NodeHandler<T> getNodeHandler() { 152 return nodeHandler; 153 } 154 } 155 }