Beispiel #1
0
  @Override
  public Iter iter(final QueryContext ctx) throws QueryException {
    switch (def) {
      case ERROR:
        final int al = expr.length;
        if (al == 0) FUNERR1.thrw(input);

        String code = FUNERR1.code();
        String msg = FUNERR1.desc;

        final Item it = expr[0].item(ctx, input);
        if (it == null) {
          if (al == 1) XPEMPTY.thrw(input, desc());
        } else {
          code = Token.string(((QNm) checkType(it, AtomType.QNM)).ln());
        }
        if (al > 1) msg = Token.string(checkEStr(expr[1], ctx));
        final Value val = al > 2 ? expr[2].value(ctx) : null;
        final QueryException ex = new QueryException(input, code, val, msg);
        throw ex;
      case TRACE:
        return new Iter() {
          final Iter ir = expr[0].iter(ctx);
          final byte[] s = checkEStr(expr[1], ctx);

          @Override
          public Item next() throws QueryException {
            final Item i = ir.next();
            if (i != null) {
              final TokenBuilder tb = new TokenBuilder(s);
              if (s.length != 0) tb.add(": ");
              tb.add(i.toString());
              // if GUI is used, or if user is no admin, trace info is cached
              if (Prop.gui || !ctx.context.user.perm(User.ADMIN)) {
                ctx.evalInfo(tb.finish());
              } else {
                Util.errln(tb);
              }
            }
            return i;
          }
        };
      case ENVARS:
        final ItemCache ic = new ItemCache();
        for (final Object k : System.getenv().keySet().toArray()) {
          ic.add(Str.get(k));
        }
        return ic;
      default:
        return super.iter(ctx);
    }
  }
Beispiel #2
0
 /**
  * Returns the combo box selections and the keys of the specified set.
  *
  * @param key keys
  * @return key array
  */
 private String[] entries(final byte[][] key) {
   final StringList sl = new StringList();
   sl.add(Util.info(INFOENTRIES, key.length));
   for (final byte[] k : key) sl.add(Token.string(k));
   sl.sort(true, true, 1);
   return sl.toArray();
 }
Beispiel #3
0
    @Override
    public void execute(final GUI gui) {
      final DialogExport dialog = new DialogExport(gui);
      if (!dialog.ok()) return;

      final IOFile root = new IOFile(dialog.path());

      // check if existing files will be overwritten
      if (root.exists()) {
        IO file = null;
        boolean overwrite = false;
        final Data d = gui.context.data();
        final IntList il = d.resources.docs();
        final int is = il.size();
        for (int i = 0; i < is; i++) {
          file = root.merge(Token.string(d.text(il.get(i), true)));
          if (file.exists()) {
            if (overwrite) {
              // more than one file will be overwritten; check remaining tests
              file = null;
              break;
            }
            overwrite = true;
          }
        }
        if (overwrite) {
          // show message for overwriting files or directories
          final String msg = file == null ? FILES_REPLACE_X : FILE_EXISTS_X;
          if (file == null) file = root;
          if (!BaseXDialog.confirm(gui, Util.info(msg, file))) return;
        }
      }
      DialogProgress.execute(gui, new Export(root.path()));
    }
Beispiel #4
0
 @Override
 public String getItemAsString(final Properties props) throws XQException {
   try {
     return it.node() && it.type != NodeType.TXT ? serialize() : Token.string(it.atom(null));
   } catch (final QueryException e) {
     throw new XQException(e.getMessage(), e.code());
   }
 }
Beispiel #5
0
 @Override
 public void execute(final GUI gui) {
   final int pre = gui.context.marked.pres[0];
   final byte[] txt = ViewData.path(gui.context.data(), pre);
   // copy path to clipboard
   final Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
   clip.setContents(new StringSelection(Token.string(txt)), null);
 }
Beispiel #6
0
 /**
  * Constructor.
  *
  * @param value value
  * @param type item type
  * @param ii input info
  * @throws QueryException query exception
  */
 private Dur(final byte[] value, final Type type, final InputInfo ii) throws QueryException {
   this(type);
   final String val = Token.string(value).trim();
   final Matcher mt = DUR.matcher(val);
   if (!mt.matches() || val.endsWith("P") || val.endsWith("T")) throw dateError(value, XDURR, ii);
   yearMonth(value, mt, ii);
   dayTime(value, mt, 6, ii);
 }
Beispiel #7
0
 /**
  * Notifies all views of a focus change.
  *
  * @param pre focused pre value
  * @param vw the calling view
  */
 public void focus(final int pre, final View vw) {
   if (gui.context.focused == pre) return;
   gui.context.focused = pre;
   for (final View v : view) if (v != vw && v.visible()) v.refreshFocus();
   if (pre != -1) {
     gui.status.setText(Token.string(ViewData.path(gui.context.data(), pre)));
   }
 }
Beispiel #8
0
 @Override
 public Item item(final QueryContext ctx, final InputInfo ii) throws QueryException {
   switch (def) {
     case ENVAR:
       final String e = System.getenv(Token.string(checkEStr(expr[0], ctx)));
       return e != null ? Str.get(e) : null;
     default:
       return super.item(ctx, ii);
   }
 }
Beispiel #9
0
 @Override
 public URI getNodeUri() throws XQException {
   opened();
   if (!it.node()) throw new BXQException(NODE);
   final ANode node = (ANode) it;
   try {
     return new URI(Token.string(node.base()));
   } catch (final URISyntaxException ex) {
     throw new BXQException(ex.toString());
   }
 }
