| 5 |
ebruchez |
1 |
/**
|
|
|
2 |
* Copyright (C) 2004 Orbeon, Inc.
|
|
|
3 |
*
|
|
|
4 |
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
5 |
* GNU Lesser General Public License as published by the Free Software Foundation; either version
|
|
|
6 |
* 2.1 of the License, or (at your option) any later version.
|
|
|
7 |
*
|
|
|
8 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
9 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
10 |
* See the GNU Lesser General Public License for more details.
|
|
|
11 |
*
|
|
|
12 |
* The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
|
|
|
13 |
*/
|
|
|
14 |
package org.orbeon.oxf.xml;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Defines a mapping from JavaBean property names to XML element names.
|
|
|
18 |
*
|
|
|
19 |
* <p>If the capitalization changes from lowercase to uppercase, a dash is
|
|
|
20 |
* inserted before the first uppercase character. If it changes from
|
|
|
21 |
* uppercase to lower case, a dash is inserted before the last uppercase
|
|
|
22 |
* character.
|
|
|
23 |
*
|
|
|
24 |
* <p><b>Examples:</b>
|
|
|
25 |
* <pre>
|
|
|
26 |
* Java Name XML Name SQL Name
|
|
|
27 |
* --------- -------- --------
|
|
|
28 |
* SOLineNumber so-line-number SO_LINE_NUMBER
|
|
|
29 |
* CHLPOAlertId chlpo-alert-id CHLPO_ALERT_ID
|
|
|
30 |
* </pre>
|
|
|
31 |
*/
|
|
|
32 |
public class NamingConvention {
|
|
|
33 |
public static String javaToXMLName(String javaName) {
|
|
|
34 |
|
|
|
35 |
// Strip "package name".
|
|
|
36 |
int lastDot = javaName.lastIndexOf('.');
|
|
|
37 |
if (lastDot != -1 && lastDot + 1 < javaName.length())
|
|
|
38 |
javaName = javaName.substring(lastDot + 1);
|
|
|
39 |
|
|
|
40 |
// Strip "inner class prefix".
|
|
|
41 |
int lastDollar = javaName.lastIndexOf('$');
|
|
|
42 |
if (lastDollar != -1)
|
|
|
43 |
javaName = javaName.substring(lastDollar + 1);
|
|
|
44 |
|
|
|
45 |
StringBuffer result = new StringBuffer();
|
|
|
46 |
for (int i = 0; i < javaName.length(); i++) {
|
|
|
47 |
// ASTWhen switch from lower to upper, add a dash before upper.
|
|
|
48 |
if (i > 0 && Character.isLowerCase(javaName.charAt(i - 1))
|
|
|
49 |
&& Character.isUpperCase(javaName.charAt(i)))
|
|
|
50 |
result.append('-');
|
|
|
51 |
// ASTWhen switch from upper to lower, add dash before upper.
|
|
|
52 |
else if (i > 0 && i < javaName.length() - 1
|
|
|
53 |
&& Character.isUpperCase(javaName.charAt(i))
|
|
|
54 |
&& Character.isLowerCase(javaName.charAt(i + 1)))
|
|
|
55 |
result.append('-');
|
|
|
56 |
result.append(Character.toLowerCase(javaName.charAt(i)));
|
|
|
57 |
}
|
|
|
58 |
return result.toString();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public static String javaToSQLName(String javaName) {
|
|
|
62 |
return javaToXMLName(javaName).replace('-', '_').toUpperCase();
|
|
|
63 |
}
|
|
|
64 |
}
|