Пример #1
0
  @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);
      }
    }
  }
Пример #2
0
 static {
   Attributes m = null;
   final URL loc = Prop.LOCATION;
   if (loc != null) {
     final String jar = loc.getFile();
     try {
       final ClassLoader cl = JarManifest.class.getClassLoader();
       final Enumeration<URL> list = cl.getResources("META-INF/MANIFEST.MF");
       while (list.hasMoreElements()) {
         final URL url = list.nextElement();
         if (!url.getFile().contains(jar)) continue;
         final InputStream in = url.openStream();
         try {
           m = new Manifest(in).getMainAttributes();
           break;
         } finally {
           in.close();
         }
       }
     } catch (final IOException ex) {
       Util.errln(ex);
     }
   }
   MAP = m;
 }
Пример #3
0
  /** Launches the console mode, which reads and executes user input. */
  private void console() {
    Util.outln(header() + NL + TRY_MORE_X);
    verbose = true;

    // create console reader
    try (final ConsoleReader cr = ConsoleReader.get()) {
      // loop until console is set to false (may happen in server mode)
      while (console) {
        // get next line
        final String in = cr.readLine(PROMPT);
        // end of input: break loop
        if (in == null) break;
        // skip empty lines
        if (in.isEmpty()) continue;

        try {
          if (!execute(new CommandParser(in, context).pwReader(cr.pwReader()))) {
            // show goodbye message if method returns false
            Util.outln(BYE[new Random().nextInt(4)]);
            break;
          }
        } catch (final IOException ex) {
          // output error messages
          Util.errln(ex);
        }
      }
    }
  }
Пример #4
0
 /**
  * Returns the contents of the specified file.
  *
  * @param f file to be read
  * @return content
  */
 private String read(final IO f) {
   try {
     return TextInput.content(f).toString().replaceAll("\r\n?", "\n");
   } catch (final IOException ex) {
     Util.errln(ex);
     return "";
   }
 }
Пример #5
0
 /**
  * Main method, launching the server process. Command-line arguments are listed with the {@code
  * -h} argument.
  *
  * @param args command-line arguments
  */
 public static void main(final String[] args) {
   try {
     new BaseXServer(args);
   } catch (final IOException ex) {
     Util.errln(ex);
     System.exit(1);
   }
 }
Пример #6
0
 static {
   try {
     final Class<?>[] classes = Loader.load(IFileParser.class.getPackage(), IFileParser.class);
     for (final Class<?> c : classes) {
       final String name = Util.name(c);
       if (!REGISTRY.containsValue(c) && fallbackParser != c)
         Util.debug("Loading % ... FAILED", name);
     }
   } catch (final IOException ex) {
     Util.errln("Failed to load parsers (%)", ex.getMessage());
   }
 }
