/** * 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; }
/** * Parses the query. * * @param p performance * @throws QueryException query exception */ private void parse(final Performance p) throws QueryException { qp.http(http); for (final Entry<String, String[]> entry : vars.entrySet()) { final String name = entry.getKey(); final String[] value = entry.getValue(); if (name == null) qp.context(value[0], value[1]); else qp.bind(name, value[0], value[1]); } qp.parse(); if (p != null) info.parsing += p.time(); }
/** * Initializes the servlet context, based on the servlet context. Parses all context parameters * and passes them on to the database context. * * @param sc servlet context * @throws IOException I/O exception */ static synchronized void init(final ServletContext sc) throws IOException { // skip process if context has already been initialized if (context != null) return; // set servlet path as home directory final String path = sc.getRealPath("/"); System.setProperty(Prop.PATH, path); // parse all context parameters final HashMap<String, String> map = new HashMap<String, String>(); // store default web root map.put(MainProp.HTTPPATH[0].toString(), path); final Enumeration<?> en = sc.getInitParameterNames(); while (en.hasMoreElements()) { final String key = en.nextElement().toString(); if (!key.startsWith(Prop.DBPREFIX)) continue; // only consider parameters that start with "org.basex." String val = sc.getInitParameter(key); if (eq(key, DBUSER, DBPASS, DBMODE, DBVERBOSE)) { // store servlet-specific parameters as system properties System.setProperty(key, val); } else { // prefix relative paths with absolute servlet path if (key.endsWith("path") && !new File(val).isAbsolute()) { val = path + File.separator + val; } // store remaining parameters (without project prefix) in map map.put(key.substring(Prop.DBPREFIX.length()).toUpperCase(Locale.ENGLISH), val); } } context = new Context(map); if (SERVER.equals(System.getProperty(DBMODE))) { new BaseXServer(context); } else { context.log = new Log(context); } }
/** Reads in the property file. */ static { try { final String file = "/completions.properties"; final InputStream is = TextPanel.class.getResourceAsStream(file); if (is == null) { Util.errln(file + " not found."); } else { try (final NewlineInput nli = new NewlineInput(is)) { for (String line; (line = nli.readLine()) != null; ) { final int i = line.indexOf('='); if (i == -1 || line.startsWith("#")) continue; final String key = line.substring(0, i), value = line.substring(i + 1); if (REPLACE.put(key, value.replace("\\n", "\n")) != null) { Util.errln(file + ": " + key + " was assigned twice"); } } } } } catch (final IOException ex) { Util.errln(ex); } }
/** * Binds a variable. * * @param name name of variable (if {@code null}, value will be bound as context value) * @param value value to be bound * @param type type * @return reference */ public AQuery bind(final String name, final String value, final String type) { vars.put(name, new String[] {value, type}); return this; }