001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.jaggenerator;
019:
020: import java.text.StringCharacterIterator;
021:
022: /**
023: *
024: * @author hillie
025: */
026: public class Utils {
027:
028: public static String toClassName(String unformatted) {
029: try {
030: if (unformatted.startsWith("T_")) {
031: unformatted = unformatted.substring(2);
032: }
033: String s = format(unformatted);
034: s = initCap(s);
035: return s;
036: } catch (Exception e) {
037: return unformatted;
038: }
039: }
040:
041: /**
042: * Format the name by only allowing lowercase.
043: */
044: public static String formatLowercase(String unformatted) {
045: try {
046: if (unformatted == null) {
047: return null;
048: }
049: StringBuffer sb = new StringBuffer(unformatted
050: .toLowerCase());
051: StringBuffer formattedString = new StringBuffer();
052: //remove underscores
053: for (int i = 0; i < sb.length(); i++) {
054: char c = sb.charAt(i);
055: if (c >= 'a' && c <= 'z') {
056: formattedString.append(c);
057: }
058: }
059: return formattedString.toString();
060: } catch (Exception e) {
061: return unformatted;
062: }
063: }
064:
065: /**
066: * Format the name by only allowing lowercase and uppercase and
067: * always start with uppercase.
068: */
069: public static String formatLowerAndUpperCase(String unformatted) {
070: try {
071: if (unformatted == null) {
072: return null;
073: }
074: StringBuffer sb = new StringBuffer(unformatted);
075: StringBuffer formattedString = new StringBuffer();
076: //remove underscores
077: for (int i = 0; i < sb.length(); i++) {
078: char c = sb.charAt(i);
079: if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
080: formattedString.append(c);
081: }
082: }
083: return firstToUpperCase(formattedString.toString());
084: } catch (Exception e) {
085: return unformatted;
086: }
087: }
088:
089: public static String format(String unformatted) {
090: try {
091: StringBuffer sb = new StringBuffer(unformatted
092: .toLowerCase());
093: //remove underscore and capitalize nest character
094: int i = sb.toString().indexOf("_");
095: while (i >= 0) {
096: if (i > -1)
097: sb.replace(i, i + 2, sb.substring(i + 1, i + 2)
098: .toUpperCase());
099: i = sb.toString().indexOf("_");
100: }
101: //remove dash and capitalize nest character
102: i = sb.toString().indexOf("-");
103: while (i >= 0) {
104: if (i > -1)
105: sb.replace(i, i + 2, sb.substring(i + 1, i + 2)
106: .toUpperCase());
107: i = sb.toString().indexOf("-");
108: }
109: return sb.toString();
110: } catch (Exception e) {
111: return unformatted;
112: }
113: }
114:
115: /**
116: * Undoes a format. Everything is capitalised and any uppercase character
117: * forces a '_' to be prefixed, e.g. "theColumnName" is unformatted to "THE_COLUMN_NAME", and
118: * "theATrain" unformats to "THE_A_TRAIN".
119: * @param formatted
120: * @return the unformatted String.
121: */
122: public static String unformat(String formatted) {
123: StringBuffer result = new StringBuffer();
124: for (int i = 0; i < formatted.length(); i++) {
125: if (Character.isUpperCase(formatted.charAt(i))) {
126: result.append('_');
127: }
128: result.append(Character.toUpperCase(formatted.charAt(i)));
129: }
130: return result.toString();
131: }
132:
133: public static String initCap(String unformatted) {
134: StringBuffer sb = new StringBuffer(unformatted);
135: //capitalize first character
136: if ((unformatted != null) && (unformatted.length() > 0)) {
137: sb.replace(0, 1, sb.substring(0, 1).toUpperCase());
138: return sb.toString();
139: } else
140: return unformatted;
141: }
142:
143: public static String formatFKName(String fkColumnName) {
144: return format(fkColumnName) + "FK";
145: }
146:
147: public static String firstToLowerCase(String text) {
148: return (text == null || text.length() == 0) ? "" : Character
149: .toLowerCase(text.charAt(0))
150: + (text.length() > 1 ? text.substring(1) : "");
151: }
152:
153: public static String firstToUpperCase(String text) {
154: return (text == null || text.length() == 0) ? "" : Character
155: .toUpperCase(text.charAt(0))
156: + (text.length() > 1 ? text.substring(1) : "");
157: }
158:
159: }
|