Пример #1
1
 @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);
 }
Пример #2
1
 /**
  * Returns a tooltip for the specified options string.
  *
  * @param opts serialization options
  * @return string
  */
 private static String tooltip(final Options opts) {
   final StringBuilder sb = new StringBuilder("<html><b>").append(PARAMETERS).append(":</b><br>");
   for (final Option<?> so : opts) {
     if (!(so instanceof OptionsOption)) sb.append("\u2022 ").append(so).append("<br/>");
   }
   return sb.append("</html>").toString();
 }
Пример #3
1
 /**
  * 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();
 }
Пример #4
1
 /**
  * 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;
 }
Пример #5
0
  /**
   * 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]);
      }
    }
  }
Пример #6
0
 /**
  * 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);
 }
Пример #7
0
 /**
  * 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);
 }
Пример #8
0
 /**
  * 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());
 }
Пример #9
0
 /**
  * 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();
 }
Пример #10
0
  /** 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());
  }
Пример #11
0
 @Override
 public void execute(final GUI gui) {
   final StringBuilder sb = new StringBuilder();
   final Nodes n = gui.context.copied;
   for (int i = 0; i < n.size(); ++i) {
     if (i > 0) sb.append(',');
     sb.append(openPre(n, i));
   }
   gui.context.copied = null;
   gui.execute(new XQuery("insert nodes (" + sb + ") into " + openPre(gui.context.marked, 0)));
 }
Пример #12
0
 /**
  * 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());
   }
 }
Пример #13
0
 @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;
 }
Пример #14
0
 /**
  * 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);
 }
Пример #15
0
  /** Tests the internal parser (Option {@link MainOptions#INTPARSE}). */
  @Test
  public void intParse() {
    set(MainOptions.CHOP, false);

    final StringBuilder sb = new StringBuilder();

    final String[] docs = {
      "<x/>",
      " <x/> ",
      "<x></x>",
      "<x>A</x>",
      "<x><x>",
      "<x/><x/>",
      "<x></x><x/>",
      "<x>",
      "</x>",
      "<x></x></x>",
      "x<x>",
      "<x>x",
      "<x><![CDATA[ ]]></x>",
    };
    for (final String doc : docs) {
      // parse document with default parser (expected to yield correct result)
      set(MainOptions.INTPARSE, false);
      boolean def = true;
      try {
        new CreateDB(NAME, doc).execute(context);
      } catch (final BaseXException ex) {
        def = false;
      }

      // parse document with internal parser
      set(MainOptions.INTPARSE, true);
      boolean cust = true;
      try {
        new CreateDB(NAME, doc).execute(context);
      } catch (final BaseXException ex) {
        cust = false;
      }

      // compare results
      if (def != cust) {
        sb.append('\n').append(def ? "- not accepted: " : "- not rejected: ").append(doc);
      }
    }

    // list all errors
    if (sb.length() != 0) fail(sb.toString());

    set(MainOptions.MAINMEM, false);
  }
Пример #16
0
 @Override
 public void execute(final GUI gui) {
   if (!BaseXDialog.confirm(gui, DELETE_NODES)) return;
   final StringBuilder sb = new StringBuilder();
   final Nodes n = gui.context.marked;
   for (int i = 0; i < n.size(); ++i) {
     if (i > 0) sb.append(',');
     sb.append(openPre(n, i));
   }
   gui.context.marked = new Nodes(n.data);
   gui.context.copied = null;
   gui.context.focused = -1;
   gui.execute(new XQuery("delete nodes (" + sb + ')'));
 }
