src/share/jaxws_classes/com/sun/xml/internal/bind/api/impl/NameUtil.java

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.api.impl;
  27 
  28 import java.util.ArrayList;

  29 import java.util.HashSet;
  30 import java.util.List;
  31 import java.util.Locale;
  32 
  33 /**
  34  * Methods that convert strings into various formats.
  35  *
  36  * <p>
  37  * What JAX-RPC name binding tells us is that even such basic method
  38  * like "isLetter" can be different depending on the situation.
  39  *
  40  * For this reason, a whole lot of methods are made non-static,
  41  * even though they look like they should be static.
  42  */
  43 class NameUtil {
  44     protected boolean isPunct(char c) {
  45         return c == '-' || c == '.' || c == ':' || c == '_' || c == '\u00b7' || c == '\u0387' || c == '\u06dd' || c == '\u06de';
  46     }
  47 
  48     protected static boolean isDigit(char c) {


 306                 else if (c <= '\u00ff') sb.append("00");
 307                 else if (c <= '\u0fff') sb.append('0');
 308                 sb.append(Integer.toString(c, 16));
 309             }
 310         }
 311     }
 312 
 313     /**
 314      * Escapes characters that are unusable as Java identifiers
 315      * by replacing unsafe characters with safe characters.
 316      */
 317     private static String escape(String s) {
 318         int n = s.length();
 319         for (int i = 0; i < n; i++)
 320             if (!Character.isJavaIdentifierPart(s.charAt(i))) {
 321                 StringBuilder sb = new StringBuilder(s.substring(0, i));
 322                 escape(sb, s, i);
 323                 return sb.toString();
 324             }
 325         return s;
 326     }
 327 
 328 
 329     /**
 330      * Checks if a given string is usable as a Java identifier.
 331      */
 332     public static boolean isJavaIdentifier(String s) {
 333         if(s.length()==0)   return false;
 334         if( reservedKeywords.contains(s) )  return false;
 335 
 336         if(!Character.isJavaIdentifierStart(s.charAt(0)))   return false;
 337 
 338         for (int i = 1; i < s.length(); i++)
 339             if (!Character.isJavaIdentifierPart(s.charAt(i)))
 340                 return false;
 341 
 342         return true;
 343     }
 344 
 345     /**
 346      * Checks if the given string is a valid Java package name.
 347      */
 348     public static boolean isJavaPackageName(String s) {
 349         while(s.length()!=0) {
 350             int idx = s.indexOf('.');
 351             if(idx==-1) idx=s.length();
 352             if( !isJavaIdentifier(s.substring(0,idx)) )
 353                 return false;
 354 
 355             s = s.substring(idx);
 356             if(s.length()!=0)    s = s.substring(1);    // remove '.'
 357         }
 358         return true;
 359     }
 360 
 361 
 362     /** All reserved keywords of Java. */
 363     private static HashSet<String> reservedKeywords = new HashSet<String>();
 364 
 365     static {
 366         // see http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
 367         String[] words = new String[]{
 368             "abstract",
 369             "boolean",
 370             "break",
 371             "byte",
 372             "case",
 373             "catch",
 374             "char",
 375             "class",
 376             "const",
 377             "continue",
 378             "default",
 379             "do",
 380             "double",
 381             "else",
 382             "extends",
 383             "final",
 384             "finally",
 385             "float",
 386             "for",
 387             "goto",
 388             "if",
 389             "implements",
 390             "import",
 391             "instanceof",
 392             "int",
 393             "interface",
 394             "long",
 395             "native",
 396             "new",
 397             "package",
 398             "private",
 399             "protected",
 400             "public",
 401             "return",
 402             "short",
 403             "static",
 404             "strictfp",
 405             "super",
 406             "switch",
 407             "synchronized",
 408             "this",
 409             "throw",
 410             "throws",
 411             "transient",
 412             "try",
 413             "void",
 414             "volatile",
 415             "while",
 416 
 417             // technically these are not reserved words but they cannot be used as identifiers.
 418             "true",
 419             "false",
 420             "null",
 421 
 422             // and I believe assert is also a new keyword
 423             "assert",
 424 
 425             // and 5.0 keywords
 426             "enum"
 427             };
 428         for (String word : words)
 429             reservedKeywords.add(word);
 430     }
 431 }


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.api.impl;
  27 
  28 import java.util.ArrayList;
  29 import java.util.Collections;
  30 import java.util.HashSet;
  31 import java.util.List;
  32 import java.util.Locale;
  33 
  34 /**
  35  * Methods that convert strings into various formats.
  36  *
  37  * <p>
  38  * What JAX-RPC name binding tells us is that even such basic method
  39  * like "isLetter" can be different depending on the situation.
  40  *
  41  * For this reason, a whole lot of methods are made non-static,
  42  * even though they look like they should be static.
  43  */
  44 class NameUtil {
  45     protected boolean isPunct(char c) {
  46         return c == '-' || c == '.' || c == ':' || c == '_' || c == '\u00b7' || c == '\u0387' || c == '\u06dd' || c == '\u06de';
  47     }
  48 
  49     protected static boolean isDigit(char c) {


 307                 else if (c <= '\u00ff') sb.append("00");
 308                 else if (c <= '\u0fff') sb.append('0');
 309                 sb.append(Integer.toString(c, 16));
 310             }
 311         }
 312     }
 313 
 314     /**
 315      * Escapes characters that are unusable as Java identifiers
 316      * by replacing unsafe characters with safe characters.
 317      */
 318     private static String escape(String s) {
 319         int n = s.length();
 320         for (int i = 0; i < n; i++)
 321             if (!Character.isJavaIdentifierPart(s.charAt(i))) {
 322                 StringBuilder sb = new StringBuilder(s.substring(0, i));
 323                 escape(sb, s, i);
 324                 return sb.toString();
 325             }
 326         return s;








































































































 327     }
 328 }