Пример #1
0
 /**
  * Returns the value of the specified attribute or {@code null}.
  *
  * @param name attribute to be found
  * @return attribute value
  */
 public byte[] attribute(final QNm name) {
   final BasicNodeIter iter = attributes();
   while (true) {
     final ANode node = iter.next();
     if (node == null) return null;
     if (node.qname().eq(name)) return node.string();
   }
 }
Пример #2
0
 @Override
 public Str item(final QueryContext qc, final InputInfo ii) throws QueryException {
   ANode node, par = toNode(exprs[0], qc);
   do {
     node = par;
     par = node.parent();
   } while (par != null);
   final DBNode dbn = toDBNode(node);
   return dbn.kind() == Data.DOC ? Str.get(dbn.data().text(dbn.pre(), true)) : Str.ZERO;
 }
Пример #3
0
 /**
  * Recursively finds the uri for the specified prefix.
  *
  * @param pref prefix
  * @return uri
  */
 public final byte[] uri(final byte[] pref) {
   final Atts at = namespaces();
   if (at != null) {
     final byte[] s = at.value(pref);
     if (s != null) return s;
     final ANode n = parent();
     if (n != null) return n.uri(pref);
   }
   return pref.length == 0 ? Token.EMPTY : null;
 }
Пример #4
0
  /**
   * Parses a string as XML and adds the resulting nodes to the specified parent.
   *
   * @param value string to parse
   * @param elem element
   */
  public static void add(final byte[] value, final FElem elem) {

    try {
      final Parser parser = new XMLParser(new IOContent(value), MainOptions.get(), true);
      for (final ANode node : new DBNode(parser).children()) elem.add(node.copy());
    } catch (final IOException ex) {
      // fallback: add string representation
      Util.debug(ex);
      elem.add(value);
    }
  }
Пример #5
0
 /**
  * Returns a copy of the namespace hierarchy.
  *
  * @param sc static context (can be {@code null})
  * @return namespaces
  */
 public final Atts nsScope(final StaticContext sc) {
   final Atts ns = new Atts();
   ANode node = this;
   do {
     final Atts nsp = node.namespaces();
     if (nsp != null) {
       for (int a = nsp.size() - 1; a >= 0; a--) {
         final byte[] key = nsp.name(a);
         if (!ns.contains(key)) ns.add(key, nsp.value(a));
       }
     }
     node = node.parent();
   } while (node != null && node.type == NodeType.ELM);
   if (sc != null) sc.ns.inScope(ns);
   return ns;
 }
Пример #6
0
 /**
  * Compares two nodes for their unique order.
  *
  * @param node1 first node
  * @param node2 node to be compared
  * @return {@code 0} if the nodes are identical, or {@code 1}/{@code -1} if the first node appears
  *     after/before the second
  */
 static int diff(final ANode node1, final ANode node2) {
   // cache parents of first node
   final ANodeList nl = new ANodeList();
   for (ANode n = node1; n != null; n = n.parent()) {
     if (n == node2) return 1;
     nl.add(n);
   }
   // find lowest common ancestor
   ANode c2 = node2;
   LOOP:
   for (ANode n = node2; (n = n.parent()) != null; ) {
     final int is = nl.size();
     for (int i = 1; i < is; i++) {
       if (n == node1) return -1;
       if (!nl.get(i).is(n)) continue;
       // check which node appears as first LCA child
       final ANode c1 = nl.get(i - 1);
       final BasicNodeIter ir = n.children();
       for (ANode c; (c = ir.next()) != null; ) {
         if (c.is(c1)) return -1;
         if (c.is(c2)) return 1;
       }
       break LOOP;
     }
     c2 = n;
   }
   // subtraction is used instead of comparison to support overflow of node id
   return node1.id - node2.id < 0 ? -1 : 1;
 }
