xdoclet.sdk.xgg.binding
Class NamingUtils

java.lang.Object
  |
  +--xdoclet.sdk.xgg.binding.NamingUtils

public class NamingUtils
extends Object

Utility class that converts from xml type names to java names. This class is largely inspired from Zeus.

Version:
$Revision: 1.4 $
Author:
Aslak Hellesøy

Field Summary
static String DEFAULT_MAPPING
          The default alternative representation for illegal characters.
static String PCDATA_JAVA_NAME
          Java Name for PCDATA variable
static String PCDATA_XML_NAME
          XML Name for PCDATA elements
static String XML_PREFIX
          Prefix to append to names that clash with Java reserved words.
 
Method Summary
static String allLower(String original)
           This will take a String with unknown capitalization, and convert all the letters to lowercase letters.
static String allUpper(String original)
           This will take a String with unknown capitalization, and convert all the letters to uppercase letters.
static String getCharMapping(Character key)
           Returns the value that the given key is mapped to.
static String getJavaClassName(String xmlName)
           Returns the conventional Java class name from an XML name.
static String getJavaCollectionVariableName(String className)
           Returns the Java variable name from a Java class name assuming that the variable will be a collection based variable (i.e. ending with "List" as in "myVariableList").
static String getJavaName(String xmlName)
           Returns a conventional Java name from an XML name.
static String getJavaType(String xmlType)
           This will handle conversion from an XML type to a Java type.
static String getJavaVariableName(String xmlName)
           Returns the conventional Java variable name from an XML name.
static String getSchemaJavaType(String schemaType)
           This will take as input an XML Schema data type (such as "string") and convert it to a Java data type (such as "String").
static String getXMLElementNameFromAccessor(String accessor)
           Returns the XML name from a typical accessor method.
static String initialLower(String original)
           This will take a String with unknown capitalization, and convert the first letter to a lowercase letter, while leaving all other letters the same.
static String initialUpper(String original)
           This will take a String with unknown capitalization, and convert the first letter to a capital letter, while leaving all other letters the same.
static boolean isLegalJavaClassName(String className)
           This will indicate whether the supplied name is a legal Java class name.
static boolean isLegalJavaPackageName(String packageName)
           This will indicate whether the supplied name is a legal Java package name.
static String justInitialUpper(String original)
           This will take a String with unknown capitalization, and convert the first letter to a capital letter, while converting all other letters to lowercase.
static String removePackage(String className)
           This will take a fully qualified Java class, and return just the name of the class, without any package qualifier.
static void setCharMapping(Character key, String value)
           Maps the given key to the given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MAPPING

public static final String DEFAULT_MAPPING
The default alternative representation for illegal characters.

See Also:
Constant Field Values

PCDATA_JAVA_NAME

public static final String PCDATA_JAVA_NAME
Java Name for PCDATA variable

See Also:
Constant Field Values

PCDATA_XML_NAME

public static final String PCDATA_XML_NAME
XML Name for PCDATA elements

See Also:
Constant Field Values

XML_PREFIX

public static final String XML_PREFIX
Prefix to append to names that clash with Java reserved words.

See Also:
Constant Field Values
Method Detail

allLower

public static String allLower(String original)

This will take a String with unknown capitalization, and convert all the letters to lowercase letters. Note that if you want just the first letter converted to lowercase, then initialLower(String) should be used.

And for all you geniuses out there, yes, I know that toLowerCase() can be used directly; but placing the code within this method allows this class to be uniformly used for all capitalization purposes.

Parameters:
original - String to convert.
Returns:
String - the converted String.

allUpper

public static String allUpper(String original)

This will take a String with unknown capitalization, and convert all the letters to uppercase letters. Note that if you want just the first letter converted to lowercase, then initialUpper(String) should be used.

And for all you geniuses out there, yes, I know that toUpperCase() can be used directly; but placing the code within this method allows this class to be uniformly used for all capitalization purposes.

Parameters:
original - String to convert.
Returns:
String - the converted String.

getCharMapping

public static String getCharMapping(Character key)

Returns the value that the given key is mapped to. This value will replace any occurence of the key when a name contains illegal characters for a Java identifier.

Parameters:
key - the key mapped to the value to return.
Returns:
String - the value the given key is mapped to.

getJavaClassName

public static String getJavaClassName(String xmlName)

Returns the conventional Java class name from an XML name. The initial character is capitalized, and any illegal characters are replaced. There is no check for a collision with a Java reserved word because all Java reserved words have their initial character lower case.

For example, calling getJavaClassName("my-element") would return "MyElement" as the Java class name.

Parameters:
xmlName - the XML name to convert to a Java class name.
Returns:
String - the converted Java name.

getJavaCollectionVariableName

public static String getJavaCollectionVariableName(String className)