Пример #7
0
 /** 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);
   }
 }
Пример #8
0
  @Override
  public JapaneseTokenizer init(final byte[] txt) {
    String source = string(txt);
    if (wc) { // convert wide-space to space
      source = source.replace('\u3000', '\u0020');
    }
    final ArrayList<?> morpheme = (ArrayList<?>) Reflect.invoke(parse, tagger, source);
    final ArrayList<Morpheme> list = new ArrayList<>();
    try {
      int prev = 0;
      final int ms = morpheme.size();
      for (int i = 0; i < ms; i++) {
        final Object m = morpheme.get(i);
        final String srfc = surface.get(m).toString();
        final String ftr = feature.get(m).toString();
        final int strt = start.getInt(m);
        if (i != 0) {
          final int l = strt - prev;
          if (l != 0) {
            list.add(new Morpheme(source.substring(strt - 1, strt + l - 1), KIGOU_FEATURE));
          }
        }
        prev = srfc.length() + strt;

        // separates continuous mark (ASCII)
        boolean cont = true;
        final ArrayList<Morpheme> marks = new ArrayList<>();
        final int sl = srfc.length();
        for (int s = 0; s < sl; s++) {
          final String c = String.valueOf(srfc.charAt(s));
          final byte[] t = token(c);
          if (t.length == 1) {
            if (letter(t[0]) || digit(t[0])) cont = false;
            else marks.add(new Morpheme(c, KIGOU_FEATURE));
          } else {
            cont = false;
          }
        }

        if (cont) list.addAll(marks);
        else list.add(new Morpheme(srfc, ftr));
      }
    } catch (final Exception ex) {
      Util.errln(Util.className(this) + ": " + ex);
    }
    tokenList = list;
    tokens = list.iterator();

    return this;
  }
Пример #9
0
  @Override
  public DiskData build() throws IOException {
    meta.assign(parser);
    meta.dirty = true;

    // calculate optimized output buffer sizes to reduce disk fragmentation
    final Runtime rt = Runtime.getRuntime();
    final long max = Math.min(1 << 22, rt.maxMemory() - rt.freeMemory() >> 2);
    int bs = (int) Math.min(meta.filesize, max);
    bs = Math.max(IO.BLOCKSIZE, bs - bs % IO.BLOCKSIZE);

    // drop old database (if available) and create new one
    DropDB.drop(dbname, sopts);
    sopts.dbpath(dbname).md();

    elemNames = new Names(meta);
    attrNames = new Names(meta);
    try {
      tout = new DataOutput(new TableOutput(meta, DATATBL));
      xout = new DataOutput(meta.dbfile(DATATXT), bs);
      vout = new DataOutput(meta.dbfile(DATAATV), bs);
      sout = new DataOutput(meta.dbfile(DATATMP), bs);

      final Performance perf = Prop.debug ? new Performance() : null;
      Util.debug(tit() + DOTS);
      parse();
      if (Prop.debug) Util.errln(" " + perf + " (" + Performance.getMemory() + ')');

    } catch (final IOException ex) {
      try {
        close();
      } catch (final IOException ignored) {
      }
      throw ex;
    }
    close();

    // copy temporary values into database table
    try (final DataInput in = new DataInput(meta.dbfile(DATATMP))) {
      final TableAccess ta = new TableDiskAccess(meta, true);
      for (; spos < ssize; ++spos) ta.write4(in.readNum(), 8, in.readNum());
      ta.close();
    }
    meta.dbfile(DATATMP).delete();

    // return database instance
    return new DiskData(meta, elemNames, attrNames, path, ns);
  }
Пример #10
0
 static {
   if (Reflect.available(PATTERN, "German")) {
     for (final Language l : Language.ALL.values()) {
       final Class<?> clz = Reflect.find(PATTERN, l);
       if (clz == null) continue;
       final Method m1 = Reflect.method(clz, "setCurrent", String.class);
       final Method m2 = Reflect.method(clz, "stem");
       final Method m3 = Reflect.method(clz, "getCurrent");
       if (m1 == null || m2 == null || m3 == null) {
         Util.errln("Could not initialize \"%\" Snowball stemmer.", l);
       } else {
         CLASSES.put(l, new StemmerClass(clz, m1, m2, m3));
       }
     }
   }
 }
Пример #11
0
  /**
   * 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;
  }
Пример #12
0
  /**
   * Converts an HTML document to XML.
   *
   * @param io io reference
   * @param opts html options
   * @return parser
   * @throws IOException I/O exception
   */
  private static IO toXML(final IO io, final HtmlOptions opts) throws IOException {
    // reader could not be initialized; fall back to XML
    if (READER == null) return io;

    try {
      // tries to extract the encoding from the input
      final TextInput ti = new TextInput(io);
      String enc = ti.encoding();
      final byte[] content = ti.content();

      // looks for a charset definition
      final byte[] encoding = token("charset=");
      int cs = indexOf(content, encoding);
      if (cs > 0) {
        // extracts the encoding string
        cs += encoding.length;
        int ce = cs;
        final int cl = content.length;
        while (++ce < cl && content[ce] > 0x28) ;
        enc = string(substring(content, cs, ce));
      }

      // define input
      final InputSource is = new InputSource(new ArrayInput(content));
      is.setEncoding(supported(enc) ? normEncoding(enc) : UTF8);
      // define output
      final StringWriter sw = new StringWriter();
      final XMLReader reader = (XMLReader) Reflect.get(READER);
      final Object writer = Reflect.get(WRITER, sw);

      // set TagSoup options
      if (opts.get(HtmlOptions.HTML)) {
        reader.setFeature("http://xml.org/sax/features/namespaces", false);
        opt("method", "html");
        opt("omit-xml-declaration", "yes");
      }
      if (opts.get(HtmlOptions.NONS))
        reader.setFeature("http://xml.org/sax/features/namespaces", false);
      if (opts.get(HtmlOptions.OMITXML)) opt("omit-xml-declaration", "yes");
      if (opts.get(HtmlOptions.NOBOGONS)) reader.setFeature(FEATURES + "ignore-bogons", true);
      if (opts.get(HtmlOptions.NODEFAULTS))
        reader.setFeature(FEATURES + "default-attributes", false);
      if (opts.get(HtmlOptions.NOCOLONS)) reader.setFeature(FEATURES + "translate-colons", true);
      if (opts.get(HtmlOptions.NORESTART)) reader.setFeature(FEATURES + "restart-elements", false);
      if (opts.get(HtmlOptions.IGNORABLE))
        reader.setFeature(FEATURES + "ignorable-whitespace", true);
      if (opts.get(HtmlOptions.EMPTYBOGONS)) reader.setFeature(FEATURES + "bogons-empty", true);
      if (opts.get(HtmlOptions.ANY)) reader.setFeature(FEATURES + "bogons-empty", false);
      if (opts.get(HtmlOptions.NOROOTBOGONS)) reader.setFeature(FEATURES + "root-bogons", false);
      if (opts.get(HtmlOptions.NOCDATA)) reader.setFeature(FEATURES + "cdata-elements", false);
      if (opts.get(HtmlOptions.LEXICAL))
        reader.setProperty("http://xml.org/sax/properties/lexical-handler", writer);
      if (opts.contains(HtmlOptions.METHOD)) opt("method", opts.get(HtmlOptions.METHOD));
      if (opts.contains(HtmlOptions.DOCTYPESYS))
        opt("doctype-system", opts.get(HtmlOptions.DOCTYPESYS));
      if (opts.contains(HtmlOptions.DOCTYPEPUB))
        opt("doctype-public", opts.get(HtmlOptions.DOCTYPEPUB));
      if (opts.contains(HtmlOptions.ENCODING)) is.setEncoding(opts.get(HtmlOptions.ENCODING));
      // end TagSoup options

      reader.setContentHandler((ContentHandler) writer);
      reader.parse(is);
      return new IOContent(token(sw.toString()), io.name());

    } catch (final SAXException ex) {
      Util.errln(ex);
      return io;
    }
  }
