All Downloads are FREE. Search and download functionalities are using the official Maven repository.

impl.com.sun.xml.bind.api.impl.NameUtil Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.xml.bind.api.impl;

import java.util.ArrayList;
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; i * This method uses a change in character type as a splitter * of two words. For example, "abc100ghi" will be splitted into * {"Abc", "100","Ghi"}. */ public List toWordList(String s) { ArrayList ss = new ArrayList(); int n = s.length(); for (int i = 0; i < n;) { // Skip punctuation while (i < n) { if (!isPunct(s.charAt(i))) break; i++; } if (i >= n) break; // Find next break and collect word int b = nextBreak(s, i); String w = (b == -1) ? s.substring(i) : s.substring(i, b); ss.add(escape(capitalize(w))); if (b == -1) break; i = b; } // we can't guarantee a valid Java identifier anyway, // so there's not much point in rejecting things in this way. // if (ss.size() == 0) // throw new IllegalArgumentException("Zero-length identifier"); return ss; } protected String toMixedCaseName(List ss, boolean startUpper) { StringBuilder sb = new StringBuilder(); if(!ss.isEmpty()) { sb.append(startUpper ? ss.get(0) : toLowerCase(ss.get(0))); for (int i = 1; i < ss.size(); i++) sb.append(ss.get(i)); } return sb.toString(); } protected String toMixedCaseVariableName(String[] ss, boolean startUpper, boolean cdrUpper) { if (cdrUpper) for (int i = 1; i < ss.length; i++) ss[i] = capitalize(ss[i]); StringBuilder sb = new StringBuilder(); if( ss.length>0 ) { sb.append(startUpper ? ss[0] : toLowerCase(ss[0])); for (int i = 1; i < ss.length; i++) sb.append(ss[i]); } return sb.toString(); } /** * Formats a string into "THIS_KIND_OF_FORMAT_ABC_DEF". * * @return * Always return a string but there's no guarantee that * the generated code is a valid Java identifier. */ public String toConstantName(String s) { return toConstantName(toWordList(s)); } /** * Formats a string into "THIS_KIND_OF_FORMAT_ABC_DEF". * * @return * Always return a string but there's no guarantee that * the generated code is a valid Java identifier. */ public String toConstantName(List ss) { StringBuilder sb = new StringBuilder(); if( !ss.isEmpty() ) { sb.append(toUpperCase(ss.get(0))); for (int i = 1; i < ss.size(); i++) { sb.append('_'); sb.append(toUpperCase(ss.get(i))); } } return sb.toString(); } /** * Escapes characters is the given string so that they can be * printed by only using US-ASCII characters. * * The escaped characters will be appended to the given * StringBuffer. * * @param sb * StringBuffer that receives escaped string. * @param s * String to be escaped. s.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; } /** * Checks if a given string is usable as a Java identifier. */ public static boolean isJavaIdentifier(String s) { if(s.length()==0) return false; if( reservedKeywords.contains(s) ) return false; if(!Character.isJavaIdentifierStart(s.charAt(0))) return false; for (int i = 1; i < s.length(); i++) if (!Character.isJavaIdentifierPart(s.charAt(i))) return false; return true; } /** * Checks if the given string is a valid Java package name. */ public static boolean isJavaPackageName(String s) { while(s.length()!=0) { int idx = s.indexOf('.'); if(idx==-1) idx=s.length(); if( !isJavaIdentifier(s.substring(0,idx)) ) return false; s = s.substring(idx); if(s.length()!=0) s = s.substring(1); // remove '.' } return true; } /** All reserved keywords of Java. */ private static HashSet reservedKeywords = new HashSet(); static { // see http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html String[] words = new String[]{ "abstract", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while", // technically these are not reserved words but they cannot be used as identifiers. "true", "false", "null", // and I believe assert is also a new keyword "assert", // and 5.0 keywords "enum" }; for (String word : words) reservedKeywords.add(word); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy