/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Utility methods that convert arbitrary strings into Java identifiers.
*/
public class JJavaName {
/**
* Checks if a given string is usable as a Java identifier.
*/
if(s.length()==0) return false;
if( reservedKeywords.contains(s) ) return false;
for (int i = 1; i < s.length(); i++)
return false;
return true;
}
/**
* Checks if the given string is a valid fully qualified name.
*/
return isJavaPackageName(s);
}
/**
* Checks if the given string is a valid Java package name.
*/
while(s.length()!=0) {
return false;
}
return true;
}
/**
* <b>Experimental API:</b> converts an English word into a plural form.
*
* @param word
* a word, such as "child", "apple". Must not be null.
* It accepts word concatanation forms
* that are common in programming languages, such as "my_child", "MyChild",
* "myChild", "MY-CHILD", "CODE003-child", etc, and mostly tries to do the right thing.
* ("my_children","MyChildren","myChildren", and "MY-CHILDREN", "CODE003-children" respectively)
* <p>
* Although this method only works for English words, it handles non-English
* words gracefully (by just returning it as-is.) For example, 日本語
* will be returned as-is without modified, not "日本語s"
* <p>
* This method doesn't handle suffixes very well. For example, passing
* "person56" will return "person56s", not "people56".
*
* @return
* always non-null.
*/
// remember the casing of the word
boolean allUpper = true;
// check if the word looks like an English word.
// if we see non-ASCII characters, abort
if(ch >=0x80)
return word;
// note that this isn't the same as allUpper &= Character.isUpperCase(ch);
}
if(r!=null) {
if(allUpper) r=r.toUpperCase();
return r;
}
}
// failed
return word;
}
/** All reserved keywords of Java. */
static {
"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"
};
reservedKeywords.add(w);
}
private static class Entry {
this.replacement = replacement;
}
if(m.matches()) {
} else {
return null;
}
}
}
static {
"(.*)child","$1children",
"(.+)fe","$1ves",
"(.*)mouse","$1mise",
"(.+)f","$1ves",
"(.+)ch","$1ches",
"(.+)sh","$1shes",
"(.*)tooth","$1teeth",
"(.+)um","$1a",
"(.+)an","$1en",
"(.+)ato","$1atoes",
"(.*)basis","$1bases",
"(.*)axis","$1axes",
"(.+)is","$1ises",
"(.+)ss","$1sses",
"(.+)us","$1uses",
"(.+)s","$1s",
"(.*)foot","$1feet",
"(.+)ix","$1ixes",
"(.+)ex","$1ices",
"(.+)nx","$1nxes",
"(.+)x","$1xes",
"(.+)y","$1ies",
"(.+)","$1s",
};
}
}
}