1 /* 2 * Copyright 2007 Joachim Grueneis 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.castor.xml; 15 16 import java.lang.reflect.Field; 17 import java.lang.reflect.Method; 18 19 /** 20 * JavaNaming is a service which collects all methods that are related to create (modify) Java 21 * names. E.g. convert from XML name to Java name, get a Java member name or such. These rules can 22 * be exchanged by a different implementation to get a different naming style for e.g. JAXB. 23 * 24 * @author <a href="mailto:jgrueneis_at_gmail_dot_com">Joachim Grueneis</a> 25 * @version $Id$ 26 */ 27 public interface JavaNaming { 28 /** Add methods start with: add. */ 29 public static final String METHOD_PREFIX_ADD = "add"; 30 31 /** Get methods start with: get. */ 32 public static final String METHOD_PREFIX_GET = "get"; 33 34 /** Is methods start with: is. */ 35 public static final String METHOD_PREFIX_IS = "is"; 36 37 /** Set methods start with: set. */ 38 public static final String METHOD_PREFIX_SET = "set"; 39 40 /** Create methods start with: create. */ 41 public static final String METHOD_PREFIX_CREATE = "create"; 42 43 /** The underscore field prefix. */ 44 public static final char FIELD_UNDERSCORE_PREFIX = '_'; 45 46 /** 47 * Returns true if the given String is a Java keyword which will cause a problem when used as a 48 * variable name. 49 * 50 * @param name The name to check. 51 * @return true if it is a keyword. 52 */ 53 boolean isKeyword(String name); 54 55 /** 56 * Returns true if the given String matches the production of a valid Java identifier. 57 * 58 * @param string The String to check the production of. 59 * @return true if the given String matches the production of a valid Java name, otherwise false. 60 */ 61 boolean isValidJavaIdentifier(String string); 62 63 /** 64 * Cuts away a leading namespace prefix (if there is one in place). 65 * 66 * @param name The XML name to convert to a Java name. 67 * @return A name which follows Java naming conventions. 68 */ 69 String toJavaClassName(String name); 70 71 /** 72 * Appends a leading '_' and converts the given name to a java name. 73 * 74 * @param name the XML name to convert. 75 * @return A Java member name starting with a leading '_'. 76 */ 77 String toJavaMemberName(String name); 78 79 /** 80 * Appends a leading '_' and converts the given name to a java name. 81 * 82 * @param name The XML name to convert. 83 * @param useKeywordSubstitutions Set to true to turn on keyword substitution. 84 * @return A Java member name starting with a leading '_'. 85 */ 86 String toJavaMemberName(String name, boolean useKeywordSubstitutions); 87 88 /** 89 * Checks if the given package name is valid or not. Empty package names are considered valid! 90 * 91 * @param packageName Name of package as String with periods. 92 * @return true if package name is valid. 93 */ 94 boolean isValidPackageName(String packageName); 95 96 /** 97 * Converts the given Package name to it's corresponding Path. The path will be a relative path. 98 * 99 * @param packageName The package name to convert. 100 * @return A String containing the resulting patch. 101 */ 102 String packageToPath(String packageName); 103 104 /** 105 * Qualifies the given <code>fileName</code> with the given <code>packageName</code> and returns 106 * the resulting file path.<br> 107 * If <code>packageName</code> is <code>null</code> or a zero-length String, this method will 108 * return <code>fileName</code>.<br> 109 * 110 * @param fileName The file name to be qualified. 111 * @param packageName The package name to be used for qualifying. 112 * @return The qualified file path. 113 */ 114 String getQualifiedFileName(String fileName, String packageName); 115 116 /** 117 * Gets the class name without package part. 118 * 119 * @param clazz The class to retrieve the name from 120 * @return the class name without package part or null 121 */ 122 String getClassName(Class<?> clazz); 123 124 /** 125 * Gets the package name of the given class name. 126 * 127 * @param className The class name to retrieve the package name from. 128 * @return The package name or the empty String if <code>className</code> is <code>null</code> or 129 * does not contain a package. 130 */ 131 String getPackageName(String className); 132 133 /** 134 * Extracts the filed name part from the methods name. Mostly it cuts away the method prefix. 135 * 136 * @param method The Method to process. 137 * @return The extracted field name. 138 */ 139 String extractFieldNameFromMethod(Method method); 140 141 /** 142 * Extracts the field name part from the Field. Mostly it cuts away prefixes like '_'. 143 * 144 * @param field the Field to process 145 * @return The extracted field name. 146 */ 147 String extractFieldNameFromField(Field field); 148 149 /** 150 * Checks if the given method is a set method. 151 * 152 * @param method The Method to check 153 * @return true if it is a set method 154 */ 155 boolean isSetMethod(Method method); 156 157 /** 158 * Checks if the given method is a create method. 159 * 160 * @param method The Method to check. 161 * @return true if it is a create method. 162 */ 163 boolean isCreateMethod(Method method); 164 165 /** 166 * Checks if the given method is a get method. 167 * 168 * @param method The Method to check. 169 * @return true if it is a get method. 170 */ 171 boolean isGetMethod(Method method); 172 173 /** 174 * Checks if the given method is an is method. 175 * 176 * @param method The Method to check. 177 * @return true if it is an is method. 178 */ 179 boolean isIsMethod(Method method); 180 181 /** 182 * Checks if the given method is an add method. 183 * 184 * @param method The Method to check. 185 * @return true if it is an add method. 186 */ 187 boolean isAddMethod(Method method); 188 189 /** 190 * Generates the name of an add method for the given field name. 191 * 192 * @param fieldName The field name to generate a method name for. 193 * @return The generated add method name. 194 */ 195 String getAddMethodNameForField(String fieldName); 196 197 /** 198 * Generates the name of a set method for the given field name. 199 * 200 * @param fieldName The field name to generate a method name for. 201 * @return The generated set method name. 202 */ 203 String getSetMethodNameForField(String fieldName); 204 205 /** 206 * Generates the name of a get method for the given field name. 207 * 208 * @param fieldName The field name to generate a method name for. 209 * @return The generated get method name. 210 */ 211 String getGetMethodNameForField(String fieldName); 212 213 /** 214 * Generates the name of an is method for the given field name. 215 * 216 * @param fieldName The field name to generate a method name for. 217 * @return The generated is method name. 218 */ 219 String getIsMethodNameForField(String fieldName); 220 221 /** 222 * Generates the name of a create method for the given field name. 223 * 224 * @param fieldName The field name to generate a method name for. 225 * @return The generated create method name. 226 */ 227 String getCreateMethodNameForField(String fieldName); 228 }