Пример #7
0
  /**
   * Adds the specified entry to the output stream.
   *
   * @param entry entry descriptor
   * @param con contents
   * @param out output archive
   * @param level default compression level
   * @throws QueryException query exception
   * @throws IOException I/O exception
   */
  private void add(final Item entry, final Item con, final ArchiveOut out, final int level)
      throws QueryException, IOException {

    // create new zip entry
    final String name = string(entry.string(info));
    if (name.isEmpty()) ARCH_EMPTY.thrw(info);
    final ZipEntry ze = new ZipEntry(name);
    String en = null;

    // compression level
    byte[] lvl = null;
    if (entry instanceof ANode) {
      final ANode el = (ANode) entry;
      lvl = el.attribute(Q_LEVEL);

      // last modified
      final byte[] mod = el.attribute(Q_LAST_MOD);
      if (mod != null) {
        try {
          ze.setTime(new Int(new Dtm(mod, info)).itr());
        } catch (final QueryException qe) {
          ARCH_DATETIME.thrw(info, mod);
        }
      }

      // encoding
      final byte[] enc = el.attribute(Q_ENCODING);
      if (enc != null) {
        en = string(enc);
        if (!Charset.isSupported(en)) ARCH_ENCODING.thrw(info, enc);
      }
    }

    // data to be compressed
    byte[] val = checkStrBin(con);
    if (con instanceof AStr && en != null && en != Token.UTF8) val = encode(val, en);

    try {
      out.level(lvl == null ? level : toInt(lvl));
    } catch (final IllegalArgumentException ex) {
      ARCH_LEVEL.thrw(info, lvl);
    }
    out.write(ze, val);
  }
Пример #8
0
 @Override
 public boolean eq(final ANode node) {
   return node.type == type
       && (name == null || node.qname(tmpq).eq(name))
       && (ext == null
           || ext == AtomType.ATY
           || (node instanceof DBNode || strip) && ext == AtomType.UTY
           || type == NodeType.ATT
               && (ext == AtomType.AST || ext == AtomType.AAT || ext == AtomType.ATM));
 }
Пример #9
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)));
    }
Пример #10
0
  /**
   * Parses a jar descriptor.
   *
   * @param io XML input
   * @return jar descriptor container
   * @throws QueryException query exception
   */
  public JarDesc parse(final IO io) throws QueryException {
    final JarDesc desc = new JarDesc();
    try {
      final ANode node = new DBNode(io).children().next();
      for (final ANode next : node.children()) {
        if (next.type != NodeType.ELM) continue;

        final QNm name = next.qname();
        // ignore namespace to improve compatibility
        if (eq(JAR, name.local())) desc.jars.add(next.string());
        else if (eq(CLASS, name.local())) desc.classes.add(next.string());
        // [CG] Packaging: add warning if unknown elements are encountered
      }
      if (desc.jars.isEmpty()) throw BXRE_JARDESC_X.get(info, NOJARS);
      else if (desc.classes.isEmpty()) throw BXRE_JARDESC_X.get(info, NOCLASSES);
      return desc;
    } catch (final IOException ex) {
      throw BXRE_JARFAIL_X.get(info, ex);
    }
  }
Пример #11
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   final ANode node = toEmptyNode(arg(0, qc), qc);
   final QNm qname = node != null ? node.qname() : null;
   return qname != null ? Uri.uri(qname.uri(), false) : Uri.EMPTY;
 }
Пример #12
0
 /**
  * Adds children of a sub node.
  *
  * @param ch child nodes
  * @param nb node cache
  */
 static void addDesc(final BasicNodeIter ch, final ANodeList nb) {
   for (ANode n; (n = ch.next()) != null; ) {
     nb.add(n.finish());
     addDesc(n.children(), nb);
   }
 }
Пример #13
0
 /**
  * Returns the root of a node (the topmost ancestor without parent node).
  *
  * @return root node
  */
 public final ANode root() {
   final ANode p = parent();
   return p == null ? this : p.root();
 }