下面列出了javax.swing.text.ChangedCharSetException#getCharSetSpec ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static String getCharSet(ChangedCharSetException e) {
String spec = e.getCharSetSpec();
if (e.keyEqualsCharSet()) {
//charsetspec contains only charset
return spec;
}
//charsetspec is in form "text/html; charset=UTF-8"
int index = spec.indexOf(";"); // NOI18N
if (index != -1) {
spec = spec.substring(index + 1);
}
spec = spec.toLowerCase();
StringTokenizer st = new StringTokenizer(spec, " \t=", true); //NOI18N
boolean foundCharSet = false;
boolean foundEquals = false;
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.equals(" ") || token.equals("\t")) { //NOI18N
continue;
}
if (foundCharSet == false && foundEquals == false
&& token.equals("charset")) { //NOI18N
foundCharSet = true;
continue;
} else if (foundEquals == false && token.equals("=")) {//NOI18N
foundEquals = true;
continue;
} else if (foundEquals == true && foundCharSet == true) {
return token;
}
foundCharSet = false;
foundEquals = false;
}
return null;
}
/**
* Returns the charset from given {@link ChangedCharSetException}
* @param e the {@link ChangedCharSetException}
* @return the charset or null
*/
@CheckForNull
public static String getCharSet(ChangedCharSetException e) {
String spec = e.getCharSetSpec();
if (e.keyEqualsCharSet()) {
//charsetspec contains only charset
return spec;
}
//charsetspec is in form "text/html; charset=UTF-8"
int index = spec.indexOf(";"); // NOI18N
if (index != -1) {
spec = spec.substring(index + 1);
}
spec = spec.toLowerCase();
StringTokenizer st = new StringTokenizer(spec, " \t=", true); //NOI18N
boolean foundCharSet = false;
boolean foundEquals = false;
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.equals(" ") || token.equals("\t")) { //NOI18N
continue;
}
if (foundCharSet == false && foundEquals == false
&& token.equals("charset")) { //NOI18N
foundCharSet = true;
continue;
} else if (foundEquals == false && token.equals("=")) {//NOI18N
foundEquals = true;
continue;
} else if (foundEquals == true && foundCharSet == true) {
return token;
}
foundCharSet = false;
foundEquals = false;
}
return null;
}
private static String getCharSet(ChangedCharSetException e) {
String spec = e.getCharSetSpec();
if (e.keyEqualsCharSet()) {
//charsetspec contains only charset
return spec;
}
//charsetspec is in form "text/html; charset=UTF-8"
int index = spec.indexOf(";"); // NOI18N
if (index != -1) {
spec = spec.substring(index + 1);
}
spec = spec.toLowerCase(Locale.ENGLISH);
StringTokenizer st = new StringTokenizer(spec, " \t=", true); //NOI18N
boolean foundCharSet = false;
boolean foundEquals = false;
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.equals(" ") || token.equals("\t")) { //NOI18N
continue;
}
if (foundCharSet == false && foundEquals == false
&& token.equals("charset")) { //NOI18N
foundCharSet = true;
continue;
} else if (foundEquals == false && token.equals("=")) {//NOI18N
foundEquals = true;
continue;
} else if (foundEquals == true && foundCharSet == true) {
return token;
}
foundCharSet = false;
foundEquals = false;
}
return null;
}