下面列出了java.io.StreamTokenizer#ordinaryChars ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* <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 type The type to convert the value to
* @param value String value to be parsed
* @return List of parsed elements.
*
* @throws ConversionException if the syntax of <code>svalue</code>
* is not syntactically valid
* @throws NullPointerException if <code>svalue</code>
* is <code>null</code>
*/
private List parseElements(final Class type, String value) {
// Trim any matching '{' and '}' delimiters
value = value.trim();
if (value.startsWith("{") && value.endsWith("}")) {
value = value.substring(1, value.length() - 1);
}
try {
// Set up a StreamTokenizer on the characters in this String
StreamTokenizer st = new StreamTokenizer(new StringReader(value));
st.whitespaceChars(this.delimiter, this.delimiter); // Set the delimiters
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
st.wordChars('0', '9'); // Needed to make part of tokens
for (char allowedChar : this.allowedChars) {
st.ordinaryChars(allowedChar, allowedChar);
st.wordChars(allowedChar, allowedChar);
}
// Split comma-delimited tokens into a List
List list = null;
while (true) {
int ttype = st.nextToken();
if (ttype == StreamTokenizer.TT_WORD || ttype > 0) {
if (st.sval != null) {
if (list == null) {
list = new ArrayList();
}
list.add(st.sval);
}
} else if (ttype == StreamTokenizer.TT_EOF) {
break;
} else {
throw new ConversionException("Encountered token of type " + ttype + " parsing elements to '" + this.toString(type) + ".");
}
}
if (list == null) {
list = Collections.EMPTY_LIST;
}
// Return the completed list
return list;
} catch (IOException e) {
throw new ConversionException("Error converting from String to '" + this.toString(type) + "': " + e.getMessage(), e);
}
}
/**
* <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 type The type to convert the value to
* @param value String value to be parsed
* @return List of parsed elements.
*
* @throws ConversionException if the syntax of <code>svalue</code>
* is not syntactically valid
* @throws NullPointerException if <code>svalue</code>
* is <code>null</code>
*/
private List<String> parseElements(Class<?> type, String value) {
if (log().isDebugEnabled()) {
log().debug("Parsing elements, delimiter=[" + delimiter + "], value=[" + value + "]");
}
// Trim any matching '{' and '}' delimiters
value = value.trim();
if (value.startsWith("{") && value.endsWith("}")) {
value = value.substring(1, value.length() - 1);
}
try {
// Set up a StreamTokenizer on the characters in this String
StreamTokenizer st = new StreamTokenizer(new StringReader(value));
st.whitespaceChars(delimiter , delimiter); // Set the delimiters
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
st.wordChars('0', '9'); // Needed to make part of tokens
for (int i = 0; i < allowedChars.length; i++) {
st.ordinaryChars(allowedChars[i], allowedChars[i]);
st.wordChars(allowedChars[i], allowedChars[i]);
}
// Split comma-delimited tokens into a List
List<String> list = null;
while (true) {
int ttype = st.nextToken();
if ((ttype == StreamTokenizer.TT_WORD) || (ttype > 0)) {
if (st.sval != null) {
if (list == null) {
list = new ArrayList<String>();
}
list.add(st.sval);
}
} else if (ttype == StreamTokenizer.TT_EOF) {
break;
} else {
throw new ConversionException("Encountered token of type "
+ ttype + " parsing elements to '" + toString(type) + ".");
}
}
if (list == null) {
list = Collections.emptyList();
}
if (log().isDebugEnabled()) {
log().debug(list.size() + " elements parsed");
}
// Return the completed list
return (list);
} catch (IOException e) {
throw new ConversionException("Error converting from String to '"
+ toString(type) + "': " + e.getMessage(), e);
}
}
public static NodeMatcher parse( final String s ) throws IOException {
final StreamTokenizer tokenizer = new StreamTokenizer( new StringReader( s ) );
tokenizer.wordChars( '0', '9' );
tokenizer.ordinaryChar( '.' );
tokenizer.ordinaryChar( ',' );
tokenizer.ordinaryChars( 0, ' ' );
ElementMatcher elementMatcher = null;
NodeMatcher n = null;
Type selectorType = Type.Start;
int token;
while ( ( token = tokenizer.nextToken() ) != StreamTokenizer.TT_EOF ) {
if ( token == StreamTokenizer.TT_WORD || token == '*' ) {
NodeMatcher matcher = null;
switch ( selectorType ) {
case Start:
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
break;
case Child:
n = new ChildMatcher( n );
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
break;
case Descendant:
n = new DescendantMatcher( n );
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
break;
case Id:
if ( elementMatcher == null ) {
if ( n != null ) {
n = new DescendantMatcher( n );
}
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
}
elementMatcher.add( new AttributeMatcher( AttributeNames.Xml.NAMESPACE, AttributeNames.Xml.ID,
tokenizer.sval ) );
break;
case Class:
if ( elementMatcher == null ) {
if ( n != null ) {
n = new DescendantMatcher( n );
}
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
}
elementMatcher.add( new AttributeMatcher( AttributeNames.Core.NAMESPACE, AttributeNames.Core.STYLE_CLASS,
tokenizer.sval ) );
break;
default:
throw new IOException();
}
selectorType = Type.Element;
if ( matcher != null ) {
if ( n != null ) {
n = new AndMatcher( matcher, n );
} else {
n = matcher;
}
}
} else {
if ( token == '>' ) {
selectorType = Type.Child;
}
if ( token == '.' ) {
selectorType = Type.Class;
}
if ( token == '#' ) {
selectorType = Type.Id;
}
if ( Character.isWhitespace( token ) ) {
if ( selectorType == Type.Class || selectorType == Type.Id ) {
throw new IllegalStateException();
}
if ( selectorType != Type.Child ) {
selectorType = Type.Descendant;
}
}
}
}
return n;
}
public static NodeMatcher parse( final String s ) throws IOException {
final StreamTokenizer tokenizer = new StreamTokenizer( new StringReader( s ) );
tokenizer.wordChars( '0', '9' );
tokenizer.ordinaryChar( '.' );
tokenizer.ordinaryChar( ',' );
tokenizer.ordinaryChars( 0, ' ' );
ElementMatcher elementMatcher = null;
NodeMatcher n = null;
Type selectorType = Type.Start;
int token;
while ( ( token = tokenizer.nextToken() ) != StreamTokenizer.TT_EOF ) {
if ( token == StreamTokenizer.TT_WORD || token == '*' ) {
NodeMatcher matcher = null;
switch ( selectorType ) {
case Start:
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
break;
case Child:
n = new ChildMatcher( n );
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
break;
case Descendant:
n = new DescendantMatcher( n );
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
break;
case Id:
if ( elementMatcher == null ) {
if ( n != null ) {
n = new DescendantMatcher( n );
}
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
}
elementMatcher.add( new AttributeMatcher( AttributeNames.Xml.NAMESPACE, AttributeNames.Xml.ID,
tokenizer.sval ) );
break;
case Class:
if ( elementMatcher == null ) {
if ( n != null ) {
n = new DescendantMatcher( n );
}
elementMatcher = createMatcher( tokenizer );
matcher = elementMatcher;
}
elementMatcher.add( new AttributeMatcher( AttributeNames.Core.NAMESPACE, AttributeNames.Core.STYLE_CLASS,
tokenizer.sval ) );
break;
default:
throw new IOException();
}
selectorType = Type.Element;
if ( matcher != null ) {
if ( n != null ) {
n = new AndMatcher( matcher, n );
} else {
n = matcher;
}
}
} else {
if ( token == '>' ) {
selectorType = Type.Child;
}
if ( token == '.' ) {
selectorType = Type.Class;
}
if ( token == '#' ) {
selectorType = Type.Id;
}
if ( Character.isWhitespace( token ) ) {
if ( selectorType == Type.Class || selectorType == Type.Id ) {
throw new IllegalStateException();
}
if ( selectorType != Type.Child ) {
selectorType = Type.Descendant;
}
}
}
}
return n;
}
/**
* <p>Parse an incoming String of the form similar to an array initializer
* in the Java language into a {@code List} 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 type The type to convert the value to
* @param value String value to be parsed
* @return List of parsed elements.
*
* @throws ConversionException if the syntax of {@code value}
* is not syntactically valid
* @throws NullPointerException if {@code value}
* is {@code null}
*/
private List<String> parseElements(final Class<?> type, String value) {
if (log().isDebugEnabled()) {
log().debug("Parsing elements, delimiter=[" + delimiter + "], value=[" + value + "]");
}
// Trim any matching '{' and '}' delimiters
value = value.trim();
if (value.startsWith("{") && value.endsWith("}")) {
value = value.substring(1, value.length() - 1);
}
try {
// Set up a StreamTokenizer on the characters in this String
final StreamTokenizer st = new StreamTokenizer(new StringReader(value));
st.whitespaceChars(delimiter , delimiter); // Set the delimiters
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
st.wordChars('0', '9'); // Needed to make part of tokens
for (final char allowedChar : allowedChars) {
st.ordinaryChars(allowedChar, allowedChar);
st.wordChars(allowedChar, allowedChar);
}
// Split comma-delimited tokens into a List
List<String> list = null;
while (true) {
final int ttype = st.nextToken();
if (ttype == StreamTokenizer.TT_WORD || ttype > 0) {
if (st.sval != null) {
if (list == null) {
list = new ArrayList<>();
}
list.add(st.sval);
}
} else if (ttype == StreamTokenizer.TT_EOF) {
break;
} else {
throw new ConversionException("Encountered token of type "
+ ttype + " parsing elements to '" + toString(type) + ".");
}
}
if (list == null) {
list = Collections.emptyList();
}
if (log().isDebugEnabled()) {
log().debug(list.size() + " elements parsed");
}
// Return the completed list
return list;
} catch (final IOException e) {
throw new ConversionException("Error converting from String to '"
+ toString(type) + "': " + e.getMessage(), e);
}
}