@Override public String getHeaderField(final String field) { final List<String> values = headers.get(field); final StringBuilder sb = new StringBuilder(); for (final String v : values) sb.append(v).append(';'); return sb.substring(0, sb.length() - 1); }
/** * Adds human readable shortcuts to the specified string. * * @param str text of tooltip * @param sc shortcut * @return tooltip */ public static String addShortcut(final String str, final String sc) { if (sc == null || str == null) return str; final StringBuilder sb = new StringBuilder(); for (final String s : sc.split(" ")) { String t = "%".equals(s) ? Prop.MAC ? "meta" : "control" : s; if (t.length() != 1) t = Toolkit.getProperty("AWT." + t.toLowerCase(Locale.ENGLISH), t); sb.append('+').append(t); } return str + " (" + sb.substring(1) + ')'; }
/** * Sets a mnemomic for the specified button. * * @param b button * @param mnem mnemonics that have already been assigned */ public static void setMnemonic(final AbstractButton b, final StringBuilder mnem) { // do not set mnemonics for Mac! Alt+key used for special characters. if (Prop.MAC) return; // find and assign unused mnemomic final String label = b.getText(); final int ll = label.length(); for (int l = 0; l < ll; l++) { final char ch = Character.toLowerCase(label.charAt(l)); if (!letter(ch) || mnem.indexOf(Character.toString(ch)) != -1) continue; b.setMnemonic(ch); mnem.append(ch); break; } }
/** * Parses and returns a number. * * @param cmd referring command; if specified, the result must not be empty * @return name * @throws QueryException query exception */ private String number(final Cmd cmd) throws QueryException { consumeWS(); final StringBuilder sb = new StringBuilder(); if (parser.curr() == '-') sb.append(parser.consume()); while (digit(parser.curr())) sb.append(parser.consume()); return finish(eoc() || ws(parser.curr()) ? sb : null, cmd); }
@Override public String toString() { final StringBuilder sb = new StringBuilder("["); sb.append(socket.getInetAddress().getHostAddress()); sb.append(COL).append(socket.getPort()).append(']'); if (context.data() != null) sb.append(COLS).append(context.data().meta.name); return 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(); }
@Override protected boolean run() { final String query = find(args[0], context, root); final boolean ok = query(query); final StringBuilder sb = new StringBuilder(); if (prop.is(Prop.QUERYINFO)) { sb.append(NL).append(QUERY_CC).append(NL).append(query).append(NL); } sb.append(info()); error(sb.toString()); return ok; }
/** * Constructor. * * @param rq request * @param rs response * @throws IOException I/O exception */ public HTTPContext(final HttpServletRequest rq, final HttpServletResponse rs) throws IOException { req = rq; res = rs; final String m = rq.getMethod(); method = HTTPMethod.get(m); final StringBuilder uri = new StringBuilder(req.getRequestURL()); final String qs = req.getQueryString(); if (qs != null) uri.append('?').append(qs); log(false, m, uri); // set UTF8 as default encoding (can be overwritten) res.setCharacterEncoding(UTF8); segments = toSegments(req.getPathInfo()); path = join(0); user = System.getProperty(DBUSER); pass = System.getProperty(DBPASS); // set session-specific credentials final String auth = req.getHeader(AUTHORIZATION); if (auth != null) { final String[] values = auth.split(" "); if (values[0].equals(BASIC)) { final String[] cred = Base64.decode(values[1]).split(":", 2); if (cred.length != 2) throw new LoginException(NOPASSWD); user = cred[0]; pass = cred[1]; } else { throw new LoginException(WHICHAUTH, values[0]); } } }
@Override public void keyTyped(final KeyEvent e) { if (undo == null || control(e) || DELNEXT.is(e) || DELPREV.is(e) || ESCAPE.is(e)) return; text.pos(text.cursor()); // string to be added String ch = String.valueOf(e.getKeyChar()); // remember if marked text is to be deleted boolean del = true; final byte[] txt = text.text(); if (TAB.is(e)) { if (text.marked()) { // check if lines are to be indented final int s = Math.min(text.pos(), text.start()); final int l = Math.max(text.pos(), text.start()) - 1; for (int p = s; p <= l && p < txt.length; p++) del &= txt[p] != '\n'; if (!del) { text.indent(s, l, e.isShiftDown()); ch = null; } } else { boolean c = true; for (int p = text.pos() - 1; p >= 0 && c; p--) { final byte b = txt[p]; c = ws(b); if (b == '\n') break; } if (c) ch = " "; } } // delete marked text if (text.marked() && del) text.delete(); if (ENTER.is(e)) { // adopt indentation from previous line final StringBuilder sb = new StringBuilder(1).append(e.getKeyChar()); int s = 0; for (int p = text.pos() - 1; p >= 0; p--) { final byte b = txt[p]; if (b == '\n') break; if (b == '\t') { s += 2; } else if (b == ' ') { s++; } else { s = 0; } } for (int p = 0; p < s; p++) sb.append(' '); ch = sb.toString(); } if (ch != null) text.add(ch); text.setCaret(); rend.calc(); showCursor(2); e.consume(); }
/** * Parses and returns a command. A command is limited to letters. * * @param cmd referring command; if specified, the result must not be empty * @return name * @throws QueryException query exception */ private String command(final Cmd cmd) throws QueryException { consumeWS(); final StringBuilder sb = new StringBuilder(); while (!eoc() && !ws(parser.curr())) { sb.append(parser.consume()); } return finish(sb, cmd); }
/** * Parses and returns a glob expression, which extends {@link #name(Cmd)} function with asterisks, * question marks and commands. * * @param cmd referring command; if specified, the result must not be empty * @return glob expression * @throws QueryException query exception */ private String glob(final Cmd cmd) throws QueryException { consumeWS(); final StringBuilder sb = new StringBuilder(); while (true) { final char ch = parser.curr(); if (!Databases.validChar(ch) && ch != '*' && ch != '?' && ch != ',') { return finish(eoc() || ws(ch) ? sb : null, cmd); } sb.append(parser.consume()); } }
/** * Splits the string and returns an array. * * @param str string to be split * @return array */ private static String[] split(final String str) { final int l = str.length(); final String[] split = new String[l]; int s = 0; char delim = 0; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < l; ++i) { final char c = str.charAt(i); if (delim == 0) { if (c == '\'' || c == '"') { delim = c; } else if (!XMLToken.isChar(c) && c != '@' && c != '=' && c != '<' && c != '>' && c != '~') { if (sb.length() != 0) { split[s++] = sb.toString(); sb.setLength(0); } } else { sb.append(c); } } else { if (c == delim) { delim = 0; if (sb.length() != 0) { split[s++] = sb.toString(); sb.setLength(0); } } else { if (c != '\'' && c != '"') sb.append(c); } } } if (sb.length() != 0) split[s++] = sb.toString(); return Array.copyOf(split, s); }
/** * Parses and returns a string, delimited by a semicolon or, optionally, a space. Quotes can be * used to include spaces. * * @param cmd referring command; if specified, the result must not be empty * @param space stop when encountering space * @return string * @throws QueryException query exception */ private String string(final Cmd cmd, final boolean space) throws QueryException { final StringBuilder sb = new StringBuilder(); consumeWS(); boolean q = false; while (parser.more()) { final char c = parser.curr(); if (!q && ((space ? c <= ' ' : c < ' ') || eoc())) break; if (c == '"') q ^= true; else sb.append(c); parser.consume(); } return finish(sb, cmd); }
/** * Returns a number character sequence. * * @param tb token builder * @param n number to be formatted * @param mp marker parser * @param form language-dependent formatter * @param start start character */ private static void number( final TokenBuilder tb, final long n, final FormatParser mp, final Formatter form, final int start) { // count optional-digit-signs final String pres = mp.pres; int o = 0; for (int i = 0; i < pres.length(); ++i) { if (pres.charAt(i) == '#') ++o; } // count digits int d = 0; for (int i = 0; i < pres.length(); ++i) { final char ch = pres.charAt(i); if (ch >= start && ch <= start + 9) ++d; } // create string representation and build string final String s = Long.toString(n); final StringBuilder tmp = new StringBuilder(); final int r = o + d - s.length(); for (int i = r; i > o; i--) tmp.append((char) start); for (int i = 0; i < s.length(); i++) { tmp.append((char) (s.charAt(i) - '0' + start)); } for (int p = pres.length() - 1, t = tmp.length() - 1; p >= 0 && t >= 0; p--, t--) { final char ch = pres.charAt(p); if (ch < start && ch > start + 9 && ch != '#') tmp.insert(t, ch); } // add ordinal suffix tb.add(tmp.toString()).add(form.ordinal(n, mp.ord)); }
/** * Parses and returns the remaining string. Quotes at the beginning and end of the argument will * be stripped. * * @param cmd referring command; if specified, the result must not be empty * @return remaining string * @throws QueryException query exception */ private String remaining(final Cmd cmd) throws QueryException { if (single) { final StringBuilder sb = new StringBuilder(); consumeWS(); while (parser.more()) sb.append(parser.consume()); String arg = finish(sb, cmd); if (arg != null) { // chop quotes; substrings are faster than replaces... if (arg.startsWith("\"")) arg = arg.substring(1); if (arg.endsWith("\"")) arg = arg.substring(0, arg.length() - 1); } return arg; } return string(cmd, false); }
/** * Parses and returns an xquery expression. * * @param cmd referring command; if specified, the result must not be empty * @return path * @throws QueryException query exception */ private String xquery(final Cmd cmd) throws QueryException { consumeWS(); final StringBuilder sb = new StringBuilder(); if (!eoc()) { final QueryContext qc = new QueryContext(ctx); try { final QueryParser p = new QueryParser(parser.input, null, qc, null); p.pos = parser.pos; p.parseMain(); sb.append(parser.input.substring(parser.pos, p.pos)); parser.pos = p.pos; } finally { qc.close(); } } return finish(sb, cmd); }
/** * Constructor. * * @param rq request * @param rs response * @param servlet calling servlet instance * @throws IOException I/O exception */ public HTTPContext( final HttpServletRequest rq, final HttpServletResponse rs, final BaseXServlet servlet) throws IOException { req = rq; res = rs; params = new HTTPParams(this); method = rq.getMethod(); final StringBuilder uri = new StringBuilder(req.getRequestURL()); final String qs = req.getQueryString(); if (qs != null) uri.append('?').append(qs); log('[' + method + "] " + uri, null); // set UTF8 as default encoding (can be overwritten) res.setCharacterEncoding(UTF8); segments = decode(toSegments(req.getPathInfo())); // adopt servlet-specific credentials or use global ones final GlobalOptions mprop = context().globalopts; user = servlet.user != null ? servlet.user : mprop.get(GlobalOptions.USER); pass = servlet.pass != null ? servlet.pass : mprop.get(GlobalOptions.PASSWORD); // overwrite credentials with session-specific data final String auth = req.getHeader(AUTHORIZATION); if (auth != null) { final String[] values = auth.split(" "); if (values[0].equals(BASIC)) { final String[] cred = org.basex.util.Base64.decode(values[1]).split(":", 2); if (cred.length != 2) throw new LoginException(NOPASSWD); user = cred[0]; pass = cred[1]; } else { throw new LoginException(WHICHAUTH, values[0]); } } }
/** * Returns whether the following token exists (using wildcards). * * @return result of check */ private boolean moreWC() { final StringBuilder word = new StringBuilder(); final int size = tokenList.size(); boolean period = false, bs = false, more = false; for (; cpos < size; cpos++) { String cSrfc = tokenList.get(cpos).getSurface(); final boolean cMark = tokenList.get(cpos).isMark(); String nSrfc = null; boolean nMark = false; if (cpos < size - 1) { nSrfc = tokenList.get(cpos + 1).getSurface(); nMark = tokenList.get(cpos + 1).isMark(); } if (nSrfc != null) { if ("\\".equals(cSrfc)) bs = true; // delimiter if (cMark && !isFtChar(cSrfc) || "\\".equals(cSrfc) && nMark) { period = false; bs = false; if (word.length() != 0) { more = true; break; } if ("\\".equals(cSrfc) && nMark) cpos++; continue; } word.append(cSrfc); if (bs || "\\".equals(nSrfc)) { more = true; continue; } if (".".equals(cSrfc) || ".".equals(nSrfc)) { period = true; continue; } if (period) { if ("{".equals(cSrfc)) { cpos++; for (; cpos < size; cpos++) { cSrfc = tokenList.get(cpos).getSurface(); word.append(cSrfc); if ("}".equals(cSrfc)) { more = true; break; } } cpos++; break; } continue; } } else { // last token. if (cMark) { if ("\\".equals(cSrfc)) continue; if (word.length() != 0) { word.append(cSrfc); } more = true; continue; } } if (period) { word.append(cSrfc); } else { if (bs) if (!isFtChar(cSrfc)) word.append(cSrfc); else word.setLength(0); } more = true; cpos++; break; } if (more) { currToken = word.length() == 0 ? tokenList.get(cpos - 1) : new Morpheme(word.toString(), MEISHI_FEATURE); } return more; }
/** Writes the properties to disk. */ public final synchronized void write() { final StringBuilder user = new StringBuilder(); BufferedReader br = null; try { // caches options specified by the user if (file.exists()) { br = new BufferedReader(new FileReader(file.file())); for (String line; (line = br.readLine()) != null; ) { if (line.equals(PROPUSER)) break; } for (String line; (line = br.readLine()) != null; ) { user.append(line).append(NL); } } } catch (final Exception ex) { Util.debug(ex); } finally { if (br != null) try { br.close(); } catch (final IOException e) { } } BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(file.file())); bw.write(PROPHEADER + NL); for (final Field f : getClass().getFields()) { final Object obj = f.get(null); if (!(obj instanceof Object[])) continue; final String key = ((Object[]) obj)[0].toString(); final Object val = props.get(key); if (val instanceof String[]) { final String[] str = (String[]) val; bw.write(key + " = " + str.length + NL); final int is = str.length; for (int i = 0; i < is; ++i) { if (str[i] != null) bw.write(key + (i + 1) + " = " + str[i] + NL); } } else if (val instanceof int[]) { final int[] num = (int[]) val; final int ns = num.length; for (int i = 0; i < ns; ++i) { bw.write(key + i + " = " + num[i] + NL); } } else { bw.write(key + " = " + val + NL); } } bw.write(NL + PROPUSER + NL); bw.write(user.toString()); } catch (final Exception ex) { Util.errln("% could not be written.", file); Util.debug(ex); } finally { if (bw != null) try { bw.close(); } catch (final IOException e) { } } }
/** * Parses the specified test case. * * @param root root node * @throws Exception exception * @return true if the query, specified by {@link #single}, was evaluated */ private boolean parse(final Nodes root) throws Exception { final String pth = text("@FilePath", root); final String outname = text("@name", root); if (single != null && !outname.startsWith(single)) return true; final Performance perf = new Performance(); if (verbose) Util.out("- " + outname); boolean inspect = false; boolean correct = true; final Nodes nodes = states(root); for (int n = 0; n < nodes.size(); ++n) { final Nodes state = new Nodes(nodes.list[n], nodes.data); final String inname = text("*:query/@name", state); context.query = new IOFile(queries + pth + inname + IO.XQSUFFIX); final String in = read(context.query); String er = null; ItemCache iter = null; boolean doc = true; final Nodes cont = nodes("*:contextItem", state); Nodes curr = null; if (cont.size() != 0) { final Data d = Check.check(context, srcs.get(string(data.atom(cont.list[0])))); curr = new Nodes(d.doc(), d); curr.root = true; } context.prop.set(Prop.QUERYINFO, compile); final QueryProcessor xq = new QueryProcessor(in, curr, context); context.prop.set(Prop.QUERYINFO, false); // limit result sizes to 1MB final ArrayOutput ao = new ArrayOutput(); final TokenBuilder files = new TokenBuilder(); try { files.add( file(nodes("*:input-file", state), nodes("*:input-file/@variable", state), xq, n == 0)); files.add(file(nodes("*:defaultCollection", state), null, xq, n == 0)); var(nodes("*:input-URI", state), nodes("*:input-URI/@variable", state), xq); eval(nodes("*:input-query/@name", state), nodes("*:input-query/@variable", state), pth, xq); parse(xq, state); for (final int p : nodes("*:module", root).list) { final String uri = text("@namespace", new Nodes(p, data)); final String file = mods.get(string(data.atom(p))) + IO.XQSUFFIX; xq.module(file, uri); } // evaluate and serialize query final SerializerProp sp = new SerializerProp(); sp.set(SerializerProp.S_INDENT, context.prop.is(Prop.CHOP) ? DataText.YES : DataText.NO); final XMLSerializer xml = new XMLSerializer(ao, sp); iter = xq.value().cache(); for (Item it; (it = iter.next()) != null; ) { doc &= it.type == NodeType.DOC; it.serialize(xml); } xml.close(); } catch (final Exception ex) { if (!(ex instanceof QueryException || ex instanceof IOException)) { System.err.println("\n*** " + outname + " ***"); System.err.println(in + "\n"); ex.printStackTrace(); } er = ex.getMessage(); if (er.startsWith(STOPPED)) er = er.substring(er.indexOf('\n') + 1); if (er.startsWith("[")) er = er.replaceAll("\\[(.*?)\\] (.*)", "$1 $2"); // unexpected error - dump stack trace } // print compilation steps if (compile) { Util.errln("---------------------------------------------------------"); Util.err(xq.info()); Util.errln(in); } final Nodes expOut = nodes("*:output-file/text()", state); final TokenList result = new TokenList(); for (int o = 0; o < expOut.size(); ++o) { final String resFile = string(data.atom(expOut.list[o])); final IOFile exp = new IOFile(expected + pth + resFile); result.add(read(exp)); } final Nodes cmpFiles = nodes("*:output-file/@compare", state); boolean xml = false; boolean frag = false; boolean ignore = false; for (int o = 0; o < cmpFiles.size(); ++o) { final byte[] type = data.atom(cmpFiles.list[o]); xml |= eq(type, XML); frag |= eq(type, FRAGMENT); ignore |= eq(type, IGNORE); } String expError = text("*:expected-error/text()", state); final StringBuilder log = new StringBuilder(pth + inname + IO.XQSUFFIX); if (files.size() != 0) { log.append(" ["); log.append(files); log.append("]"); } log.append(NL); /** Remove comments. */ log.append(norm(in)); log.append(NL); final String logStr = log.toString(); // skip queries with variable results final boolean print = currTime || !logStr.contains("current-"); boolean correctError = false; if (er != null && (expOut.size() == 0 || !expError.isEmpty())) { expError = error(pth + outname, expError); final String code = er.substring(0, Math.min(8, er.length())); for (final String e : SLASH.split(expError)) { if (code.equals(e)) { correctError = true; break; } } } if (correctError) { if (print) { logOK.append(logStr); logOK.append("[Right] "); logOK.append(norm(er)); logOK.append(NL); logOK.append(NL); addLog(pth, outname + ".log", er); } ++ok; } else if (er == null) { int s = -1; final int rs = result.size(); while (!ignore && ++s < rs) { inspect |= s < cmpFiles.list.length && eq(data.atom(cmpFiles.list[s]), INSPECT); final byte[] res = result.get(s), actual = ao.toArray(); if (res.length == ao.size() && eq(res, actual)) break; if (xml || frag) { iter.reset(); try { final ItemCache ic = toIter(string(res).replaceAll("^<\\?xml.*?\\?>", "").trim(), frag); if (FNSimple.deep(null, iter, ic)) break; ic.reset(); final ItemCache ia = toIter(string(actual), frag); if (FNSimple.deep(null, ia, ic)) break; } catch (final Throwable ex) { System.err.println("\n" + outname + ":"); ex.printStackTrace(); } } } if ((rs > 0 || !expError.isEmpty()) && s == rs && !inspect) { if (print) { if (expOut.size() == 0) result.add(error(pth + outname, expError)); logErr.append(logStr); logErr.append("[" + testid + " ] "); logErr.append(norm(string(result.get(0)))); logErr.append(NL); logErr.append("[Wrong] "); logErr.append(norm(ao.toString())); logErr.append(NL); logErr.append(NL); addLog(pth, outname + (xml ? IO.XMLSUFFIX : ".txt"), ao.toString()); } correct = false; ++err; } else { if (print) { logOK.append(logStr); logOK.append("[Right] "); logOK.append(norm(ao.toString())); logOK.append(NL); logOK.append(NL); addLog(pth, outname + (xml ? IO.XMLSUFFIX : ".txt"), ao.toString()); } ++ok; } } else { if (expOut.size() == 0 || !expError.isEmpty()) { if (print) { logOK2.append(logStr); logOK2.append("[" + testid + " ] "); logOK2.append(norm(expError)); logOK2.append(NL); logOK2.append("[Rght?] "); logOK2.append(norm(er)); logOK2.append(NL); logOK2.append(NL); addLog(pth, outname + ".log", er); } ++ok2; } else { if (print) { logErr2.append(logStr); logErr2.append("[" + testid + " ] "); logErr2.append(norm(string(result.get(0)))); logErr2.append(NL); logErr2.append("[Wrong] "); logErr2.append(norm(er)); logErr2.append(NL); logErr2.append(NL); addLog(pth, outname + ".log", er); } correct = false; ++err2; } } if (curr != null) Close.close(curr.data, context); xq.close(); } if (reporting) { logReport.append(" <test-case name=\""); logReport.append(outname); logReport.append("\" result='"); logReport.append(correct ? "pass" : "fail"); if (inspect) logReport.append("' todo='inspect"); logReport.append("'/>"); logReport.append(NL); } // print verbose/timing information final long nano = perf.getTime(); final boolean slow = nano / 1000000 > timer; if (verbose) { if (slow) Util.out(": " + Performance.getTimer(nano, 1)); Util.outln(); } else if (slow) { Util.out(NL + "- " + outname + ": " + Performance.getTimer(nano, 1)); } return single == null || !outname.equals(single); }
/** * Parses and returns a name. A name may contain letters, numbers and any of the special * characters <code>!#$%&'()+-=@[]^_`{}~</code>. * * @param cmd referring command; if specified, the result must not be empty * @return name * @throws QueryException query exception */ private String name(final Cmd cmd) throws QueryException { consumeWS(); final StringBuilder sb = new StringBuilder(); while (Databases.validChar(parser.curr())) sb.append(parser.consume()); return finish(eoc() || ws(parser.curr()) ? sb : null, cmd); }
@Override public String toString() { final StringBuilder sb = new StringBuilder(); for (final UserFunc f : funcs) sb.append(f.toString()); return sb.toString(); }
@Override public String toString() { final StringBuilder sb = new StringBuilder(); for (int i = 0; i < func.length; ++i) sb.append(func[i].toString()); return sb.toString(); }