KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > CodeStyle


1 /*
2  * CodeStyle.java
3  *
4  * This work is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This work is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  *
31  * Copyright (c) 2003-2004 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code;
35
36 /**
37  * The abstract base class for all code styles. The code style classes
38  * allows configuring some aspects of the source code generated. The
39  * code style classes also contain helper methods for the code
40  * generators.
41  *
42  * @author Per Cederberg, <per at percederberg dot net>
43  * @version 1.5
44  */

45 public class CodeStyle {
46
47     /**
48      * The default Java code style.
49      */

50     public static final CodeStyle JAVA = new CodeStyle(70, " ");
51
52     /**
53      * The default C# code style.
54      */

55     public static final CodeStyle CSHARP = new CodeStyle(70, " ");
56
57     /**
58      * The default Visual Basic code style.
59      */

60     public static final CodeStyle VISUAL_BASIC = new CodeStyle(70, " ");
61
62     /**
63      * The right print margin.
64      */

65     private int margin;
66
67     /**
68      * The indentation string.
69      */

70     private String JavaDoc indentString;
71
72     /**
73      * Creates a new code style.
74      *
75      * @param margin the print margin
76      * @param indent the indentation string
77      */

78     public CodeStyle(int margin, String JavaDoc indent) {
79         this.margin = margin;
80         this.indentString = indent;
81     }
82
83     /**
84      * Returns the right print margin.
85      *
86      * @return the print margin
87      */

88     public int getMargin() {
89         return margin;
90     }
91
92     /**
93      * Returns the indentation string for the specified level.
94      *
95      * @param level the indentation level
96      *
97      * @return a string containing the indentation
98      */

99     public String JavaDoc getIndent(int level) {
100         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
101
102         for (int i = 0; i < level; i++) {
103             buffer.append(indentString);
104         }
105         return buffer.toString();
106     }
107
108     /**
109      * Creates a string constant from the specified string. This will
110      * add " characters to start and the end, and all " character
111      * inside the string will be escaped. Also, any occurence of the
112      * escape character itself will be doubled (i.e. "\" becomes "\\").
113      *
114      * @param str the string to convert
115      * @param escape the escape character to use
116      *
117      * @return a string constant
118      */

119     public String JavaDoc getStringConstant(String JavaDoc str, char escape) {
120         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
121
122         res.append('"');
123         res.append(addStringEscapes(str, escape));
124         res.append('"');
125
126         return res.toString();
127     }
128
129     /**
130      * Returns the upper-case version of a string. This method also
131      * handles transitions from a lower-case letter to an upper-case
132      * letter (inside the string) by appending a '_'. This will
133      * maintain the word stem separation, also in the upper-case
134      * string. Any characters outside the [A-Za-z0-9_] range will be
135      * removed from the string.
136      *
137      * @param str the string to transform
138      *
139      * @return the all upper-case version of the string
140      */

141     public String JavaDoc getUpperCase(String JavaDoc str) {
142         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
143         char last = 'A';
144
145         for (int i = 0; i < str.length(); i++) {
146             if (Character.isLowerCase(last)
147              && Character.isUpperCase(str.charAt(i))) {
148
149                 res.append("_");
150             }
151             last = str.charAt(i);
152             if (('A' <= last && last <= 'Z')
153              || ('a' <= last && last <= 'z')
154              || ('0' <= last && last <= '9')
155              || last == '_') {
156
157                 res.append(Character.toUpperCase(last));
158             }
159         }
160
161         return res.toString();
162     }
163
164     /**
165      * Returns the lower-case version of a string. This method also
166      * handles transitions from a lower-case letter to an upper-case
167      * letter (inside the string) by appending a '_'. This will
168      * maintain the word stem separation, also in the lower-case
169      * string. Any characters outside the [A-Za-z0-9_] range will be
170      * removed from the string.
171      *
172      * @param str the string to transform
173      *
174      * @return the all lower-case version of the string
175      */

176     public String JavaDoc getLowerCase(String JavaDoc str) {
177         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
178         char last = 'A';
179
180         for (int i = 0; i < str.length(); i++) {
181             if (Character.isLowerCase(last)
182              && Character.isUpperCase(str.charAt(i))) {
183
184                 res.append("_");
185             }
186             last = str.charAt(i);
187             if (('A' <= last && last <= 'Z')
188              || ('a' <= last && last <= 'z')
189              || ('0' <= last && last <= '9')
190              || last == '_') {
191
192                 res.append(Character.toLowerCase(last));
193             }
194         }
195
196         return res.toString();
197     }
198
199     /**
200      * Returns the mixed-case version of a string. This method also
201      * handles '_' characters by adding a transition from a lower-case
202      * letter to an upper-case letter (in the result string). This
203      * will maintain the word stem separation, in mixed-case. Any
204      * characters outside the [A-Za-z0-9] range will be removed from
205      * the string.
206      *
207      * @param str the string to transform
208      * @param initialUpper the first character upper-case flag
209      *
210      * @return the mixed-case version of the string
211      */

212     public String JavaDoc getMixedCase(String JavaDoc str, boolean initialUpper) {
213         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
214         char last = 'A';
215
216         for (int i = 0; i < str.length(); i++) {
217             if (Character.isLowerCase(last)
218              && Character.isUpperCase(str.charAt(i))) {
219
220                 initialUpper = true;
221             } else if (str.charAt(i) == '_') {
222                 initialUpper = true;
223             }
224             last = str.charAt(i);
225             if (('A' <= last && last <= 'Z')
226              || ('a' <= last && last <= 'z')
227              || ('0' <= last && last <= '9')) {
228
229                 if (initialUpper) {
230                     res.append(Character.toUpperCase(last));
231                     initialUpper = false;
232                 } else {
233                     res.append(Character.toLowerCase(last));
234                 }
235             }
236         }
237
238         return res.toString();
239     }
240
241     /**
242      * Adds escapes in front of all " characters in a string. Any
243      * occurence of the escape character itself will also be escaped.
244      *
245      * @param str the string to convert
246      * @param escape the escape character to use
247      *
248      * @return the converted string
249      */

250     public String JavaDoc addStringEscapes(String JavaDoc str, char escape) {
251         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
252
253         for (int i = 0; i < str.length(); i++) {
254             if (str.charAt(i) == '"') {
255                 res.append(escape);
256                 res.append("\"");
257             } else if (str.charAt(i) == escape) {
258                 res.append(escape);
259                 res.append(escape);
260             } else {
261                 res.append(str.charAt(i));
262             }
263         }
264
265         return res.toString();
266     }
267 }
268
Popular Tags