Пример #17
0
 /**
  * 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);
 }
Пример #18
0
 /**
  * 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);
 }
Пример #19
0
 @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 ? " ! " : "/");
     if (s instanceof Step) sb.append(s);
     else sb.append(s);
   }
   return sb.toString();
 }
Пример #20
0
  /**
   * 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]);
      }
    }
  }
Пример #21
0
 @Override
 public void action(final Object cmp) {
   final StringBuilder bind = new StringBuilder();
   final int cl = context.length;
   for (int c = 0; c < cl; c += 2) {
     final String key = context[c].getText().replaceAll("^\\$", "");
     if (key.isEmpty()) continue;
     if (bind.length() != 0) bind.append(',');
     bind.append(key.replaceAll(",", ",,")).append('=');
     bind.append(context[c + 1].getText().replaceAll(",", ",,"));
   }
   gui.context.options.set(MainOptions.BINDINGS, bind.toString());
 }
Пример #22
0
  /**
   * 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);
  }
Пример #23
0
  /**
   * Constructor.
   *
   * @param args command-line arguments
   * @throws IOException I/O exception
   */
  public BaseX(final String... args) throws IOException {
    super(args);

    // create session to show optional login request
    session();

    console = true;
    try {
      // loop through all commands
      final StringBuilder bind = new StringBuilder();
      SerializerOptions sopts = null;
      boolean v = false, qi = false, qp = false;
      final int os = ops.size();
      for (int o = 0; o < os; o++) {
        final int c = ops.get(o);
        String val = vals.get(o);

        if (c == 'b') {
          // set/add variable binding
          if (bind.length() != 0) bind.append(',');
          // commas are escaped by a second comma
          val = bind.append(val.replaceAll(",", ",,")).toString();
          execute(new Set(MainOptions.BINDINGS, val), false);
        } else if (c == 'c') {
          // evaluate commands
          final IO io = IO.get(val);
          String base = ".";
          if (io.exists() && !io.isDir()) {
            val = io.string();
            base = io.path();
          }
          execute(new Set(MainOptions.QUERYPATH, base), false);
          execute(val);
          execute(new Set(MainOptions.QUERYPATH, ""), false);
          console = false;
        } else if (c == 'D') {
          // hidden option: show/hide dot query graph
          execute(new Set(MainOptions.DOTPLAN, null), false);
        } else if (c == 'i') {
          // open database or create main memory representation
          execute(new Set(MainOptions.MAINMEM, true), false);
          execute(new Check(val), verbose);
          execute(new Set(MainOptions.MAINMEM, false), false);
        } else if (c == 'I') {
          // set/add variable binding
          if (bind.length() != 0) bind.append(',');
          // commas are escaped by a second comma
          val = bind.append("=").append(val.replaceAll(",", ",,")).toString();
          execute(new Set(MainOptions.BINDINGS, val), false);
        } else if (c == 'o') {
          // change output stream
          if (out != System.out) out.close();
          out = new PrintOutput(val);
          session().setOutputStream(out);
        } else if (c == 'q') {
          // evaluate query
          execute(new XQuery(val), verbose);
          console = false;
        } else if (c == 'Q') {
          // evaluate file contents or string as query
          final IO io = IO.get(val);
          String base = ".";
          if (io.exists() && !io.isDir()) {
            val = io.string();
            base = io.path();
          }
          execute(new Set(MainOptions.QUERYPATH, base), false);
          execute(new XQuery(val), verbose);
          execute(new Set(MainOptions.QUERYPATH, ""), false);
          console = false;
        } else if (c == 'r') {
          // parse number of runs
          execute(new Set(MainOptions.RUNS, Strings.toInt(val)), false);
        } else if (c == 'R') {
          // toggle query evaluation
          execute(new Set(MainOptions.RUNQUERY, null), false);
        } else if (c == 's') {
          // set/add serialization parameter
          if (sopts == null) sopts = new SerializerOptions();
          final String[] kv = val.split("=", 2);
          sopts.assign(kv[0], kv.length > 1 ? kv[1] : "");
          execute(new Set(MainOptions.SERIALIZER, sopts), false);
        } else if (c == 't') {
          // evaluate query
          execute(new Test(val), verbose);
          console = false;
        } else if (c == 'u') {
          // (de)activate write-back for updates
          execute(new Set(MainOptions.WRITEBACK, null), false);
        } else if (c == 'v') {
          // show/hide verbose mode
          v ^= true;
        } else if (c == 'V') {
          // show/hide query info
          qi ^= true;
          execute(new Set(MainOptions.QUERYINFO, null), false);
        } else if (c == 'w') {
          // toggle chopping of whitespaces
          execute(new Set(MainOptions.CHOP, null), false);
        } else if (c == 'x') {
          // show/hide xml query plan
          execute(new Set(MainOptions.XMLPLAN, null), false);
          qp ^= true;
        } else if (c == 'X') {
          // show query plan before/after query compilation
          execute(new Set(MainOptions.COMPPLAN, null), false);
        } else if (c == 'z') {
          // toggle result serialization
          execute(new Set(MainOptions.SERIALIZE, null), false);
        }
        verbose = qi || qp || v;
      }
      if (console) console();
    } finally {
      quit();
    }
  }
Пример #24
0
 @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();
 }
Пример #25
0
 /**
  * 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);
 }
Пример #26
0
 /**
  * Parses and returns a string result.
  *
  * @param input input string or {@code null} if invalid
  * @param cmd referring command; if specified, the result must not be empty
  * @return string result or {@code null}
  * @throws QueryException query exception
  */
 private String finish(final StringBuilder input, final Cmd cmd) throws QueryException {
   if (input != null && input.length() != 0) return input.toString();
   if (cmd != null) throw help(null, cmd);
   return null;
 }