/bundles/plugins-trunk/XML/xml/parser/javacc/XmlParser.java
Java | 722 lines | 646 code | 53 blank | 23 comment | 147 complexity | 4eedb48f9c619ce025eee898440f4637 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- /* Generated By:JavaCC: Do not edit this line. XmlParser.java */
- package xml.parser.javacc;
- import java.text.MessageFormat;
- import java.util.*;
- import java.util.regex.*;
- import sidekick.util.*;
- import java.io.Reader;
- public class XmlParser implements XmlParserConstants {
- static String NL = System.getProperty("line.separator");
- private List<ParseError> parseErrors = new ArrayList<ParseError>();
- public void setLineSeparator(String ls) {
- NL = ls;
- }
- private static String getTokenText(Token first, Token cur) {
- Token t;
- StringBuffer sb = new StringBuffer();
- for (t=first; t != cur.next; t = t.next) {
- if (t.specialToken != null) {
- Token tt=t.specialToken;
- while (tt.specialToken != null)
- tt = tt.specialToken;
- for (; tt != null; tt = tt.next)
- sb.append(tt.image);
- };
- sb.append(t.image);
- };
- return sb.toString();
- }
- /**
- * The line offset is used when the XML to be parsed is only part of a file,
- * @param lineOffset The line number of the first line of the fragment.
- * @param columnOffset The column number of the first character of the fragment.
- */
- public XmlParser(Reader in, int lineOffset, int columnOffset){
- this(in);
- jj_input_stream.ReInit(in,lineOffset,columnOffset);
- }
- public static void main(String[] args) throws ParseException {
- XmlParser parser = new XmlParser(System.in);
- XmlDocument doc = parser.XmlDocument();
- doc.accept(new XmlDebugDumper(System.out));
- System.exit(0);
- }
- public void setTabSize(int size) {
- jj_input_stream.setTabSize(size);
- }
- public int getTabSize() {
- return jj_input_stream.getTabSize(0);
- }
- private void addException(ParseException pe) {
- Range range = getExceptionLocation( pe );
- parseErrors.add(new ParseError(pe.getMessage(), range));
- pe.printStackTrace();
- }
- public List<ParseError> getParseErrors() {
- System.out.println("getParserErrors, there are " + parseErrors.size() + " errors");
- return parseErrors;
- }
- // regex to extract line and colun from a ParseException message
- // ParseException message look like: "Parse error at line 116, column 5. Encountered: }"
- private Pattern pePattern = Pattern.compile( "(.*?)(\\d+)(.*?)(\\d+)(.*?)" );
- /**
- * @return attempts to return a Location indicating the location of a parser
- * exception. If the ParseException contains a Token reference, all is well,
- * otherwise, this method attempts to parse the message string for the
- * exception.
- */
- private Range getExceptionLocation( ParseException pe ) {
- Token t = pe.currentToken;
- if ( t != null ) {
- return new Range( new Location( t.next.beginLine - 1, t.next.beginColumn ), new Location( t.next.endLine - 1, t.next.endColumn ) );
- }
- // ParseException message look like: "Parse error at line 116, column 5. Encountered: }"
- try {
- Matcher m = pePattern.matcher( pe.getMessage() );
- if ( m.matches() ) {
- String ln = m.group( 2 );
- String cn = m.group( 4 );
- int line_number = -1;
- int column_number = 0;
- if ( ln != null )
- line_number = Integer.parseInt( ln );
- if ( cn != null )
- column_number = Integer.parseInt( cn );
- return line_number > -1 ? new Range( new Location( line_number - 1, column_number - 1 ), new Location( line_number - 1, column_number ) ) : null;
- }
- return new Range();
- }
- catch ( Exception e ) {
- //e.printStackTrace();
- return new Range();
- }
- }
- // regex pattern for a valid non-quoted attribute.
- // Attributes can be single or double quoted, or consist solely of
- // letters in the range A-Z and a-z, digits (0-9), hyphens ("-"),
- // and periods (".")
- private Pattern attributePattern = Pattern.compile( "([a-zA-Z0-9.-])*" );
- private boolean isProperAttribute(String s) {
- // could have double quotes
- if (s.startsWith("\"") && s.endsWith("\"")) {
- return true;
- }
- // or single quotes
- else if (s.startsWith("'") && s.endsWith("'")) {
- return true;
- }
- // or might be jsp
- else if (s.startsWith("<%") && (s.endsWith("%>") || s.endsWith("%")) ) {
- return true;
- }
- boolean rtn = attributePattern.matcher(s).matches();
- if (rtn == false) {
- System.out.println("bad attribute: " + s);
- }
- return rtn;
- }
- final public XmlDocument XmlDocument() throws ParseException {
- XmlDocument.ElementSequence s;
- s = ElementSequence();
- jj_consume_token(0);
- {if (true) return new XmlDocument(s);}
- throw new Error("Missing return statement in function");
- }
- final public XmlDocument.ElementSequence ElementSequence() throws ParseException {
- XmlDocument.ElementSequence s = new XmlDocument.ElementSequence();
- XmlDocument.XmlElement h;
- label_1:
- while (true) {
- switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
- case EOL:
- case COMMENT_START:
- case ENDTAG_START:
- case TAG_START:
- case DECL_START:
- case PCDATA:
- ;
- break;
- default:
- jj_la1[0] = jj_gen;
- break label_1;
- }
- h = Element();
- s.addElement(h);
- }
- {if (true) return s;}
- throw new Error("Missing return statement in function");
- }
- final public XmlDocument.XmlElement Element() throws ParseException {
- XmlDocument.XmlElement e;
- Token text;
- if (jj_2_1(2)) {
- e = Tag();
- {if (true) return e;}
- } else {
- switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
- case ENDTAG_START:
- e = EndTag();
- {if (true) return e;}
- break;
- case COMMENT_START:
- e = CommentTag();
- {if (true) return e;}
- break;
- case DECL_START:
- e = DeclTag();
- {if (true) return e;}
- break;
- default:
- jj_la1[1] = jj_gen;
- if (jj_2_2(2)) {
- jj_consume_token(TAG_START);
- text = jj_consume_token(LST_ERROR);
- {if (true) return new XmlDocument.Text("<" + text.image);}
- } else {
- switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
- case PCDATA:
- text = jj_consume_token(PCDATA);
- {if (true) return new XmlDocument.Text(text.image);}
- break;
- case EOL:
- jj_consume_token(EOL);
- {if (true) return new XmlDocument.Newline();}
- break;
- default:
- jj_la1[2] = jj_gen;
- jj_consume_token(-1);
- throw new ParseException();
- }
- }
- }
- }
- throw new Error("Missing return statement in function");
- }
- final public XmlDocument.Attribute Attribute() throws ParseException {
- XmlDocument.Attribute a;
- Token t1, t2=null;
- try {
- t1 = jj_consume_token(ATTR_NAME);
- switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
- case ATTR_EQ:
- jj_consume_token(ATTR_EQ);
- t2 = jj_consume_token(ATTR_VAL);
- break;
- default:
- jj_la1[3] = jj_gen;
- ;
- }
- if (t2 == null) {
- a = new XmlDocument.Attribute(t1.image);
- a.setStartLocation(t1.beginLine, t1.beginColumn);
- a.setEndLocation(t1.endLine, t1.endColumn + 1);
- }
- else {
- a = new XmlDocument.Attribute(t1.image, t2.image);
- a.setStartLocation(t1.beginLine, t1.beginColumn);
- a.setValueStartLocation(t2.beginLine,t2.beginColumn);
- a.setEndLocation(t2.endLine, t2.endColumn + 1);
- if (!isProperAttribute(t2.image)) {
- ParseException e = new ParseException("Parse error at line " + t2.beginLine + ", column " + t2.beginColumn + ". Attribute is improperly quoted." );
- addException(e);
- }
- }
- {if (true) return a;}
- } catch (ParseException e) {
- addException(e);
- {if (true) return null;}
- }
- throw new Error("Missing return statement in function");
- }
- final public XmlDocument.AttributeList AttributeList() throws ParseException {
- XmlDocument.AttributeList alist = new XmlDocument.AttributeList();
- XmlDocument.Attribute a;
- label_2:
- while (true) {
- switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
- case ATTR_NAME:
- ;
- break;
- default:
- jj_la1[4] = jj_gen;
- break label_2;
- }
- a = Attribute();
- alist.addAttribute(a);
- }
- {if (true) return alist;}
- throw new Error("Missing return statement in function");
- }
- final public XmlDocument.XmlElement Tag() throws ParseException {
- Token t, et;
- XmlDocument.AttributeList alist;
- Token firstToken = getToken(1);
- Token st = null;
- try {
- st = jj_consume_token(TAG_START);
- t = jj_consume_token(TAG_NAME);
- alist = AttributeList();
- switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
- case TAG_END:
- et = jj_consume_token(TAG_END);
- break;
- case TAG_SLASHEND:
- et = jj_consume_token(TAG_SLASHEND);
- break;
- default:
- jj_la1[5] = jj_gen;
- jj_consume_token(-1);
- throw new ParseException();
- }
- String tag_start = "<";
- String tag_name = "";
- if (st.image.startsWith("<") && st.image.endsWith(":")) {
- tag_start = "<";
- tag_name = st.image.substring(1) + t.image;
- }
- else {
- tag_name = t.image;
- }
- XmlDocument.Tag rtn_tag = new XmlDocument.Tag(tag_start, tag_name, alist, et.image);
- if (et.kind == TAG_SLASHEND) {
- rtn_tag.setEmpty(true);
- }
- rtn_tag.setStartLocation(st.beginLine, st.beginColumn);
- rtn_tag.setEndLocation(et.endLine, et.endColumn + 1);
- {if (true) return rtn_tag;}
- } catch (ParseException ex) {
- addException(ex);
- token_source.SwitchTo(DEFAULT);
- String s = getTokenText(firstToken, getNextToken());
- {if (true) return new XmlDocument.Text(s);}
- }
- throw new Error("Missing return statement in function");
- }
- final public XmlDocument.XmlElement EndTag() throws ParseException {
- Token t;
- Token firstToken = getToken(1);
- Token st, et;
- try {
- st = jj_consume_token(ENDTAG_START);
- t = jj_consume_token(TAG_NAME);
- et = jj_consume_token(TAG_END);
- String tag_name = "";
- if (st.image.startsWith("</") && st.image.endsWith(":")) {
- tag_name = st.image.substring(2) + t.image;
- }
- else
- tag_name = t.image;
- XmlDocument.EndTag b = new XmlDocument.EndTag(tag_name);
- b.setStartLocation(st.beginLine, st.beginColumn);
- b.setEndLocation(et.endLine, et.endColumn + 1);
- {if (true) return b;}
- } catch (ParseException ex) {
- addException(ex);
- token_source.SwitchTo(DEFAULT);
- String s = getTokenText(firstToken, getNextToken());
- {if (true) return new XmlDocument.Text(s);}
- }
- throw new Error("Missing return statement in function");
- }
- final public XmlDocument.Comment CommentTag() throws ParseException {
- Token t, comment_start, comment_end = null;
- StringBuffer s = new StringBuffer();
- try {
- comment_start = jj_consume_token(COMMENT_START);
- label_3:
- while (true) {
- switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
- case DASH:
- case COMMENT_EOL:
- case COMMENT_WORD:
- ;
- break;
- default:
- jj_la1[6] = jj_gen;
- break label_3;
- }
- switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
- case DASH:
- t = jj_consume_token(DASH);
- s.append(t.image);
- break;
- case COMMENT_EOL:
- jj_consume_token(COMMENT_EOL);
- s.append(NL);
- break;
- case COMMENT_WORD:
- t = jj_consume_token(COMMENT_WORD);
- s.append(t.image);
- break;
- default:
- jj_la1[7] = jj_gen;
- jj_consume_token(-1);
- throw new ParseException();
- }
- }
- switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
- case 0:
- jj_consume_token(0);
- break;
- case COMMENT_END:
- comment_end = jj_consume_token(COMMENT_END);
- break;
- default:
- jj_la1[8] = jj_gen;
- jj_consume_token(-1);
- throw new ParseException();
- }
- {if (true) return new XmlDocument.Comment(comment_start.image + s.toString() + (comment_end == null ? "" : comment_end.image));}
- } catch (ParseException e) {
- addException(e);
- }
- throw new Error("Missing return statement in function");
- }
- final public XmlDocument.Comment DeclTag() throws ParseException {
- Token t;
- try {
- jj_consume_token(DECL_START);
- t = jj_consume_token(DECL_ANY);
- jj_consume_token(DECL_END);
- {if (true) return new XmlDocument.Comment(t.image);}
- } catch (ParseException e) {
- addException(e);
- }
- throw new Error("Missing return statement in function");
- }
- final private boolean jj_2_1(int xla) {
- jj_la = xla; jj_lastpos = jj_scanpos = token;
- try { return !jj_3_1(); }
- catch(LookaheadSuccess ls) { return true; }
- finally { jj_save(0, xla); }
- }
- final private boolean jj_2_2(int xla) {
- jj_la = xla; jj_lastpos = jj_scanpos = token;
- try { return !jj_3_2(); }
- catch(LookaheadSuccess ls) { return true; }
- finally { jj_save(1, xla); }
- }
- final private boolean jj_3_2() {
- if (jj_scan_token(TAG_START)) return true;
- if (jj_scan_token(LST_ERROR)) return true;
- return false;
- }
- final private boolean jj_3R_4() {
- if (jj_scan_token(TAG_START)) return true;
- if (jj_scan_token(TAG_NAME)) return true;
- return false;
- }
- final private boolean jj_3_1() {
- if (jj_3R_4()) return true;
- return false;
- }
- public XmlParserTokenManager token_source;
- SimpleCharStream jj_input_stream;
- public Token token, jj_nt;
- private int jj_ntk;
- private Token jj_scanpos, jj_lastpos;
- private int jj_la;
- public boolean lookingAhead = false;
- private boolean jj_semLA;
- private int jj_gen;
- final private int[] jj_la1 = new int[9];
- static private int[] jj_la1_0;
- static private int[] jj_la1_1;
- static {
- jj_la1_0();
- jj_la1_1();
- }
- private static void jj_la1_0() {
- jj_la1_0 = new int[] {0x1f800,0xb000,0x10800,0x1000000,0x200000,0xc00000,0x80000000,0x80000000,0x40000001,};
- }
- private static void jj_la1_1() {
- jj_la1_1 = new int[] {0x0,0x0,0x0,0x0,0x0,0x0,0x3,0x3,0x0,};
- }
- final private JJCalls[] jj_2_rtns = new JJCalls[2];
- private boolean jj_rescan = false;
- private int jj_gc = 0;
- public XmlParser(java.io.InputStream stream) {
- this(stream, null);
- }
- public XmlParser(java.io.InputStream stream, String encoding) {
- try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
- token_source = new XmlParserTokenManager(jj_input_stream);
- token = new Token();
- jj_ntk = -1;
- jj_gen = 0;
- for (int i = 0; i < 9; i++) jj_la1[i] = -1;
- for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
- }
- public void ReInit(java.io.InputStream stream) {
- ReInit(stream, null);
- }
- public void ReInit(java.io.InputStream stream, String encoding) {
- try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
- token_source.ReInit(jj_input_stream);
- token = new Token();
- jj_ntk = -1;
- jj_gen = 0;
- for (int i = 0; i < 9; i++) jj_la1[i] = -1;
- for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
- }
- public XmlParser(java.io.Reader stream) {
- jj_input_stream = new SimpleCharStream(stream, 1, 1);
- token_source = new XmlParserTokenManager(jj_input_stream);
- token = new Token();
- jj_ntk = -1;
- jj_gen = 0;
- for (int i = 0; i < 9; i++) jj_la1[i] = -1;
- for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
- }
- public void ReInit(java.io.Reader stream) {
- jj_input_stream.ReInit(stream, 1, 1);
- token_source.ReInit(jj_input_stream);
- token = new Token();
- jj_ntk = -1;
- jj_gen = 0;
- for (int i = 0; i < 9; i++) jj_la1[i] = -1;
- for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
- }
- public XmlParser(XmlParserTokenManager tm) {
- token_source = tm;
- token = new Token();
- jj_ntk = -1;
- jj_gen = 0;
- for (int i = 0; i < 9; i++) jj_la1[i] = -1;
- for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
- }
- public void ReInit(XmlParserTokenManager tm) {
- token_source = tm;
- token = new Token();
- jj_ntk = -1;
- jj_gen = 0;
- for (int i = 0; i < 9; i++) jj_la1[i] = -1;
- for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
- }
- final private Token jj_consume_token(int kind) throws ParseException {
- Token oldToken;
- if ((oldToken = token).next != null) token = token.next;
- else token = token.next = token_source.getNextToken();
- jj_ntk = -1;
- if (token.kind == kind) {
- jj_gen++;
- if (++jj_gc > 100) {
- jj_gc = 0;
- for (int i = 0; i < jj_2_rtns.length; i++) {
- JJCalls c = jj_2_rtns[i];
- while (c != null) {
- if (c.gen < jj_gen) c.first = null;
- c = c.next;
- }
- }
- }
- return token;
- }
- token = oldToken;
- jj_kind = kind;
- throw generateParseException();
- }
- static private final class LookaheadSuccess extends java.lang.Error { }
- final private LookaheadSuccess jj_ls = new LookaheadSuccess();
- final private boolean jj_scan_token(int kind) {
- if (jj_scanpos == jj_lastpos) {
- jj_la--;
- if (jj_scanpos.next == null) {
- jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
- } else {
- jj_lastpos = jj_scanpos = jj_scanpos.next;
- }
- } else {
- jj_scanpos = jj_scanpos.next;
- }
- if (jj_rescan) {
- int i = 0; Token tok = token;
- while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
- if (tok != null) jj_add_error_token(kind, i);
- }
- if (jj_scanpos.kind != kind) return true;
- if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
- return false;
- }
- final public Token getNextToken() {
- if (token.next != null) token = token.next;
- else token = token.next = token_source.getNextToken();
- jj_ntk = -1;
- jj_gen++;
- return token;
- }
- final public Token getToken(int index) {
- Token t = lookingAhead ? jj_scanpos : token;
- for (int i = 0; i < index; i++) {
- if (t.next != null) t = t.next;
- else t = t.next = token_source.getNextToken();
- }
- return t;
- }
- final private int jj_ntk() {
- if ((jj_nt=token.next) == null)
- return (jj_ntk = (token.next=token_source.getNextToken()).kind);
- else
- return (jj_ntk = jj_nt.kind);
- }
- private java.util.Vector jj_expentries = new java.util.Vector();
- private int[] jj_expentry;
- private int jj_kind = -1;
- private int[] jj_lasttokens = new int[100];
- private int jj_endpos;
- private void jj_add_error_token(int kind, int pos) {
- if (pos >= 100) return;
- if (pos == jj_endpos + 1) {
- jj_lasttokens[jj_endpos++] = kind;
- } else if (jj_endpos != 0) {
- jj_expentry = new int[jj_endpos];
- for (int i = 0; i < jj_endpos; i++) {
- jj_expentry[i] = jj_lasttokens[i];
- }
- boolean exists = false;
- for (java.util.Enumeration e = jj_expentries.elements(); e.hasMoreElements();) {
- int[] oldentry = (int[])(e.nextElement());
- if (oldentry.length == jj_expentry.length) {
- exists = true;
- for (int i = 0; i < jj_expentry.length; i++) {
- if (oldentry[i] != jj_expentry[i]) {
- exists = false;
- break;
- }
- }
- if (exists) break;
- }
- }
- if (!exists) jj_expentries.addElement(jj_expentry);
- if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
- }
- }
- public ParseException generateParseException() {
- jj_expentries.removeAllElements();
- boolean[] la1tokens = new boolean[36];
- for (int i = 0; i < 36; i++) {
- la1tokens[i] = false;
- }
- if (jj_kind >= 0) {
- la1tokens[jj_kind] = true;
- jj_kind = -1;
- }
- for (int i = 0; i < 9; i++) {
- if (jj_la1[i] == jj_gen) {
- for (int j = 0; j < 32; j++) {
- if ((jj_la1_0[i] & (1<<j)) != 0) {
- la1tokens[j] = true;
- }
- if ((jj_la1_1[i] & (1<<j)) != 0) {
- la1tokens[32+j] = true;
- }
- }
- }
- }
- for (int i = 0; i < 36; i++) {
- if (la1tokens[i]) {
- jj_expentry = new int[1];
- jj_expentry[0] = i;
- jj_expentries.addElement(jj_expentry);
- }
- }
- jj_endpos = 0;
- jj_rescan_token();
- jj_add_error_token(0, 0);
- int[][] exptokseq = new int[jj_expentries.size()][];
- for (int i = 0; i < jj_expentries.size(); i++) {
- exptokseq[i] = (int[])jj_expentries.elementAt(i);
- }
- return new ParseException(token, exptokseq, tokenImage);
- }
- final public void enable_tracing() {
- }
- final public void disable_tracing() {
- }
- final private void jj_rescan_token() {
- jj_rescan = true;
- for (int i = 0; i < 2; i++) {
- try {
- JJCalls p = jj_2_rtns[i];
- do {
- if (p.gen > jj_gen) {
- jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
- switch (i) {
- case 0: jj_3_1(); break;
- case 1: jj_3_2(); break;
- }
- }
- p = p.next;
- } while (p != null);
- } catch(LookaheadSuccess ls) { }
- }
- jj_rescan = false;
- }
- final private void jj_save(int index, int xla) {
- JJCalls p = jj_2_rtns[index];
- while (p.gen > jj_gen) {
- if (p.next == null) { p = p.next = new JJCalls(); break; }
- p = p.next;
- }
- p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
- }
- static final class JJCalls {
- int gen;
- Token first;
- int arg;
- JJCalls next;
- }
- }