KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > gen > StringUtils


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.gen;
22
23 import org.apache.cayenne.project.validator.MappingNamesHelper;
24 import org.apache.cayenne.util.NameConverter;
25
26 /**
27  * Methods for mangling strings.
28  *
29  * @author Mike Kienenberger
30  */

31 public class StringUtils {
32
33     private static StringUtils sharedInstance = null;
34     
35     public static StringUtils getInstance()
36     {
37         if (null == sharedInstance)
38         {
39             sharedInstance = new StringUtils();
40         }
41         
42         return sharedInstance;
43     }
44     
45     public StringUtils()
46     {
47         super();
48     }
49     
50     /**
51      * Prepends underscore to variable name if necessary to remove conflict with reserved keywords.
52      */

53     public String JavaDoc formatVariableName(String JavaDoc variableName) {
54         if (MappingNamesHelper.getInstance().isReservedJavaKeyword(variableName)) {
55             return "_" + variableName;
56         } else {
57             return variableName;
58         }
59     }
60
61     /**
62      * Removes package name, leaving base name.
63      *
64      * @since 1.2
65      */

66    public String JavaDoc stripPackageName(String JavaDoc aString)
67    {
68        if (aString == null || aString.length() == 0)
69            return aString;
70
71        int lastDot = aString.lastIndexOf('.');
72        
73        if ((-1 == lastDot) || ((aString.length() - 1) == lastDot) ) return aString;
74
75        return aString.substring(lastDot + 1);
76    }
77
78    /**
79     * Removes base name, leaving package name.
80     *
81     * @since 1.2
82     */

83   public String JavaDoc stripClass(String JavaDoc aString)
84   {
85       if (aString == null || aString.length() == 0)
86           return aString;
87
88       int lastDot = aString.lastIndexOf('.');
89       
90       if (-1 == lastDot) return "";
91       
92       return aString.substring(0, lastDot);
93   }
94
95     /**
96      * Capitalizes the first letter of the property name.
97      *
98      * @since 1.1
99      */

100     public String JavaDoc capitalized(String JavaDoc name) {
101         if (name == null || name.length() == 0)
102             return name;
103
104         char c = Character.toUpperCase(name.charAt(0));
105         return (name.length() == 1) ? Character.toString(c) : c + name.substring(1);
106     }
107     
108     /**
109      * Returns string with lowercased first letter
110      *
111      * @since 1.2
112      */

113    public static String JavaDoc uncapitalized(String JavaDoc aString)
114    {
115        if (aString == null || aString.length() == 0)
116            return aString;
117
118        char c = Character.toLowerCase(aString.charAt(0));
119        return (aString.length() == 1) ? Character.toString(c) : c + aString.substring(1);
120    }
121    
122     /**
123      * Converts property name to Java constants naming convention.
124      *
125      * @since 1.1
126      */

127     public String JavaDoc capitalizedAsConstant(String JavaDoc name) {
128         if (name == null || name.length() == 0)
129             return name;
130
131         return NameConverter.javaToUnderscored(name);
132     }
133 }
134
Popular Tags