KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > util > TemplateString


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.util;
19
20 /**
21  * This class is used to add special formatting capabilities to a String, as needed by the
22  * JAG Velocity templates. For example, in a template you can access the 'crazy struts'
23  * format of an entity bean field name with something like <code>${entity.Name.CrazyStruts}</code>.
24  *
25  * @author Michael O'Connor - Finalist IT Group
26  */

27 public class TemplateString {
28    private String JavaDoc string;
29
30    /**
31     * Creates a TemplateString with a given String.
32     *
33     * @param string
34     */

35    public TemplateString(String JavaDoc string) {
36       this.string = string;
37    }
38
39    /**
40     * Lower cases the entire string.
41     *
42     * @return
43     */

44    public String JavaDoc getLower() {
45       return string.toLowerCase();
46    }
47
48    /**
49     * Upper cases the entire string.
50     *
51     * @return
52     */

53    public String JavaDoc getUpper() {
54       return string.toUpperCase();
55    }
56
57    /**
58     * Upper cases the first letter of the string.
59     *
60     * @return
61     */

62    public TemplateString getSentensized() {
63       if (string.length() > 1) {
64          return new TemplateString(string.substring(0, 1).toUpperCase() + string.substring(1));
65       }
66       return new TemplateString(string.toUpperCase());
67    }
68
69    /**
70     * Lower cases the first letter of the string.
71     *
72     * @return
73     */

74    public TemplateString getDesentensized() {
75       if (string.length() > 1) {
76          return new TemplateString(string.substring(0, 1).toLowerCase() + string.substring(1));
77       }
78       return new TemplateString(string.toLowerCase());
79    }
80
81    /**
82     * Formats the string to the strange format used by Struts:
83     *
84     * <li>anElephant --> anElephant</li>
85     * <li>aHorse --> AHorse</li>
86     *
87     * @return
88     */

89    public String JavaDoc getCrazyStruts() {
90       if (string.length() > 1 &&
91             Character.isLowerCase(string.charAt(0)) && Character.isUpperCase(string.charAt(1))) {
92          return Character.toUpperCase(string.charAt(0)) + string.substring(1);
93       }
94       return string;
95    }
96
97    /**
98     * Applies a 'database-table to Java-classname' formatting, for example:
99     * both "the_table" and "THE_TABLE" becomes "TheTable", and
100     * "table-name" becomes "TableName".
101     *
102     * @return
103     */

104    public String JavaDoc getClassNameFormat() {
105       if (string == null || string.trim().equals("")) {
106          return string;
107       }
108       String JavaDoc temp = Character.toUpperCase(string.charAt(0)) +
109             (string.length() > 1 ? string.substring(1).toLowerCase() : "");
110       try {
111          StringBuffer JavaDoc sb = new StringBuffer JavaDoc(temp);
112          //remove underscore and capitalize nest character
113
int i = sb.toString().indexOf("_");
114          while (i >= 0) {
115             if (i > -1) sb.replace(i, i + 2, sb.substring(i + 1, i + 2).toUpperCase());
116             i = sb.toString().indexOf("_");
117          }
118          //remove dash and capitalize nest character
119
i = sb.toString().indexOf("-");
120          while (i >= 0) {
121             if (i > -1) sb.replace(i, i + 2, sb.substring(i + 1, i + 2).toUpperCase());
122             i = sb.toString().indexOf("-");
123          }
124          return sb.toString();
125       } catch (Exception JavaDoc e) {
126          return string;
127       }
128    }
129
130    public String JavaDoc toString() {
131       return string;
132    }
133
134    /**
135     * <b>NOTE:</b> To enable simple String comparisons, a TemplateString is equal to a String with the same value.
136     * <p>
137     * For example, the following template snippet is a valid equality check:<br>
138     * <code>#if (${datasource.Mapping.equals("Oracle 8")}) ...</code>
139     *
140     * @param o
141     * @return
142     */

143    public boolean equals(Object JavaDoc o) {
144       if (this == o) return true;
145       if (!(o instanceof TemplateString || o instanceof String JavaDoc)) return false;
146       final String JavaDoc other = o.toString();
147       if (string != null ? !string.equals(other) : other != null) return false;
148       return true;
149    }
150
151    public int hashCode() {
152       return (string != null ? string.hashCode() : 0);
153    }
154 }
155
Popular Tags