📄 val.java
字号:
package com.esri.solutions.jitk.datasources.ogc.wcs;
/**
* Provides basic validation and conversion support.
* @author UM
*/
public class Val {
/**
* Converts a string to a boolean value.
* @param s the string to convert
* @param defaultVal the default value to return if the string is invalid
* @return the converted value
*/
public static boolean chkBool(String s, boolean defaultVal) {
boolean b = defaultVal;
String v = chkStr(s).toLowerCase();
if (v.length() > 0) {
if (v.equals("true")) b = true;
else if (v.equals("false")) b = false;
else if (v.equals("y")) b = true;
else if (v.equals("n")) b = false;
else if (v.equals("yes")) b = true;
else if (v.equals("no")) b = false;
else if (v.equals("on")) b = true;
else if (v.equals("off")) b = false;
else if (v.equals("0")) b = false;
else if (v.equals("1")) b = true;
else if (v.equals("-1")) b = false;
}
return b;
}
/**
* Converts a string to a double value.
* @param s the string to convert
* @param defaultVal the default value to return if the string is invalid
* @return the converted value
*/
public static double chkDbl(String s, double defaultVal) {
double n = defaultVal;
try { n = Double.parseDouble(s.replaceAll(",",".")); }
catch (Exception e) {n = defaultVal;}
return n;
}
/**
* Converts a string to an int value.
* @param s the string to convert
* @param defaultVal the default value to return if the string is invalid
* @return the converted value
*/
public static int chkInt(String s, int defaultVal) {
int n = defaultVal;
try {n = Integer.parseInt(s);}
catch (Exception e) {n = defaultVal;}
return n;
}
/**
* Converts a string to a long value.
* @param s the string to convert
* @param defaultVal the default value to return if the string is invalid
* @return the converted value
*/
public static long chkLong(String s, long defaultVal) {
long n = defaultVal;
try {n = Long.parseLong(s);}
catch (Exception e) {n = defaultVal;}
return n;
}
/**
* Check a string value.
* @param s the string to check
* @return the checked string (trimmed, zero length if the supplied String was null)
*/
public static String chkStr(String s) {
if (s == null) return "";
else return s.trim();
}
/**
* Check a string value.
* @param s the string to check
@param defaultVal the default value to return if the string is null or empty
* @return the checked string (trimmed, default value if the
* supplied String was null or empty)
*/
public static String chkStr(String s, String defaultVal) {
s = chkStr(s);
if (s.length() == 0) return defaultVal;
else return s;
}
/**
* Escapes single and double quotes within a string.
* The escape character inserted is a backslash.
* @param s the string to escape
* @return the escaped string
*/
public static String escapeQuotes(String s) {
if ((s == null) || (s.length() == 0)) return "";
else {
int nIdx1 = s.indexOf("\"");
int nIdx2 = s.indexOf("'");
if ((nIdx1 != -1) || (nIdx2 != -1)) {
char c;
StringBuffer sb = new StringBuffer(s.length()+20);
for (int i=0; i<s.length(); i++) {
c = s.charAt(i);
if ((c == '\'') || (c == '"')) sb.append("\\");
sb.append(c);
}
return sb.toString();
} else return s;
}
}
/**
* Escapes special xml characters within a string.
* <p> < > & " are escaped. The single quote character is not escaped.
* @param s the string to escape
* @return the escaped string
*/
public static String escapeXmlForBrowser(String s) {
if ((s == null) || (s.length() == 0)) {
return "";
} else {
char c;
StringBuffer sb = new StringBuffer(s.length()+20);
for (int i=0; i<s.length(); i++) {
c = s.charAt(i);
if (c == '&') sb.append("&");
else if (c == '<') sb.append("<");
else if (c == '>') sb.append(">");
else if (c == '\'') sb.append("'");
else if (c == '"') sb.append(""");
else sb.append(c);
}
return sb.toString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -