    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.configuration2.tree.xpath;
    018
    019import java.util.Locale;
    020
    021import org.apache.commons.configuration2.tree.NodeHandler;
    022import org.apache.commons.jxpath.ri.QName;
    023import org.apache.commons.jxpath.ri.model.NodePointer;
    024import org.apache.commons.jxpath.ri.model.NodePointerFactory;
    025
    026/**
    027 * <p>
    028 * Implementation of the {@code NodePointerFactory} interface for configuration nodes.
    029 * </p>
    030 * <p>
    031 * This class is able to create {@code NodePointer}s for the nodes of hierarchical configurations. Because there is no
    032 * common base class for configuration nodes (any specific configuration implementation can use its own node class) a
    033 * trick is needed for activating this factory for a concrete JXPath query: The {@code wrapNode()} method has to be
    034 * called with the node object and its corresponding {@code NodeHandler}. This creates a wrapper object containing all
    035 * information required by the factory for processing a query. Then this wrapper object has to be passed to the query
    036 * methods of the JXPath context.
    037 * </p>
    038 *
    039 * @since 1.3
    040 */
    041public class ConfigurationNodePointerFactory implements NodePointerFactory {
    042    /** Constant for the order of this factory. */
    043    public static final int CONFIGURATION_NODE_POINTER_FACTORY_ORDER = 200;
    044
    045    /**
    046     * Gets the order of this factory between other factories.
    047     *
    048     * @return this order's factory
    049     */
    050    @Override
    051    public int getOrder() {
    052        return CONFIGURATION_NODE_POINTER_FACTORY_ORDER;
    053    }
    054
    055    /**
    056     * Creates a node pointer for the specified bean. If the bean is a configuration node (indicated by a wrapper object), a
    057     * corresponding pointer is returned.
    058     *
    059     * @param name the name of the node
    060     * @param bean the bean
    061     * @param locale the locale
    062     * @return a pointer for a configuration node if the bean is such a node
    063     */
    064    @Override
    065    @SuppressWarnings("unchecked")
    066    /*
    067     * Type casts are safe here; because of the way the NodeWrapper was constructed the node handler must be compatible with
    068     * the node.
    069     */
    070    public NodePointer createNodePointer(final QName name, final Object bean, final Locale locale) {
    071        if (bean instanceof NodeWrapper) {
    072            final NodeWrapper<Object> wrapper = (NodeWrapper<Object>) bean;
    073            return new ConfigurationNodePointer<>(wrapper.getNode(), locale, wrapper.getNodeHandler());
    074        }
    075        return null;
    076    }
    077
    078    /**
    079     * Creates a node pointer for the specified bean. If the bean is a configuration node, a corresponding pointer is
    080     * returned.
    081     *
    082     * @param parent the parent node
    083     * @param name the name
    084     * @param bean the bean
    085     * @return a pointer for a configuration node if the bean is such a node
    086     */
    087    @Override
    088    @SuppressWarnings("unchecked")
    089    /*
    090     * Type casts are safe here, see above. Also, the hierarchy of node pointers is consistent, so a parent is compatible to
    091     * a child.
    092     */
    093    public NodePointer createNodePointer(final NodePointer parent, final QName name, final Object bean) {
    094        if (bean instanceof NodeWrapper) {
    095            final NodeWrapper<Object> wrapper = (NodeWrapper<Object>) bean;
    096            return new ConfigurationNodePointer<>((ConfigurationNodePointer<Object>) parent, wrapper.getNode(), wrapper.getNodeHandler());
    097        }
    098        return null;
    099    }
    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}