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.xbean.spring.context.impl;
018
019import org.springframework.beans.factory.BeanDefinitionStoreException;
020import org.springframework.beans.factory.support.AbstractBeanDefinition;
021import org.w3c.dom.Element;
022
023import java.beans.PropertyDescriptor;
024import java.lang.reflect.Method;
025
026/**
027 * To avoid a runtime dependency on the QName class lets use reflection to
028 * process QName instances.
029 * 
030 * @version $Revision: 1.1 $
031 */
032public class QNameReflectionHelper {
033
034    protected static Method coerceMethod;
035    protected static Method createMethod;
036
037    public static void coerceNamespaceAwarePropertyValues(AbstractBeanDefinition beanDefinition, Element element,
038            PropertyDescriptor[] descriptors, int index) {
039        QNameReflectionParams params = new QNameReflectionParams(beanDefinition, element, descriptors, index);
040        if (coerceMethod == null) {
041            coerceMethod = findMethod("coerceQNamePropertyValues");
042        }
043        if (coerceMethod != null) {
044            try {
045                coerceMethod.invoke(null, new Object[] { params });
046            }
047            catch (Exception e) {
048                throw new BeanDefinitionStoreException("Failed to invoke method: " + coerceMethod + " via reflection: " + e,
049                        e);
050            }
051        }
052    }
053    
054    public static Object createQName(Element element, String text) {
055        if (createMethod == null) {
056            createMethod = findMethod("createQName");
057        }
058        if (createMethod != null) {
059            try {
060                return createMethod.invoke(null, new Object[] { element, text });
061            }
062            catch (Exception e) {
063                throw new BeanDefinitionStoreException("Failed to invoke method: " + createMethod + " via reflection: " + e,
064                        e);
065            }
066        }
067        return null;
068    }
069
070    protected static Method findMethod(String name) {
071        try {
072            Class type = PropertyEditorHelper.loadClass("org.apache.xbean.spring.context.impl.QNameHelper");
073            if (type != null) {
074                Method[] methods = type.getMethods();
075                for (int i = 0; i < methods.length; i++) {
076                    Method method = methods[i];
077                    if (method.getName().equals(name)) {
078                        return method;
079                    }
080                }
081            }
082        } catch (Throwable t) {
083            // Ignore, this is usually because QName method is not in the classpath 
084        }
085        return null;
086    }
087
088}