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