byte[] name = consumeToken(Type.ELEMNAME); if(stripNS) name = local(name); skipSpace(); if(elms.isEmpty()) throw new BuildException(OPEN, detailedInfo(), name); final byte[] open = elms.pop(); if(!eq(open, name)) throw new BuildException(CLOSINGELEM, detailedInfo(), name, open); chops.pop(); return consume(Type.R_BR); consume(Type.L_BR); atts.reset(); nsp.reset(); byte[] en = consumeToken(Type.ELEMNAME); if(stripNS) en = local(en); skipSpace(); final byte[] an = consumeToken(Type.ATTNAME); skipSpace(); consume(Type.EQ); skipSpace(); consume(Type.QUOTE); byte[] av = EMPTY; if(scanner.type == Type.ATTVALUE) { consume(Type.QUOTE);
/** * Returns a document node for the parsed XML input. * @param qc query context * @param frag parse fragments * @return result or {@code null} * @throws QueryException query exception */ ANode parseXml(final QueryContext qc, final boolean frag) throws QueryException { final Item item = exprs[0].atomItem(qc, info); if(item == null) return null; final IO io = new IOContent(toToken(item), string(sc.baseURI().string())); try { return new DBNode(frag ? new XMLParser(io, MainOptions.get(), true) : Parser.xmlParser(io)); } catch(final IOException ex) { throw SAXERR_X.get(info, ex); } } }
@Override public final void parse() throws IOException { // loop until all tokens have been processed scanner.more(); while(true) { if(scanner.type == Type.TEXT) { final byte[] text = scanner.token.toArray(); if(!elms.isEmpty() || fragment || !ws(text)) { if(chops.peek()) scanner.token.trim(); builder.text(scanner.token.toArray()); } } else if(scanner.type == Type.COMMENT) { builder.comment(scanner.token.toArray()); } else if(scanner.type == Type.PI) { builder.pi(scanner.token.toArray()); } else if(scanner.type == Type.EOF) { break; } else if(scanner.type != Type.DTD) { // L_BR, L_BR_CLOSE if(!fragment && closed) throw new BuildException(MOREROOTS, detailedInfo()); if(!parseElement()) break; continue; } if(!scanner.more()) break; } scanner.close(); if(!elms.isEmpty()) throw new BuildException(DOCOPEN, detailedInfo(), elms.pop()); }
/** * Checks if the current token matches the specified type. * @param type token type to be checked * @return result of scanner step * @throws IOException I/O exception */ private boolean consume(final Type type) throws IOException { if(scanner.type == type) return scanner.more(); throw new BuildException(PARSEINV, detailedInfo(), type.string, scanner.type.string); }
/** * Returns a document node for the parsed XML input. * @param qc query context * @param frag parse fragments * @return result or {@code null} * @throws QueryException query exception */ ANode parseXml(final QueryContext qc, final boolean frag) throws QueryException { final Item item = exprs[0].atomItem(qc, info); if(item == null) return null; final IO io = new IOContent(toToken(item), string(sc.baseURI().string())); try { return new DBNode(frag ? new XMLParser(io, MainOptions.get(), true) : Parser.xmlParser(io)); } catch(final IOException ex) { throw SAXERR_X.get(info, ex); } } }
@Override public final void parse() throws IOException { // loop until all tokens have been processed scanner.more(); while(true) { if(scanner.type == Type.TEXT) { final byte[] text = scanner.token.toArray(); if(!elms.isEmpty() || fragment || !ws(text)) { if(chops.peek()) scanner.token.trim(); builder.text(scanner.token.toArray()); } } else if(scanner.type == Type.COMMENT) { builder.comment(scanner.token.toArray()); } else if(scanner.type == Type.PI) { builder.pi(scanner.token.toArray()); } else if(scanner.type == Type.EOF) { break; } else if(scanner.type != Type.DTD) { // L_BR, L_BR_CLOSE if(!fragment && closed) throw new BuildException(MOREROOTS, detailedInfo()); if(!parseElement()) break; continue; } if(!scanner.more()) break; } scanner.close(); if(!elms.isEmpty()) throw new BuildException(DOCOPEN, detailedInfo(), elms.pop()); }
/** * Checks if the current token matches the specified type. * @param type token type to be checked * @return result of scanner step * @throws IOException I/O exception */ private boolean consume(final Type type) throws IOException { if(scanner.type == type) return scanner.more(); throw new BuildException(PARSEINV, detailedInfo(), type.string, scanner.type.string); }
byte[] name = consumeToken(Type.ELEMNAME); if(stripNS) name = local(name); skipSpace(); if(elms.isEmpty()) throw new BuildException(OPEN, detailedInfo(), name); final byte[] open = elms.pop(); if(!eq(open, name)) throw new BuildException(CLOSINGELEM, detailedInfo(), name, open); chops.pop(); return consume(Type.R_BR); consume(Type.L_BR); atts.reset(); nsp.reset(); byte[] en = consumeToken(Type.ELEMNAME); if(stripNS) en = local(en); skipSpace(); final byte[] an = consumeToken(Type.ATTNAME); skipSpace(); consume(Type.EQ); skipSpace(); consume(Type.QUOTE); byte[] av = EMPTY; if(scanner.type == Type.ATTVALUE) { consume(Type.QUOTE);
/** * Writes an geometry and returns a new element. * @param qc query context * @param geometry geometry * @return DBNode database node * @throws QueryException exception */ final DBNode toElement(final Geometry geometry, final QueryContext qc) throws QueryException { final String geo; try { // write geometry and add namespace declaration geo = new GMLWriter().write(geometry).replaceAll( "^<gml:(.*?)>", "<gml:$1 xmlns:gml='" + string(URI) + "'>"); } catch(final Exception ex) { throw GEO_WRITE.get(info, ex); } try { final XMLParser parser = new XMLParser(new IOContent(geo), qc.context.options); return new DBNode(MemBuilder.build(parser)).children().next(); } catch(final IOException ex) { throw IOERR_X.get(null, ex); } }
/** * Returns the token for the specified token type. If the current token type * is wrong, a {@code null} reference is returned. * @param type token type * @return token or {@code null} if the token type is wrong * @throws IOException I/O exception */ private byte[] consumeToken(final Type type) throws IOException { if(scanner.type == type) { final byte[] tok = scanner.token.toArray(); scanner.more(); return tok; } throw new BuildException(PARSEINV, detailedInfo(), type.string, scanner.type.string); }
/** * 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.finish()); } catch(final IOException ex) { // fallback: add string representation Util.debug(ex); elem.add(value); } }
/** * Returns the token for the specified token type. If the current token type * is wrong, a {@code null} reference is returned. * @param type token type * @return token or {@code null} if the token type is wrong * @throws IOException I/O exception */ private byte[] consumeToken(final Type type) throws IOException { if(scanner.type == type) { final byte[] tok = scanner.token.toArray(); scanner.more(); return tok; } throw new BuildException(PARSEINV, detailedInfo(), type.string, scanner.type.string); }
/** * 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.finish()); } catch(final IOException ex) { // fallback: add string representation Util.debug(ex); elem.add(value); } }
/** * Returns a parser instance, based on the current options. * @param source input source * @param options database options * @param target relative path reference * @return parser * @throws IOException I/O exception */ public static SingleParser singleParser(final IO source, final MainOptions options, final String target) throws IOException { // use file specific parser final SingleParser p; final MainParser mp = options.get(MainOptions.PARSER); switch(mp) { case HTML: p = new HtmlParser(source, options); break; case TEXT: p = new TextParser(source, options); break; case JSON: p = new JsonParser(source, options); break; case CSV: p = new CsvParser(source, options); break; default: p = options.get(MainOptions.INTPARSE) ? new XMLParser(source, options) : new SAXWrapper(source, options); break; } p.target(target); return p; } }
/** * Returns a parser instance, based on the current options. * @param source input source * @param options database options * @param target relative path reference * @return parser * @throws IOException I/O exception */ public static SingleParser singleParser(final IO source, final MainOptions options, final String target) throws IOException { // use file specific parser final SingleParser p; final MainParser mp = options.get(MainOptions.PARSER); switch(mp) { case HTML: p = new HtmlParser(source, options); break; case TEXT: p = new TextParser(source, options); break; case JSON: p = new JsonParser(source, options); break; case CSV: p = new CsvParser(source, options); break; default: p = options.get(MainOptions.INTPARSE) ? new XMLParser(source, options) : new SAXWrapper(source, options); break; } p.target(target); return p; } }