예제 #1
0
  /**
   * Searches a string in a file.
   *
   * @param path file path
   * @param search codepoints of search string
   * @return success flag
   */
  private static boolean filterContent(final String path, final int[] search) {
    final int cl = search.length;
    if (cl == 0) return true;

    try (final TextInput ti = new TextInput(new IOFile(path))) {
      final IntList il = new IntList(cl - 1);
      int c = 0;
      while (true) {
        if (!il.isEmpty()) {
          if (il.remove(0) == search[c++]) continue;
          c = 0;
        }
        while (true) {
          final int cp = ti.read();
          if (cp == -1 || !XMLToken.valid(cp)) return false;
          final int lc = Token.lc(cp);
          if (c > 0) il.add(lc);
          if (lc == search[c]) {
            if (++c == cl) return true;
          } else {
            c = 0;
            break;
          }
        }
      }
    } catch (final IOException ex) {
      // file may not be accessible
      Util.debug(ex);
      return false;
    }
  }
예제 #2
0
파일: Find.java 프로젝트: JosuaKrause/basex
  /**
   * 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();
  }
예제 #3
0
 /**
  * Checks the specified template and adds a variable.
  *
  * @param tmp template string
  * @param type allowed type
  * @param declared variable declaration flags
  * @return resulting variable
  * @throws QueryException query exception
  */
 private QNm checkVariable(final String tmp, final Type type, final boolean[] declared)
     throws QueryException {
   final Var[] args = function.args;
   final Matcher m = TEMPLATE.matcher(tmp);
   if (!m.find()) error(INV_TEMPLATE, tmp);
   final byte[] vn = token(m.group(1));
   if (!XMLToken.isQName(vn)) error(INV_VARNAME, vn);
   final QNm qnm = new QNm(vn, context);
   int r = -1;
   while (++r < args.length && !args[r].name.eq(qnm)) ;
   if (r == args.length) error(UNKNOWN_VAR, vn);
   if (declared[r]) error(VAR_ASSIGNED, vn);
   final SeqType st = args[r].declaredType();
   if (args[r].checksType() && !st.type.instanceOf(type)) error(INV_VARTYPE, vn, type);
   declared[r] = true;
   return qnm;
 }
예제 #4
0
파일: Find.java 프로젝트: JosuaKrause/basex
  /**
   * 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);
  }