Beispiel #10
0
 @Override
 public String getAtomicValue() throws XQException {
   opened();
   if (it.node()) throw new BXQException(ATOM);
   try {
     return Token.string(it.atom(null));
   } catch (final QueryException e) {
     // function item
     throw new BXQException(ATOM);
   }
 }
Beispiel #11
0
  /**
   * Initializes the date format.
   *
   * @param d input
   * @param e example format
   * @param ii input info
   * @throws QueryException query exception
   */
  final void date(final byte[] d, final String e, final InputInfo ii) throws QueryException {
    final Matcher mt = DATE.matcher(Token.string(d).trim());
    if (!mt.matches()) throw dateError(d, e, ii);
    yea = toLong(mt.group(1), false, ii);
    // +1 is added to BC values to simplify computations
    if (yea < 0) yea++;
    mon = (byte) (Strings.toInt(mt.group(3)) - 1);
    day = (byte) (Strings.toInt(mt.group(4)) - 1);

    if (mon < 0 || mon >= 12 || day < 0 || day >= dpm(yea, mon)) throw dateError(d, e, ii);
    if (yea <= MIN_YEAR || yea > MAX_YEAR) throw DATERANGE_X_X.get(ii, type, chop(d, ii));
    zone(mt, 5, d, ii);
  }
Beispiel #12
0
  /**
   * Initializes the time format.
   *
   * @param d input format
   * @param e expected format
   * @param ii input info
   * @throws QueryException query exception
   */
  final void time(final byte[] d, final String e, final InputInfo ii) throws QueryException {
    final Matcher mt = TIME.matcher(Token.string(d).trim());
    if (!mt.matches()) throw dateError(d, e, ii);

    hou = (byte) Strings.toInt(mt.group(1));
    min = (byte) Strings.toInt(mt.group(2));
    sec = toDecimal(mt.group(3), false, ii);
    if (min >= 60
        || sec.compareTo(BD60) >= 0
        || hou > 24
        || hou == 24 && (min > 0 || sec.compareTo(BigDecimal.ZERO) > 0)) throw dateError(d, e, ii);
    zone(mt, 5, d, ii);
    if (hou == 24) {
      hou = 0;
      add(DAYSECONDS);
    }
  }
Beispiel #13
0
    @Override
    public void execute(final GUI gui) {
      final Nodes n = gui.context.marked;
      final DialogInsert insert = new DialogInsert(gui);
      if (!insert.ok()) return;

      final StringList sl = insert.result;
      final NodeType type = ANode.type(insert.kind);
      String item = Token.string(type.string()) + " { " + quote(sl.get(0)) + " }";

      if (type == NodeType.ATT || type == NodeType.PI) {
        item += " { " + quote(sl.get(1)) + " }";
      } else if (type == NodeType.ELM) {
        item += " { () }";
      }

      gui.context.copied = null;
      gui.execute(new XQuery("insert node " + item + " into " + openPre(n, 0)));
    }
Beispiel #14
0
  /**
   * Refreshes the view after a file has been saved.
   *
   * @param root root directory
   * @param ctx database context
   * @throws InterruptedException interruption
   */
  void parse(final IOFile root, final Context ctx) throws InterruptedException {
    final long id = ++parseId;
    final HashSet<String> parsed = new HashSet<>();
    final TreeMap<String, InputInfo> errs = new TreeMap<>();

    // collect files to be parsed
    final ProjectCache pc = cache(root);
    final StringList mods = new StringList(), lmods = new StringList();
    for (final String path : pc) {
      final IOFile file = new IOFile(path);
      if (file.hasSuffix(IO.XQSUFFIXES)) (file.hasSuffix(IO.XQMSUFFIX) ? lmods : mods).add(path);
    }
    mods.add(lmods);

    // parse modules
    for (final String path : mods) {
      if (id != parseId) throw new InterruptedException();
      if (parsed.contains(path)) continue;

      final IOFile file = new IOFile(path);
      try (final TextInput ti = new TextInput(file)) {
        // parse query
        try (final QueryContext qc = new QueryContext(ctx)) {
          final String input = ti.cache().toString();
          final boolean lib = QueryProcessor.isLibrary(input);
          qc.parse(input, lib, path, null);
          // parsing was successful: remember path
          parsed.add(path);
          for (final byte[] mod : qc.modParsed) parsed.add(Token.string(mod));
        } catch (final QueryException ex) {
          // parsing failed: remember path
          final InputInfo ii = ex.info();
          errs.put(path, ii);
          parsed.add(ii.path());
        }
      } catch (final IOException ex) {
        // file may not be accessible
        Util.debug(ex);
      }
    }
    errors = errs;
  }
Beispiel #15
0
 @Override
 public String toString() {
   return Token.string(cont);
 }
Beispiel #16
0
 /**
  * Prints a string to standard output, followed by a newline.
  *
  * @param str output string
  * @param ext text optional extensions
  */
 public static void outln(final Object str, final Object... ext) {
   out((str instanceof byte[] ? Token.string((byte[]) str) : str) + NL, ext);
 }
Beispiel #17
0
 /**
  * Returns a string and replaces all % characters by the specified extensions (see {@link
  * TokenBuilder#addExt} for details).
  *
  * @param str string to be extended
  * @param ext text text extensions
  * @return extended string
  */
 public static String info(final Object str, final Object... ext) {
   return Token.string(inf(str, ext));
 }
Beispiel #18
0
 @Override
 public final Duration toJava() {
   return ADate.DF.newDuration(Token.string(string(null)));
 }