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