Пример #13
0
  /** Writes the properties to disk. */
  public final synchronized void write() {
    final StringBuilder user = new StringBuilder();
    BufferedReader br = null;
    try {
      // caches options specified by the user
      if (file.exists()) {
        br = new BufferedReader(new FileReader(file.file()));
        for (String line; (line = br.readLine()) != null; ) {
          if (line.equals(PROPUSER)) break;
        }
        for (String line; (line = br.readLine()) != null; ) {
          user.append(line).append(NL);
        }
      }
    } catch (final Exception ex) {
      Util.debug(ex);
    } finally {
      if (br != null)
        try {
          br.close();
        } catch (final IOException e) {
        }
    }

    BufferedWriter bw = null;
    try {
      bw = new BufferedWriter(new FileWriter(file.file()));
      bw.write(PROPHEADER + NL);

      for (final Field f : getClass().getFields()) {
        final Object obj = f.get(null);
        if (!(obj instanceof Object[])) continue;
        final String key = ((Object[]) obj)[0].toString();

        final Object val = props.get(key);
        if (val instanceof String[]) {
          final String[] str = (String[]) val;
          bw.write(key + " = " + str.length + NL);
          final int is = str.length;
          for (int i = 0; i < is; ++i) {
            if (str[i] != null) bw.write(key + (i + 1) + " = " + str[i] + NL);
          }
        } else if (val instanceof int[]) {
          final int[] num = (int[]) val;
          final int ns = num.length;
          for (int i = 0; i < ns; ++i) {
            bw.write(key + i + " = " + num[i] + NL);
          }
        } else {
          bw.write(key + " = " + val + NL);
        }
      }
      bw.write(NL + PROPUSER + NL);
      bw.write(user.toString());
    } catch (final Exception ex) {
      Util.errln("% could not be written.", file);
      Util.debug(ex);
    } finally {
      if (bw != null)
        try {
          bw.close();
        } catch (final IOException e) {
        }
    }
  }
