本文整理汇总了Java中java.io.StreamTokenizer.ordinaryChars方法的典型用法代码示例。如果您正苦于以下问题:Java StreamTokenizer.ordinaryChars方法的具体用法?Java StreamTokenizer.ordinaryChars怎么用?Java StreamTokenizer.ordinaryChars使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.StreamTokenizer
的用法示例。
在下文中一共展示了StreamTokenizer.ordinaryChars方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseElements
import java.io.StreamTokenizer; //导入方法依赖的package包/类
/**
* <p>Parse an incoming String of the form similar to an array initializer
* in the Java language into a <code>List</code> individual Strings
* for each element, according to the following rules.</p>
* <ul>
* <li>The string is expected to be a comma-separated list of values.</li>
* <li>The string may optionally have matching '{' and '}' delimiters
* around the list.</li>
* <li>Whitespace before and after each element is stripped.</li>
* <li>Elements in the list may be delimited by single or double quotes.
* Within a quoted elements, the normal Java escape sequences are valid.</li>
* </ul>
*
* @param svalue String value to be parsed
* @return The parsed list of String values
*
* @throws ConversionException if the syntax of <code>svalue</code>
* is not syntactically valid
* @throws NullPointerException if <code>svalue</code>
* is <code>null</code>
*/
protected List parseElements(String svalue) {
// Validate the passed argument
if (svalue == null) {
throw new NullPointerException();
}
// Trim any matching '{' and '}' delimiters
svalue = svalue.trim();
if (svalue.startsWith("{") && svalue.endsWith("}")) {
svalue = svalue.substring(1, svalue.length() - 1);
}
try {
// Set up a StreamTokenizer on the characters in this String
final StreamTokenizer st =
new StreamTokenizer(new StringReader(svalue));
st.whitespaceChars(',',','); // Commas are delimiters
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
st.ordinaryChars('.', '.');
st.ordinaryChars('-', '-');
st.wordChars('0', '9'); // Needed to make part of tokens
st.wordChars('.', '.');
st.wordChars('-', '-');
// Split comma-delimited tokens into a List
final ArrayList list = new ArrayList();
while (true) {
final int ttype = st.nextToken();
if ((ttype == StreamTokenizer.TT_WORD) ||
(ttype > 0)) {
list.add(st.sval);
} else if (ttype == StreamTokenizer.TT_EOF) {
break;
} else {
throw new ConversionException
("Encountered token of type " + ttype);
}
}
// Return the completed list
return (list);
} catch (final IOException e) {
throw new ConversionException(e);
}
}