use of java.util.EmptyStackException in project ecf by eclipse.
the class RFC1960Filter method fromString.
/**
* get a filter instance from filter string.
*
* @param filterString
* the filter string.
* @return a filter instance.
* @throws InvalidSyntaxException
* is the string is invalid.
*/
public static Filter fromString(final String filterString) {
if (filterString == null || "".equals(filterString)) {
return NULL_FILTER;
}
Stack stack = new Stack();
try {
final int len = filterString.length();
int last = -1;
int oper = 0;
String id = null;
int comparator = -1;
final char[] chars = filterString.toCharArray();
stack.clear();
for (int i = 0; i < chars.length; i++) {
switch(chars[i]) {
case '(':
// lookahead ...
final char nextChar = chars[i + 1];
if (nextChar == '&') {
stack.push(new RFC1960Filter(AND_OPERATOR));
continue;
} else if (nextChar == '|') {
stack.push(new RFC1960Filter(OR_OPERATOR));
continue;
} else if (nextChar == '!') {
stack.push(new RFC1960Filter(NOT_OPERATOR));
continue;
} else {
if (last == -1) {
last = i;
} else {
throw new IllegalStateException("Surplus left paranthesis at: " + filterString.substring(i));
}
}
continue;
case ')':
if (last == -1) {
RFC1960Filter filter = (RFC1960Filter) stack.pop();
if (stack.isEmpty()) {
return filter;
}
RFC1960Filter parent = (RFC1960Filter) stack.peek();
if (parent.operator == NOT_OPERATOR && !parent.operands.isEmpty()) {
throw new IllegalStateException("Unexpected literal: " + filterString.substring(i));
}
parent.operands.add(filter);
if (i == len - 1) {
throw new IllegalStateException("Missing right paranthesis at the end.");
}
} else {
if (oper == 0) {
throw new IllegalStateException("Missing operator.");
}
if (stack.isEmpty()) {
if (i == len - 1) {
// just a single simple filter
String value = filterString.substring(++oper, len - 1);
if (value.equals("*") && comparator == EQUALS) {
comparator = PRESENT;
value = null;
}
return new RFC1960SimpleFilter(id, comparator, value);
} else {
throw new IllegalStateException("Unexpected literal: " + filterString.substring(i));
}
}
// get the parent from stack
RFC1960Filter parent = ((RFC1960Filter) stack.peek());
String value = filterString.substring(++oper, i);
if (value.equals("*") && comparator == EQUALS) {
comparator = PRESENT;
value = null;
}
// link current element to parent
parent.operands.add(new RFC1960SimpleFilter(id, comparator, value));
oper = 0;
last = -1;
id = null;
comparator = -1;
}
continue;
case '~':
if (oper == 0 && chars[i + 1] == '=') {
id = filterString.substring(last + 1, i).trim();
comparator = APPROX;
oper = ++i;
continue;
} else {
throw new IllegalStateException("Unexpected character " + chars[i + 1]);
}
case '>':
if (oper == 0 && chars[i + 1] == '=') {
id = filterString.substring(last + 1, i).trim();
comparator = GREATER;
oper = ++i;
continue;
} else {
throw new IllegalStateException("Unexpected character " + chars[i + 1]);
}
case '<':
if (oper == 0 && chars[i + 1] == '=') {
id = filterString.substring(last + 1, i).trim();
comparator = LESS;
oper = ++i;
continue;
} else {
throw new IllegalStateException("Unexpected character " + chars[i + 1]);
}
case '=':
// could also be a "=*" present production.
// if this is the case, it is fixed later, because
// value=* and value=*key would require a lookahead of at
// least two. (the symbol "=*" alone is ambigous).
id = filterString.substring(last + 1, i).trim();
comparator = EQUALS;
oper = i;
continue;
}
}
return (RFC1960Filter) stack.pop();
} catch (EmptyStackException e) {
throw new IllegalStateException("Filter expression not well-formed.");
}
}
use of java.util.EmptyStackException in project hbase by apache.
the class ParseFilter method popArguments.
/**
* Pops an argument from the operator stack and the number of arguments required by the operator
* from the filterStack and evaluates them
* <p>
* @param operatorStack the stack containing the operators
* @param filterStack the stack containing the filters
* @return the evaluated filter
*/
public static Filter popArguments(Stack<ByteBuffer> operatorStack, Stack<Filter> filterStack) {
ByteBuffer argumentOnTopOfStack = operatorStack.peek();
if (argumentOnTopOfStack.equals(ParseConstants.OR_BUFFER)) {
// The top of the stack is an OR
try {
ArrayList<Filter> listOfFilters = new ArrayList<>();
while (!operatorStack.empty() && operatorStack.peek().equals(ParseConstants.OR_BUFFER)) {
Filter filter = filterStack.pop();
listOfFilters.add(0, filter);
operatorStack.pop();
}
Filter filter = filterStack.pop();
listOfFilters.add(0, filter);
Filter orFilter = new FilterList(FilterList.Operator.MUST_PASS_ONE, listOfFilters);
return orFilter;
} catch (EmptyStackException e) {
throw new IllegalArgumentException("Incorrect input string - an OR needs two filters");
}
} else if (argumentOnTopOfStack.equals(ParseConstants.AND_BUFFER)) {
// The top of the stack is an AND
try {
ArrayList<Filter> listOfFilters = new ArrayList<>();
while (!operatorStack.empty() && operatorStack.peek().equals(ParseConstants.AND_BUFFER)) {
Filter filter = filterStack.pop();
listOfFilters.add(0, filter);
operatorStack.pop();
}
Filter filter = filterStack.pop();
listOfFilters.add(0, filter);
Filter andFilter = new FilterList(FilterList.Operator.MUST_PASS_ALL, listOfFilters);
return andFilter;
} catch (EmptyStackException e) {
throw new IllegalArgumentException("Incorrect input string - an AND needs two filters");
}
} else if (argumentOnTopOfStack.equals(ParseConstants.SKIP_BUFFER)) {
// The top of the stack is a SKIP
try {
Filter wrappedFilter = filterStack.pop();
Filter skipFilter = new SkipFilter(wrappedFilter);
operatorStack.pop();
return skipFilter;
} catch (EmptyStackException e) {
throw new IllegalArgumentException("Incorrect input string - a SKIP wraps a filter");
}
} else if (argumentOnTopOfStack.equals(ParseConstants.WHILE_BUFFER)) {
// The top of the stack is a WHILE
try {
Filter wrappedFilter = filterStack.pop();
Filter whileMatchFilter = new WhileMatchFilter(wrappedFilter);
operatorStack.pop();
return whileMatchFilter;
} catch (EmptyStackException e) {
throw new IllegalArgumentException("Incorrect input string - a WHILE wraps a filter");
}
} else if (argumentOnTopOfStack.equals(ParseConstants.LPAREN_BUFFER)) {
// The top of the stack is a LPAREN
try {
Filter filter = filterStack.pop();
operatorStack.pop();
return filter;
} catch (EmptyStackException e) {
throw new IllegalArgumentException("Incorrect Filter String");
}
} else {
throw new IllegalArgumentException("Incorrect arguments on operatorStack");
}
}
use of java.util.EmptyStackException in project CtCI-6th-Edition by careercup.
the class MyStack method pop.
public T pop() {
if (top == null)
throw new EmptyStackException();
T item = top.getData();
top = top.next;
return item;
}
use of java.util.EmptyStackException in project googleads-java-lib by googleads.
the class PrettyPrinterTest method testTransformerExceptions.
/**
* Tests that unexpected exceptions in the transformer/format phase get logged correctly.
*/
@Test
public void testTransformerExceptions() throws TransformerException {
String html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html><a></a></html>";
List<Exception> exceptions = Lists.newArrayList(new NullPointerException(), new TransformerException("transformer exception"), new EmptyStackException());
for (Exception exception : exceptions) {
Transformer transformer = Mockito.mock(Transformer.class);
Mockito.doThrow(exception).when(transformer).transform(Mockito.<Source>any(), Mockito.<Result>any());
PrettyPrinter prettyPrinter = new PrettyPrinter(adsApiConfiguration, logger, xpathSupplier, Suppliers.ofInstance(transformer), documentBuilderSupplier);
assertEquals(html, prettyPrinter.prettyPrint(html));
verify(logger).warn("Unable to pretty print XML: {}", exception);
}
}
Aggregations