KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > codegen > JavaLang


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: JavaLang.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.codegen;
25
26 import java.io.File JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 /**
30  * Collection of methods and constants dealing with the Java language.
31  */

32 public final class JavaLang {
33     /**
34      * Indentation amount, as a string of blanks.
35      */

36     public static final String JavaDoc INDENT_STR = " ";
37
38     /**
39      * Line seperator to use on this platform
40      */

41     public static final char lineSeparator;
42
43     /*
44      * Class initializer.
45      */

46     static {
47         String JavaDoc lineSepStr = System.getProperty("line.separator");
48         lineSeparator = ((lineSepStr != null) && (lineSepStr.length() > 0))
49             ? lineSepStr.charAt(0) : '\n';
50     }
51     
52     /**
53      * Prevent instanciation.
54      */

55     private JavaLang() {
56     }
57
58     /*
59      * Parse a class or package name.
60      */

61     public static String JavaDoc[] parseClassName(String JavaDoc name) {
62         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(name, ".");
63         String JavaDoc[] names = new String JavaDoc[tokens.countTokens()];
64
65         for (int i = 0; i < names.length; i++) {
66             names[i] = tokens.nextToken();
67         }
68         return names;
69     }
70
71     /*
72      * Get the class name without the package.
73      * @param obj Any object or class object.
74      */

75     public static String JavaDoc simpleClassName(Object JavaDoc obj) {
76         if (obj instanceof Class JavaDoc) {
77             return simpleClassName(((Class JavaDoc)obj).getName());
78         } else {
79             return simpleClassName(obj.getClass().getName());
80         }
81     }
82
83     /*
84      * Get the class name without the package.
85      */

86     public static String JavaDoc simpleClassName(String JavaDoc className) {
87         int lastDot = className.lastIndexOf('.');
88         if (lastDot < 0) {
89             return className;
90         } else {
91             return className.substring(lastDot+1);
92         }
93     }
94
95     /*
96      * Get the package name of a class, or null if the default
97      * package is used.
98      */

99     public static String JavaDoc getPackageName(String JavaDoc className) {
100         int lastDot = className.lastIndexOf('.');
101         if (lastDot < 0) {
102             return "";
103         } else {
104             return className.substring(0, lastDot);
105         }
106     }
107
108     /**
109      * Convert a package name to a relative platform file name.
110      */

111     public static String JavaDoc packageNameToFileName(String JavaDoc packageName) {
112         return packageName.replace('.', File.separatorChar);
113     }
114
115     /**
116      * Convert a package name to a relative Unix file name.
117      */

118     public static String JavaDoc packageNameToUnixFileName(String JavaDoc packageName) {
119         return packageName.replace('.', '/');
120     }
121
122     /**
123      * Convert a unicode character to its \\uXXXX representation
124      * and append to a buffer.
125      */

126     private static void toUnicodeEncoding(char ch, StringBuffer JavaDoc buf) {
127         String JavaDoc numStr = Integer.toHexString(ch);
128         buf.append("\\u");
129         for (int cnt = 4-numStr.length(); cnt > 0; cnt--) {
130             buf.append('0');
131         }
132         buf.append(numStr);
133     }
134
135     /**
136      * Generate a Java string constant, quoting `\' and `"' and converting
137      * newlines.
138      *
139      * @param text Text to quote.
140      * @return The quoted string.
141      */

142     public static String JavaDoc createStringConst(String JavaDoc text) {
143         if (text == null) {
144             return "null";
145         }
146
147         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
148         int len = text.length ();
149
150         str.append ("\"");
151         for (int idx = 0; idx < len; idx++) {
152             char textChar = text.charAt (idx);
153             switch (textChar) {
154             case '\\':
155             case '"':
156                 str.append ('\\');
157                 str.append (textChar);
158                 break;
159             case '\t':
160                 str.append ("\\t");
161                 break;
162             case '\n':
163                 str.append ("\\n");
164                 break;
165             case '\r':
166                 str.append ("\\r");
167                 break;
168             default:
169                 if ((textChar > 127) || (Character.isISOControl(textChar))) {
170                     toUnicodeEncoding(textChar, str);
171                 } else {
172                     str.append (textChar);
173                 }
174                 break;
175             }
176         }
177         str.append ("\"");
178         return str.toString();
179     }
180
181     /*
182      * Determine if a string is a legal Java identifier
183      */

184     public static boolean legalJavaIdentifier(String JavaDoc str) {
185         if (str.length() == 0) {
186             return false;
187         }
188         if (!Character.isJavaIdentifierStart(str.charAt(0))) {
189             return false;
190         }
191         for (int j = 1; j < str.length(); j++) {
192             if (!Character.isJavaIdentifierPart(str.charAt(j))) {
193                 return false;
194             }
195         }
196         return true;
197     }
198
199     /**
200      * Construct a white-space string of a given length to use
201      * for indentation.
202      */

203     public static String JavaDoc makeIndent(int len) {
204         StringBuffer JavaDoc prefix = new StringBuffer JavaDoc();
205         for (int idx = 0; idx < len; idx++) {
206             prefix.append(' ');
207         }
208         return prefix.toString();
209     }
210
211 }
212
Popular Tags