KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > Util


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.js;
18
19 /** <p>Various static utility methods.</p>
20  *
21  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
22  */

23 public class Util {
24     /** <p>Returns whether the given name is a valid Java identifier.
25      * Works by using {@link Character#isJavaIdentifierStart(char)} and
26      * {@link Character#isJavaIdentifierPart(char)}.</p>
27      * @throws IllegalArgumentException The name is not valid. An explanation
28      * why is given in the detail message.
29      */

30     public static void checkJavaIdentifier(String JavaDoc pName) {
31         if (pName.length() == 0) {
32             throw new IllegalArgumentException JavaDoc("A valid Java identifier must not be empty.");
33         }
34         char c = pName.charAt(0);
35         if (!Character.isJavaIdentifierStart(c)) {
36             throw new IllegalArgumentException JavaDoc("The identifier " + pName +
37                                                 " is no valid Java identifier, because its first character is " + c);
38         }
39         for (int i = 1; i < pName.length(); i++) {
40             if (!Character.isJavaIdentifierPart(c)) {
41                 throw new IllegalArgumentException JavaDoc("The identifier " + pName +
42                                                     " is no valid Java identifier, because it contains the character " + c);
43             }
44         }
45     }
46
47     /** <p>Takes as input an arbitrary String and maps it to a String,
48      * which is a valid Java identifier. Mapping works as follows:
49      * <ol>
50      * <li>For the first character, invokes {@link Character#isJavaIdentifierStart(char)}.
51      * If that method returns false, replaces the character with an
52      * underscore ('_').</li>
53      * <li>For any following character, invokes {@link Character#isJavaIdentifierPart(char)}.
54      * If that method returns false, replaces the character with an
55      * underscore ('_').</li>
56      * </ol>
57      * </p>
58      * @param pIdentifier The identifier being mapped
59      * @throws IllegalArgumentException The parameter <code>pIdentifier</code>
60      * cannot be converted into a Java identifier, because it is null or
61      * empty.
62      */

63     public static String JavaDoc asJavaIdentifier(String JavaDoc pIdentifier) {
64         if (pIdentifier == null || pIdentifier.length() == 0) {
65             throw new IllegalArgumentException JavaDoc("A null or empty String cannot be converted into a valid Java identifier.");
66         }
67         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
68         char c = pIdentifier.charAt(0);
69         sb.append(Character.isJavaIdentifierStart(c) ? c : '_');
70         for (int i = 1; i < pIdentifier.length(); i++) {
71             c = pIdentifier.charAt(i);
72             sb.append(Character.isJavaIdentifierPart(c) ? c : '_');
73         }
74         return sb.toString();
75     }
76 }
77
Popular Tags