Here you can find the source of toJavaPackageName(String name)
public static String toJavaPackageName(String name)
//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(); } }