@Override public String det() { final String path = input.io().path(); return path.isEmpty() ? Util.info(LINE_X, input.line()) : Util.info(SCANPOS_X_X, input.io().path(), input.line()); }
@Override public void action(final Object comp) { final boolean valid = general.action(comp, true); ft.action(ftxindex.isSelected()); // ...must be located before remaining checks if (comp == general.browse || comp == general.input) dbname.setText(general.dbname); final String nm = dbname.getText().trim(); ok = valid && !nm.isEmpty(); String inf = valid ? ok ? null : ENTER_DB_NAME : RES_NOT_FOUND; Msg icon = Msg.ERROR; if (ok) { ok = Databases.validName(nm); if (ok) gui.gopts.set(GUIOptions.DBNAME, nm); if (!ok) { // name of database is invalid inf = Util.info(INVALID_X, NAME); } else if (general.input.getText().trim().isEmpty()) { // database will be empty inf = EMPTY_DB; icon = Msg.WARN; } else if (db.contains(nm)) { // old database will be overwritten inf = OVERWRITE_DB; icon = Msg.WARN; } } general.info.setText(inf, icon); enableOK(buttons, B_OK, ok); }
/** Saves the displayed text. */ private void save() { final BaseXFileChooser fc = new BaseXFileChooser(SAVE_AS, gui.gopts.get(GUIOptions.WORKPATH), gui).suffix(IO.XMLSUFFIX); final IO file = fc.select(Mode.FSAVE); if (file == null) return; gui.gopts.set(GUIOptions.WORKPATH, file.path()); gui.cursor(CURSORWAIT, true); final MainOptions opts = gui.context.options; final int mh = opts.get(MainOptions.MAXHITS); opts.set(MainOptions.MAXHITS, -1); opts.set(MainOptions.CACHEQUERY, false); try (final PrintOutput out = new PrintOutput(file.toString())) { if (cmd != null) { cmd.execute(gui.context, out); } else if (ns != null) { ns.serialize(Serializer.get(out)); } else { final byte[] txt = text.getText(); for (final byte t : txt) if (t < 0 || t > ' ' || ws(t)) out.write(t); } } catch (final IOException ex) { BaseXDialog.error(gui, Util.info(FILE_NOT_SAVED_X, file)); } finally { opts.set(MainOptions.MAXHITS, mh); opts.set(MainOptions.CACHEQUERY, true); gui.cursor(CURSORARROW, true); } }
@Override public final void service(final HttpServletRequest req, final HttpServletResponse res) throws IOException { final HTTPContext http = new HTTPContext(req, res, this); final boolean restxq = this instanceof RestXqServlet; try { http.authorize(); run(http); http.log(SC_OK, ""); } catch (final HTTPException ex) { http.status(ex.getStatus(), Util.message(ex), restxq); } catch (final LoginException ex) { http.status(SC_UNAUTHORIZED, Util.message(ex), restxq); } catch (final IOException | QueryException ex) { http.status(SC_BAD_REQUEST, Util.message(ex), restxq); } catch (final ProcException ex) { http.status(SC_BAD_REQUEST, Text.INTERRUPTED, restxq); } catch (final Exception ex) { final String msg = Util.bug(ex); Util.errln(msg); http.status(SC_INTERNAL_SERVER_ERROR, Util.info(UNEXPECTED, msg), restxq); } finally { if (Prop.debug) { Util.outln("_ REQUEST _________________________________" + Prop.NL + req); final Enumeration<String> en = req.getHeaderNames(); while (en.hasMoreElements()) { final String key = en.nextElement(); Util.outln(Text.LI + key + Text.COLS + req.getHeader(key)); } Util.out("_ RESPONSE ________________________________" + Prop.NL + res); } } }
/** * 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(); }
@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())); }
/** * Shows a quit dialog for the specified editor. * * @param edit editor to be saved * @return {@code false} if confirmation was canceled */ private boolean confirm(final EditorArea edit) { if (edit.modified && (edit.opened() || edit.getText().length != 0)) { final Boolean ok = BaseXDialog.yesNoCancel(gui, Util.info(CLOSE_FILE_X, edit.file.name())); if (ok == null || ok && !save()) return false; } return true; }
/** Notifies all views of a data reference change. */ public void init() { final Data data = initHistory(gui.context); if (data != null) { // if a large database is opened, the user is asked if complex /// visualizations should be closed first final long size = data.meta.dbsize(); boolean open = false; for (final View v : view) open |= v.visible() && v.db(); if (open && size > LARGEDB && BaseXDialog.confirm(gui, Util.info(H_LARGE_DB, Performance.format(size)))) { for (final View v : view) if (v.visible() && v.db()) v.visible(false); } } else { // database closed: close open dialogs for (final Window w : gui.getOwnedWindows()) { if (w.isVisible() && w instanceof BaseXDialog) ((BaseXDialog) w).cancel(); } } gui.context.focused = -1; for (final View v : view) v.refreshInit(); gui.layoutViews(); gui.setTitle(data != null ? data.meta.name : null); }
/** * Creates the error message from the specified text and extension array. * * @param text text message with optional placeholders * @param ext info extensions * @return argument */ private static String message(final String text, final Object[] ext) { final int es = ext.length; for (int e = 0; e < es; e++) { if (ext[e] instanceof ExprInfo) ext[e] = chop(((ExprInfo) ext[e]).toErrorString(), null); } return Util.info(text, ext); }
@Override public void run() { running = true; while (running) { try { final Socket s = socket.accept(); if (stop.exists()) { if (!stop.delete()) log.write(Util.info(FILE_NOT_DELETED_X, stop)); quit(); } else { // drop inactive connections final long ka = context.mprop.num(MainProp.KEEPALIVE) * 1000L; if (ka > 0) { final long ms = System.currentTimeMillis(); for (final ClientListener cs : context.sessions) { if (ms - cs.last > ka) cs.quit(); } } new ClientListener(s, context, log, this).start(); } } catch (final IOException ex) { // socket was closed.. break; } } }
/** Open all selected files externally. */ private void openExternal() { for (final IOFile file : selectedValues()) { try { file.open(); } catch (final IOException ex) { BaseXDialog.error(project.gui, Util.info(FILE_NOT_OPENED_X, file)); } } }
/** * Performs a search. * * @param sc search context * @param jump jump to next hit */ final void search(final SearchContext sc, final boolean jump) { try { rend.search(sc); if (!sc.search.isEmpty()) gui.status.setText(Util.info(Text.STRINGS_FOUND_X, sc.nr())); if (jump) jump(SearchDir.CURRENT, false); } catch (final Exception ex) { final String msg = Util.message(ex).replaceAll(Prop.NL + ".*", ""); gui.status.setError(Text.REGULAR_EXPR + Text.COLS + msg); } }
@Override public void action(final Object cmp) { for (int i = 1; i < EDITKIND.length; ++i) if (radio[i].isSelected()) kind = i; gui.gprop.set(GUIProp.LASTINSERT, kind); String msg = null; ok = kind != Data.TEXT || input2.getText().length != 0; if (kind != Data.TEXT && kind != Data.COMM) { ok = XMLToken.isQName(token(input1.getText())); if (!ok && !input1.getText().isEmpty()) msg = Util.info(INVALID, EDITNAME); } info.setText(msg, Msg.ERROR); enableOK(buttons, BUTTONOK, ok); }
@Override public void run() { try { for (int i = 0; i < runs; ++i) { Performance.sleep((long) (50 * RND.nextDouble())); // Return nth text of the database final int n = RND.nextInt() % MAX + 1; final String qu = Util.info(QUERY, n); new XQuery(qu).execute(context); } } catch (final BaseXException ex) { ex.printStackTrace(); } }
/** * Returns a keystroke for the specified string. * * @param cmd command * @return keystroke */ public static KeyStroke keyStroke(final GUICommand cmd) { final Object sc = cmd.shortcuts(); if (sc == null) return null; final String scut; if (sc instanceof BaseXKeys[]) { final BaseXKeys[] scs = (BaseXKeys[]) sc; if (scs.length == 0) return null; scut = scs[0].shortCut(); } else { scut = Util.info(sc, META); } final KeyStroke ks = KeyStroke.getKeyStroke(scut); if (ks == null) Util.errln("Could not assign shortcut: " + sc + " / " + scut); return ks; }
@Override public boolean parseArguments(final String[] args) { final Args arg = new Args(args, this, JAXRXINFO, Util.info(CONSOLE, JAXRX)); boolean daemon = false; final StringBuilder serial = new StringBuilder(); while (arg.more()) { if (arg.dash()) { final char c = arg.next(); if (c == 'D') { // hidden flag: daemon mode daemon = true; } else if (c == 'j') { // parse JAX-RX server port context.prop.set(Prop.JAXRXPORT, arg.num()); } else if (c == 'p') { // parse server port set(BXJaxRx.SERVERPORT, arg.num(), true); } else if (c == 'P') { // specify password pass = arg.string(); } else if (c == 's') { // set service flag service = !daemon; } else if (c == 'S') { // set/add serialization parameter if (serial.length() != 0) serial.append(','); serial.append(arg); set(BXJaxRx.SERIALIZER, serial, true); } else if (c == 'U') { // specify user name user = arg.string(); } else if (c == 'z') { // suppress logging quiet = true; } else { arg.check(false); } } else { arg.check(false); if (arg.string().equalsIgnoreCase("stop")) { stop(context.prop.num(Prop.SERVERPORT)); return false; } } } return arg.finish(); }
@Override public void run() { try { // Perform some queries for (int i = 0; i < runs; ++i) { Performance.sleep((long) (50 * RND.nextDouble())); // Return nth text of the database final int n = RND.nextInt() % MAX + 1; final String qu = Util.info(QUERY, n); session.execute("xquery " + qu); } session.close(); } catch (final Exception ex) { ex.printStackTrace(); } }
/** * Unwatches an event. * * @throws IOException I/O exception */ private void unwatch() throws IOException { final String name = in.readString(); final Sessions s = context.events.get(name); final boolean ok = s != null && s.contains(this); final String message; if (ok) { s.remove(this); message = UNWATCHING_EVENT_X; } else if (s == null) { message = EVENT_UNKNOWN_X; } else { message = EVENT_NOT_WATCHED_X; } info(Util.info(message, name), ok); out.flush(); }
@Override protected void parseArguments(final String[] args) throws IOException { final Args arg = new Args(args, this, SERVERINFO, Util.info(CONSOLE, SERVERMODE)); commands = new StringList(); boolean daemon = false; while (arg.more()) { if (arg.dash()) { switch (arg.next()) { case 'c': // send database commands commands.add(arg.string()); break; case 'd': // activate debug mode context.mprop.set(MainProp.DEBUG, true); break; case 'D': // hidden flag: daemon mode daemon = true; break; case 'e': // parse event port context.mprop.set(MainProp.EVENTPORT, arg.number()); break; case 'i': // activate interactive mode console = true; break; case 'p': // parse server port context.mprop.set(MainProp.SERVERPORT, arg.number()); break; case 'S': // set service flag service = !daemon; break; case 'z': // suppress logging quiet = true; break; default: arg.usage(); } } else { if (arg.string().equalsIgnoreCase("stop")) { stopped = true; } else { arg.usage(); } } } }
/** * Watches an event. * * @throws IOException I/O exception */ private void watch() throws IOException { server.initEvents(); // initialize server-based event handling if (!events) { out.writeString(Integer.toString(context.mprop.num(MainProp.EVENTPORT))); out.writeString(Long.toString(getId())); out.flush(); events = true; } final String name = in.readString(); final Sessions s = context.events.get(name); final boolean ok = s != null && !s.contains(this); final String message; if (ok) { s.add(this); message = WATCHING_EVENT_X; } else if (s == null) { message = EVENT_UNKNOWN_X; } else { message = EVENT_WATCHED_X; } info(Util.info(message, name), ok); }
@Override public String header() { return Util.info(S_CONSOLE_X, local() ? S_STANDALONE : S_CLIENT); }
/** * Creates an exception with the specified message. * * @param msg message * @param ext error extension * @return exception * @throws QueryException query exception */ QueryException error(final String msg, final Object... ext) throws QueryException { throw new QueryException(function.info, Err.BASX_RESTXQ, Util.info(msg, ext)); }
@Override public final String toString() { return Util.info("\"%\"", string(null)); }
@Override public String det() { return Util.info(NODESPARSED, filename, nodes); }
/** * Constructs an exception with the specified message and extension. * * @param s message * @param e message extension */ public LoginException(final String s, final Object... e) { super(Util.info(s, e)); }
/** * Returns an error message for an unknown database. * * @param name name of database * @return error message */ public static String dbnf(final String name) { return Util.info(DB_NOT_FOUND_X, name); }
@Override public String toString() { return Util.info("\"%\"", org.basex.util.Base64.encode(data)); }
/** * Returns an error string for an unknown key. * * @param key key * @return error string */ public final synchronized String unknown(final String key) { final String sim = similar(key); return Util.info(sim != null ? Text.UNKNOWN_OPT_SIMILAR_X_X : Text.UNKNOWN_OPTION_X, key, sim); }