Example #1
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   checkCreate(qc);
   final String driver = string(toToken(exprs[0], qc));
   if (Reflect.find(driver) == null) throw BXSQ_DRIVER_X.get(info, driver);
   return null;
 }
Example #2
0
  static {
    IOFile dic = null;
    if (Reflect.available(PATTERN)) {
      dic = new IOFile(LANG);
      if (!dic.exists()) {
        dic = new IOFile(Prop.HOME, "etc/" + LANG);
        if (!dic.exists()) {
          available = false;
        }
      }
    } else {
      available = false;
    }

    if (available) {
      Class<?> clz = Reflect.find(PATTERN);
      if (clz == null) {
        Util.debug("Could not initialize Igo Japanese lexer.");
      } else {
        /* Igo constructor. */
        final Constructor<?> tgr = Reflect.find(clz, String.class);
        tagger = Reflect.get(tgr, dic.path());
        if (tagger == null) {
          available = false;
          Util.debug("Could not initialize Igo Japanese lexer.");
        } else {
          parse = Reflect.method(clz, "parse", CharSequence.class);
          if (parse == null) {
            Util.debug("Could not initialize Igo lexer method.");
          }
          clz = Reflect.find("net.reduls.igo.Morpheme");
          surface = Reflect.field(clz, "surface");
          feature = Reflect.field(clz, "feature");
          start = Reflect.field(clz, "start");
        }
      }
    }
  }
Example #3
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));
       }
     }
   }
 }
Example #4
0
 /**
  * Creates a new instance of the function.
  *
  * @param ii input info
  * @param arg arguments
  * @return function
  */
 public FuncCall get(final InputInfo ii, final Expr... arg) {
   return (FuncCall)
       Reflect.get(
           Reflect.find(func, InputInfo.class, Function.class, Expr[].class), ii, this, arg);
 }
Example #5
0
/**
 * This class uses TagSoup to convert HTML input to well-formed XML. If TagSoup is not found in the
 * classpath, the original document is passed on.
 *
 * <p>TagSoup was written by John Cowan and is based on the Apache 2.0 License: {@code
 * http://home.ccil.org/~cowan/XML/tagsoup/}.
 *
 * @author BaseX Team 2005-14, BSD License
 * @author Christian Gruen
 */
public final class HtmlParser extends XMLParser {
  /** Name of HTML Parser. */
  private static final String NAME = "TagSoup";
  /** TagSoup URL. */
  private static final String FEATURES = "http://www.ccil.org/~cowan/tagsoup/features/";

  /** XML parser class string. */
  private static final String PCLASS = "org.ccil.cowan.tagsoup.Parser";
  /** XML writer class string. */
  private static final String WCLASS = "org.ccil.cowan.tagsoup.XMLWriter";
  /** HTML reader. */
  private static final Class<?> READER = Reflect.find(PCLASS);
  /** HTML writer. */
  private static final Constructor<?> WRITER = Reflect.find(Reflect.find(WCLASS), Writer.class);
  /** XML writer output property method. */
  private static final Method METHOD =
      Reflect.method(
          Reflect.find(WCLASS), "setOutputProperty", new Class[] {String.class, String.class});

  /**
   * Checks if a CatalogResolver is available.
   *
   * @return result of check
   */
  public static boolean available() {
    return READER != null;
  }

  /**
   * Returns the name of the parser, or an empty string.
   *
   * @return name of parser
   */
  public static String parser() {
    return available() ? NAME : "";
  }

  /**
   * Constructor.
   *
   * @param source document source
   * @param opts database options
   * @throws IOException I/O exception
   */
  public HtmlParser(final IO source, final MainOptions opts) throws IOException {
    this(source, opts, opts.get(MainOptions.HTMLPARSER));
  }

  /**
   * Constructor.
   *
   * @param source document source
   * @param opts database options
   * @param hopts html options
   * @throws IOException I/O exception
   */
  public HtmlParser(final IO source, final MainOptions opts, final HtmlOptions hopts)
      throws IOException {
    super(toXML(source, hopts), opts);
  }

