} else
} else if (ch == ']') { if (r.skipChar(']')) { while (r.skipChar(']')) { } if (r.skipChar('>')) throw new ParseException( "Error in text content: ]]> in text content" + formatLineNumber());  parser_state = STATE_TEXT; } }else
	public void parse(boolean NS)		throws EncodingException, EOFException, EntityException, ParseException {		// define internal variables			ns = NS;		int temp_offset = 0, length1 = 0, length2 = 0, increment = 1;		int depth = -1, attr_count = 0, ch = 0, ch_temp = 0;		int prev_ch = 0, prev2_ch = 0, parser_state = STATE_DOC_START;		//boolean has_amp = false; 		boolean is_ns = false;		encoding = FORMAT_UTF8;		boolean main_loop = true,			hasDTD = false,			hasDecl = false,			docEnd = false,			firstLT = true;		char char_temp; //holds the ' or " indicating start of attr val		boolean must_utf_8 = false;		boolean BOM_detected = false;		//long[] tag_stack = new long[256];		//long[] attr_name_array = new long[512]; // 512 attributes limit		//ASCII UTF-8 UTF-16 UTF-16BE UTF-16LE ISO-8859-1		//		//int[] scratch_buffer = new int[10];		// first check first several bytes BOM to determine if encoding is UTF16		if (XMLDoc[offset] == -2) {			increment = 2;			if (XMLDoc[offset + 1] == -1) {				offset += 2;				encoding = FORMAT_UTF_16BE;				BOM_detected = true;				r = new UTF16BEReader();				//increment = 2;			} else				throw new EncodingException("Unknown Character encoding: not UTF-16LE");		} else if (XMLDoc[offset] == -1) {			increment = 2;			if (XMLDoc[offset + 1] == -2) {				offset += 2;				//increment = 2;				encoding = FORMAT_UTF_16LE;				BOM_detected = true;				r = new UTF16LEReader();			} else				throw new EncodingException("Unknown Character encoding: not UTF-16LE");		} else if (XMLDoc[offset] == -17){		    if (XMLDoc[offset+1] == -69 && XMLDoc[offset+2]==-65){		      offset +=3;		      must_utf_8= true;		    }		    else 		    	throw new EncodingException("Unknown Character encoding: not UTF-8");		}		else if (XMLDoc[offset]==0){			if (XMLDoc[offset+1] == 0x3c 					&& XMLDoc[offset+2] == 0 					&& XMLDoc[offset+3] == 0x3f){				encoding = FORMAT_UTF_16BE;				increment = 2;				r = new UTF16BEReader();				}			else				throw new EncodingException("Unknown Character encoding: not UTF-16BE");		}		else if (XMLDoc[offset]==0x3c){			if (XMLDoc[offset+1] == 0 					&& XMLDoc[offset+2] == 0x3f 					&& XMLDoc[offset+3] == 0){				increment = 2;				encoding = FORMAT_UTF_16LE;								r = new UTF16LEReader();				}					}		// check for max file size exception		if (encoding < FORMAT_UTF_16BE) {			if ((offset + docLen) >= 1L << 30)				throw new ParseException("Other error: file size too large ");		} else {			if ((offset - 2 + docLen) >= 1L << 31)				throw new ParseException("Other error: file size too large ");		}		// enter the main finite state machine		try {			while (main_loop) {				switch (parser_state) {					case STATE_DOC_START :						if (r.getChar() == '<') {							temp_offset = offset;							// xml decl has to be right after the start of the document							if (r.skipChar('?')								&& (r.skipChar('x') || r.skipChar('X'))								&& (r.skipChar('m') || r.skipChar('M'))								&& (r.skipChar('l') || r.skipChar('L'))) {								if (r.skipChar(' ')									|| r.skipChar('\t')									|| r.skipChar('\n')									|| r.skipChar('\r')) {									ch = getCharAfterS();									temp_offset = offset;									parser_state = STATE_DEC_ATTR_NAME;									break;								} else if (r.skipChar('?'))									throw new ParseException(										"Error in XML decl: premature ending"											+ formatLineNumber());							}							offset = temp_offset;							parser_state = STATE_LT_SEEN;							break;						}						throw new ParseException(							"Other Error: XML not starting properly"								+ formatLineNumber());					case STATE_DOC_END :						docEnd = true;						ch = getCharAfterS();						// eof exception should be thrown here for premature ending						if (ch == '<') {							if (r.skipChar('?')) {								// processing instruction after end tag of root element								temp_offset = offset;								parser_state = STATE_END_PI;								break;							} else if (								r.skipChar('!')									&& r.skipChar('-')									&& r.skipChar('-')) {								// comments allowed after the end tag of the root element								temp_offset = offset;								parser_state = STATE_END_COMMENT;								break;							}						}						throw new ParseException(							"Other Error: XML not terminated properly"								+ formatLineNumber());					case STATE_LT_SEEN : //if (depth < -1)						//    throw new ParseException("Other Errors: Invalid depth");						temp_offset = offset;						ch = r.getChar();						if (XMLChar.isNameStartChar(ch)) {							//temp_offset = offset;							//length1++;							depth++;							//if (ch == ':')							//   length2 = 0;							parser_state = STATE_START_TAG;						} else {							switch (ch) {								case '/' :									parser_state = STATE_END_TAG;									break;								case '?' :									temp_offset = offset;									ch = r.getChar();									if (XMLChar.isNameStartChar(ch)) {										//temp_offset = offset;										if ((ch == 'x' || ch == 'X')											&& (r.skipChar('m')												|| r.skipChar('M'))											&& (r.skipChar('l')												|| r.skipChar('L'))) {											ch = r.getChar();											if (ch == '?'												|| XMLChar.isSpaceChar(ch))												throw new ParseException(													"Error in PI: [xX][mM][lL] not a valid PI targetname"														+ formatLineNumber());											offset = getPrevOffset();										}										parser_state = STATE_PI_TAG;										break;									}									throw new ParseException(										"Other Error: First char after <? invalid"											+ formatLineNumber());								case '!' : // three possibility (comment, CDATA, DOCTYPE)									ch = r.getChar();									switch (ch) {										case '-' :											if (r.skipChar('-')) {												temp_offset = offset;												parser_state = STATE_COMMENT;												break;											} else												throw new ParseException(													"Error in comment: Invalid char sequence to start a comment"														+ formatLineNumber());										case '[' :											if (r.skipChar('C')												&& r.skipChar('D')												&& r.skipChar('A')												&& r.skipChar('T')												&& r.skipChar('A')												&& r.skipChar('[')												&& (depth != -1)) {												temp_offset = offset;												parser_state = STATE_CDATA;												break;											} else {												if (depth == -1)													throw new ParseException(														"Error in CDATA: Wrong place for CDATA"															+ formatLineNumber());												throw new ParseException(													"Error in CDATA: Invalid char sequence for CDATA"														+ formatLineNumber());											}										case 'D' :											if (r.skipChar('O')												&& r.skipChar('C')												&& r.skipChar('T')												&& r.skipChar('Y')												&& r.skipChar('P')												&& r.skipChar('E')												&& (depth == -1)												&& !hasDTD) {												hasDTD = true;												temp_offset = offset;												parser_state = STATE_DOCTYPE;												break;											} else {												if (hasDTD == true)													throw new ParseException(														"Error for DOCTYPE: Only DOCTYPE allowed"															+ formatLineNumber());												if (depth != -1)													throw new ParseException(														"Error for DOCTYPE: DTD at wrong place"															+ formatLineNumber());												throw new ParseException(													"Error for DOCTYPE: Invalid char sequence for DOCTYPE"														+ formatLineNumber());											}										default :											throw new ParseException(												"Other Error: Unrecognized char after <!"													+ formatLineNumber());									}									break;								default :									throw new ParseException(										"Other Error: Invalid char after <"											+ formatLineNumber());							}						}						break;					case STATE_START_TAG : //name space is handled by						while (true) {							ch = r.getChar();							if (XMLChar.isNameChar(ch)) {								if (ch == ':') {									length2 = offset - temp_offset - increment;								}							} else								break;						}						length1 = offset - temp_offset - increment;						if (depth > MAX_DEPTH) {							throw new ParseException(								"Other Error: Depth exceeds MAX_DEPTH"									+ formatLineNumber());						}						//writeVTD(offset, TOKEN_STARTING_TAG, length2:length1, depth)						long x = ((long) length1 << 32) + temp_offset;						tag_stack[depth] = x;						// System.out.println(						//     " " + (temp_offset) + " " + length2 + ":" + length1 + " startingTag " + depth);						if (depth > VTDDepth)							VTDDepth = depth;						if (encoding < FORMAT_UTF_16BE){							if (length2>MAX_PREFIX_LENGTH									|| length1 > MAX_QNAME_LENGTH)								throw new ParseException(										"Token Length Error: Starting tag prefix or qname length too long"															+ formatLineNumber());							writeVTD(								(temp_offset),								(length2 << 11) | length1,								TOKEN_STARTING_TAG,								depth);}						else{							if (length2>(MAX_PREFIX_LENGTH<<1)									|| (length1 > MAX_QNAME_LENGTH<<1))								throw new ParseException(										"Token Length Error: Starting tag prefix or qname length too long"										+ formatLineNumber());							writeVTD(								(temp_offset) >> 1,								(length2 << 10) | (length1 >> 1),								TOKEN_STARTING_TAG,								depth);						}						//offset += length1;						length2 = 0;						if (XMLChar.isSpaceChar(ch)) {							ch = getCharAfterS();							if (XMLChar.isNameStartChar(ch)) {								// seen an attribute here								temp_offset = getPrevOffset();								parser_state = STATE_ATTR_NAME;								break;							}						}						if (ch == '/') {							depth--;							ch = r.getChar();						}						if (ch == '>') {							if (depth != -1) {								temp_offset = offset;								ch = getCharAfterSe(); // consume WSs								if (ch == '<') {									parser_state = STATE_LT_SEEN;									if (r.skipChar('/')) {										length1 =											offset												- temp_offset												- 2 * increment;										if (length1 > 0) {											if (encoding < FORMAT_UTF_16BE)												writeVTD(													(temp_offset),													length1,													TOKEN_CHARACTER_DATA,													depth);											else												writeVTD(													(temp_offset) >> 1,													(length1 >> 1),													TOKEN_CHARACTER_DATA,													depth);											//offset += length1;										}										parser_state = STATE_END_TAG;										break;									}								} else if (XMLChar.isContentChar(ch)) {									//temp_offset = offset;									parser_state = STATE_TEXT;								} else if (ch == '&') {									//has_amp = true;									//temp_offset = offset;									entityIdentifier();									parser_state = STATE_TEXT;								} else									throw new ParseException(										"Error in text content: Invalid char"											+ formatLineNumber());							} else {								parser_state = STATE_DOC_END;							}							break;						}						throw new ParseException(							"Starting tag Error: Invalid char in starting tag"								+ formatLineNumber());					case STATE_END_TAG :						temp_offset = offset;						while (true) {							ch = r.getChar();							if (!XMLChar.isNameChar(ch)) {								break;							}						}						length1 = offset - temp_offset - increment;						int sos = (int) tag_stack[depth];						int sl = (int) (tag_stack[depth] >> 32);						if (sl == length1) {							for (int i = 0; i < length1; i++) {								if (XMLDoc[sos + i] != XMLDoc[temp_offset + i])									throw new ParseException(										"Ending tag error: Start/ending tag mismatch"											+ formatLineNumber());								// System.out.println(								//     " " + (char) XMLDoc[sos + i] + "<==> " + (char) XMLDoc[temp_offset + i]);							}						} else							throw new ParseException(								"Ending tag error: Start/ending tag mismatch, length different"									+ formatLineNumber());						//System.out.println(" " + temp_offset + " " + length1 + " ending tag " + depth);						//writeVTD(temp_offset, length1, TOKEN_ENDING_TAG, depth);						depth--;						if (XMLChar.isSpaceChar(ch)) {							ch = getCharAfterS();						}						if (ch == '>') {							if (depth != -1) {								temp_offset = offset;								ch = getCharAfterS();								if (ch == '<')									parser_state = STATE_LT_SEEN;								else if (XMLChar.isContentChar(ch)) {									parser_state = STATE_TEXT;								} else if (ch == '&') {									//has_amp = true;									entityIdentifier();									parser_state = STATE_TEXT;								} else									throw new ParseException(										"Other Error: Invalid char in xml"											+ formatLineNumber());							} else								parser_state = STATE_DOC_END;							break;						} else							throw new ParseException(								"Other Error: Invalid char in ending tag"									+ formatLineNumber());					case STATE_UNRECORDED_TEXT :						break;					case STATE_PI_TAG :						while (true) {							ch = r.getChar();							if (!XMLChar.isNameChar(ch))								break;						}						length1 = offset - temp_offset - increment;						/*System.out.println(						    ((char) XMLDoc[temp_offset])						        + " "						        + (temp_offset)						        + " "						        + length1						        + " PI Target "						        + depth); */						if (encoding < FORMAT_UTF_16BE){							if (length1 > MAX_TOKEN_LENGTH)								  throw new ParseException("Token Length Error:"											  +" PI name too long (>0xfffff)"											  + formatLineNumber());							writeVTD(								(temp_offset),								length1,								TOKEN_PI_NAME,								depth);						}						else{							if (length1 > (MAX_TOKEN_LENGTH<<1))								  throw new ParseException("Token Length Error:"											  +" PI name too long (>0xfffff)"											  + formatLineNumber());							writeVTD(								(temp_offset) >> 1,								(length1 >> 1),								TOKEN_PI_NAME,								depth);						}						//length1 = 0;						temp_offset = offset;						if (XMLChar.isSpaceChar(ch)) {							ch = r.getChar();						}						if (ch == '?') {							if (r.skipChar('>')) {								temp_offset = offset;								ch = getCharAfterSe();								if (ch == '<') {									parser_state = STATE_LT_SEEN;								} else if (XMLChar.isContentChar(ch)) {									parser_state = STATE_TEXT;								} else if (ch == '&') {									//has_amp = true;									entityIdentifier();									parser_state = STATE_TEXT;								} else									throw new ParseException(										"Error in text content: Invalid char"											+ formatLineNumber());								break;							} else								throw new ParseException(									"Error in PI: invalid termination sequence"										+ formatLineNumber());						}						parser_state = STATE_PI_VAL;						break;						//throw new ParseException("Error in PI: Invalid char");					case STATE_PI_VAL :						//temp_offset = offset;						while (true) {							if (XMLChar.isValidChar(ch)) {								//System.out.println(""+(char)ch);								if (ch == '?')									if (r.skipChar('>')) {										break;									} else										throw new ParseException(											"Error in PI: invalid termination sequence for PI"												+ formatLineNumber());							} else								throw new ParseException(									"Errors in PI: Invalid char in PI val"										+ formatLineNumber());							ch = r.getChar();						}						length1 = offset - temp_offset - 2 * increment;						/*System.out.println(						    ((char) XMLDoc[temp_offset])						        + " "						        + (temp_offset)						        + " "						        + length1						        + " PI val "						        + depth);*/						if (encoding < FORMAT_UTF_16BE){							if (length1 > MAX_TOKEN_LENGTH)								  throw new ParseException("Token Length Error:"											  +" PI val too long (>0xfffff)"											  + formatLineNumber());							writeVTD(temp_offset,									length1, 									TOKEN_PI_VAL,									depth);						}						else{							if (length1 > (MAX_TOKEN_LENGTH<<1))								  throw new ParseException("Token Length Error:"											  +" PI val too long (>0xfffff)"											  + formatLineNumber());							writeVTD(								temp_offset >> 1,								length1 >> 1,								TOKEN_PI_VAL,								depth);						}						//length1 = 0;						temp_offset = offset;						ch = getCharAfterSe();												if (ch == '<') {							parser_state = STATE_LT_SEEN;						} else if (XMLChar.isContentChar(ch)) {							//temp_offset = offset;							parser_state = STATE_TEXT;						} else if (ch == '&') {							//has_amp = true;							//temp_offset = offset;							entityIdentifier();							parser_state = STATE_TEXT;						} else							throw new ParseException(								"Error in text content: Invalid char"									+ formatLineNumber());						break;					case STATE_DEC_ATTR_NAME :						if (ch == 'v'							&& r.skipChar('e')							&& r.skipChar('r')							&& r.skipChar('s')							&& r.skipChar('i')							&& r.skipChar('o')							&& r.skipChar('n')) {							ch = getCharAfterS();							if (ch == '=') {								/*System.out.println(								    " " + (temp_offset - 1) + " " + 7 + " dec attr name version " + depth);*/								if (encoding < FORMAT_UTF_16BE)									writeVTD(										temp_offset - 1,										7,										TOKEN_DEC_ATTR_NAME,										depth);								else									writeVTD(										(temp_offset - 2) >> 1,										7,										TOKEN_DEC_ATTR_NAME,										depth);							} else								throw new ParseException(									"XML decl error: Invalid char"										+ formatLineNumber());						} else							throw new ParseException(								"XML decl error: should be version"									+ formatLineNumber());						ch_temp = getCharAfterS();						if (ch_temp != '\'' && ch_temp != '"')							throw new ParseException(								"XML decl error: Invalid char to start attr name"									+ formatLineNumber());						temp_offset = offset;						// support 1.0 or 1.1						if (r.skipChar('1')							&& r.skipChar('.')							&& (r.skipChar('0') || r.skipChar('1'))) {							/*System.out.println(							    " " + temp_offset + " " + 3 + " dec attr val (version)" + depth);*/							if (encoding < FORMAT_UTF_16BE)								writeVTD(									temp_offset,									3,									TOKEN_DEC_ATTR_VAL,									depth);							else								writeVTD(									temp_offset >> 1,									3,									TOKEN_DEC_ATTR_VAL,									depth);						} else							throw new ParseException(								"XML decl error: Invalid version(other than 1.0 or 1.1) detected"									+ formatLineNumber());						if (!r.skipChar(ch_temp))							throw new ParseException(								"XML decl error: version not terminated properly"									+ formatLineNumber());						ch = r.getChar();						//? space or e 						if (XMLChar.isSpaceChar(ch)) {							ch = getCharAfterS();							temp_offset = offset - increment;							if (ch == 'e') {								if (r.skipChar('n')									&& r.skipChar('c')									&& r.skipChar('o')									&& r.skipChar('d')									&& r.skipChar('i')									&& r.skipChar('n')									&& r.skipChar('g')) {									ch = r.getChar();									if (XMLChar.isSpaceChar(ch))										ch = getCharAfterS();									if (ch == '=') {										/*System.out.println(										    " " + (temp_offset) + " " + 8 + " dec attr name (encoding) " + depth);*/										if (encoding < FORMAT_UTF_16BE)											writeVTD(												temp_offset,												8,												TOKEN_DEC_ATTR_NAME,												depth);										else											writeVTD(												temp_offset >> 1,												8,												TOKEN_DEC_ATTR_NAME,												depth);									} else										throw new ParseException(											"XML decl error: Invalid char"												+ formatLineNumber());									ch_temp = getCharAfterS();									if (ch_temp != '"' && ch_temp != '\'')										throw new ParseException(											"XML decl error: Invalid char to start attr name"												+ formatLineNumber());									temp_offset = offset;									ch = r.getChar();									switch (ch) {										case 'a' :										case 'A' :											if ((r.skipChar('s')												|| r.skipChar('S'))												&& (r.skipChar('c')													|| r.skipChar('C'))												&& (r.skipChar('i')													|| r.skipChar('I'))												&& (r.skipChar('i')													|| r.skipChar('I'))												&& r.skipChar(ch_temp)) {																								if (encoding != FORMAT_UTF_16LE													&& encoding														!= FORMAT_UTF_16BE) {													if (must_utf_8)														throw new EncodingException("Can't switch from UTF-8"																+ formatLineNumber());													encoding = FORMAT_ASCII;													r = new ASCIIReader();													/*System.out.println(													    " " + (temp_offset) + " " + 5 + " dec attr val (encoding) " + depth);*/													if (encoding														< FORMAT_UTF_16BE)														writeVTD(															temp_offset,															5,															TOKEN_DEC_ATTR_VAL,															depth);													else														writeVTD(															temp_offset >> 1,															5,															TOKEN_DEC_ATTR_VAL,															depth);													break;												} else													throw new ParseException(														"XML decl error: Can't switch encoding to ASCII"															+ formatLineNumber());											}											throw new ParseException(												"XML decl error: Invalid Encoding"													+ formatLineNumber());										case 'i' :										case 'I' :											if ((r.skipChar('s')												|| r.skipChar('S'))												&& (r.skipChar('o')													|| r.skipChar('O'))												&& r.skipChar('-')												&& r.skipChar('8')												&& r.skipChar('8')												&& r.skipChar('5')												&& r.skipChar('9')												&& r.skipChar('-')												&& r.skipChar('1')												&& r.skipChar(ch_temp)) {												if (encoding != FORMAT_UTF_16LE													&& encoding														!= FORMAT_UTF_16BE) {													if (must_utf_8)														throw new EncodingException("Can't switch from UTF-8"																+ formatLineNumber());													encoding = FORMAT_ISO_8859;													r = new ISO8859Reader();													/*System.out.println(													    " " + (temp_offset) + " " + 10 + " dec attr val (encoding) " + depth);*/													if (encoding														< FORMAT_UTF_16BE)														writeVTD(															temp_offset,															10,															TOKEN_DEC_ATTR_VAL,															depth);													else														writeVTD(															temp_offset >> 1,															10,															TOKEN_DEC_ATTR_VAL,															depth);													break;												} else													throw new ParseException(														"XML decl error: Can't switch encoding to ISO-8859"															+ formatLineNumber());											}											throw new ParseException(												"XML decl error: Invalid Encoding"													+ formatLineNumber());										case 'u' :										case 'U' :											if ((r.skipChar('s')												|| r.skipChar('S')))												if (r.skipChar('-')													&& (r.skipChar('a')														|| r.skipChar('A'))													&& (r.skipChar('s')														|| r.skipChar('S'))													&& (r.skipChar('c')														|| r.skipChar('C'))													&& (r.skipChar('i')														|| r.skipChar('I'))													&& (r.skipChar('i')														|| r.skipChar('I'))													&& r.skipChar(ch_temp)) {													if (encoding														!= FORMAT_UTF_16LE														&& encoding															!= FORMAT_UTF_16BE) {														if (must_utf_8)															throw new EncodingException("Can't switch from UTF-8"																	+ formatLineNumber());														encoding = FORMAT_ASCII;														r = new ASCIIReader();														//System.out.println(														//    " " + (temp_offset) + " " + 5 + " dec attr val (encoding) " + depth);														if (encoding															< FORMAT_UTF_16BE)															writeVTD(																temp_offset,																5,																TOKEN_DEC_ATTR_VAL,																depth);														else															writeVTD(																temp_offset																	>> 1,																5,																TOKEN_DEC_ATTR_VAL,																depth);														break;													} else														throw new ParseException(															"XML decl error: Can't switch encoding to US-ASCII"																+ formatLineNumber());												} else													throw new ParseException(														"XML decl error: Invalid Encoding"															+ formatLineNumber());											if ((r.skipChar('t')												|| r.skipChar('T'))												&& (r.skipChar('f')													|| r.skipChar('F'))												&& r.skipChar('-')) {												if (r.skipChar('8')													&& r.skipChar(ch_temp)) {													if (encoding														!= FORMAT_UTF_16LE														&& encoding															!= FORMAT_UTF_16BE) {														//encoding = FORMAT_UTF8;														/*System.out.println(														    " " + (temp_offset) + " " + 5 + " dec attr val (encoding) " + depth);*/														if (encoding															< FORMAT_UTF_16BE)															writeVTD(																temp_offset,																5,																TOKEN_DEC_ATTR_VAL,																depth);														else															writeVTD(																temp_offset																	>> 1,																5,																TOKEN_DEC_ATTR_VAL,																depth);														break;													} else														throw new ParseException(															"XML decl error: Can't switch encoding to UTF-8"																+ formatLineNumber());												}												if (r.skipChar('1')													&& r.skipChar('6')) {													if (r.skipChar(ch_temp)) {														if (encoding															== FORMAT_UTF_16LE															|| encoding																== FORMAT_UTF_16BE) {															if (!BOM_detected)																throw new EncodingException("BOM not detected for UTF-16"																		+ formatLineNumber());															/*System.out.println(															    " " + (temp_offset) + " " + 6 + " dec attr val (encoding) " + depth);*/															if (encoding																< FORMAT_UTF_16BE)																writeVTD(																	temp_offset,																	6,																	TOKEN_DEC_ATTR_VAL,																	depth);															else																writeVTD(																	temp_offset																		>> 1,																	6,																	TOKEN_DEC_ATTR_VAL,																	depth);															break;														}														throw new ParseException(															"XML decl error: Can't switch encoding to UTF-16"																+ formatLineNumber());													} else if (														(r.skipChar('l')															|| r.skipChar('L'))															&& (r.skipChar('e')																|| r.skipChar(																	'E'))															&& r.skipChar(																ch_temp)) {														if (encoding															== FORMAT_UTF_16LE) {															/*System.out.println(															    " " + (temp_offset) + " " + 7 + " dec attr val (encoding) " + depth);*/															if (encoding																< FORMAT_UTF_16BE)																writeVTD(																	temp_offset,																	7,																	TOKEN_DEC_ATTR_VAL,																	depth);															else																writeVTD(																	temp_offset																		>> 1,																	7,																	TOKEN_DEC_ATTR_VAL,																	depth);															break;														}														throw new ParseException(															"XML del error: Can't switch encoding to UTF-16LE"																+ formatLineNumber());													} else if (														(r.skipChar('b')															|| r.skipChar('B'))															&& (r.skipChar('e')																|| r.skipChar(																	'E'))															&& r.skipChar(																ch_temp)) {														if (encoding															== FORMAT_UTF_16BE) {															/*System.out.println(															    " " + (temp_offset) + " " + 7 + " dec attr val (encoding) " + depth);*/															if (encoding																< FORMAT_UTF_16BE)																writeVTD(																	temp_offset,																	7,																	TOKEN_DEC_ATTR_VAL,																	depth);															else																writeVTD(																	temp_offset																		>> 1,																	7,																	TOKEN_DEC_ATTR_VAL,																	depth);															break;														}														throw new ParseException(															"XML del error: Can't swtich encoding to UTF-16BE"																+ formatLineNumber());													}													throw new ParseException(														"XML decl error: Invalid encoding"															+ formatLineNumber());												}											}										default :											throw new ParseException(												"XML decl Error: invalid encoding"													+ formatLineNumber());									}									ch = r.getChar();									if (XMLChar.isSpaceChar(ch))										ch = getCharAfterS();									temp_offset = offset - increment;								} else									throw new ParseException(										"XML decl Error: Invalid char"											+ formatLineNumber());							}							if (ch == 's') {								if (r.skipChar('t')									&& r.skipChar('a')									&& r.skipChar('n')									&& r.skipChar('d')									&& r.skipChar('a')									&& r.skipChar('l')									&& r.skipChar('o')									&& r.skipChar('n')									&& r.skipChar('e')) {									ch = getCharAfterS();									if (ch != '=')										throw new ParseException(											"XML decl error: Invalid char"												+ formatLineNumber());									/*System.out.println(									    " " + temp_offset + " " + 3 + " dec attr name (standalone) " + depth);*/									if (encoding < FORMAT_UTF_16BE)										writeVTD(											temp_offset,											10,											TOKEN_DEC_ATTR_NAME,											depth);									else										writeVTD(											temp_offset >> 1,											10,											TOKEN_DEC_ATTR_NAME,											depth);									ch_temp = getCharAfterS();									temp_offset = offset;									if (ch_temp != '"' && ch_temp != '\'')										throw new ParseException(											"XML decl error: Invalid char to start attr name"												+ formatLineNumber());									ch = r.getChar();									if (ch == 'y') {										if (r.skipChar('e')											&& r.skipChar('s')											&& r.skipChar(ch_temp)) {											/*System.out.println(											    " " + (temp_offset) + " " + 3 + " dec attr val (standalone) " + depth);*/											if (encoding < FORMAT_UTF_16BE)												writeVTD(													temp_offset,													3,													TOKEN_DEC_ATTR_VAL,													depth);											else												writeVTD(													temp_offset >> 1,													3,													TOKEN_DEC_ATTR_VAL,													depth);										} else											throw new ParseException(												"XML decl error: invalid val for standalone"													+ formatLineNumber());									} else if (ch == 'n') {										if (r.skipChar('o')											&& r.skipChar(ch_temp)) {											/*System.out.println(											    " " + (temp_offset) + " " + 2 + " dec attr val (standalone)" + depth);*/											if (encoding < FORMAT_UTF_16BE)												writeVTD(													temp_offset,													2,													TOKEN_DEC_ATTR_VAL,													depth);											else												writeVTD(													temp_offset >> 1,													2,													TOKEN_DEC_ATTR_VAL,													depth);										} else											throw new ParseException(												"XML decl error: invalid val for standalone"													+ formatLineNumber());									} else										throw new ParseException(											"XML decl error: invalid val for standalone"												+ formatLineNumber());								} else									throw new ParseException(										"XML decl error" + formatLineNumber());								ch = r.getChar();								if (XMLChar.isSpaceChar(ch))									ch = getCharAfterS();							}						}						if (ch == '?' && r.skipChar('>')) {							temp_offset = offset;							ch = getCharAfterS();							if (ch == '<') {								parser_state = STATE_LT_SEEN;							} else								throw new ParseException(									"Other Error: Invalid Char in XML"										+ formatLineNumber());						} else							throw new ParseException(								"XML decl Error: Invalid termination sequence"									+ formatLineNumber());						break;					case STATE_COMMENT :						while (true) {							ch = r.getChar();							if (XMLChar.isValidChar(ch)) {								if (ch == '-' && r.skipChar('-')) {									length1 =										offset - temp_offset - 2 * increment;									break;								}							} else								throw new ParseException(									"Error in comment: Invalid Char"										+ formatLineNumber());						}						if (r.getChar() == '>') {							//System.out.println(" " + (temp_offset) + " " + length1 + " comment " + depth);							if (encoding < FORMAT_UTF_16BE)								writeVTD(									temp_offset,									length1,									TOKEN_COMMENT,									depth);							else								writeVTD(									temp_offset >> 1,									length1 >> 1,									TOKEN_COMMENT,									depth);							//length1 = 0;							temp_offset = offset;							ch = getCharAfterSe();							if (ch == '<') {								parser_state = STATE_LT_SEEN;							} else if (XMLChar.isContentChar(ch)) {								//temp_offset = offset;								parser_state = STATE_TEXT;							} else if (ch == '&') {								//has_amp = true;								//temp_offset = offset;								entityIdentifier();								parser_state = STATE_TEXT;							} else								throw new ParseException(									"Error in text content: Invalid char"										+ formatLineNumber());							break;						} else							throw new ParseException(								"Error in comment: Invalid terminating sequence"									+ formatLineNumber());					case STATE_CDATA :						while (true) {							ch = r.getChar();							if (XMLChar.isValidChar(ch)) {								if (ch == ']' && r.skipChar(']')) {									while (r.skipChar(']'));									if (r.skipChar('>')) {										break;									} else										throw new ParseException(											"Error in CDATA: Invalid termination sequence"												+ formatLineNumber());								}							} else								throw new ParseException(									"Error in CDATA: Invalid Char"										+ formatLineNumber());						}						length1 = offset - temp_offset - 3 * increment;						if (encoding < FORMAT_UTF_16BE){														writeVTD(								temp_offset,								length1,								TOKEN_CDATA_VAL,								depth);						}						else {														writeVTD(								temp_offset >> 1,								length1 >> 1,								TOKEN_CDATA_VAL,								depth);						}						//System.out.println(" " + (temp_offset) + " " + length1 + " CDATA " + depth);						ch = getCharAfterSe();						if (ch == '<') {							parser_state = STATE_LT_SEEN;						} else if (XMLChar.isContentChar(ch)) {							temp_offset = offset;							parser_state = STATE_TEXT;						} else if (ch == '&') {							//has_amp = true;							temp_offset = offset;							entityIdentifier();							parser_state = STATE_TEXT;							//temp_offset = offset;						} else							throw new ParseException("Other Error: Invalid char in xml");						break;					case STATE_DOCTYPE :						int z = 1;						while (true) {							ch = r.getChar();							if (XMLChar.isValidChar(ch)) {								if (ch == '>')									z--;								else if (ch == '<')									z++;								if (z == 0)									break;							} else								throw new ParseException(									"Error in DOCTYPE: Invalid char"										+ formatLineNumber());						}						length1 = offset - temp_offset - increment;						/*System.out.println(						    " " + (temp_offset) + " " + length1 + " DOCTYPE val " + depth);*/						if (encoding < FORMAT_UTF_16BE){							if (length1 > MAX_TOKEN_LENGTH)								  throw new ParseException("Token Length Error:"											  +" DTD val too long (>0xfffff)"											  + formatLineNumber());							writeVTD(								temp_offset,								length1,								TOKEN_DTD_VAL,								depth);						}						else{							if (length1 > (MAX_TOKEN_LENGTH<<1))								  throw new ParseException("Token Length Error:"											  +" DTD val too long (>0xfffff)"											  + formatLineNumber());							writeVTD(								temp_offset >> 1,								length1 >> 1,								TOKEN_DTD_VAL,								depth);						}						ch = getCharAfterS();						if (ch == '<') {							parser_state = STATE_LT_SEEN;						} else							throw new ParseException(								"Other Error: Invalid char in xml"									+ formatLineNumber());						break;					case STATE_TEXT :						if (depth == -1)							throw new ParseException(								"Error in text content: Char data at the wrong place"									+ formatLineNumber());						while (true) {							ch = r.getChar();							if (XMLChar.isContentChar(ch)) {							} else if (ch == '&') {								//has_amp = true;								if (!XMLChar.isValidChar(entityIdentifier()))									throw new ParseException(										"Error in text content: Invalid char in text content "											+ formatLineNumber());								//parser_state = STATE_TEXT;							} else if (ch == '<') {								break;							} else if (ch == ']') {								if (r.skipChar(']')) {									while (r.skipChar(']')) {									}									if (r.skipChar('>'))										throw new ParseException(											"Error in text content: ]]> in text content"												+ formatLineNumber());								}							} else								throw new ParseException(									"Error in text content: Invalid char in text content "										+ formatLineNumber());						}						length1 = offset - increment - temp_offset;						if (encoding < FORMAT_UTF_16BE)							writeVTD(								temp_offset,								length1,								TOKEN_CHARACTER_DATA,								depth);						else							writeVTD(								temp_offset >> 1,								length1 >> 1,								TOKEN_CHARACTER_DATA,								depth);						//has_amp = true;						parser_state = STATE_LT_SEEN;						break;					case STATE_ATTR_NAME :						if (ch == 'x') {							if (r.skipChar('m')								&& r.skipChar('l')								&& r.skipChar('n')								&& r.skipChar('s')) {								ch = r.getChar();								if (ch == '='									|| XMLChar.isSpaceChar(ch)									|| ch == ':') {									is_ns = true; //break;								}							}						}						while (true) {							if (XMLChar.isNameChar(ch)) {								if (ch == ':') {									length2 = offset - temp_offset - increment;								}								ch = r.getChar();							} else								break;						}						length1 = getPrevOffset() - temp_offset;						// check for uniqueness here						boolean unique = true;						boolean unequal;						for (int i = 0; i < attr_count; i++) {							unequal = false;							int prevLen = (int) attr_name_array[i];							if (length1 == prevLen) {								int prevOffset =									(int) (attr_name_array[i] >> 32);								for (int j = 0; j < prevLen; j++) {									if (XMLDoc[prevOffset + j]										!= XMLDoc[temp_offset + j]) {										unequal = true;										break;									}								}							} else								unequal = true;							unique = unique && unequal;						}						if (!unique && attr_count != 0)							throw new ParseException(								"Error in attr: Attr name not unique"									+ formatLineNumber());						unique = true;						if (attr_count < attr_name_array.length) {							//System.out.println("attr_count is "+ attr_count);							//System.out.println("attr_name_array.length is " +attr_name_array.length);							attr_name_array[attr_count] =								((long) (temp_offset) << 32) + length1;							attr_count++;						} else // grow the attr_name_array by 16							{							long[] temp_array = attr_name_array;							System.out.println(								"size increase from "									+ temp_array.length									+ "  to "									+ (attr_count + 16));							attr_name_array =								new long[attr_count + ATTR_NAME_ARRAY_SIZE];							for (int i = 0; i < attr_count; i++) {								attr_name_array[i] = temp_array[i];							}							attr_name_array[attr_count] =								((long) (temp_offset) << 32) + length1;							attr_count++;						}						// after checking, write VTD						if (is_ns) {							if (encoding < FORMAT_UTF_16BE){								if (length2>MAX_PREFIX_LENGTH										|| length1 > MAX_QNAME_LENGTH)									throw new ParseException(											"Token Length Error: Attr ns prefix or qname length too long"											+ formatLineNumber());								writeVTD(									temp_offset,									(length2 << 11) | length1,									TOKEN_ATTR_NS,									depth);							}							else{								if (length2>(MAX_PREFIX_LENGTH << 1)										|| length1 > (MAX_QNAME_LENGTH <<1))									throw new ParseException(											"Token Length Error: Attr ns prefix or qname length too long"											+ formatLineNumber());								writeVTD(									temp_offset >> 1,									(length2 << 10) | (length1 >> 1),									TOKEN_ATTR_NS,									depth);							}							is_ns = false;						} else {							if (encoding < FORMAT_UTF_16BE){								if (length2>MAX_PREFIX_LENGTH										|| length1 > MAX_QNAME_LENGTH)									throw new ParseException(											"Token Length Error: attr name prefix or qname length too long"											+ formatLineNumber());								writeVTD(									temp_offset,									(length2 << 11) | length1,									TOKEN_ATTR_NAME,									depth);							}							else{								if (length2>(MAX_PREFIX_LENGTH<<1)										|| length1 > (MAX_QNAME_LENGTH<<1))									throw new ParseException(											"Token Length Error: Attr name prefix or qname length too long" 											+ formatLineNumber());								writeVTD(									temp_offset >> 1,									(length2 << 10) | (length1 >> 1),									TOKEN_ATTR_NAME,									depth);							}						}						/*System.out.println(						    " " + temp_offset + " " + length2 + ":" + length1 + " attr name " + depth);*/						length2 = 0;						if (XMLChar.isSpaceChar(ch)) {							ch = getCharAfterS();						}						if (ch != '=')							throw new ParseException(								"Error in attr: invalid char"									+ formatLineNumber());						ch_temp = getCharAfterS();						if (ch_temp != '"' && ch_temp != '\'')							throw new ParseException(								"Error in attr: invalid char (should be ' or \" )"									+ formatLineNumber());						temp_offset = offset;						parser_state = STATE_ATTR_VAL;						break;					case STATE_ATTR_VAL :						while (true) {							ch = r.getChar();							if (XMLChar.isValidChar(ch) && ch != '<') {								if (ch == ch_temp)									break;								if (ch == '&') {									// as in vtd spec, we mark attr val with entities									if (!XMLChar										.isValidChar(entityIdentifier())) {										throw new ParseException(											"Error in attr: Invalid XML char"												+ formatLineNumber());									}								}							} else								throw new ParseException(									"Error in attr: Invalid XML char"										+ formatLineNumber());						}						length1 = offset - temp_offset - increment;						if (encoding < FORMAT_UTF_16BE){							if (length1 > MAX_TOKEN_LENGTH)								  throw new ParseException("Token Length Error:"											  +" Attr val too long (>0xfffff)"											  + formatLineNumber());							writeVTD(								temp_offset,								length1,								TOKEN_ATTR_VAL,								depth);						}						else{							if (length1 > MAX_TOKEN_LENGTH <<1)								  throw new ParseException("Token Length Error:"											  +" Attr val too long (>0xfffff)"											  + formatLineNumber());							writeVTD(								temp_offset >> 1,								length1 >> 1,								TOKEN_ATTR_VAL,								depth);						}						//System.out.println(" " + temp_offset + " " + length1 + " attr val " + depth);						//has_amp = false;						//length1 = 0;						ch = r.getChar();						if (XMLChar.isSpaceChar(ch)) {							ch = getCharAfterS();							if (XMLChar.isNameStartChar(ch)) {								temp_offset = offset - increment;								parser_state = STATE_ATTR_NAME;								break;							}						}						if (ch == '/') {							depth--;							ch = r.getChar();						}						if (ch == '>') {							attr_count = 0;							if (depth != -1) {								temp_offset = offset;								ch = getCharAfterSe();								if (ch == '<') {									parser_state = STATE_LT_SEEN;								} else if (XMLChar.isContentChar(ch)) {									//temp_offset = offset;									parser_state = STATE_TEXT;								} else if (ch == '&') {									//has_amp = true;									//temp_offset = offset;									entityIdentifier();									parser_state = STATE_TEXT;								} else									throw new ParseException(										"Error in text content: Invalid char"											+ formatLineNumber());							} else {								parser_state = STATE_DOC_END;							}							break;						}						throw new ParseException(							"Starting tag Error: Invalid char in starting tag"								+ formatLineNumber());					case STATE_END_PI :						ch = r.getChar();						if (XMLChar.isNameStartChar(ch)) {							if ((ch == 'x' || ch == 'X')								&& (r.skipChar('m') || r.skipChar('M'))								&& (r.skipChar('l') && r.skipChar('L'))) {								//temp_offset = offset;								ch = r.getChar();								if (XMLChar.isSpaceChar(ch) || ch == '?')									throw new ParseException(										"Error in PI: [xX][mM][lL] not a valid PI target"											+ formatLineNumber());								//offset = temp_offset;							}							while (true) {								//ch = getChar();								if (!XMLChar.isNameChar(ch)) {									break;								}								ch = r.getChar();							}							length1 = offset - temp_offset - increment;							/*System.out.println(							    ""							        + (char) XMLDoc[temp_offset]							        + " "							        + (temp_offset)							        + " "							        + length1							        + " PI Target "							        + depth);*/							if (encoding < FORMAT_UTF_16BE){								if (length1 > MAX_TOKEN_LENGTH)									  throw new ParseException("Token Length Error:"												  +"PI name too long (>0xfffff)"												  + formatLineNumber());								writeVTD(									temp_offset,									length1,									TOKEN_PI_NAME,									depth);							}							else{								if (length1 > (MAX_TOKEN_LENGTH<<1))								  throw new ParseException("Token Length Error:"										  +"PI name too long (>0xfffff)"										  + formatLineNumber());								writeVTD(									temp_offset >> 1,									length1 >> 1,									TOKEN_PI_NAME,									depth);							}							//length1 = 0;							temp_offset = offset;							if (XMLChar.isSpaceChar(ch)) {								ch = getCharAfterS();								while (true) {									if (XMLChar.isValidChar(ch)) {										if (ch == '?')											if (r.skipChar('>')) {												parser_state = STATE_DOC_END;												break;											} else												throw new ParseException(													"Error in PI: invalid termination sequence"														+ formatLineNumber());									} else										throw new ParseException(											"Error in PI: Invalid char in PI val"												+ formatLineNumber());									ch = r.getChar();								}								length1 = offset - temp_offset - 2 * increment;								if (encoding < FORMAT_UTF_16BE){									if (length1 > MAX_TOKEN_LENGTH)										  throw new ParseException("Token Length Error:"													  +"PI val too long (>0xfffff)"													  + formatLineNumber());									writeVTD(										temp_offset,										length1,										TOKEN_PI_VAL,										depth);								}								else{									if (length1 > (MAX_TOKEN_LENGTH<<1))										  throw new ParseException("Token Length Error:"													  +"PI val too long (>0xfffff)"													  + formatLineNumber());									writeVTD(										temp_offset >> 1,										length1 >> 1,										TOKEN_PI_VAL,										depth);								}								//System.out.println(" " + temp_offset + " " + length1 + " PI val " + depth);							} else {								if ((ch == '?') && r.skipChar('>')) {									parser_state = STATE_DOC_END;								} else									throw new ParseException(										"Error in PI: invalid termination sequence"											+ formatLineNumber());							}							//parser_state = STATE_DOC_END;						} else							throw new ParseException("Error in PI: invalid char in PI target"									+formatLineNumber());						break;					case STATE_END_COMMENT :						while (true) {							ch = r.getChar();							if (XMLChar.isValidChar(ch)) {								if (ch == '-' && r.skipChar('-')) {									length1 =										offset - temp_offset - 2 * increment;									break;								}							} else								throw new ParseException(									"Error in comment: Invalid Char"										+ formatLineNumber());						}						if (r.getChar() == '>') {							//System.out.println(" " + temp_offset + " " + length1 + " comment " + depth);							if (encoding < FORMAT_UTF_16BE)								writeVTD(									temp_offset,									length1,									TOKEN_COMMENT,									depth);							else								writeVTD(									temp_offset >> 1,									length1 >> 1,									TOKEN_COMMENT,									depth);							//length1 = 0;							parser_state = STATE_DOC_END;							break;						}						throw new ParseException(							"Error in comment: '-->' expected"								+ formatLineNumber());					default :						throw new ParseException(							"Other error: invalid parser state"								+formatLineNumber());				}			}		} catch (EOFException e) {			if (parser_state != STATE_DOC_END)				throw e;		}	}