/** * Joins the path. * * @param s segment to start with * @return joined path */ private String join(final int s) { final TokenBuilder tb = new TokenBuilder(); for (int p = s; p < segments.length; p++) { if (!tb.isEmpty()) tb.add('/'); tb.add(segments[p]); } return tb.toString(); }
@Override public String toString() { final TokenBuilder tb = new TokenBuilder(Util.className(this) + '['); for (int i = 0; i < size; ++i) { if (i > 0) tb.add(", "); tb.add(nm[i]).add("=\"").add(vl[i]).add("\""); } return tb.add("]").toString(); }
/** * Returns the database path (i.e., all path entries except for the first). * * @return path depth */ public String dbpath() { final TokenBuilder tb = new TokenBuilder(); final int ps = segments.length; for (int p = 1; p < ps; p++) { if (!tb.isEmpty()) tb.add('/'); tb.add(segments[p]); } return tb.toString(); }
@Override public final synchronized String toString() { final TokenBuilder tb = new TokenBuilder(); for (final Entry<String, Object> e : props.entrySet()) { if (!tb.isEmpty()) tb.add(','); tb.add(e.getKey()).add('=').addExt(e.getValue()); } return tb.toString(); }
/** * Creates an XQuery representation for the specified table query. * * @param filter filter terms * @param cols filter columns * @param elem element flag * @param name name of root element * @param root root flag * @return query */ public static String findTable( final StringList filter, final TokenList cols, final BoolList elem, final byte[] name, final boolean root) { final TokenBuilder tb = new TokenBuilder(); final int is = filter.size(); for (int i = 0; i < is; ++i) { final String[] spl = split(filter.get(i)); for (final String s : spl) { final byte[] term = trim(replace(token(s), '"', ' ')); if (term.length == 0) continue; tb.add('['); final boolean elm = elem.get(i); tb.add(elm ? ".//" : "@"); tb.add("*:"); tb.add(cols.get(i)); if (term[0] == '<' || term[0] == '>') { tb.add(term[0]); tb.addLong(calcNum(substring(term, 1))); } else { tb.add(" contains text \""); tb.add(term); tb.add('"'); } tb.add(']'); } } return tb.isEmpty() ? "/" : (root ? "/" : "") + Axis.DESCORSELF + "::*:" + string(name) + tb; }
@Override public synchronized byte[] info(final MainOptions options) { final TokenBuilder tb = new TokenBuilder(); final long l = inX.length() + inY.length() + inZ.length(); tb.add(LI_NAMES).add(data.meta.ftinclude).add(NL); tb.add(LI_SIZE + Performance.format(l, true) + NL); final IndexStats stats = new IndexStats(options.get(MainOptions.MAXSTAT)); addOccs(stats); stats.print(tb); return tb.finish(); }
@Override public String getMessage() { final TokenBuilder tb = new TokenBuilder(); if (info != null) tb.add(STOPPED_AT).add(info.toString()).add(COL).add(NL); final byte[] code = name.local(); if (code.length != 0) tb.add('[').add(name.prefixId(QueryText.ERROR_URI)).add("] "); tb.add(getLocalizedMessage()); if (!stack.isEmpty()) { tb.add(NL).add(NL).add(STACK_TRACE).add(COL); for (final InputInfo ii : stack) tb.add(NL).add(LI).add(ii.toString()); } return tb.toString(); }
@Override public byte[] info(final MainOptions options) { final TokenBuilder tb = new TokenBuilder(); tb.add(LI_STRUCTURE).add(HASH).add(NL); tb.add(LI_NAMES).add(data.meta.names(type)).add(NL); final IndexStats stats = new IndexStats(options.get(MainOptions.MAXSTAT)); final int s = values.size(); for (int p = 1; p <= s; p++) { final int oc = lenList.get(p); if (oc > 0 && stats.adding(oc)) stats.add(values.key(p), oc); } stats.print(tb); return tb.finish(); }
/** * Returns an atomized content for any node kind. The atomized value can be an attribute value or * XML content. * * @param pre pre value * @return atomized value */ public final byte[] atom(final int pre) { switch (kind(pre)) { case TEXT: case COMM: return text(pre, true); case ATTR: return text(pre, false); case PI: byte[] txt = text(pre, true); final int i = indexOf(txt, ' '); return i == -1 ? EMPTY : substring(txt, i + 1); default: // create atomized text node TokenBuilder tb = null; byte[] t = EMPTY; int p = pre; final int s = p + size(p, kind(p)); while (p != s) { final int k = kind(p); if (k == TEXT) { txt = text(p, true); if (t == EMPTY) { t = txt; } else { if (tb == null) tb = new TokenBuilder(t); tb.add(txt); } } p += attSize(p, k); } return tb == null ? t : tb.finish(); } }
/** 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()); }
/** * Returns an information string for an unexpected exception. * * @param ex exception * @return dummy object */ public static String bug(final Throwable ex) { final TokenBuilder tb = new TokenBuilder(BUGINFO); tb.add(NL).add("Contact: ").add(MAIL); tb.add(NL).add("Version: ").add(TITLE); tb.add(NL).add("Java: ").add(System.getProperty("java.vendor")); tb.add(", ").add(System.getProperty("java.version")); tb.add(NL).add("OS: ").add(System.getProperty("os.name")); tb.add(", ").add(System.getProperty("os.arch")); tb.add(NL).add("Stack Trace: "); for (final String e : toArray(ex)) tb.add(NL).add(e); return tb.toString(); }
/** * Returns a regular expression pattern. * * @param pattern input pattern * @param modifier modifier item * @param ctx query context * @return pattern modifier * @throws QueryException query exception */ private Pattern pattern(final Expr pattern, final Expr modifier, final QueryContext ctx) throws QueryException { final byte[] pat = checkStr(pattern, ctx); final byte[] mod = modifier != null ? checkStr(modifier, ctx) : null; final TokenBuilder tb = new TokenBuilder(pat); if (mod != null) tb.add(0).add(mod); final byte[] key = tb.finish(); Pattern p = patterns.get(key); if (p == null) { p = RegExParser.parse(pat, mod, ctx.sc.xquery3(), info); patterns.add(key, p); } return p; }
/** * Converts the path to a string array, containing the single segments. * * @param path path, or {@code null} * @return path depth */ public static String[] toSegments(final String path) { final StringList sl = new StringList(); if (path != null) { final TokenBuilder tb = new TokenBuilder(); for (int s = 0; s < path.length(); s++) { final char ch = path.charAt(s); if (ch == '/') { if (tb.isEmpty()) continue; sl.add(tb.toString()); tb.reset(); } else { tb.add(ch); } } if (!tb.isEmpty()) sl.add(tb.toString()); } return sl.toArray(); }
/** * Creates an XQuery representation for the specified query. * * @param query query * @param ctx database context * @param root start from root node * @return query */ public static String find(final String query, final Context ctx, final boolean root) { // treat input as XQuery if (query.startsWith("/")) return query; final boolean r = root || ctx.root(); if (query.isEmpty()) return r ? "/" : "."; // parse user input final String qu = query.replaceAll(" \\+", " "); final String[] terms = split(qu); String pre = ""; String preds = ""; final String tag = "*"; for (String term : terms) { if (term.startsWith("@=")) { preds += "[@* = \"" + term.substring(2) + "\"]"; } else if (term.startsWith("=")) { preds += "[text() = \"" + term.substring(1) + "\"]"; } else if (term.startsWith("~")) { preds += "[text() contains text \"" + term.substring(1) + "\" using fuzzy]"; } else if (term.startsWith("@")) { if (term.length() == 1) continue; preds += "[@* contains text \"" + term.substring(1) + "\"]"; term = term.substring(1); // add valid name tests if (XMLToken.isName(token(term))) { pre += (r ? "" : ".") + "//@" + term + " | "; } } else { preds += "[text() contains text \"" + term + "\"]"; // add valid name tests if (XMLToken.isName(token(term))) { pre += (r ? "/" : "") + Axis.DESC + "::*:" + term + " | "; } } } if (pre.isEmpty() && preds.isEmpty()) return root ? "/" : "."; // create final string final TokenBuilder tb = new TokenBuilder(); tb.add(pre + (r ? "/" : "") + Axis.DESCORSELF + "::" + tag + preds); return tb.toString(); }
/** * Returns a string representation of the index structure. * * @param all include database contents in the representation. During updates, database lookups * must be avoided, as the data structures will be inconsistent. * @return string */ public String toString(final boolean all) { final TokenBuilder tb = new TokenBuilder(); tb.addExt(type).add(" INDEX, '").add(data.meta.name).add("':\n"); final int s = lenList.size(); for (int m = 1; m < s; m++) { final int len = lenList.get(m); if (len == 0) continue; final int[] ids = idsList.get(m); tb.add(" ").addInt(m); if (all) tb.add(", key: \"").add(data.text(data.pre(ids[0]), type == IndexType.TEXT)).add('"'); tb.add(", ids"); if (all) tb.add("/pres"); tb.add(": "); for (int n = 0; n < len; n++) { if (n != 0) tb.add(","); tb.addInt(ids[n]); if (all) tb.add('/').addInt(data.pre(ids[n])); } tb.add("\n"); } return tb.toString(); }
/** * Caches and returns all unique tokens specified in a query. * * @param list token list * @return token set */ private TokenSet unique(final TokenList list) { // cache all query tokens in a set (duplicates are removed) final TokenSet ts = new TokenSet(); switch (mode) { case ALL: case ANY: for (final byte[] t : list) ts.add(t); break; case ALL_WORDS: case ANY_WORD: final FTLexer l = new FTLexer(ftt.opt); for (final byte[] t : list) { l.init(t); while (l.hasNext()) ts.add(l.nextToken()); } break; case PHRASE: final TokenBuilder tb = new TokenBuilder(); for (final byte[] t : list) tb.add(t).add(' '); ts.add(tb.trim().finish()); } return ts; }
/** * Throws a runtime exception for an unimplemented method. * * @param ext optional extension * @return runtime exception (indicates that an error is raised) */ public static RuntimeException notimplemented(final Object... ext) { final TokenBuilder tb = new TokenBuilder("Not Implemented"); if (ext.length != 0) tb.addExt(" (%)", ext); throw new UnsupportedOperationException(tb.add('.').toString()); }
/** * Constructor. * * @param info input info * @param map decimal format * @throws QueryException query exception */ public DecFormatter(final InputInfo info, final TokenMap map) throws QueryException { // assign map values int z = '0'; if (map != null) { for (final byte[] key : map) { final String k = string(key); final byte[] v = map.get(key); if (k.equals(DF_INF)) { inf = v; } else if (k.equals(DF_NAN)) { nan = v; } else if (v.length != 0 && cl(v, 0) == v.length) { final int cp = cp(v, 0); switch (k) { case DF_DEC: decimal = cp; break; case DF_GRP: grouping = cp; break; case DF_EXP: exponent = cp; break; case DF_PAT: pattern = cp; break; case DF_MIN: minus = cp; break; case DF_DIG: optional = cp; break; case DF_PC: percent = cp; break; case DF_PM: permille = cp; break; case DF_ZD: z = zeroes(cp); if (z == -1) throw INVDECFORM_X_X.get(info, k, v); if (z != cp) throw INVDECZERO_X.get(info, (char) cp); break; } } else { // signs must have single character throw INVDECSINGLE_X_X.get(info, k, v); } } } // check for duplicate characters zero = z; final IntSet is = new IntSet(); for (int i = 0; i < 10; i++) is.add(zero + i); final int[] ss = {decimal, grouping, exponent, percent, permille, optional, pattern}; for (final int s : ss) if (!is.add(s)) throw DUPLDECFORM_X.get(info, (char) s); // create auxiliary strings final TokenBuilder tb = new TokenBuilder(); for (int i = 0; i < 10; i++) tb.add(zero + i); digits = tb.toArray(); // "decimal-separator-sign, exponent-separator-sign, grouping-sign, decimal-digit-family, // optional-digit-sign and pattern-separator-sign are classified as active characters" // -> decimal-digit-family: added above. pattern-separator-sign: will never occur at this stage actives = tb.add(decimal).add(exponent).add(grouping).add(optional).finish(); // "all other characters (including the percent-sign and per-mille-sign) are classified // as passive characters." }