/** * 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) { } } }
@Override public String toString() { final TokenBuilder tb = new TokenBuilder().add(var.toString()).add(' ').add(ASSIGN); tb.add(' ').add(expr.toString()); if (coll != null) tb.add(' ').add(COLLATION).add(" \"").add(coll.uri()).add('"'); return tb.toString(); }
/** * 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(); }
/** * 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(); }
/** * 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(); }
/** * 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()); } } }
/** * 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(); }
/** * Reads the configuration file and initializes the project properties. The file is located in the * project home directory. * * @param prop property file extension */ protected synchronized void read(final String prop) { file = new IOFile(HOME + IO.BASEXSUFFIX + prop); final StringList read = new StringList(); final TokenBuilder err = new TokenBuilder(); if (!file.exists()) { err.addExt("Saving properties in \"%\"..." + NL, file); } else { BufferedReader br = null; try { br = new BufferedReader(new FileReader(file.file())); for (String line; (line = br.readLine()) != null; ) { line = line.trim(); if (line.isEmpty() || line.charAt(0) == '#') continue; final int d = line.indexOf('='); if (d < 0) { err.addExt("%: \"%\" ignored. " + NL, file, line); continue; } final String val = line.substring(d + 1).trim(); String key = line.substring(0, d).trim(); // extract numeric value in key int num = 0; final int ss = key.length(); for (int s = 0; s < ss; ++s) { if (Character.isDigit(key.charAt(s))) { num = Integer.parseInt(key.substring(s)); key = key.substring(0, s); break; } } read.add(key); final Object entry = props.get(key); if (entry == null) { err.addExt("%: \"%\" not found. " + NL, file, key); } else if (entry instanceof String) { props.put(key, val); } else if (entry instanceof Integer) { props.put(key, Integer.parseInt(val)); } else if (entry instanceof Boolean) { props.put(key, Boolean.parseBoolean(val)); } else if (entry instanceof String[]) { if (num == 0) { props.put(key, new String[Integer.parseInt(val)]); } else { ((String[]) entry)[num - 1] = val; } } else if (entry instanceof int[]) { ((int[]) entry)[num] = Integer.parseInt(val); } } } catch (final Exception ex) { err.addExt("% could not be parsed." + NL, file); Util.debug(ex); } finally { if (br != null) try { br.close(); } catch (final IOException ex) { } } } // check if all mandatory files have been read try { if (err.isEmpty()) { boolean ok = true; for (final Field f : getClass().getFields()) { final Object obj = f.get(null); if (!(obj instanceof Object[])) continue; final String key = ((Object[]) obj)[0].toString(); ok &= read.contains(key); } if (!ok) err.addExt("Saving properties in \"%\"..." + NL, file); } } catch (final IllegalAccessException ex) { Util.notexpected(ex); } if (!err.isEmpty()) { Util.err(err.toString()); write(); } }
/** * Returns command information. * * @return info string */ public final String info() { return info.toString(); }