/* * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.xml.internal.bind.api.impl; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Locale; /** * Methods that convert strings into various formats. * *
* What JAX-RPC name binding tells us is that even such basic method
* like "isLetter" can be different depending on the situation.
*
* For this reason, a whole lot of methods are made non-static,
* even though they look like they should be static.
*/
class NameUtil {
protected boolean isPunct(char c) {
return c == '-' || c == '.' || c == ':' || c == '_' || c == '\u00b7' || c == '\u0387' || c == '\u06dd' || c == '\u06de';
}
protected static boolean isDigit(char c) {
return c >= '0' && c <= '9' || Character.isDigit(c);
}
protected static boolean isUpper(char c) {
return c >= 'A' && c <= 'Z' || Character.isUpperCase(c);
}
protected static boolean isLower(char c) {
return c >= 'a' && c <= 'z' || Character.isLowerCase(c);
}
protected boolean isLetter(char c) {
return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || Character.isLetter(c);
}
private String toLowerCase(String s)
{
return s.toLowerCase(Locale.ENGLISH);
}
private String toUpperCase(char c)
{
return String.valueOf(c).toUpperCase(Locale.ENGLISH);
}
private String toUpperCase(String s)
{
return s.toUpperCase(Locale.ENGLISH);
}
/**
* Capitalizes the first character of the specified string,
* and de-capitalize the rest of characters.
*/
public String capitalize(String s) {
if (!isLower(s.charAt(0)))
return s;
StringBuilder sb = new StringBuilder(s.length());
sb.append(toUpperCase(s.charAt(0)));
sb.append(toLowerCase(s.substring(1)));
return sb.toString();
}
// Precondition: s[start] is not punctuation
private int nextBreak(String s, int start) {
int n = s.length();
char c1 = s.charAt(start);
int t1 = classify(c1);
for (int i=start+1; is.substring(start)
* will be escaped and copied to the string buffer.
*/
public static void escape(StringBuilder sb, String s, int start) {
int n = s.length();
for (int i = start; i < n; i++) {
char c = s.charAt(i);
if (Character.isJavaIdentifierPart(c))
sb.append(c);
else {
sb.append('_');
if (c <= '\u000f') sb.append("000");
else if (c <= '\u00ff') sb.append("00");
else if (c <= '\u0fff') sb.append('0');
sb.append(Integer.toString(c, 16));
}
}
}
/**
* Escapes characters that are unusable as Java identifiers
* by replacing unsafe characters with safe characters.
*/
private static String escape(String s) {
int n = s.length();
for (int i = 0; i < n; i++)
if (!Character.isJavaIdentifierPart(s.charAt(i))) {
StringBuilder sb = new StringBuilder(s.substring(0, i));
escape(sb, s, i);
return sb.toString();
}
return s;
}
}