Пример #14
0
  /**
   * Parses the specified test case.
   *
   * @param root root node
   * @throws Exception exception
   * @return true if the query, specified by {@link #single}, was evaluated
   */
  private boolean parse(final Nodes root) throws Exception {
    final String pth = text("@FilePath", root);
    final String outname = text("@name", root);
    if (single != null && !outname.startsWith(single)) return true;

    final Performance perf = new Performance();
    if (verbose) Util.out("- " + outname);

    boolean inspect = false;
    boolean correct = true;

    final Nodes nodes = states(root);
    for (int n = 0; n < nodes.size(); ++n) {
      final Nodes state = new Nodes(nodes.list[n], nodes.data);

      final String inname = text("*:query/@name", state);
      context.query = new IOFile(queries + pth + inname + IO.XQSUFFIX);
      final String in = read(context.query);
      String er = null;
      ItemCache iter = null;
      boolean doc = true;

      final Nodes cont = nodes("*:contextItem", state);
      Nodes curr = null;
      if (cont.size() != 0) {
        final Data d = Check.check(context, srcs.get(string(data.atom(cont.list[0]))));
        curr = new Nodes(d.doc(), d);
        curr.root = true;
      }

      context.prop.set(Prop.QUERYINFO, compile);
      final QueryProcessor xq = new QueryProcessor(in, curr, context);
      context.prop.set(Prop.QUERYINFO, false);

      // limit result sizes to 1MB
      final ArrayOutput ao = new ArrayOutput();
      final TokenBuilder files = new TokenBuilder();

      try {
        files.add(
            file(nodes("*:input-file", state), nodes("*:input-file/@variable", state), xq, n == 0));
        files.add(file(nodes("*:defaultCollection", state), null, xq, n == 0));

        var(nodes("*:input-URI", state), nodes("*:input-URI/@variable", state), xq);
        eval(nodes("*:input-query/@name", state), nodes("*:input-query/@variable", state), pth, xq);

        parse(xq, state);

        for (final int p : nodes("*:module", root).list) {
          final String uri = text("@namespace", new Nodes(p, data));
          final String file = mods.get(string(data.atom(p))) + IO.XQSUFFIX;
          xq.module(file, uri);
        }

        // evaluate and serialize query
        final SerializerProp sp = new SerializerProp();
        sp.set(SerializerProp.S_INDENT, context.prop.is(Prop.CHOP) ? DataText.YES : DataText.NO);
        final XMLSerializer xml = new XMLSerializer(ao, sp);

        iter = xq.value().cache();
        for (Item it; (it = iter.next()) != null; ) {
          doc &= it.type == NodeType.DOC;
          it.serialize(xml);
        }
        xml.close();
      } catch (final Exception ex) {
        if (!(ex instanceof QueryException || ex instanceof IOException)) {
          System.err.println("\n*** " + outname + " ***");
          System.err.println(in + "\n");
          ex.printStackTrace();
        }
        er = ex.getMessage();
        if (er.startsWith(STOPPED)) er = er.substring(er.indexOf('\n') + 1);
        if (er.startsWith("[")) er = er.replaceAll("\\[(.*?)\\] (.*)", "$1 $2");
        // unexpected error - dump stack trace
      }

      // print compilation steps
      if (compile) {
        Util.errln("---------------------------------------------------------");
        Util.err(xq.info());
        Util.errln(in);
      }

      final Nodes expOut = nodes("*:output-file/text()", state);
      final TokenList result = new TokenList();
      for (int o = 0; o < expOut.size(); ++o) {
        final String resFile = string(data.atom(expOut.list[o]));
        final IOFile exp = new IOFile(expected + pth + resFile);
        result.add(read(exp));
      }

      final Nodes cmpFiles = nodes("*:output-file/@compare", state);
      boolean xml = false;
      boolean frag = false;
      boolean ignore = false;
      for (int o = 0; o < cmpFiles.size(); ++o) {
        final byte[] type = data.atom(cmpFiles.list[o]);
        xml |= eq(type, XML);
        frag |= eq(type, FRAGMENT);
        ignore |= eq(type, IGNORE);
      }

      String expError = text("*:expected-error/text()", state);

      final StringBuilder log = new StringBuilder(pth + inname + IO.XQSUFFIX);
      if (files.size() != 0) {
        log.append(" [");
        log.append(files);
        log.append("]");
      }
      log.append(NL);

      /** Remove comments. */
      log.append(norm(in));
      log.append(NL);
      final String logStr = log.toString();
      // skip queries with variable results
      final boolean print = currTime || !logStr.contains("current-");

      boolean correctError = false;
      if (er != null && (expOut.size() == 0 || !expError.isEmpty())) {
        expError = error(pth + outname, expError);
        final String code = er.substring(0, Math.min(8, er.length()));
        for (final String e : SLASH.split(expError)) {
          if (code.equals(e)) {
            correctError = true;
            break;
          }
        }
      }

      if (correctError) {
        if (print) {
          logOK.append(logStr);
          logOK.append("[Right] ");
          logOK.append(norm(er));
          logOK.append(NL);
          logOK.append(NL);
          addLog(pth, outname + ".log", er);
        }
        ++ok;
      } else if (er == null) {
        int s = -1;
        final int rs = result.size();

        while (!ignore && ++s < rs) {
          inspect |= s < cmpFiles.list.length && eq(data.atom(cmpFiles.list[s]), INSPECT);

          final byte[] res = result.get(s), actual = ao.toArray();
          if (res.length == ao.size() && eq(res, actual)) break;

          if (xml || frag) {
            iter.reset();

            try {
              final ItemCache ic =
                  toIter(string(res).replaceAll("^<\\?xml.*?\\?>", "").trim(), frag);
              if (FNSimple.deep(null, iter, ic)) break;

              ic.reset();
              final ItemCache ia = toIter(string(actual), frag);
              if (FNSimple.deep(null, ia, ic)) break;
            } catch (final Throwable ex) {
              System.err.println("\n" + outname + ":");
              ex.printStackTrace();
            }
          }
        }
        if ((rs > 0 || !expError.isEmpty()) && s == rs && !inspect) {
          if (print) {
            if (expOut.size() == 0) result.add(error(pth + outname, expError));
            logErr.append(logStr);
            logErr.append("[" + testid + " ] ");
            logErr.append(norm(string(result.get(0))));
            logErr.append(NL);
            logErr.append("[Wrong] ");
            logErr.append(norm(ao.toString()));
            logErr.append(NL);
            logErr.append(NL);
            addLog(pth, outname + (xml ? IO.XMLSUFFIX : ".txt"), ao.toString());
          }
          correct = false;
          ++err;
        } else {
          if (print) {
            logOK.append(logStr);
            logOK.append("[Right] ");
            logOK.append(norm(ao.toString()));
            logOK.append(NL);
            logOK.append(NL);
            addLog(pth, outname + (xml ? IO.XMLSUFFIX : ".txt"), ao.toString());
          }
          ++ok;
        }
      } else {
        if (expOut.size() == 0 || !expError.isEmpty()) {
          if (print) {
            logOK2.append(logStr);
            logOK2.append("[" + testid + " ] ");
            logOK2.append(norm(expError));
            logOK2.append(NL);
            logOK2.append("[Rght?] ");
            logOK2.append(norm(er));
            logOK2.append(NL);
            logOK2.append(NL);
            addLog(pth, outname + ".log", er);
          }
          ++ok2;
        } else {
          if (print) {
            logErr2.append(logStr);
            logErr2.append("[" + testid + " ] ");
            logErr2.append(norm(string(result.get(0))));
            logErr2.append(NL);
            logErr2.append("[Wrong] ");
            logErr2.append(norm(er));
            logErr2.append(NL);
            logErr2.append(NL);
            addLog(pth, outname + ".log", er);
          }
          correct = false;
          ++err2;
        }
      }
      if (curr != null) Close.close(curr.data, context);
      xq.close();
    }

    if (reporting) {
      logReport.append("    <test-case name=\"");
      logReport.append(outname);
      logReport.append("\" result='");
      logReport.append(correct ? "pass" : "fail");
      if (inspect) logReport.append("' todo='inspect");
      logReport.append("'/>");
      logReport.append(NL);
    }

    // print verbose/timing information
    final long nano = perf.getTime();
    final boolean slow = nano / 1000000 > timer;
    if (verbose) {
      if (slow) Util.out(": " + Performance.getTimer(nano, 1));
      Util.outln();
    } else if (slow) {
      Util.out(NL + "- " + outname + ": " + Performance.getTimer(nano, 1));
    }

    return single == null || !outname.equals(single);
  }
Пример #15
0
 /**
  * Prints the stack of the specified error to standard error.
  *
  * @param th error/exception instance
  */
 public static void stack(final Throwable th) {
   for (final String s : toArray(th)) errln(s);
 }
Пример #16
0
 /**
  * Prints the current stack trace to System.err.
  *
  * @param i number of steps to print
  */
 public static void stack(final int i) {
   errln("You're here:");
   final String[] stack = toArray(new Throwable());
   final int l = Math.min(Math.max(2, i + 2), stack.length);
   for (int s = 2; s < l; ++s) errln(stack[s]);
 }
Пример #17
0
 /**
  * Prints performance information if the {@link Prop#debug} flag is set.
  *
  * @param perf performance reference
  */
 public static void memory(final Performance perf) {
   if (!Prop.debug) return;
   errln(" " + perf + " (" + Performance.getMemory() + ')');
 }
Пример #18
0
 /**
  * Prints a string to standard error if the {@link Prop#debug} flag is set.
  *
  * @param str debug string
  * @param ext text optional extensions
  */
 public static void debug(final Object str, final Object... ext) {
   if (Prop.debug) errln(str, ext);
 }