Java String Normalize normalizePackageNamePart(String name)

Here you can find the source of normalizePackageNamePart(String name)

Description

normalize Package Name Part

License

Apache License

Declaration

private static String normalizePackageNamePart(String name) 

Method Source Code

//package com.java2s;
/**/*  ww  w . java  2  s  .c  o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License" ); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.*;

public class Main {
    /**
     * These are java keywords as specified at the following URL.
     * http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#229308
     * Note that false, true, and null are not strictly keywords; they are
     * literal values, but for the purposes of this array, they can be treated
     * as literals.
     */
    private static final Set<String> KEYWORDS = new HashSet<String>(Arrays.asList("abstract", "assert", "boolean",
            "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double",
            "else", "enum", "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements",
            "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private",
            "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized",
            "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while"));

    private static String normalizePackageNamePart(String name) {
        StringBuffer sname = new StringBuffer(name.toLowerCase());

        for (int i = 0; i < sname.length(); i++) {
            sname.setCharAt(i, Character.toLowerCase(sname.charAt(i)));
        }

        for (int i = 0; i < sname.length(); i++) {
            if (!Character.isJavaIdentifierPart(sname.charAt(i))) {
                sname.setCharAt(i, '_');
            }
        }

        if (isJavaKeyword(sname.toString())) {
            sname.insert(0, '_');
        }

        if (!Character.isJavaIdentifierStart(sname.charAt(0))) {
            sname.insert(0, '_');
        }

        return sname.toString();
    }

    /**
     * checks if the input string is a valid java keyword.
     *
     * @return boolean true/false
     */
    public static boolean isJavaKeyword(String keyword) {
        return KEYWORDS.contains(keyword);
    }
}

Related

  1. normalizeDose(String dose)
  2. normalizeEnglishIdentifier(String id)
  3. normalizeFieldNameOrPath(final String nameOrPath)
  4. normalizeIndex(String input, String[] indexList)
  5. normalizeMatchup(final String matchup)
  6. normalizeRepositoryName(String input)
  7. normalizeSearchString(String src)
  8. normalizeString(String input)
  9. normalizeString(String str)