List of usage examples for java.io StringReader mark
int mark
To view the source code for java.io StringReader mark.
Click Source Link
From source file:Main.java
public static void main(String[] argv) throws IOException { StringReader stringReader = new StringReader("java2s.com"); stringReader.mark(2); System.out.println(stringReader.markSupported()); System.out.println(stringReader.read()); stringReader.reset();/*from w w w .j a v a 2s .c o m*/ stringReader.close(); }
From source file:com.unboundid.scim2.common.utils.Parser.java
/** * Read a path token. A token is either: * <ul>/*from w w w .j a v a2 s . c o m*/ * <li> * An attribute name terminated by a period. * </li> * <li> * An attribute name terminated by an opening brace. * </li> * <li> * </ul> * * @param reader The reader to read from. * * @return The token at the current position, or {@code null} if the end of * the input has been reached. * @throws BadRequestException If the path string could not be parsed. */ private static String readPathToken(final StringReader reader) throws BadRequestException { reader.mark(0); int c = reader.read(); StringBuilder b = new StringBuilder(); while (c > 0) { if (c == '.') { if (reader.pos >= reader.string.length()) { // There is nothing after the period. throw BadRequestException.invalidPath("Unexpected end of path string"); } // Terminating period. Consume it and return token. return b.toString(); } if (c == '[') { // Terminating opening brace. Consume it and return token. b.append((char) c); return b.toString(); } if (c == '-' || c == '_' || c == '$' || Character.isLetterOrDigit(c)) { b.append((char) c); } else { final String msg = String.format( "Unexpected character '%s' at position %d for token starting at %d", (char) c, reader.pos - 1, reader.mark); throw BadRequestException.invalidPath(msg); } c = reader.read(); } if (b.length() > 0) { return b.toString(); } return null; }
From source file:com.unboundid.scim2.common.utils.Parser.java
/** * Read a filter token. A token is either: * <ul>/*w w w . j a v a2 s. co m*/ * <li> * An attribute path terminated by a space or an opening parenthesis. * </li> * <li> * An attribute path terminated by an opening brace. * </li> * <li> * An operator terminated by a space or an opening parenthesis. * </li> * <li> * An opening parenthesis. * </li> * <li> * An closing parenthesis. * </li> * <li> * An closing brace. * </li> * <li> * * </li> * </ul> * * @param reader The reader to read from. * @param isValueFilter Whether to read the token for a value filter. * * @return The token at the current position, or {@code null} if the end of * the input has been reached. * @throws BadRequestException If the filter string could not be parsed. */ private static String readFilterToken(final StringReader reader, final boolean isValueFilter) throws BadRequestException { int c; do { // Skip over any leading spaces. reader.mark(0); c = reader.read(); } while (c == ' '); StringBuilder b = new StringBuilder(); while (c > 0) { if (c == ' ') { // Terminating space. Consume it and return token. return b.toString(); } if (c == '(' || c == ')') { if (b.length() > 0) { // Do not consume the parenthesis. reader.unread(); } else { b.append((char) c); } return b.toString(); } if (!isValueFilter && c == '[') { // Terminating opening brace. Consume it and return token. b.append((char) c); return b.toString(); } if (isValueFilter && c == ']') { if (b.length() > 0) { // Do not consume the closing brace. reader.unread(); } else { b.append((char) c); } return b.toString(); } if (c == '-' || c == '_' || c == '.' || c == ':' || c == '$' || Character.isLetterOrDigit(c)) { b.append((char) c); } else { final String msg = String.format( "Unexpected character '%s' at position %d for token starting at %d", (char) c, reader.pos - 1, reader.mark); throw BadRequestException.invalidFilter(msg); } c = reader.read(); } if (b.length() > 0) { return b.toString(); } return null; }
From source file:com.unboundid.scim2.common.utils.Parser.java
/** * Read a filter from the reader.//from w ww . ja va 2 s . c om * * @param reader The reader to read the filter from. * @param isValueFilter Whether to read the filter as a value filter. * @return The parsed filter. * @throws BadRequestException If the filter string could not be parsed. */ private static Filter readFilter(final StringReader reader, final boolean isValueFilter) throws BadRequestException { final Stack<Filter> outputStack = new Stack<Filter>(); final Stack<String> precedenceStack = new Stack<String>(); String token; String previousToken = null; while ((token = readFilterToken(reader, isValueFilter)) != null) { if (token.equals("(") && expectsNewFilter(previousToken)) { precedenceStack.push(token); } else if (token.equalsIgnoreCase(FilterType.NOT.getStringValue()) && expectsNewFilter(previousToken)) { // "not" should be followed by an ( String nextToken = readFilterToken(reader, isValueFilter); if (nextToken == null) { throw BadRequestException.invalidFilter("Unexpected end of filter string"); } if (!nextToken.equals("(")) { final String msg = String.format("Expected '(' at position %d", reader.mark); throw BadRequestException.invalidFilter(msg); } precedenceStack.push(token); } else if (token.equals(")") && !expectsNewFilter(previousToken)) { String operator = closeGrouping(precedenceStack, outputStack, false); if (operator == null) { final String msg = String.format( "No opening parenthesis matching closing " + "parenthesis at position %d", reader.mark); throw BadRequestException.invalidFilter(msg); } if (operator.equalsIgnoreCase(FilterType.NOT.getStringValue())) { // Treat "not" the same as "(" except wrap everything in a not filter. outputStack.push(Filter.not(outputStack.pop())); } } else if (token.equalsIgnoreCase(FilterType.AND.getStringValue()) && !expectsNewFilter(previousToken)) { // and has higher precedence than or. precedenceStack.push(token); } else if (token.equalsIgnoreCase(FilterType.OR.getStringValue()) && !expectsNewFilter(previousToken)) { // pop all the pending ands first before pushing or. LinkedList<Filter> andComponents = new LinkedList<Filter>(); while (!precedenceStack.isEmpty()) { if (precedenceStack.peek().equalsIgnoreCase(FilterType.AND.getStringValue())) { precedenceStack.pop(); andComponents.addFirst(outputStack.pop()); } else { break; } if (!andComponents.isEmpty()) { andComponents.addFirst(outputStack.pop()); outputStack.push(Filter.and(andComponents)); } } precedenceStack.push(token); } else if (token.endsWith("[") && expectsNewFilter(previousToken)) { // This is a complex value filter. final Path filterAttribute; try { filterAttribute = parsePath(token.substring(0, token.length() - 1)); } catch (final BadRequestException e) { Debug.debugException(e); final String msg = String.format("Invalid attribute path at position %d: %s", reader.mark, e.getMessage()); throw BadRequestException.invalidFilter(msg); } if (filterAttribute.isRoot()) { final String msg = String.format("Attribute path expected at position %d", reader.mark); throw BadRequestException.invalidFilter(msg); } outputStack.push(Filter.hasComplexValue(filterAttribute, readFilter(reader, true))); } else if (isValueFilter && token.equals("]") && !expectsNewFilter(previousToken)) { break; } else if (expectsNewFilter(previousToken)) { // This must be an attribute path followed by operator and maybe value. final Path filterAttribute; try { filterAttribute = parsePath(token); } catch (final BadRequestException e) { Debug.debugException(e); final String msg = String.format("Invalid attribute path at position %d: %s", reader.mark, e.getMessage()); throw BadRequestException.invalidFilter(msg); } if (filterAttribute.isRoot()) { final String msg = String.format("Attribute path expected at position %d", reader.mark); throw BadRequestException.invalidFilter(msg); } String op = readFilterToken(reader, isValueFilter); if (op == null) { throw BadRequestException.invalidFilter("Unexpected end of filter string"); } if (op.equalsIgnoreCase(FilterType.PRESENT.getStringValue())) { outputStack.push(Filter.pr(filterAttribute)); } else { ValueNode valueNode; try { // Mark the beginning of the JSON value so we can later reset back // to this position and skip the actual chars that were consumed // by Jackson. The Jackson parser is buffered and reads everything // until the end of string. reader.mark(0); ScimJsonFactory scimJsonFactory = (ScimJsonFactory) JsonUtils.getObjectReader() .getFactory(); JsonParser parser = scimJsonFactory.createScimFilterParser(reader); // The object mapper will return a Java null for JSON null. // Have to distinguish between reading a JSON null and encountering // the end of string. if (parser.getCurrentToken() == null && parser.nextToken() == null) { // End of string. valueNode = null; } else { valueNode = parser.readValueAsTree(); // This is actually a JSON null. Use NullNode. if (valueNode == null) { valueNode = JsonUtils.getJsonNodeFactory().nullNode(); } } // Reset back to the beginning of the JSON value. reader.reset(); // Skip the number of chars consumed by JSON parser. reader.skip(parser.getCurrentLocation().getCharOffset()); } catch (IOException e) { final String msg = String.format("Invalid comparison value at position %d: %s", reader.mark, e.getMessage()); throw BadRequestException.invalidFilter(msg); } if (valueNode == null) { throw BadRequestException.invalidFilter("Unexpected end of filter string"); } if (op.equalsIgnoreCase(FilterType.EQUAL.getStringValue())) { outputStack.push(Filter.eq(filterAttribute, valueNode)); } else if (op.equalsIgnoreCase(FilterType.NOT_EQUAL.getStringValue())) { outputStack.push(Filter.ne(filterAttribute, valueNode)); } else if (op.equalsIgnoreCase(FilterType.CONTAINS.getStringValue())) { outputStack.push(Filter.co(filterAttribute, valueNode)); } else if (op.equalsIgnoreCase(FilterType.STARTS_WITH.getStringValue())) { outputStack.push(Filter.sw(filterAttribute, valueNode)); } else if (op.equalsIgnoreCase(FilterType.ENDS_WITH.getStringValue())) { outputStack.push(Filter.ew(filterAttribute, valueNode)); } else if (op.equalsIgnoreCase(FilterType.GREATER_THAN.getStringValue())) { outputStack.push(Filter.gt(filterAttribute, valueNode)); } else if (op.equalsIgnoreCase(FilterType.GREATER_OR_EQUAL.getStringValue())) { outputStack.push(Filter.ge(filterAttribute, valueNode)); } else if (op.equalsIgnoreCase(FilterType.LESS_THAN.getStringValue())) { outputStack.push(Filter.lt(filterAttribute, valueNode)); } else if (op.equalsIgnoreCase(FilterType.LESS_OR_EQUAL.getStringValue())) { outputStack.push(Filter.le(filterAttribute, valueNode)); } else { final String msg = String.format("Unrecognized attribute operator '%s' at position %d. " + "Expected: eq,ne,co,sw,ew,pr,gt,ge,lt,le", op, reader.mark); throw BadRequestException.invalidFilter(msg); } } } else { final String msg = String.format("Unexpected character '%s' at position %d", token, reader.mark); throw BadRequestException.invalidFilter(msg); } previousToken = token; } closeGrouping(precedenceStack, outputStack, true); if (outputStack.isEmpty()) { throw BadRequestException.invalidFilter("Unexpected end of filter string"); } return outputStack.pop(); }