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