Java String Sanitize sanitizePackageName(String raw)

Here you can find the source of sanitizePackageName(String raw)

Description

Convert a raw package name to a legal Java package name.

License

Open Source License

Parameter

Parameter Description
raw basic package name, which may include illegal characters

Return

sanitized package name

Declaration

public static String sanitizePackageName(String raw) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from www  .j  ava2 s  .  c om
     * Convert a raw package name to a legal Java package name. The raw package name must be in standard package name
     * form, with periods separating the individual directory components of the package name.
     * 
     * @param raw basic package name, which may include illegal characters
     * @return sanitized package name
     */
    public static String sanitizePackageName(String raw) {
        StringBuffer buff = new StringBuffer(raw.length());
        boolean first = true;
        for (int i = 0; i < raw.length();) {
            char chr = buff.charAt(i);
            if (first) {
                if (Character.isJavaIdentifierStart(chr)) {
                    first = false;
                    i++;
                } else {
                    buff.deleteCharAt(i);
                }
            } else if (chr == '.') {
                first = true;
                i++;
            } else if (!Character.isJavaIdentifierPart(chr)) {
                buff.deleteCharAt(i);
            } else {
                i++;
            }
        }
        return buff.toString();
    }
}

Related

  1. sanitizeName(String name)
  2. sanitizeName(String name)
  3. sanitizeName(String origName)
  4. sanitizeNamespace(String namespace)
  5. sanitizeOperationId(String operationId)
  6. sanitizePackageVersion(String string)
  7. sanitizePath(String apiPath)
  8. sanitizePath(String input)
  9. sanitizePrimitives(Class clazz)