KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > util > JavaNamer


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.generator.util;
18
19 import org.apache.ws.jaxme.generator.sg.SchemaSG;
20
21
22 /**
23  * @author <a HREF="mailto:iasandcb@tmax.co.kr">Ias</a>
24  */

25 public class JavaNamer {
26   
27   /** punctuation characters defined in JAXB spec */
28   public static final char[] PUNCTUATION_CHARS = {'-', '.', ':', '_', '\u00B7', '\u0387', '\u06DD', '\u06DE'};
29
30   /**
31    * Check whether the given character is a punctuation one or not.
32    *
33    * @param c character to check out
34    * @param pSchema hint for handling underscore
35    * @return true if c belongs to the punctuation characters, otherwise false.
36    */

37   public static boolean isPunctuationCharacter(char c, SchemaSG pSchema) {
38     for (int i = 0; i < PUNCTUATION_CHARS.length; i++) {
39       char pc = PUNCTUATION_CHARS[i];
40       if (pc == '_') {
41         if (!pSchema.isUnderscoreWordSeparator()) {
42           continue;
43         }
44       }
45       if (c == pc) {
46         return true;
47       }
48     }
49     return false;
50   }
51
52   /**
53    * Convert a local part name in XML to a class or field name in Java.
54    *
55    * @param pLocalName a given local name
56    * @param pSchema hint for following Java naming conventions and handling underscore
57    * @return the converted name based on the given hints.
58    */

59   
60   public static String JavaDoc convert(String JavaDoc pLocalName, SchemaSG pSchema) {
61     if (!pSchema.isJavaNamingConventionsEnabled()) {
62       return pLocalName;
63     }
64     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
65     char c = pLocalName.charAt(0);
66     if (Character.isJavaIdentifierStart(c)) {
67       result.append(Character.toUpperCase(c));
68     } else {
69       result.append('_');
70     }
71     for (int i = 1; i < pLocalName.length(); i++) {
72       c = pLocalName.charAt(i);
73       if (Character.isJavaIdentifierPart(c) && (c != '_')) {
74         result.append(c);
75       } else {
76         if (isPunctuationCharacter(c, pSchema)) {
77           i++;
78           c = pLocalName.charAt(i);
79           result.append(Character.toUpperCase(c));
80         } else {
81           result.append('_');
82         }
83       }
84     }
85     return result.toString();
86   }
87 }
88
Popular Tags