Returns the Java variable name from a Java class name assuming that the variable will be a collection based variable (i.e. ending with "List" as in "myVariableList"). The initial character is uncapitalized, and any illegal characters are replaced. There is no check for a collision with a Java reserved word because the assumption is that the variable has some word appended to it that identifies the fact that the variable denotes a collection.

For example, calling getJavaCollectionVariableName("MyClass") would return "myClass" as the Java variable name.

Parameters:
className - the class name to convert to a collection based variable name.
Returns:
String - the converted Java variable name.

getJavaName

public static String getJavaName(String xmlName)

Returns a conventional Java name from an XML name. Any illegal characters are replaced. Collision with reserved words is not checked, as this is not used directly in class generation.

For example, calling getJavaName("my-element") would return "myElement" as the Java class name.

Parameters:
xmlName - the XML name to convert to a Java name.
Returns:
String - the converted Java name.

getJavaType

public static String getJavaType(String xmlType)

This will handle conversion from an XML type to a Java type. It first attempts to perform an XML Schema type to Java type conversion. For example, if the XML type is "string", it would convert this to the Java type "String". If that is unsuccessful, it returns the type unchanged, assuming that it is a proper Java class name.

Parameters:
xmlType - the XML type to convert to a Java type.
Returns:
String - the converted Java type.

getJavaVariableName

public static String getJavaVariableName(String xmlName)

Returns the conventional Java variable name from an XML name. The initial character is uncapitalized, and any illegal characters are replaced. If there is a collision with a Java reserved word, the name is prefixed with XML_PREFIX.

For example, calling getJavaVariableName("XmlName") would return "xmlName" as the Java variable name.

Parameters:
xmlName - the XML name to convert to a variable name.
Returns:
String - the converted Java variable name.

getSchemaJavaType

public static String getSchemaJavaType(String schemaType)
                                throws XDocletException

This will take as input an XML Schema data type (such as "string") and convert it to a Java data type (such as "String"). It includes, if needed, the fully-qualified package (such as "java.util.List") to prevent import problems.

Note: Eventually this should probably work in concert with some methodology that handles package imports and the like.

Parameters:
schemaType - the XML Schema data type (as a String)
Returns:
String - the Java data type.
Throws:
XDocletException - - when the supplied schema type is not supported.

getXMLElementNameFromAccessor

public static String getXMLElementNameFromAccessor(String accessor)

Returns the XML name from a typical accessor method. The naming convention for such a method is "get<variable-name>". The first three characters are discarded, and the initial character is uncapitalized.

For example, calling getXMLElementNameFromAccessor("getMyVariable") would return "myVariable" as the XML name. This method would also work with typical mutator methods.

Parameters:
accessor - the name of the accessor method to derive the XML name from.
Returns:
String - the converted XML name.

initialLower

public static String initialLower(String original)

This will take a String with unknown capitalization, and convert the first letter to a lowercase letter, while leaving all other letters the same. Note that if you want the rest of the letters converted to lowercase, then allLower(String) should be used.

Parameters:
original - String to convert.
Returns:
String - the converted String.

initialUpper

public static String initialUpper(String original)

This will take a String with unknown capitalization, and convert the first letter to a capital letter, while leaving all other letters the same. Note that if you want the rest of the letters converted to lowercase, then justInitialUpper(String) should be used.

Parameters:
original - String to convert.
Returns:
String - the converted String.

isLegalJavaClassName

public static boolean isLegalJavaClassName(String className)

This will indicate whether the supplied name is a legal Java class name.

Parameters:
className - the Java class name to check
Returns:
boolean - whether the supplied class is a legal Java class name.

isLegalJavaPackageName

public static boolean isLegalJavaPackageName(String packageName)

This will indicate whether the supplied name is a legal Java package name.

Parameters:
packageName - the Java class name to check
Returns:
boolean - whether the supplied package is a legal Java package name.

justInitialUpper

public static String justInitialUpper(String original)

This will take a String with unknown capitalization, and convert the first letter to a capital letter, while converting all other letters to lowercase. Note that if you want the rest of the letters left alone, then initialUpper(String) should be used.

Parameters:
original - String to convert.
Returns:
String - the converted String.

removePackage

public static String removePackage(String className)

This will take a fully qualified Java class, and return just the name of the class, without any package qualifier. For example, invoking removePackage("java.util.List"); would return "List".

Parameters:
className - the Java class name to remove the package from.
Returns:
String - the Java class, without package.

setCharMapping

public static void setCharMapping(Character key,
                                  String value)

Maps the given key to the given value. This value will replace any occurence of the key when a name contains illegal characters for a Java identifier.

Parameters:
key - the key to map the given value to.
value - the value to map the given key to.


Copyright © 2000-2003 XDoclet Team. All Rights Reserved.