- if(st == "content") {
- if(c == '<') {
- st = "tag";
- c = in.read();
- } else if(c < 0) {
- if(stack.peek() == frag)
- return(frag);
- else
- throw(new ParseException("Unexpected end-of-file while parsing non-root element"));
- } else {
- st = "text";
- }
- } else if(st == "tag") {
- if(Character.isWhitespace((char)c)) {
- c = in.read();
- } else if(c == '!') {
- cdashcnt = 0;
- c = in.read();
- st = "comment";
- } else if(namechar((char)c)) {
- st = "stag";
- } else if(c == '/') {
- c = in.read();
- st = "etag";
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file while parsing tag"));
- } else {
- throw(new ParseException("Unexpected character `" + printable((char)c) + "' encountered in tag name"));
- }
- } else if(st == "stag") {
- boolean flush = false;
- if(namechar((char)c)) {
- buf.append((char)c);
- c = in.read();
- } else if(c == '>') {
- flush = true;
- } else if(Character.isWhitespace((char)c)) {
- flush = true;
- c = in.read();
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file while parsing tag name"));
- } else {
- throw(new ParseException("Unexpected character `" + printable((char)c) + "' encountered in tag name"));
- }
- if(flush) {
- Element n = makenode(doc, buf.toString());
- buf = new StringBuilder();
- stack.peek().appendChild(n);
- stack.push(n);
- st = "attr";
- }
- } else if(st == "comment") {
- if(c == '-') {
- cdashcnt++;
- c = in.read();
- } else if((c == '>') && (cdashcnt == 4)) {
- stack.peek().appendChild(doc.createComment(buf.toString()));
- buf = new StringBuilder();
- st = "content";
- c = in.read();
- } else if(cdashcnt >= 2) {
- if(cdashcnt > 2)
- cdashcnt = 2;
- buf.append((char)c);
- c = in.read();
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file while parsing comment"));
- } else {
- throw(new ParseException("Unexpected character `" + printable((char)c) + "' encountered in comment"));
- }
- } else if(st == "attr") {
- if(namechar((char)c)) {
- st = "aname";
- } else if(c == '>') {
- st = "content";
- c = in.read();
- } else if(c == '/') {
- st = "stagend";
- c = in.read();
- } else if(Character.isWhitespace((char)c)) {
- c = in.read();
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file while parsing attributes"));
- } else {
- throw(new ParseException("Unexpected character `" + printable((char)c) + "' encountered inside tag"));
- }
- } else if(st == "stagend") {
- if(c == '>') {
- stack.pop();
- c = in.read();
- st = "content";
- } else if(Character.isWhitespace((char)c)) {
- c = in.read();
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file at end of empty tag"));
- } else {
- throw(new ParseException("Unexpected character `" + printable((char)c) + "' encountered at and of empty tag"));
- }
- } else if(st == "aname") {
- if(namechar((char)c)) {
- buf.append((char)c);
- c = in.read();
- } else if(Character.isWhitespace((char)c)) {
- c = in.read();
- } else if(c == '=') {
- Element el = (Element)stack.peek();
- Attr attr = makeattr(doc, el, buf.toString());
- el.setAttributeNodeNS(attr);
- buf = new StringBuilder();
- stack.push(attr);
- st = "avalstart";
- c = in.read();
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file while parsing attribute name"));
- } else {
- throw(new ParseException("Unexpected character `" + printable((char)c) + "' encountered in attribute name"));
- }
- } else if(st == "avalstart") {
- if((c == '\'') || (c == '"')) {
- atype = (char)c;
- c = in.read();
- st = "aval";
- } else if(Character.isWhitespace((char)c)) {
- c = in.read();
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file while parsing attribute value"));
- } else {
- throw(new ParseException("Unexpected character `" + printable((char)c) + "' encountered in attribute value"));
- }
- } else if(st == "aval") {
- if(c == atype) {
- c = in.read();
- Attr a = (Attr)stack.pop();
- a.setValue(buf.toString());
- buf = new StringBuilder();
- st = "attr";
- } else if(c == '&') {
- c = in.read();
- st = "aent";
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file while parsing attribute value"));
- } else {
- buf.append((char)c);
- c = in.read();
- }
- } else if(st == "etag") {
- if(namechar((char)c)) {
- buf.append((char)c);
- c = in.read();
- } else if(c == '>') {
- String nm = buf.toString();
- buf = new StringBuilder();
- Node n = stack.pop();
- if(n instanceof DocumentFragment)
- throw(new ParseException("Unexpected end tag for `" + nm + "' while parsing root content"));
- Element el = (Element)n;
- if(!nm.equals(el.getTagName()))
- throw(new ParseException("Unexpected end tag for `" + nm + "' while parsing `" + el.getTagName() + "'"));
- c = in.read();
- st = "content";
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file while parsing end tag"));
- } else {
- throw(new ParseException("Unexpected character `" + printable((char)c) + "' encountered in end tag"));
- }
- } else if(st == "text") {
- boolean flush = false;
- if(c == '&') {
- st = "ent";
- c = in.read();
- } else if(c == '<') {
- flush = true;
- st = "content";
- } else if(c < 0) {
- flush = true;
- st = "content";
- } else {
- buf.append((char)c);
- c = in.read();
- }
- if(flush) {
- Text n = doc.createTextNode(buf.toString());
- buf = new StringBuilder();
- stack.peek().appendChild(n);
- }
- } else if(st == "ent") {
- if(c == ';') {
- String ename = ebuf.toString();
- ebuf = new StringBuilder();
- String rep = entity(ename);
- if(rep == null)
- throw(new ParseException("Unknown entity `" + ename + "' encountered"));
- buf.append(rep);
- st = "text";
- c = in.read();
- } else if(c < 0) {
- throw(new ParseException("Unexpected end-of-file while parsing entity name"));
- } else if(namechar((char)c)) {
- ebuf.append((char)c);
- c = in.read();
+ c = s.in.peek();
+ if(c < 0) {
+ throw(new ParseException("Unexpected end-of-file while reading attribute value"));
+ } else if(c == qt) {
+ s.in.read();
+ break;
+ } else if(c == '&') {
+ buf.append(entity(s));
+ } else {
+ buf.append((char)s.in.read());
+ }
+ }
+ a.setValue(buf.toString());
+ return(a);
+ }
+
+ protected Element element(State s) throws IOException {
+ Element n = makenode(s.doc, name(s));
+ while(true) {
+ int c = s.in.peek(true);
+ if(c < 0) {
+ throw(new ParseException("Unexpected end-of-file while parsing start tag"));
+ } else if(c == '>') {
+ s.in.read();
+ break;
+ } else if(c == '/') {
+ s.in.read();
+ s.in.peek(true);
+ c = s.in.read();
+ if(c != '>')
+ throw(new ParseException("Unexpected character `" + printable(c) + "' encountered in end of empty tag"));
+ return(n);
+ } else if(namechar((char)c)) {
+ n.setAttributeNodeNS(attribute(s, n));
+ } else {
+ throw(new ParseException("Unexpected character `" + printable(c) + "' encountered in start tag"));
+ }
+ }
+ while(true) {
+ int c = s.in.peek();
+ if(c < 0) {
+ break;
+ } else if(c == '<') {
+ s.in.read();
+ c = s.in.peek(true);
+ if(c == '/') {
+ s.in.read();
+ s.in.peek(true);
+ String nm = name(s);
+ if(!nm.equals(n.getTagName()))
+ throw(new ParseException("Unexpected end tag for `" + nm + "' while parsing `" + n.getTagName() + "'"));
+ if(s.in.peek(true) != '>')
+ throw(new ParseException("Expected `>' while reading end tag, got `" + printable(c) + "'"));
+ s.in.read();
+ break;