| 1 | /* |
|---|
| 2 | * fmgVen - A Convention over Configuration Java ORM Tool |
|---|
| 3 | * Copyright 2010 Fatih Mehmet Güler |
|---|
| 4 | * |
|---|
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 6 | * you may not use this file except in compliance with the License. |
|---|
| 7 | * You may obtain a copy of the License at |
|---|
| 8 | * |
|---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 10 | * |
|---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
|---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 14 | * See the License for the specific language governing permissions and |
|---|
| 15 | * limitations under the License. |
|---|
| 16 | * under the License. |
|---|
| 17 | */ |
|---|
| 18 | package com.fmguler.ven.util; |
|---|
| 19 | |
|---|
| 20 | import java.util.Locale; |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Utility class for conversions from database naming convention to java, and vice versa |
|---|
| 24 | * @author Fatih Mehmet Güler |
|---|
| 25 | */ |
|---|
| 26 | public class Convert { |
|---|
| 27 | /** |
|---|
| 28 | * Convert from java camelCase case to database naming convention |
|---|
| 29 | * <p> |
|---|
| 30 | * For example, if the name of the given name is SomeDomainObject |
|---|
| 31 | * it will be converted to some_domain_object |
|---|
| 32 | * |
|---|
| 33 | * @param camelCase the camelCase case name |
|---|
| 34 | * @return database style name |
|---|
| 35 | */ |
|---|
| 36 | public static String toDB(String camelCase) { |
|---|
| 37 | if (camelCase.equals("")) return ""; |
|---|
| 38 | StringBuffer result = new StringBuffer(); |
|---|
| 39 | result.append(camelCase.substring(0, 1).toLowerCase(Locale.ENGLISH)); |
|---|
| 40 | for (int i = 1; i < camelCase.length(); i++) { |
|---|
| 41 | String sCamel = camelCase.substring(i, i + 1); |
|---|
| 42 | String sLower = sCamel.toLowerCase(Locale.ENGLISH); |
|---|
| 43 | if (!sCamel.equals(sLower)) { |
|---|
| 44 | result.append("_").append(sLower); |
|---|
| 45 | } else result.append(sCamel); |
|---|
| 46 | } |
|---|
| 47 | return result.toString(); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Convert the full package name to simple name. |
|---|
| 52 | * For Java 1.4 compatibility of Class.getSimpleName() |
|---|
| 53 | * @param fullName Class.getName() |
|---|
| 54 | * @return the part after the last . |
|---|
| 55 | */ |
|---|
| 56 | public static String toSimpleName(String fullName) { |
|---|
| 57 | int i = fullName.lastIndexOf("."); |
|---|
| 58 | if (i < 0) return fullName; |
|---|
| 59 | return fullName.substring(i + 1); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|