  /**
   * 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;
    }
  }

  /**
   * Reflection invoke XMLWriter.setOutputProperty().
   *
   * @param name property
   * @param value value
   */
  private static void opt(final String name, final String value) {
    Reflect.invoke(METHOD, WRITER, name, value);
  }
}
Example #6
0
 /**
  * Initializes JDBC with the specified driver.
  *
  * @param ctx query context
  * @return {@code null}
  * @throws QueryException query exception
  */
 private Item init(final QueryContext ctx) throws QueryException {
   final String driver = string(checkStr(expr[0], ctx));
   if (Reflect.find(driver) == null) SQLINIT.thrw(input, driver);
   return null;
 }
Example #7
0
  /**
   * Returns the specified function.
   *
   * @param name name of the function
   * @param args optional arguments
   * @param dyn compile-/run-time flag
   * @param ctx query context
   * @param ii input info
   * @return function instance
   * @throws QueryException query exception
   */
  public TypedFunc get(
      final QNm name,
      final Expr[] args,
      final boolean dyn,
      final QueryContext ctx,
      final InputInfo ii)
      throws QueryException {

    // find function
    final byte[] uri = name.uri();
    final byte[] ln = name.local();

    // parse data type constructors
    if (eq(uri, XSURI)) {
      final Type type = AtomType.find(name, true);
      if (type == null || type == AtomType.NOT || type == AtomType.AAT) {
        final Levenshtein ls = new Levenshtein();
        for (final AtomType t : AtomType.values()) {
          if (t.par != null && ls.similar(lc(ln), lc(t.string()), 0))
            FUNSIMILAR.thrw(ii, name.string(), t.string());
        }
        FUNCUNKNOWN.thrw(ii, name.string());
      }
      if (args.length != 1) FUNCTYPE.thrw(ii, name.string());
      final SeqType to = SeqType.get(type, SeqType.Occ.ZO);
      return TypedFunc.constr(new Cast(ii, args[0], to), to);
    }

    // check Java functions - only allowed with administrator permissions
    if (startsWith(uri, JAVAPRE) && ctx.context.user.perm(User.ADMIN)) {
      final String c = string(substring(uri, JAVAPRE.length));
      // convert dashes to upper-case initials
      final TokenBuilder tb = new TokenBuilder().add(c).add('.');
      boolean dash = false;
      for (int p = 0; p < ln.length; p += cl(ln, p)) {
        final int ch = cp(ln, p);
        if (dash) {
          tb.add(Character.toUpperCase(ch));
          dash = false;
        } else {
          dash = ch == '-';
          if (!dash) tb.add(ch);
        }
      }

      final String java = tb.toString();
      final int i = java.lastIndexOf(".");
      final Class<?> cls = Reflect.find(java.substring(0, i));
      if (cls == null) FUNCJAVA.thrw(ii, java);
      final String mth = java.substring(i + 1);
      return TypedFunc.java(new JavaFunc(ii, cls, mth, args));
    }

    // check predefined functions
    final FuncCall fun = Functions.get().get(ln, uri, args, ctx, ii);
    if (fun != null) {
      for (final Function f : Function.UPDATING) {
        if (fun.def == f) {
          ctx.updating = true;
          break;
        }
      }
      return new TypedFunc(fun, fun.def.type(args.length));
    }

    // find local function
    for (int l = 0; l < func.length; ++l) {
      final QNm qn = func[l].name;
      if (eq(ln, qn.local()) && eq(uri, qn.uri()) && args.length == func[l].args.length)
        return new TypedFunc(add(ii, qn, l, args), FuncType.get(func[l]));
    }

    // add function call for function that has not been defined yet
    if (!dyn && Types.find(name, false) == null) {
      return new TypedFunc(
          add(ii, name, add(new UserFunc(ii, name, new Var[args.length], null, false), ii), args),
          FuncType.arity(args.length));
    }
    return null;
  }