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.jxpath.ri.model.dynabeans;
018
019import java.util.Locale;
020
021import org.apache.commons.beanutils.LazyDynaBean;
022import org.apache.commons.beanutils.LazyDynaClass;
023import org.apache.commons.jxpath.ri.QName;
024import org.apache.commons.jxpath.ri.model.NodePointer;
025import org.apache.commons.jxpath.ri.model.NodePointerFactory;
026import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
027
028/**
029 * Implemented in response to [JXPATH-144]. Optionally pluggable
030 * <code>NodePointerFactory</code> that returns a special type of
031 * <code>NodePointer</code> for <code>LazyDynaBean</code>s. The
032 * <code>PropertyPointer</code>s returned by these will respect
033 * {@link LazyDynaClass#isDynaProperty(String)} when determining
034 * {@link PropertyPointer#isActual()}.
035 *
036 * @version $Revision: 1234255 $ $Date: 2012-01-21 04:11:46 +0100 (Sa, 21 Jan 2012) $
037 */
038public class StrictLazyDynaBeanPointerFactory implements NodePointerFactory {
039    /**
040     * Pointer implementation.
041     */
042    private static class StrictLazyDynaBeanPointer extends DynaBeanPointer {
043        private static final long serialVersionUID = 1L;
044
045        private final LazyDynaBean lazyDynaBean;
046
047        /**
048         * Create a new StrictLazyDynaBeanPointer instance.
049         *
050         * @param parent pointer
051         * @param name is the name given to the first node
052         * @param lazyDynaBean pointed
053         */
054        public StrictLazyDynaBeanPointer(NodePointer parent, QName name, LazyDynaBean lazyDynaBean) {
055            super(parent, name, lazyDynaBean);
056            this.lazyDynaBean = lazyDynaBean;
057        }
058
059        /**
060         * Create a new StrictLazyDynaBeanPointer instance.
061         *
062         * @param name is the name given to the first node
063         * @param lazyDynaBean pointed
064         * @param locale Locale
065         */
066        public StrictLazyDynaBeanPointer(QName name, LazyDynaBean lazyDynaBean, Locale locale) {
067            super(name, lazyDynaBean, locale);
068            this.lazyDynaBean = lazyDynaBean;
069        }
070
071        /**
072         * {@inheritDoc}
073         */
074        public PropertyPointer getPropertyPointer() {
075            return new DynaBeanPropertyPointer(this, lazyDynaBean) {
076                private static final long serialVersionUID = 1L;
077
078                protected boolean isActualProperty() {
079                    return ((LazyDynaClass) lazyDynaBean.getDynaClass())
080                            .isDynaProperty(getPropertyName());
081                }
082            };
083        }
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    public int getOrder() {
090        return DynaBeanPointerFactory.DYNA_BEAN_POINTER_FACTORY_ORDER - 1;
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    public NodePointer createNodePointer(QName name, Object object, Locale locale) {
097        return object instanceof LazyDynaBean ? new StrictLazyDynaBeanPointer(name,
098                (LazyDynaBean) object, locale) : null;
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    public NodePointer createNodePointer(NodePointer parent, QName name, Object object) {
105        return object instanceof LazyDynaBean ? new StrictLazyDynaBeanPointer(parent, name,
106                (LazyDynaBean) object) : null;
107    }
108
109}