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 */
017
018package org.apache.commons.jxpath.ri.model;
019
020import java.util.Locale;
021
022import org.apache.commons.jxpath.JXPathContext;
023import org.apache.commons.jxpath.Variables;
024import org.apache.commons.jxpath.ri.QName;
025
026/**
027 * NodePointerFactory to create {@link VariablePointer VariablePointers}.
028 *
029 * @since JXPath 1.3
030 */
031public class VariablePointerFactory implements NodePointerFactory {
032
033    /**
034     * Node value wrapper to trigger a VariablePointerFactory.
035     */
036    public static final class VariableContextWrapper {
037
038        private final JXPathContext context;
039
040        /**
041         * Constructs a new VariableContextWrapper.
042         *
043         * @param context to wrap
044         */
045        private VariableContextWrapper(final JXPathContext context) {
046            this.context = context;
047        }
048
049        /**
050         * Gets the original (unwrapped) context.
051         *
052         * @return JXPathContext.
053         */
054        public JXPathContext getContext() {
055            return context;
056        }
057    }
058
059    /** Factory order constant */
060    public static final int VARIABLE_POINTER_FACTORY_ORDER = 890;
061
062    /**
063     * VariableContextWrapper factory method.
064     *
065     * @param context the JXPathContext to wrap.
066     * @return VariableContextWrapper.
067     */
068    public static VariableContextWrapper contextWrapper(final JXPathContext context) {
069        return new VariableContextWrapper(context);
070    }
071
072    /**
073     * Constructs a new instance.
074     */
075    public VariablePointerFactory() {
076        // empty
077    }
078
079    @Override
080    public NodePointer createNodePointer(final NodePointer parent, final QName qName, final Object object) {
081        return createNodePointer(qName, object, null);
082    }
083
084    @Override
085    public NodePointer createNodePointer(final QName qName, final Object object, final Locale locale) {
086        if (object instanceof VariableContextWrapper) {
087            JXPathContext varCtx = ((VariableContextWrapper) object).getContext();
088            while (varCtx != null) {
089                final Variables vars = varCtx.getVariables();
090                if (vars.isDeclaredVariable(qName.toString())) {
091                    return new VariablePointer(vars, qName);
092                }
093                varCtx = varCtx.getParentContext();
094            }
095            // The variable is not declared, but we will create
096            // a pointer anyway in case the user wants to set, rather
097            // than get, the value of the variable.
098            return new VariablePointer(qName);
099        }
100        return null;
101    }
102
103    @Override
104    public int getOrder() {
105        return VARIABLE_POINTER_FACTORY_ORDER;
106    }
107}