📄 jsputil.java
字号:
} else {
if (s == null || s.length() == 0)
return "0";
else
return Integer.valueOf(s).toString();
}
}
public static String coerceToInteger(String s, boolean isNamedAttribute) {
if (isNamedAttribute) {
return "(Integer) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Integer.class)";
} else {
if (s == null || s.length() == 0) {
return "new Integer(0)";
} else {
// Detect format error at translation time
return "new Integer(" + Integer.valueOf(s).toString() + ")";
}
}
}
public static String coerceToPrimitiveShort(String s,
boolean isNamedAttribute) {
if (isNamedAttribute) {
return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToShort(" + s + ")";
} else {
if (s == null || s.length() == 0)
return "(short) 0";
else
return "((short) " + Short.valueOf(s).toString() + ")";
}
}
public static String coerceToShort(String s, boolean isNamedAttribute) {
if (isNamedAttribute) {
return "(Short) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Short.class)";
} else {
if (s == null || s.length() == 0) {
return "new Short((short) 0)";
} else {
// Detect format error at translation time
return "new Short(\"" + Short.valueOf(s).toString() + "\")";
}
}
}
public static String coerceToPrimitiveLong(String s,
boolean isNamedAttribute) {
if (isNamedAttribute) {
return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToLong(" + s + ")";
} else {
if (s == null || s.length() == 0)
return "(long) 0";
else
return Long.valueOf(s).toString() + "l";
}
}
public static String coerceToLong(String s, boolean isNamedAttribute) {
if (isNamedAttribute) {
return "(Long) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Long.class)";
} else {
if (s == null || s.length() == 0) {
return "new Long(0)";
} else {
// Detect format error at translation time
return "new Long(" + Long.valueOf(s).toString() + "l)";
}
}
}
public static InputStream getInputStream(String fname, JarFile jarFile,
JspCompilationContext ctxt,
ErrorDispatcher err)
throws JasperException, IOException {
InputStream in = null;
if (jarFile != null) {
String jarEntryName = fname.substring(1, fname.length());
ZipEntry jarEntry = jarFile.getEntry(jarEntryName);
if (jarEntry == null) {
err.jspError("jsp.error.file.not.found", fname);
}
in = jarFile.getInputStream(jarEntry);
} else {
in = ctxt.getResourceAsStream(fname);
}
if (in == null) {
err.jspError("jsp.error.file.not.found", fname);
}
return in;
}
/**
* Gets the fully-qualified class name of the tag handler corresponding to
* the given tag file path.
*
* @param path Tag file path
* @param err Error dispatcher
*
* @return Fully-qualified class name of the tag handler corresponding to
* the given tag file path
*/
public static String getTagHandlerClassName(String path,
ErrorDispatcher err)
throws JasperException {
String className = null;
int begin = 0;
int index;
index = path.lastIndexOf(".tag");
if (index == -1) {
err.jspError("jsp.error.tagfile.badSuffix", path);
}
//It's tempting to remove the ".tag" suffix here, but we can't.
//If we remove it, the fully-qualified class name of this tag
//could conflict with the package name of other tags.
//For instance, the tag file
// /WEB-INF/tags/foo.tag
//would have fully-qualified class name
// org.apache.jsp.tag.web.foo
//which would conflict with the package name of the tag file
// /WEB-INF/tags/foo/bar.tag
index = path.indexOf(WEB_INF_TAGS);
if (index != -1) {
className = "org.apache.jsp.tag.web.";
begin = index + WEB_INF_TAGS.length();
} else {
index = path.indexOf(META_INF_TAGS);
if (index != -1) {
className = "org.apache.jsp.tag.meta.";
begin = index + META_INF_TAGS.length();
} else {
err.jspError("jsp.error.tagfile.illegalPath", path);
}
}
className += makeJavaPackage(path.substring(begin));
return className;
}
/**
* Converts the given path to a Java package or fully-qualified class name
*
* @param path Path to convert
*
* @return Java package corresponding to the given path
*/
public static final String makeJavaPackage(String path) {
String classNameComponents[] = split(path,"/");
StringBuffer legalClassNames = new StringBuffer();
for (int i = 0; i < classNameComponents.length; i++) {
legalClassNames.append(makeJavaIdentifier(classNameComponents[i]));
if (i < classNameComponents.length - 1) {
legalClassNames.append('.');
}
}
return legalClassNames.toString();
}
/**
* Splits a string into it's components.
* @param path String to split
* @param pat Pattern to split at
* @return the components of the path
*/
private static final String [] split(String path, String pat) {
Vector comps = new Vector();
int pos = path.indexOf(pat);
int start = 0;
while( pos >= 0 ) {
if(pos > start ) {
String comp = path.substring(start,pos);
comps.add(comp);
}
start = pos + pat.length();
pos = path.indexOf(pat,start);
}
if( start < path.length()) {
comps.add(path.substring(start));
}
String [] result = new String[comps.size()];
for(int i=0; i < comps.size(); i++) {
result[i] = (String)comps.elementAt(i);
}
return result;
}
/**
* Converts the given identifier to a legal Java identifier
*
* @param identifier Identifier to convert
*
* @return Legal Java identifier corresponding to the given identifier
*/
public static final String makeJavaIdentifier(String identifier) {
StringBuffer modifiedIdentifier =
new StringBuffer(identifier.length());
if (!Character.isJavaIdentifierStart(identifier.charAt(0))) {
modifiedIdentifier.append('_');
}
for (int i = 0; i < identifier.length(); i++) {
char ch = identifier.charAt(i);
if (Character.isJavaIdentifierPart(ch) && ch != '_') {
modifiedIdentifier.append(ch);
} else if (ch == '.') {
modifiedIdentifier.append('_');
} else {
modifiedIdentifier.append(mangleChar(ch));
}
}
if (isJavaKeyword(modifiedIdentifier.toString())) {
modifiedIdentifier.append('_');
}
return modifiedIdentifier.toString();
}
/**
* Mangle the specified character to create a legal Java class name.
*/
public static final String mangleChar(char ch) {
char[] result = new char[5];
result[0] = '_';
result[1] = Character.forDigit((ch >> 12) & 0xf, 16);
result[2] = Character.forDigit((ch >> 8) & 0xf, 16);
result[3] = Character.forDigit((ch >> 4) & 0xf, 16);
result[4] = Character.forDigit(ch & 0xf, 16);
return new String(result);
}
/**
* Test whether the argument is a Java keyword
*/
public static boolean isJavaKeyword(String key) {
int i = 0;
int j = javaKeywords.length;
while (i < j) {
int k = (i+j)/2;
int result = javaKeywords[k].compareTo(key);
if (result == 0) {
return true;
}
if (result < 0) {
i = k+1;
} else {
j = k;
}
}
return false;
}
/**
* Converts the given Xml name to a legal Java identifier. This is
* slightly more efficient than makeJavaIdentifier in that we only need
* to worry about '.', '-', and ':' in the string. We also assume that
* the resultant string is further concatenated with some prefix string
* so that we don't have to worry about it being a Java key word.
*
* @param name Identifier to convert
*
* @return Legal Java identifier corresponding to the given identifier
*/
public static final String makeXmlJavaIdentifier(String name) {
if (name.indexOf('-') >= 0)
name = replace(name, '-', "$1");
if (name.indexOf('.') >= 0)
name = replace(name, '.', "$2");
if (name.indexOf(':') >= 0)
name = replace(name, ':', "$3");
return name;
}
static InputStreamReader getReader(String fname, String encoding,
JarFile jarFile,
JspCompilationContext ctxt,
ErrorDispatcher err)
throws JasperException, IOException {
InputStreamReader reader = null;
InputStream in = getInputStream(fname, jarFile, ctxt, err);
try {
reader = new InputStreamReader(in, encoding);
} catch (UnsupportedEncodingException ex) {
err.jspError("jsp.error.unsupported.encoding", encoding);
}
return reader;
}
/**
* Handles taking input from TLDs
* 'java.lang.Object' -> 'java.lang.Object.class'
* 'int' -> 'int.class'
* 'void' -> 'Void.TYPE'
* 'int[]' -> 'int[].class'
*
* @param type
* @return
*/
public static String toJavaSourceTypeFromTld(String type) {
if (type == null || "void".equals(type)) {
return "Void.TYPE";
}
return type + ".class";
}
/**
* Class.getName() return arrays in the form "[[[<et>", where et,
* the element type can be one of ZBCDFIJS or L<classname>;
* It is converted into forms that can be understood by javac.
*/
public static String toJavaSourceType(String type) {
if (type.charAt(0) != '[') {
return type;
}
int dims = 1;
String t = null;
for (int i = 1; i < type.length(); i++) {
if (type.charAt(i) == '[') {
dims++;
} else {
switch (type.charAt(i)) {
case 'Z': t = "boolean"; break;
case 'B': t = "byte"; break;
case 'C': t = "char"; break;
case 'D': t = "double"; break;
case 'F': t = "float"; break;
case 'I': t = "int"; break;
case 'J': t = "long"; break;
case 'S': t = "short"; break;
case 'L': t = type.substring(i+1, type.indexOf(';')); break;
}
break;
}
}
StringBuffer resultType = new StringBuffer(t);
for (; dims > 0; dims--) {
resultType.append("[]");
}
return resultType.toString();
}
/**
* Compute the canonical name from a Class instance. Note that a
* simple replacment of '$' with '.' of a binary name would not work,
* as '$' is a legal Java Identifier character.
* @param c A instance of java.lang.Class
* @return The canonical name of c.
*/
public static String getCanonicalName(Class c) {
String binaryName = c.getName();
c = c.getDeclaringClass();
if (c == null) {
return binaryName;
}
StringBuffer buf = new StringBuffer(binaryName);
do {
buf.setCharAt(c.getName().length(), '.');
c = c.getDeclaringClass();
} while ( c != null);
return buf.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -