1 /*** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.xbean.spring.context.impl; 18 19 import java.lang.reflect.Constructor; 20 import java.lang.reflect.Method; 21 import java.util.ArrayList; 22 import java.util.Collections; 23 import java.util.Properties; 24 import java.util.StringTokenizer; 25 26 /*** 27 * A helper class which understands how to map an XML namespaced element to 28 * Spring bean configurations 29 * 30 * @author James Strachan 31 * @version $Id$ 32 * @since 2.0 33 */ 34 public class MappingMetaData { 35 private Properties properties; 36 private String packageName; 37 38 /*** 39 * Creates an empty MappingMetaData for the specified Java package. 40 * @param packageName the Java package to map 41 */ 42 public MappingMetaData(String packageName) { 43 this.packageName = packageName; 44 this.properties = new Properties(); 45 } 46 47 /*** 48 * Creates MappingMetaData using the specified properties which contan the package name. 49 * @param properties 50 */ 51 public MappingMetaData(Properties properties) { 52 this.properties = properties; 53 } 54 55 /*** 56 * Returns the Java class name for the given XML element name 57 */ 58 public String getClassName(String localName) { 59 String className = properties.getProperty(localName); 60 if (className == null && packageName != null) { 61 if (packageName.length() > 0) { 62 className = packageName + "." + localName; 63 } 64 else { 65 className = localName; 66 } 67 } 68 return className; 69 } 70 71 /*** 72 * Returns the property name for the given element and attribute name 73 * 74 * @param elementName the XML local name of the element 75 * @param attributeName the XML local name of the attribute 76 * @return the property name to use or null if the attribute is not a valid property 77 */ 78 public String getPropertyName(String elementName, String attributeName) { 79 return properties.getProperty(elementName + ".alias." + attributeName, attributeName); 80 } 81 82 /*** 83 * Returns a valid property name if the childElementName maps to a nested list property 84 * 85 * @param elementName the owner element 86 * @param childElementName is the child element name which maps to the nested list property 87 * @return the property name if available or null if it is not applicable 88 */ 89 public String getNestedListProperty(String elementName, String childElementName) { 90 return properties.getProperty(elementName + ".list." + childElementName); 91 } 92 93 /*** 94 * Returns a valid property name if the childElementName maps to a nested bean property 95 * 96 * @param elementName the owner element 97 * @param childElementName is the child element name which maps to the nested bean property 98 * @return the property name if available or null if it is not applicable 99 */ 100 public String getNestedProperty(String elementName, String childElementName) { 101 return properties.getProperty(elementName + ".alias." + childElementName); 102 } 103 104 public boolean isDefaultConstructor(Constructor constructor) { 105 String property = properties.getProperty(constructorToPropertyName(constructor) + ".default"); 106 if (property != null) { 107 return Boolean.valueOf(property).booleanValue(); 108 } 109 return false; 110 } 111 112 public boolean isDefaultFactoryMethod(Class beanClass, Method factoryMethod) { 113 String property = properties.getProperty(methodToPropertyName(beanClass, factoryMethod) + ".default"); 114 if (property != null) { 115 return Boolean.valueOf(property).booleanValue(); 116 } 117 return false; 118 } 119 120 public String[] getParameterNames(Constructor constructor) { 121 String property = properties.getProperty(constructorToPropertyName(constructor) + ".parameterNames"); 122 if (property != null) { 123 ArrayList names = Collections.list(new StringTokenizer(property, ", ")); 124 return (String[]) names.toArray(new String[0]); 125 } 126 return null; 127 } 128 129 public String[] getParameterNames(Class beanClass, Method factoryMethod) { 130 String property = properties.getProperty(methodToPropertyName(beanClass, factoryMethod) + ".parameterNames"); 131 if (property != null) { 132 ArrayList names = Collections.list(new StringTokenizer(property, ", ")); 133 return (String[]) names.toArray(new String[0]); 134 } 135 return null; 136 } 137 138 public static String constructorToPropertyName(Constructor constructor) { 139 StringBuffer buf = new StringBuffer(); 140 buf.append(constructor.getName()).append("("); 141 Class[] parameterTypes = constructor.getParameterTypes(); 142 for (int i = 0; i < parameterTypes.length; i++) { 143 Class parameterType = parameterTypes[i]; 144 buf.append(parameterType.getName()); 145 if (i < parameterTypes.length - 1) { 146 buf.append(","); 147 } 148 } 149 buf.append(")"); 150 return buf.toString(); 151 } 152 153 public static String methodToPropertyName(Class beanClass, Method method) { 154 StringBuffer buf = new StringBuffer(); 155 buf.append(beanClass.getName()).append("."); 156 buf.append(method.getName()).append("("); 157 Class[] parameterTypes = method.getParameterTypes(); 158 for (int i = 0; i < parameterTypes.length; i++) { 159 Class parameterType = parameterTypes[i]; 160 buf.append(parameterType.getName()); 161 if (i < parameterTypes.length - 1) { 162 buf.append(","); 163 } 164 } 165 buf.append(")"); 166 return buf.toString(); 167 } 168 169 public String getInitMethodName(String elementName) { 170 return properties.getProperty(elementName + ".initMethod"); 171 } 172 173 public String getDestroyMethodName(String elementName) { 174 return properties.getProperty(elementName + ".destroyMethod"); 175 } 176 177 public String getFactoryMethodName(String elementName) { 178 return properties.getProperty(elementName + ".factoryMethod"); 179 } 180 181 public String getContentProperty(String elementName) { 182 return properties.getProperty(elementName + ".contentProperty"); 183 } 184 185 public String getMapEntryName(String elementName, String property) { 186 return properties.getProperty(elementName + "." + property + ".map.entryName"); 187 } 188 189 public String getMapKeyName(String elementName, String property) { 190 return properties.getProperty(elementName + "." + property + ".map.keyName"); 191 } 192 193 public boolean isFlatMap(String elementName, String property) { 194 return properties.getProperty(elementName + "." + property + ".map.flat") != null; 195 } 196 197 public String getMapDupsMode(String elementName, String property) { 198 return properties.getProperty(elementName + "." + property + ".map.dups"); 199 } 200 201 public String getMapDefaultKey(String elementName, String property) { 202 return properties.getProperty(elementName + "." + property + ".map.defaultKey"); 203 } 204 205 public String getFlatCollectionProperty(String elementName, String property) 206 { 207 return properties.getProperty(elementName + "." + property + ".flatCollection"); 208 } 209 210 public boolean isFlatProperty(String elementName, String property) { 211 return properties.getProperty(elementName + "." + property + ".flat") != null; 212 } 213 214 public String getPropertyEditor(String elementName, String property) 215 { 216 return properties.getProperty(elementName + "." + property + ".propertyEditor"); 217 } 218 219 }