/** * Runs the command without permission, data and concurrency checks. * * @param ctx database context * @param os output stream * @return result of check */ public boolean run(final Context ctx, final OutputStream os) { perf = new Performance(); context = ctx; prop = ctx.prop; mprop = ctx.mprop; out = PrintOutput.get(os); try { return run(); } catch (final ProgressException ex) { // process was interrupted by the user or server abort(); return error(INTERRUPTED); } catch (final Throwable ex) { // unexpected error Performance.gc(2); abort(); if (ex instanceof OutOfMemoryError) { Util.debug(ex); return error(OUT_OF_MEM + (createWrite() ? H_OUT_OF_MEM : "")); } return error(Util.bug(ex) + NL + info.toString()); } finally { // flushes the output try { if (out != null) out.flush(); } catch (final IOException ignored) { } } }
/** * 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(); }
@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(); }
/** * Compares results. * * @param expected expected result * @param returned returned result * @throws Exception exception */ private static void compare(final ItemList expected, final ItemList returned) throws Exception { // Compare response with expected result assertEquals("Different number of results", expected.size(), returned.size()); final long es = expected.size(); for (int e = 0; e < es; e++) { final Item exp = expected.get(e), ret = returned.get(e); if (!new DeepEqual().equal(exp, ret)) { final TokenBuilder tb = new TokenBuilder("Result ").addLong(e).add(" differs:\nReturned: "); tb.addExt(ret.serialize()).add("\nExpected: ").addExt(exp.serialize()); fail(tb.toString()); } } }
/** * 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(); }
/** * Throws a runtime exception for an unexpected exception. * * @param ext optional extension * @return runtime exception (indicates that an error is raised) */ public static RuntimeException notexpected(final Object... ext) { final TokenBuilder tb = new TokenBuilder(); tb.addExt("%", ext.length == 0 ? "Not Expected." : ext[0]); throw new RuntimeException(tb.toString()); }
/** * Prints the node structure for debugging purposes. * * @param ns namespace reference * @param start start pre value * @param end end pre value * @return string */ String print(final Namespaces ns, final int start, final int end) { final TokenBuilder tb = new TokenBuilder(); print(tb, 0, ns, start, end); return tb.toString(); }
/** * Returns command information. * * @return info string */ public final String info() { return info.toString(); }