Java Java String Format toJavaPackageName(String name)

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

Description

to Java Package Name

License

Open Source License

Declaration

public static String toJavaPackageName(String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011, 2012 Red Hat, Inc. 
 * All rights reserved. //from   ww  w.  ja v a 2s .  c o  m
 * This program is made available under the terms of the 
 * Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.eclipse.org/legal/epl-v10.html 
 *
 * Contributors: 
 * Red Hat, Inc. - initial API and implementation 
 *******************************************************************************/

public class Main {
    public static String toJavaPackageName(String name) {
        if (name == null || name.isEmpty())
            return "_"; //$NON-NLS-1$
        String result = null;
        for (String part : name.split("\\.")) { //$NON-NLS-1$
            if (result == null || result.isEmpty())
                result = toJavaIdentifier(part);
            else
                result = result + "." + toJavaIdentifier(part); //$NON-NLS-1$
        }
        return result;
    }

    public static final String toJavaIdentifier(String name) {
        if (name == null || name.isEmpty())
            return "_"; //$NON-NLS-1$

        StringBuffer ncname = new StringBuffer();
        int nameLength = name.length();

        // Check first character
        char c = name.charAt(0);

        if (Character.isJavaIdentifierStart(c))
            ncname.append(c);
        else
            ncname.append('_');
        // Check the rest of the characters
        for (int i = 1; i < nameLength; i++) {
            c = name.charAt(i);
            if (!Character.isJavaIdentifierPart(c)) {
                c = '_';
            }
            ncname.append(c);
        }
        return ncname.toString();
    }
}

Related

  1. toJavaImplSig(String jsig)
  2. toJavaMethodName(String sqlNotation)
  3. toJavaMethodName(String... words)
  4. toJavaName(String name)
  5. toJavaName(String name)
  6. toJavaPath(String path)
  7. toJavaPath(String path)
  8. toJavaPattern(String pattern)
  9. toJavascriptArray(String[][] Vals)