/** * Returns a map with variable bindings. * * @param opts main options * @return bindings */ public static HashMap<String, String> bindings(final MainOptions opts) { final HashMap<String, String> bindings = new HashMap<>(); final String bind = opts.get(MainOptions.BINDINGS).trim(); final StringBuilder key = new StringBuilder(); final StringBuilder val = new StringBuilder(); boolean first = true; final int sl = bind.length(); for (int s = 0; s < sl; s++) { final char ch = bind.charAt(s); if (first) { if (ch == '=') { first = false; } else { key.append(ch); } } else { if (ch == ',') { if (s + 1 == sl || bind.charAt(s + 1) != ',') { bindings.put(key.toString().trim(), val.toString()); key.setLength(0); val.setLength(0); first = true; continue; } // literal commas are escaped by a second comma s++; } val.append(ch); } } if (key.length() != 0) bindings.put(key.toString().trim(), val.toString()); return bindings; }
/** * Removes comments from the specified string and returns the first characters of a query. * * @param qu query string * @param max maximum length of string to return * @return result */ public static String removeComments(final String qu, final int max) { final StringBuilder sb = new StringBuilder(); int m = 0; boolean s = false; final int cl = qu.length(); for (int c = 0; c < cl && sb.length() < max; ++c) { final char ch = qu.charAt(c); if (ch == 0x0d) continue; if (ch == '(' && c + 1 < cl && qu.charAt(c + 1) == ':') { if (m == 0 && !s) { sb.append(' '); s = true; } ++m; ++c; } else if (m != 0 && ch == ':' && c + 1 < cl && qu.charAt(c + 1) == ')') { --m; ++c; } else if (m == 0) { if (ch > ' ') sb.append(ch); else if (!s) sb.append(' '); s = ch <= ' '; } } if (sb.length() >= max) sb.append("..."); return sb.toString().trim(); }
@Override public String toString() { final StringBuilder sb = new StringBuilder(); final boolean str = query instanceof AStr; if (!str) sb.append("{ "); sb.append(query); if (!str) sb.append(" }"); switch (mode) { case ALL: sb.append(' ' + ALL); break; case ALL_WORDS: sb.append(' ' + ALL + ' ' + WORDS); break; case ANY_WORD: sb.append(' ' + ANY + ' ' + WORD); break; case PHRASE: sb.append(' ' + PHRASE); break; default: } if (occ != null) sb.append(OCCURS + ' ' + occ[0] + ' ' + TO + ' ' + occ[1] + ' ' + TIMES); if (ftt != null) sb.append(ftt.opt); return sb.toString(); }
/** * Returns an extended error message. * * @param err error message * @return result of check */ private boolean extError(final String err) { // will only be evaluated when an error has occurred final StringBuilder sb = new StringBuilder(); if (options.get(MainOptions.QUERYINFO)) { sb.append(info()).append(qp.info()).append(NL).append(ERROR).append(COL).append(NL); } sb.append(err); return error(sb.toString()); }
/** * Returns a string representation of all found arguments. * * @param args array with arguments * @return string representation */ static String foundArgs(final Value[] args) { // compose found arguments final StringBuilder sb = new StringBuilder(); for (final Value v : args) { if (sb.length() != 0) sb.append(", "); sb.append(v instanceof Jav ? Util.className(((Jav) v).toJava()) : v.seqType()); } return sb.toString(); }
/** Tests the specified instance. */ @Test public void test() { final StringBuilder sb = new StringBuilder(); int fail = 0; for (final Object[] qu : queries) { final boolean correct = qu.length == 3; final String query = qu[correct ? 2 : 1].toString(); final Value cmp = correct ? (Value) qu[1] : null; final QueryProcessor qp = new QueryProcessor(query, context); try { final Value val = qp.value(); if (!correct || !new DeepCompare().equal(val, cmp)) { sb.append("[" + qu[0] + "] " + query); String s = correct && cmp.size() != 1 ? "#" + cmp.size() : ""; sb.append("\n[E" + s + "] "); if (correct) { final String cp = cmp.toString(); sb.append('\''); sb.append(cp.length() > 1000 ? cp.substring(0, 1000) + "..." : cp); sb.append('\''); } else { sb.append("error"); } final TokenBuilder types = new TokenBuilder(); for (final Item it : val) types.add(it.type.toString()).add(" "); s = val.size() == 1 ? "" : "#" + val.size(); sb.append("\n[F" + s + "] '" + val + "', " + types + details() + '\n'); ++fail; } } catch (final Exception ex) { final String msg = ex.getMessage(); if (correct || msg == null || msg.contains("mailman")) { final String cp = correct && cmp.data() != null ? cmp.toString() : "()"; sb.append( "[" + qu[0] + "] " + query + "\n[E] " + cp + "\n[F] " + (msg == null ? Util.className(ex) : msg.replaceAll("\r\n?|\n", " ")) + ' ' + details() + '\n'); ex.printStackTrace(); ++fail; } } finally { qp.close(); } } if (fail != 0) fail(fail + " Errors. [E] = expected, [F] = found:\n" + sb.toString().trim()); }
@Override public final String toString() { final StringBuilder sb = new StringBuilder(); if (root != null) sb.append(root); for (final Expr s : steps) { if (sb.length() != 0) sb.append(s instanceof Bang ? " ! " : "/"); sb.append(s); } return sb.toString(); }
@Override public String toString() { final StringBuilder sb = new StringBuilder(); final int pl = post.length; for (int p = 0; p < pl; p++) { sb.append(LET).append(" (: post-group :) ").append(post[p]); sb.append(' ').append(ASSIGN).append(' ').append(preExpr[p]).append(' '); } sb.append(GROUP).append(' ').append(BY); final int sl = specs.length; for (int s = 0; s < sl; s++) sb.append(s == 0 ? " " : SEP).append(specs[s]); return sb.toString(); }
@Override public String toString() { final StringBuilder sb = new StringBuilder(); for (final Expr e : preds) sb.append('[').append(e).append(']'); return sb.toString(); }
@Override public String toString() { final StringBuilder sb = new StringBuilder("try { " + expr + " }"); for (final Catch c : catches) sb.append(' ').append(c); return sb.toString(); }