/** * 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(); } }
/** * 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; }
/** * Returns the value of the requested attribute, or an error. * * @param pref error prefix * @param node node * @param name attribute name * @return value * @throws BaseXException database exception */ public static byte[] attribute(final String pref, final ANode node, final byte[] name) throws BaseXException { final byte[] value = node.attribute(name); if (value != null) return value; throw new BaseXException("%: Missing \"%\" attribute.", pref, name); }
/** * 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; }
/** * Returns the start point of a line. * * @param node xml element containing gml object(s) * @return start point geometry as a gml element * @throws QueryException query exception */ @Deterministic public ANode startPoint(final ANode node) throws QueryException { final Geometry geo = geo(node, Q_GML_LINEARRING, Q_GML_LINESTRING); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Line"); return gmlWriter(((LineString) geo).getStartPoint()); }
/** * Returns the z-coordinate value for point. * * @param node xml element containing gml object(s) * @return z double value * @throws QueryException query exception */ @Deterministic public Dbl z(final ANode node) throws QueryException { final Geometry geo = geo(node, Q_GML_POINT); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Line"); return Dbl.get(geo.getCoordinate().z); }
/** * Returns the number of interior rings in a polygon. * * @param node xml element containing gml object(s) * @return integer number of interior rings * @throws QueryException query exception */ @Deterministic public Int numInteriorRing(final ANode node) throws QueryException { final Geometry geo = geo(node, Q_GML_POLYGON); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Polygon"); return Int.get(((Polygon) geo).getNumInteriorRing()); }
/** * Return a boolean value that shows weather the line is a ring or not. A line is a ring if it is * closed and simple. * * @param node xml element containing gml object(s) * @return boolean value * @throws QueryException query exception */ @Deterministic public Bln isRing(final ANode node) throws QueryException { final Geometry geo = geo(node, Q_GML_LINEARRING, Q_GML_LINESTRING); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Line"); return Bln.get(((LineString) geo).isRing()); }
/** * 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; }
/** * Reads an element as a gml node. Returns a geometry element or {@code null} if the element does * not match one of the specified types. * * @param node xml node containing gml object(s) * @param names allowed geometry types * @return geometry, or {@code null} * @throws QueryException query exception */ private static Geometry geo(final ANode node, final QNm... names) throws QueryException { if (node.type != NodeType.ELM) throw EXPTYPE_X_X_X.get(null, NodeType.ELM, node.type, node); final QNm qname = node.qname(); for (final QNm geo : names) { if (!qname.eq(geo)) continue; // type found... create reader and geometry element try { final String input = node.serialize().toString(); final GMLReader gmlReader = new GMLReader(); final GeometryFactory geoFactory = new GeometryFactory(); return gmlReader.read(input, geoFactory); } catch (final Throwable ex) { throw GeoErrors.gmlReaderErr(ex); } } return null; }
/** * 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); }
@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)); }
/** * Checks if the line is closed loop. That is, if the start Point is same with end Point. * * @param node xml element containing gml object(s) * @return boolean value * @throws QueryException query exception */ @Deterministic public Bln isClosed(final ANode node) throws QueryException { final Geometry geo = geo(node, Q_GML_LINEARRING, Q_GML_LINESTRING, Q_GML_MULTILINESTRING); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Line"); return Bln.get( geo instanceof LineString ? ((LineString) geo).isClosed() : ((MultiLineString) geo).isClosed()); }
/** * Returns the nth geometry of a geometry collection. * * @param node xml element containing gml object(s) * @param number index of i-th interior ring * @return n-th interior ring geometry (LineString) as a gml element * @throws QueryException query exception */ @Deterministic public ANode interiorRingN(final ANode node, final Int number) throws QueryException { final Geometry geo = geo(node, Q_GML_POLYGON); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Polygon"); final long n = number.itr(); final int max = ((Polygon) geo).getNumInteriorRing(); if (n < 1 || n > max) throw GeoErrors.outOfRangeIdx(number); return gmlWriter(((Polygon) geo).getInteriorRingN((int) n - 1)); }
/** * Returns the nth point of a line. * * @param node xml element containing gml object(s) * @param number index of i-th point * @return n-th point as a gml element * @throws QueryException query exception */ @Deterministic public ANode pointN(final ANode node, final Int number) throws QueryException { final Geometry geo = geo(node, Q_GML_LINEARRING, Q_GML_LINESTRING); if (geo == null && checkGeo(node) != null) throw GeoErrors.geoType(node.qname().local(), "Line"); final int max = geo.getNumPoints(); final long n = number.itr(); if (n < 1 || n > max) throw GeoErrors.outOfRangeIdx(number); return gmlWriter(((LineString) geo).getPointN((int) n - 1)); }
/** * Returns child elements. * * @param node node * @param name name * @return iterator */ public static BasicNodeIter children(final ANode node, final byte[] name) { final BasicNodeIter children = node.children(); return new BasicNodeIter() { @Override public ANode next() { for (ANode child; (child = children.next()) != null; ) { if (child.type == NodeType.ELM && (name == null || eq(child.qname().id(), name))) return child; } return null; } }; }
@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))); }
/** * 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); } }
/** * Evaluates the specified function and creates a response. * * @throws Exception exception (including unexpected ones) */ void create() throws Exception { // bind variables final StaticFunc sf = function.function; final Expr[] args = new Expr[sf.args.length]; function.bind(http, args, error); // wrap function with a function call final MainModule mm = new MainModule(sf, args); // assign main module and http context and register process query.mainModule(mm); query.http(http); query.context.register(query); String redirect = null, forward = null; RestXqRespBuilder resp = null; try { // compile and evaluate query query.compile(); final Iter iter = query.iter(); Item item = iter.next(); // handle response element if (item != null && item instanceof ANode) { final ANode node = (ANode) item; // send redirect to browser if (REST_REDIRECT.eq(node)) { final ANode ch = node.children().next(); if (ch == null || ch.type != NodeType.TXT) throw function.error(NO_VALUE, node.name()); redirect = string(ch.string()).trim(); return; } // server-side forwarding if (REST_FORWARD.eq(node)) { final ANode ch = node.children().next(); if (ch == null || ch.type != NodeType.TXT) throw function.error(NO_VALUE, node.name()); forward = string(ch.string()).trim(); return; } if (REST_RESPONSE.eq(node)) { resp = new RestXqRespBuilder(); resp.build(node, function, iter, http); return; } } // HEAD method must return a single response element if (function.methods.size() == 1 && function.methods.contains(HTTPMethod.HEAD.name())) throw function.error(HEAD_METHOD); // serialize result final SerializerOptions sp = function.output; http.sopts(sp); http.initResponse(); final Serializer ser = Serializer.get(http.res.getOutputStream(), sp); for (; item != null; item = iter.next()) ser.serialize(item); ser.close(); } finally { query.close(); query.context.unregister(query); if (redirect != null) { http.res.sendRedirect(redirect); } else if (forward != null) { http.req.getRequestDispatcher(forward).forward(http.req, http.res); } else if (resp != null) { if (resp.status != 0) http.status(resp.status, resp.message, resp.error); http.res.getOutputStream().write(resp.cache.toArray()); } } }
/** * 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(); }
/** * 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); } }
/** * Reads an element as a gml node. Returns a geometry element or throws an exception if the * element is of the wrong type. * * @param node xml node containing gml object(s) * @return geometry * @throws QueryException query exception */ private Geometry checkGeo(final ANode node) throws QueryException { final Geometry geo = geo(node, QNAMES); if (geo == null) throw GeoErrors.unrecognizedGeo(node.qname().local()); return geo; }