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.dynamic;
019
020import java.util.Locale;
021
022import org.apache.commons.jxpath.DynamicPropertyHandler;
023import org.apache.commons.jxpath.JXPathBeanInfo;
024import org.apache.commons.jxpath.JXPathIntrospector;
025import org.apache.commons.jxpath.ri.QName;
026import org.apache.commons.jxpath.ri.model.NodePointer;
027import org.apache.commons.jxpath.ri.model.NodePointerFactory;
028import org.apache.commons.jxpath.ri.model.beans.NullPointer;
029import org.apache.commons.jxpath.util.ValueUtils;
030
031/**
032 * Implements NodePointerFactory for Dynamic classes like Map.
033 */
034public class DynamicPointerFactory implements NodePointerFactory {
035
036    /**
037     * Factory order constant.
038     */
039    public static final int DYNAMIC_POINTER_FACTORY_ORDER = 800;
040
041    /**
042     * Constructs a new instance.
043     */
044    public DynamicPointerFactory() {
045        // empty
046    }
047
048    @Override
049    public NodePointer createNodePointer(final NodePointer parent, final QName qName, final Object bean) {
050        if (bean == null) {
051            return new NullPointer(parent, qName);
052        }
053        final JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(bean.getClass());
054        if (bi.isDynamic()) {
055            final DynamicPropertyHandler handler = ValueUtils.getDynamicPropertyHandler(bi.getDynamicPropertyHandlerClass());
056            return new DynamicPointer(parent, qName, bean, handler);
057        }
058        return null;
059    }
060
061    @Override
062    public NodePointer createNodePointer(final QName qName, final Object bean, final Locale locale) {
063        final JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(bean.getClass());
064        if (bi.isDynamic()) {
065            final DynamicPropertyHandler handler = ValueUtils.getDynamicPropertyHandler(bi.getDynamicPropertyHandlerClass());
066            return new DynamicPointer(qName, bean, handler, locale);
067        }
068        return null;
069    }
070
071    @Override
072    public int getOrder() {
073        return DYNAMIC_POINTER_FACTORY_ORDER;
074    }
075}