KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > StAXDocumentParser


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.stax;
41
42 import com.sun.xml.fastinfoset.Decoder;
43 import com.sun.xml.fastinfoset.DecoderStateTables;
44 import com.sun.xml.fastinfoset.EncodingConstants;
45 import com.sun.xml.fastinfoset.QualifiedName;
46 import com.sun.xml.fastinfoset.algorithm.BuiltInEncodingAlgorithmFactory;
47 import com.sun.xml.fastinfoset.sax.AttributesHolder;
48 import com.sun.xml.fastinfoset.util.CharArray;
49 import com.sun.xml.fastinfoset.util.CharArrayString;
50 import com.sun.xml.fastinfoset.util.XMLChar;
51 import com.sun.xml.fastinfoset.util.EventLocation;
52 import java.io.IOException JavaDoc;
53 import java.io.InputStream JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.NoSuchElementException JavaDoc;
56 import javax.xml.namespace.NamespaceContext JavaDoc;
57 import javax.xml.namespace.QName JavaDoc;
58 import javax.xml.stream.Location;
59 import javax.xml.stream.XMLStreamException;
60 import javax.xml.stream.XMLStreamReader;
61 import org.jvnet.fastinfoset.EncodingAlgorithm;
62 import org.jvnet.fastinfoset.EncodingAlgorithmException;
63 import org.jvnet.fastinfoset.EncodingAlgorithmIndexes;
64 import org.jvnet.fastinfoset.FastInfosetException;
65 import com.sun.xml.fastinfoset.CommonResourceBundle;
66
67 public class StAXDocumentParser extends Decoder implements XMLStreamReader {
68     protected static final int INTERNAL_STATE_START_DOCUMENT = 0;
69     protected static final int INTERNAL_STATE_START_ELEMENT_TERMINATE = 1;
70     protected static final int INTERNAL_STATE_SINGLE_TERMINATE_ELEMENT_WITH_NAMESPACES = 2;
71     protected static final int INTERNAL_STATE_DOUBLE_TERMINATE_ELEMENT = 3;
72     protected static final int INTERNAL_STATE_END_DOCUMENT = 4;
73     protected static final int INTERNAL_STATE_VOID = -1;
74
75     protected int _internalState;
76     
77     /**
78      * Current event
79      */

80     protected int _eventType;
81     
82     /**
83      * Stack of qualified names and namespaces
84      */

85     protected QualifiedName[] _qNameStack = new QualifiedName[32];
86     protected int[] _namespaceAIIsStartStack = new int[32];
87     protected int[] _namespaceAIIsEndStack = new int[32];
88     protected int _stackCount = -1;
89     
90     protected String JavaDoc[] _namespaceAIIsPrefix = new String JavaDoc[32];
91     protected String JavaDoc[] _namespaceAIIsNamespaceName = new String JavaDoc[32];
92     protected int[] _namespaceAIIsPrefixIndex = new int[32];
93     protected int _namespaceAIIsIndex;
94     
95     /**
96      * Namespaces associated with START_ELEMENT or END_ELEMENT
97      */

98     protected int _currentNamespaceAIIsStart;
99     protected int _currentNamespaceAIIsEnd;
100     
101     /**
102      * Qualified name associated with START_ELEMENT or END_ELEMENT.
103      */

104     protected QualifiedName _qualifiedName;
105     
106     /**
107      * List of attributes
108      */

109     protected AttributesHolder _attributes = new AttributesHolder();
110
111     protected boolean _clearAttributes = false;
112     
113     /**
114      * Characters associated with event.
115      */

116     protected char[] _characters;
117     protected int _charactersOffset;
118     protected int _charactersLength;
119
120     protected String JavaDoc _algorithmURI;
121     protected int _algorithmId;
122     protected byte[] _algorithmData;
123     protected int _algorithmDataOffset;
124     protected int _algorithmDataLength;
125     
126     /**
127      * State for processing instruction
128      */

129     protected String JavaDoc _piTarget;
130     protected String JavaDoc _piData;
131         
132     protected NamespaceContextImpl _nsContext = new NamespaceContextImpl();
133     
134     protected String JavaDoc _characterEncodingScheme;
135     
136     protected StAXManager _manager;
137     
138     public StAXDocumentParser() {
139         reset();
140     }
141     
142     public StAXDocumentParser(InputStream JavaDoc s) {
143         this();
144         setInputStream(s);
145     }
146     
147     public StAXDocumentParser(InputStream JavaDoc s, StAXManager manager) {
148         this(s);
149         _manager = manager;
150     }
151     
152     public void setInputStream(InputStream JavaDoc s) {
153         super.setInputStream(s);
154         reset();
155     }
156     
157     public void reset() {
158         super.reset();
159         if (_internalState != INTERNAL_STATE_START_DOCUMENT &&
160             _internalState != INTERNAL_STATE_END_DOCUMENT) {
161             
162             for (int i = _namespaceAIIsIndex - 1; i >= 0; i--) {
163                 _prefixTable.popScopeWithPrefixEntry(_namespaceAIIsPrefixIndex[i]);
164             }
165        
166             _stackCount = -1;
167
168             _namespaceAIIsIndex = 0;
169             _characters = null;
170             _algorithmData = null;
171         }
172      
173         _characterEncodingScheme = "UTF-8";
174         _eventType = START_DOCUMENT;
175         _internalState = INTERNAL_STATE_START_DOCUMENT;
176     }
177
178     protected void resetOnError() {
179         super.reset();
180
181         if (_v != null) {
182             _prefixTable.clearCompletely();
183         }
184         _duplicateAttributeVerifier.clear();
185
186         _stackCount = -1;
187
188         _namespaceAIIsIndex = 0;
189         _characters = null;
190         _algorithmData = null;
191         
192         _eventType = START_DOCUMENT;
193         _internalState = INTERNAL_STATE_START_DOCUMENT;
194     }
195     
196     // -- XMLStreamReader Interface -------------------------------------------
197

198     public Object JavaDoc getProperty(java.lang.String JavaDoc name)
199             throws java.lang.IllegalArgumentException JavaDoc {
200         if (_manager != null) {
201             return _manager.getProperty(name);
202         }
203         return null;
204     }
205         
206     public int next() throws XMLStreamException {
207         try {
208             if (_internalState != INTERNAL_STATE_VOID) {
209                 switch (_internalState) {
210                     case INTERNAL_STATE_START_DOCUMENT:
211                         decodeHeader();
212                         processDII();
213
214                         _internalState = INTERNAL_STATE_VOID;
215                         break;
216                     case INTERNAL_STATE_START_ELEMENT_TERMINATE:
217                         if (_currentNamespaceAIIsEnd > 0) {
218                             for (int i = _currentNamespaceAIIsEnd - 1; i >= _currentNamespaceAIIsStart; i--) {
219                                 _prefixTable.popScopeWithPrefixEntry(_namespaceAIIsPrefixIndex[i]);
220                             }
221                             _namespaceAIIsIndex = _currentNamespaceAIIsStart;
222                         }
223                         
224                         // Pop information off the stack
225
popStack();
226
227                         _internalState = INTERNAL_STATE_VOID;
228                         return _eventType = END_ELEMENT;
229                     case INTERNAL_STATE_SINGLE_TERMINATE_ELEMENT_WITH_NAMESPACES:
230                         // Undeclare namespaces
231
for (int i = _currentNamespaceAIIsEnd - 1; i >= _currentNamespaceAIIsStart; i--) {
232                             _prefixTable.popScopeWithPrefixEntry(_namespaceAIIsPrefixIndex[i]);
233                         }
234                         _namespaceAIIsIndex = _currentNamespaceAIIsStart;
235                         _internalState = INTERNAL_STATE_VOID;
236                         break;
237                     case INTERNAL_STATE_DOUBLE_TERMINATE_ELEMENT:
238                         // Undeclare namespaces
239
if (_currentNamespaceAIIsEnd > 0) {
240                             for (int i = _currentNamespaceAIIsEnd - 1; i >= _currentNamespaceAIIsStart; i--) {
241                                 _prefixTable.popScopeWithPrefixEntry(_namespaceAIIsPrefixIndex[i]);
242                             }
243                             _namespaceAIIsIndex = _currentNamespaceAIIsStart;
244                         }
245                         
246                         if (_stackCount == -1) {
247                             _internalState = INTERNAL_STATE_END_DOCUMENT;
248                             return _eventType = END_DOCUMENT;
249                         }
250                                             
251                         // Pop information off the stack
252
popStack();
253                         
254                         _internalState = (_currentNamespaceAIIsEnd > 0) ?
255                                 INTERNAL_STATE_SINGLE_TERMINATE_ELEMENT_WITH_NAMESPACES :
256                                 INTERNAL_STATE_VOID;
257                         return _eventType = END_ELEMENT;
258                     case INTERNAL_STATE_END_DOCUMENT:
259                         throw new NoSuchElementException JavaDoc(CommonResourceBundle.getInstance().getString("message.noMoreEvents"));
260                 }
261             }
262                         
263             // Reset internal state
264
_characters = null;
265             _algorithmData = null;
266             _currentNamespaceAIIsEnd = 0;
267             
268             // Process information item
269
final int b = read();
270             switch(DecoderStateTables.EII[b]) {
271                 case DecoderStateTables.EII_NO_AIIS_INDEX_SMALL:
272                     processEII(_elementNameTable._array[b], false);
273                     return _eventType;
274                 case DecoderStateTables.EII_AIIS_INDEX_SMALL:
275                     processEII(_elementNameTable._array[b & EncodingConstants.INTEGER_3RD_BIT_SMALL_MASK], true);
276                     return _eventType;
277                 case DecoderStateTables.EII_INDEX_MEDIUM:
278                     processEII(processEIIIndexMedium(b), (b & EncodingConstants.ELEMENT_ATTRIBUTE_FLAG) > 0);
279                     return _eventType;
280                 case DecoderStateTables.EII_INDEX_LARGE:
281                     processEII(processEIIIndexLarge(b), (b & EncodingConstants.ELEMENT_ATTRIBUTE_FLAG) > 0);
282                     return _eventType;
283                 case DecoderStateTables.EII_LITERAL:
284                 {
285                     final QualifiedName qn = processLiteralQualifiedName(
286                                 b & EncodingConstants.LITERAL_QNAME_PREFIX_NAMESPACE_NAME_MASK);
287                     _elementNameTable.add(qn);
288                     processEII(qn, (b & EncodingConstants.ELEMENT_ATTRIBUTE_FLAG) > 0);
289                     return _eventType;
290                 }
291                 case DecoderStateTables.EII_NAMESPACES:
292                     processEIIWithNamespaces((b & EncodingConstants.ELEMENT_ATTRIBUTE_FLAG) > 0);
293                     return _eventType;
294                 case DecoderStateTables.CII_UTF8_SMALL_LENGTH:
295                     _octetBufferLength = (b & EncodingConstants.OCTET_STRING_LENGTH_7TH_BIT_SMALL_MASK)
296                             + 1;
297                     decodeUtf8StringAsCharBuffer();
298                     if ((b & EncodingConstants.CHARACTER_CHUNK_ADD_TO_TABLE_FLAG) > 0) {
299                         _characterContentChunkTable.add(_charBuffer, _charBufferLength);
300                     }
301                     
302                     _characters = _charBuffer;
303                     _charactersOffset = 0;
304                     _charactersLength = _charBufferLength;
305                     return _eventType = CHARACTERS;
306                 case DecoderStateTables.CII_UTF8_MEDIUM_LENGTH:
307                     _octetBufferLength = read() + EncodingConstants.OCTET_STRING_LENGTH_7TH_BIT_SMALL_LIMIT;
308                     decodeUtf8StringAsCharBuffer();
309                     if ((b & EncodingConstants.CHARACTER_CHUNK_ADD_TO_TABLE_FLAG) > 0) {
310                         _characterContentChunkTable.add(_charBuffer, _charBufferLength);
311                     }
312                     
313                     _characters = _charBuffer;
314                     _charactersOffset = 0;
315                     _charactersLength = _charBufferLength;
316                     return _eventType = CHARACTERS;
317                 case DecoderStateTables.CII_UTF8_LARGE_LENGTH:
318                     _octetBufferLength = ((read() << 24) |
319                             (read() << 16) |
320                             (read() << 8) |
321                             read())
322                             + EncodingConstants.OCTET_STRING_LENGTH_7TH_BIT_MEDIUM_LIMIT;
323                     decodeUtf8StringAsCharBuffer();
324                     if ((b & EncodingConstants.CHARACTER_CHUNK_ADD_TO_TABLE_FLAG) > 0) {
325                         _characterContentChunkTable.add(_charBuffer, _charBufferLength);
326                     }
327                     
328                     _characters = _charBuffer;
329                     _charactersOffset = 0;
330                     _charactersLength = _charBufferLength;
331                     return _eventType = CHARACTERS;
332                 case DecoderStateTables.CII_UTF16_SMALL_LENGTH:
333                     _octetBufferLength = (b & EncodingConstants.OCTET_STRING_LENGTH_7TH_BIT_SMALL_MASK)
334                     + 1;
335                     decodeUtf16StringAsCharBuffer();
336                     if ((b & EncodingConstants.CHARACTER_CHUNK_ADD_TO_TABLE_FLAG) > 0) {
337                         _characterContentChunkTable.add(_charBuffer, _charBufferLength);
338                     }
339                     
340                     _characters = _charBuffer;
341                     _charactersOffset = 0;
342                     _charactersLength = _charBufferLength;
343                     return _eventType = CHARACTERS;
344                 case DecoderStateTables.CII_UTF16_MEDIUM_LENGTH:
345                     _octetBufferLength = read() + EncodingConstants.OCTET_STRING_LENGTH_7TH_BIT_SMALL_LIMIT;
346                     decodeUtf16StringAsCharBuffer();
347                     if ((b & EncodingConstants.CHARACTER_CHUNK_ADD_TO_TABLE_FLAG) > 0) {
348                         _characterContentChunkTable.add(_charBuffer, _charBufferLength);
349                     }
350                     
351                     _characters = _charBuffer;
352                     _charactersOffset = 0;
353                     _charactersLength = _charBufferLength;
354                     return _eventType = CHARACTERS;
355                 case DecoderStateTables.CII_UTF16_LARGE_LENGTH:
356                     _octetBufferLength = ((read() << 24) |
357                             (read() << 16) |
358                             (read() << 8) |
359                             read())
360                             + EncodingConstants.OCTET_STRING_LENGTH_7TH_BIT_MEDIUM_LIMIT;
361                     decodeUtf16StringAsCharBuffer();
362                     if ((b & EncodingConstants.CHARACTER_CHUNK_ADD_TO_TABLE_FLAG) > 0) {
363                         _characterContentChunkTable.add(_charBuffer, _charBufferLength);
364                     }
365                     
366                     _characters = _charBuffer;
367                     _charactersOffset = 0;
368                     _charactersLength = _charBufferLength;
369                     return _eventType = CHARACTERS;
370                 case DecoderStateTables.CII_RA:
371                 {
372                     final boolean addToTable = (_b & EncodingConstants.CHARACTER_CHUNK_ADD_TO_TABLE_FLAG) > 0;
373                     
374                     _identifier = (b & 0x02) << 6;
375                     final int b2 = read();
376                     _identifier |= (b2 & 0xFC) >> 2;
377                     
378                     decodeOctetsOnSeventhBitOfNonIdentifyingStringOnThirdBit(b2);
379                     
380                     decodeRestrictedAlphabetAsCharBuffer();
381                     
382                     if (addToTable) {
383                         _characterContentChunkTable.add(_charBuffer, _charBufferLength);
384                     }
385                     
386                     _characters = _charBuffer;
387                     _charactersOffset = 0;
388                     _charactersLength = _charBufferLength;
389                     return _eventType = CHARACTERS;
390                 }
391                 case DecoderStateTables.CII_EA:
392                 {
393                     if ((b & EncodingConstants.NISTRING_ADD_TO_TABLE_FLAG) > 0) {
394                         throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.addToTableNotSupported"));
395                     }
396                     
397                     // Decode encoding algorithm integer
398
_algorithmId = (b & 0x02) << 6;
399                     final int b2 = read();
400                     _algorithmId |= (b2 & 0xFC) >> 2;
401                     
402                     decodeOctetsOnSeventhBitOfNonIdentifyingStringOnThirdBit(b2);
403                     processCIIEncodingAlgorithm();
404                     
405                     return _eventType = CHARACTERS;
406                 }
407                 case DecoderStateTables.CII_INDEX_SMALL:
408                 {
409                     final int index = b & EncodingConstants.INTEGER_4TH_BIT_SMALL_MASK;
410                     
411                     _characters = _characterContentChunkTable._array;
412                     _charactersOffset = _characterContentChunkTable._offset[index];
413                     _charactersLength = _characterContentChunkTable._length[index];
414                     return _eventType = CHARACTERS;
415                 }
416                 case DecoderStateTables.CII_INDEX_MEDIUM:
417                 {
418                     final int index = (((b & EncodingConstants.INTEGER_4TH_BIT_MEDIUM_MASK) << 8) | read())
419                             + EncodingConstants.INTEGER_4TH_BIT_SMALL_LIMIT;
420                     
421                     _characters = _characterContentChunkTable._array;
422                     _charactersOffset = _characterContentChunkTable._offset[index];
423                     _charactersLength = _characterContentChunkTable._length[index];
424                     return _eventType = CHARACTERS;
425                 }
426                 case DecoderStateTables.CII_INDEX_LARGE:
427                 {
428                     final int index = (((b & EncodingConstants.INTEGER_4TH_BIT_LARGE_MASK) << 16) |
429                             (read() << 8) |
430                             read())
431                             + EncodingConstants.INTEGER_4TH_BIT_MEDIUM_LIMIT;
432                     
433                     _characters = _characterContentChunkTable._array;
434                     _charactersOffset = _characterContentChunkTable._offset[index];
435                     _charactersLength = _characterContentChunkTable._length[index];
436                     return _eventType = CHARACTERS;
437                 }
438                 case DecoderStateTables.CII_INDEX_LARGE_LARGE:
439                 {
440                     final int index = ((read() << 16) |
441                             (read() << 8) |
442                             read())
443                             + EncodingConstants.INTEGER_4TH_BIT_LARGE_LIMIT;
444                     
445                     _characters = _characterContentChunkTable._array;
446                     _charactersOffset = _characterContentChunkTable._offset[index];
447                     _charactersLength = _characterContentChunkTable._length[index];
448                     return _eventType = CHARACTERS;
449                 }
450                 case DecoderStateTables.COMMENT_II:
451                     processCommentII();
452                     return _eventType;
453                 case DecoderStateTables.PROCESSING_INSTRUCTION_II:
454                     processProcessingII();
455                     return _eventType;
456                 case DecoderStateTables.UNEXPANDED_ENTITY_REFERENCE_II:
457                 {
458                     /*
459                      * TODO
460                      * How does StAX report such events?
461                      */

462                     String JavaDoc entity_reference_name = decodeIdentifyingNonEmptyStringOnFirstBit(_v.otherNCName);
463                     
464                     String JavaDoc system_identifier = ((b & EncodingConstants.UNEXPANDED_ENTITY_SYSTEM_IDENTIFIER_FLAG) > 0)
465                     ? decodeIdentifyingNonEmptyStringOnFirstBit(_v.otherURI) : "";
466                     String JavaDoc public_identifier = ((b & EncodingConstants.UNEXPANDED_ENTITY_PUBLIC_IDENTIFIER_FLAG) > 0)
467                     ? decodeIdentifyingNonEmptyStringOnFirstBit(_v.otherURI) : "";
468                     return _eventType;
469                 }
470                 case DecoderStateTables.TERMINATOR_DOUBLE:
471                     if (_stackCount != -1) {
472                         // Pop information off the stack
473
popStack();
474
475                         _internalState = INTERNAL_STATE_DOUBLE_TERMINATE_ELEMENT;
476                         return _eventType = END_ELEMENT;
477                     }
478                      
479                     _internalState = INTERNAL_STATE_END_DOCUMENT;
480                     return _eventType = END_DOCUMENT;
481                 case DecoderStateTables.TERMINATOR_SINGLE:
482                     if (_stackCount != -1) {
483                         // Pop information off the stack
484
popStack();
485                         
486                         if (_currentNamespaceAIIsEnd > 0) {
487                             _internalState = INTERNAL_STATE_SINGLE_TERMINATE_ELEMENT_WITH_NAMESPACES;
488                         }
489                         return _eventType = END_ELEMENT;
490                     }
491                     
492                     _internalState = INTERNAL_STATE_END_DOCUMENT;
493                     return _eventType = END_DOCUMENT;
494                 default:
495                     throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.IllegalStateDecodingEII"));
496             }
497         } catch (IOException JavaDoc e) {
498             resetOnError();
499             e.printStackTrace();
500             throw new XMLStreamException(e);
501         } catch (FastInfosetException e) {
502             resetOnError();
503             e.printStackTrace();
504             throw new XMLStreamException(e);
505         } catch (RuntimeException JavaDoc e) {
506             resetOnError();
507             e.printStackTrace();
508             throw e;
509         }
510     }
511     
512     private final void popStack() {
513         // Pop information off the stack
514
_qualifiedName = _qNameStack[_stackCount];
515         _currentNamespaceAIIsStart = _namespaceAIIsStartStack[_stackCount];
516         _currentNamespaceAIIsEnd = _namespaceAIIsEndStack[_stackCount];
517         _qNameStack[_stackCount--] = null;
518     }
519     
520     /** Test if the current event is of the given type and if the namespace and name match the current namespace and name of the current event.
521      * If the namespaceURI is null it is not checked for equality, if the localName is null it is not checked for equality.
522      * @param type the event type
523      * @param namespaceURI the uri of the event, may be null
524      * @param localName the localName of the event, may be null
525      * @throws XMLStreamException if the required values are not matched.
526      */

527     public final void require(int type, String JavaDoc namespaceURI, String JavaDoc localName)
528     throws XMLStreamException {
529         if( type != _eventType)
530             throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.eventTypeNotMatch", new Object JavaDoc[]{getEventTypeString(type)}));
531         if( namespaceURI != null && !namespaceURI.equals(getNamespaceURI()) )
532             throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.namespaceURINotMatch", new Object JavaDoc[]{namespaceURI}));
533         if(localName != null && !localName.equals(getLocalName()))
534             throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.localNameNotMatch", new Object JavaDoc[]{localName}));
535         
536         return;
537     }
538     
539     /** Reads the content of a text-only element. Precondition:
540      * the current event is START_ELEMENT. Postcondition:
541      * The current event is the corresponding END_ELEMENT.
542      * @throws XMLStreamException if the current event is not a START_ELEMENT or if
543      * a non text element is encountered
544      */

545     public final String JavaDoc getElementText() throws XMLStreamException {
546         
547         if(getEventType() != START_ELEMENT) {
548             throw new XMLStreamException(
549                     CommonResourceBundle.getInstance().getString("message.mustBeOnSTARTELEMENT"), getLocation());
550         }
551         //current is StartElement, move to the next
552
int eventType = next();
553         return getElementText(true);
554     }
555     /**
556      * @param startElementRead flag if start element has already been read
557      */

558     public final String JavaDoc getElementText(boolean startElementRead) throws XMLStreamException {
559         if (!startElementRead) {
560             throw new XMLStreamException(
561                     CommonResourceBundle.getInstance().getString("message.mustBeOnSTARTELEMENT"), getLocation());
562         }
563         int eventType = getEventType();
564         StringBuffer JavaDoc content = new StringBuffer JavaDoc();
565         while(eventType != END_ELEMENT ) {
566             if(eventType == CHARACTERS
567                     || eventType == CDATA
568                     || eventType == SPACE
569                     || eventType == ENTITY_REFERENCE) {
570                 content.append(getText());
571             } else if(eventType == PROCESSING_INSTRUCTION
572                     || eventType == COMMENT) {
573                 // skipping
574
} else if(eventType == END_DOCUMENT) {
575                 throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.unexpectedEOF"));
576             } else if(eventType == START_ELEMENT) {
577                 throw new XMLStreamException(
578                         CommonResourceBundle.getInstance().getString("message.getElementTextExpectTextOnly"), getLocation());
579             } else {
580                 throw new XMLStreamException(
581                         CommonResourceBundle.getInstance().getString("message.unexpectedEventType")+ getEventTypeString(eventType), getLocation());
582             }
583             eventType = next();
584         }
585         return content.toString();
586     }
587     
588     /** Skips any white space (isWhiteSpace() returns true), COMMENT,
589      * or PROCESSING_INSTRUCTION,
590      * until a START_ELEMENT or END_ELEMENT is reached.
591      * If other than white space characters, COMMENT, PROCESSING_INSTRUCTION, START_ELEMENT, END_ELEMENT
592      * are encountered, an exception is thrown. This method should
593      * be used when processing element-only content seperated by white space.
594      * This method should
595      * be used when processing element-only content because
596      * the parser is not able to recognize ignorable whitespace if
597      * then DTD is missing or not interpreted.
598      * @return the event type of the element read
599      * @throws XMLStreamException if the current event is not white space
600      */

601     public final int nextTag() throws XMLStreamException {
602         int eventType = next();
603         return nextTag(true);
604     }
605     /** if the current tag has already read, such as in the case EventReader's
606      * peek() has been called, the current cursor should not move before the loop
607      */

608     public final int nextTag(boolean currentTagRead) throws XMLStreamException {
609         int eventType = getEventType();
610         if (!currentTagRead) {
611             eventType = next();
612         }
613         while((eventType == CHARACTERS && isWhiteSpace()) // skip whitespace
614
|| (eventType == CDATA && isWhiteSpace())
615         || eventType == SPACE
616                 || eventType == PROCESSING_INSTRUCTION
617                 || eventType == COMMENT) {
618             eventType = next();
619         }
620         if (eventType != START_ELEMENT && eventType != END_ELEMENT) {
621             throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.expectedStartOrEnd"), getLocation());
622         }
623         return eventType;
624     }
625     
626     public final boolean hasNext() throws XMLStreamException {
627         return (_eventType != END_DOCUMENT);
628     }
629         
630     public void close() throws XMLStreamException {
631     }
632     
633     public final String JavaDoc getNamespaceURI(String JavaDoc prefix) {
634         String JavaDoc namespace = getNamespaceDecl(prefix);
635         if (namespace == null) {
636             if (prefix == null) {
637                 throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.nullPrefix"));
638             }
639             return null; // unbound
640
}
641         return namespace;
642     }
643     
644     public final boolean isStartElement() {
645         return (_eventType == START_ELEMENT);
646     }
647     
648     public final boolean isEndElement() {
649         return (_eventType == END_ELEMENT);
650     }
651     
652     public final boolean isCharacters() {
653         return (_eventType == CHARACTERS);
654     }
655     
656     /**
657      * Returns true if the cursor points to a character data event that consists of all whitespace
658      * Application calling this method needs to cache the value and avoid calling this method again
659      * for the same event.
660      * @return true if the cursor points to all whitespace, false otherwise
661      */

662     public final boolean isWhiteSpace() {
663         if(isCharacters() || (_eventType == CDATA)){
664             char [] ch = this.getTextCharacters();
665             int start = this.getTextStart();
666             int length = this.getTextLength();
667             for (int i=start; i< length;i++){
668                 if(!XMLChar.isSpace(ch[i])){
669                     return false;
670                 }
671             }
672             return true;
673         }
674         return false;
675     }
676     
677     public final String JavaDoc getAttributeValue(String JavaDoc namespaceURI, String JavaDoc localName) {
678         if (_eventType != START_ELEMENT) {
679             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetAttributeValue"));
680         }
681         
682         // Search for the attributes in _attributes
683
for (int i = 0; i < _attributes.getLength(); i++) {
684             if (_attributes.getLocalName(i).equals(localName) &&
685                     _attributes.getURI(i).equals(namespaceURI)) {
686                 return _attributes.getValue(i);
687             }
688         }
689         return null;
690     }
691     
692     public final int getAttributeCount() {
693         if (_eventType != START_ELEMENT) {
694             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetAttributeValue"));
695         }
696         
697         return _attributes.getLength();
698     }
699     
700     public final javax.xml.namespace.QName JavaDoc getAttributeName(int index) {
701         if (_eventType != START_ELEMENT) {
702             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetAttributeValue"));
703         }
704         return _attributes.getQualifiedName(index).getQName();
705     }
706     
707     public final String JavaDoc getAttributeNamespace(int index) {
708         if (_eventType != START_ELEMENT) {
709             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetAttributeValue"));
710         }
711         
712         return _attributes.getURI(index);
713     }
714     
715     public final String JavaDoc getAttributeLocalName(int index) {
716         if (_eventType != START_ELEMENT) {
717             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetAttributeValue"));
718         }
719         return _attributes.getLocalName(index);
720     }
721     
722     public final String JavaDoc getAttributePrefix(int index) {
723         if (_eventType != START_ELEMENT) {
724             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetAttributeValue"));
725         }
726         return _attributes.getPrefix(index);
727     }
728     
729     public final String JavaDoc getAttributeType(int index) {
730         if (_eventType != START_ELEMENT) {
731             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetAttributeValue"));
732         }
733         return _attributes.getType(index);
734     }
735     
736     public final String JavaDoc getAttributeValue(int index) {
737         if (_eventType != START_ELEMENT) {
738             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetAttributeValue"));
739         }
740         return _attributes.getValue(index);
741     }
742     
743     public final boolean isAttributeSpecified(int index) {
744         return false; // non-validating parser
745
}
746     
747     public final int getNamespaceCount() {
748         if (_eventType == START_ELEMENT || _eventType == END_ELEMENT) {
749             return (_currentNamespaceAIIsEnd > 0) ? (_currentNamespaceAIIsEnd - _currentNamespaceAIIsStart) : 0;
750         } else {
751             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetNamespaceCount"));
752         }
753     }
754     
755     public final String JavaDoc getNamespacePrefix(int index) {
756         if (_eventType == START_ELEMENT || _eventType == END_ELEMENT) {
757             return _namespaceAIIsPrefix[_currentNamespaceAIIsStart + index];
758         } else {
759             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetNamespacePrefix"));
760         }
761     }
762     
763     public final String JavaDoc getNamespaceURI(int index) {
764         if (_eventType == START_ELEMENT || _eventType == END_ELEMENT) {
765             return _namespaceAIIsNamespaceName[_currentNamespaceAIIsStart + index];
766         } else {
767             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetNamespacePrefix"));
768         }
769     }
770     
771     public final NamespaceContext JavaDoc getNamespaceContext() {
772         return _nsContext;
773     }
774     
775     public final int getEventType() {
776         return _eventType;
777     }
778     
779     public final String JavaDoc getText() {
780         if (_characters == null) {
781             checkTextState();
782         }
783         
784         return new String JavaDoc(_characters,
785                 _charactersOffset,
786                 _charactersLength);
787     }
788     
789     public final char[] getTextCharacters() {
790         if (_characters == null) {
791             checkTextState();
792         }
793         
794         return _characters;
795     }
796     
797     public final int getTextStart() {
798         if (_characters == null) {
799             checkTextState();
800         }
801                 
802         return _charactersOffset;
803     }
804     
805     public final int getTextLength() {
806         if (_characters == null) {
807             checkTextState();
808         }
809         
810         return _charactersLength;
811     }
812     
813     public final int getTextCharacters(int sourceStart, char[] target,
814             int targetStart, int length) throws XMLStreamException {
815         if (_characters == null) {
816             checkTextState();
817         }
818         
819         try {
820             System.arraycopy(_characters, sourceStart, target,
821                     targetStart, length);
822             return length;
823         } catch (IndexOutOfBoundsException JavaDoc e) {
824             throw new XMLStreamException(e);
825         }
826     }
827     
828     protected final void checkTextState() {
829         if (_algorithmData == null) {
830             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.InvalidStateForText"));
831         }
832         
833         try {
834             convertEncodingAlgorithmDataToCharacters();
835         } catch (Exception JavaDoc e) {
836             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.InvalidStateForText"));
837         }
838     }
839     
840     public final String JavaDoc getEncoding() {
841         return _characterEncodingScheme;
842     }
843     
844     public final boolean hasText() {
845         return (_characters != null);
846     }
847     
848     public final Location getLocation() {
849         //location should be created in next()
850
//returns a nil location for now
851
return EventLocation.getNilLocation();
852     }
853     
854     public final QName JavaDoc getName() {
855         if (_eventType == START_ELEMENT || _eventType == END_ELEMENT) {
856             return _qualifiedName.getQName();
857         } else {
858             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetName"));
859         }
860     }
861     
862     public final String JavaDoc getLocalName() {
863         if (_eventType == START_ELEMENT || _eventType == END_ELEMENT) {
864             return _qualifiedName.localName;
865         } else {
866             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetLocalName"));
867         }
868     }
869     
870     public final boolean hasName() {
871         return (_eventType == START_ELEMENT || _eventType == END_ELEMENT);
872     }
873     
874     public final String JavaDoc getNamespaceURI() {
875         if (_eventType == START_ELEMENT || _eventType == END_ELEMENT) {
876             return _qualifiedName.namespaceName;
877         } else {
878             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetNamespaceURI"));
879         }
880     }
881     
882     public final String JavaDoc getPrefix() {
883         if (_eventType == START_ELEMENT || _eventType == END_ELEMENT) {
884             return _qualifiedName.prefix;
885         } else {
886             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetPrefix"));
887         }
888     }
889     
890     public final String JavaDoc getVersion() {
891         return null;
892     }
893     
894     public final boolean isStandalone() {
895         return false;
896     }
897     
898     public final boolean standaloneSet() {
899         return false;
900     }
901     
902     public final String JavaDoc getCharacterEncodingScheme() {
903         return null;
904     }
905     
906     public final String JavaDoc getPITarget() {
907         if (_eventType != PROCESSING_INSTRUCTION) {
908             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetPITarget"));
909         }
910         
911         return _piTarget;
912     }
913     
914     public final String JavaDoc getPIData() {
915         if (_eventType != PROCESSING_INSTRUCTION) {
916             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetPIData"));
917         }
918         
919         return _piData;
920     }
921     
922
923     
924     
925     public final String JavaDoc getNameString() {
926         if (_eventType == START_ELEMENT || _eventType == END_ELEMENT) {
927             return _qualifiedName.getQNameString();
928         } else {
929             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetName"));
930         }
931     }
932     
933     public final String JavaDoc getAttributeNameString(int index) {
934         if (_eventType != START_ELEMENT) {
935             throw new IllegalStateException JavaDoc(CommonResourceBundle.getInstance().getString("message.invalidCallingGetAttributeValue"));
936         }
937         return _attributes.getQualifiedName(index).getQNameString();
938     }
939         
940     
941     public final String JavaDoc getTextAlgorithmURI() {
942         return _algorithmURI;
943     }
944  
945     public final int getTextAlgorithmIndex() {
946         return _algorithmId;
947     }
948     
949     public final byte[] getTextAlgorithmBytes() {
950         return _algorithmData;
951     }
952     
953     public final byte[] getTextAlgorithmBytesClone() {
954         if (_algorithmData == null) {
955             return null;
956         }
957         
958         byte[] algorithmData = new byte[_algorithmDataLength];
959         System.arraycopy(_algorithmData, _algorithmDataOffset, algorithmData, 0, _algorithmDataLength);
960         return algorithmData;
961     }
962     
963     public final int getTextAlgorithmStart() {
964         return _algorithmDataOffset;
965     }
966     
967     public final int getTextAlgorithmLength() {
968         return _algorithmDataLength;
969     }
970     
971     public final int getTextAlgorithmBytes(int sourceStart, byte[] target,
972             int targetStart, int length) throws XMLStreamException {
973         try {
974             System.arraycopy(_algorithmData, sourceStart, target,
975                     targetStart, length);
976             return length;
977         } catch (IndexOutOfBoundsException JavaDoc e) {
978             throw new XMLStreamException(e);
979         }
980     }
981     
982
983     
984     //
985

986     protected final void processDII() throws FastInfosetException, IOException JavaDoc {
987         final int b = read();
988         if (b > 0) {
989             processDIIOptionalProperties(b);
990         }
991     }
992     
993     protected final void processDIIOptionalProperties(int b) throws FastInfosetException, IOException JavaDoc {
994         // Optimize for the most common case
995
if (b == EncodingConstants.DOCUMENT_INITIAL_VOCABULARY_FLAG) {
996             decodeInitialVocabulary();
997             return;
998         }
999         
1000        if ((b & EncodingConstants.DOCUMENT_ADDITIONAL_DATA_FLAG) > 0) {
1001            decodeAdditionalData();
1002            /*
1003             * TODO
1004             * how to report the additional data?
1005             */

1006        }
1007        
1008        if ((b & EncodingConstants.DOCUMENT_INITIAL_VOCABULARY_FLAG) > 0) {
1009            decodeInitialVocabulary();
1010        }
1011        
1012        if ((b & EncodingConstants.DOCUMENT_NOTATIONS_FLAG) > 0) {
1013            decodeNotations();
1014            /*
1015                try {
1016                    _dtdHandler.notationDecl(name, public_identifier, system_identifier);
1017                } catch (SAXException e) {
1018                    throw new IOException("NotationsDeclarationII");
1019                }
1020             */

1021        }
1022        
1023        if ((b & EncodingConstants.DOCUMENT_UNPARSED_ENTITIES_FLAG) > 0) {
1024            decodeUnparsedEntities();
1025            /*
1026                try {
1027                    _dtdHandler.unparsedEntityDecl(name, public_identifier, system_identifier, notation_name);
1028                } catch (SAXException e) {
1029                    throw new IOException("UnparsedEntitiesII");
1030                }
1031             */

1032        }
1033        
1034        if ((b & EncodingConstants.DOCUMENT_CHARACTER_ENCODING_SCHEME) > 0) {
1035            _characterEncodingScheme = decodeCharacterEncodingScheme();
1036        }
1037        
1038        if ((b & EncodingConstants.DOCUMENT_STANDALONE_FLAG) > 0) {
1039            boolean standalone = (read() > 0) ? true : false ;
1040            /*
1041             * TODO
1042             * how to report the standalone flag?
1043             */

1044        }
1045        
1046        if ((b & EncodingConstants.DOCUMENT_VERSION_FLAG) > 0) {
1047            String JavaDoc version = decodeVersion();
1048            /*
1049             * TODO
1050             * how to report the standalone flag?
1051             */

1052        }
1053    }
1054
1055    
1056    protected final void resizeNamespaceAIIs() {
1057        final String JavaDoc[] namespaceAIIsPrefix = new String JavaDoc[_namespaceAIIsIndex * 2];
1058        System.arraycopy(_namespaceAIIsPrefix, 0, namespaceAIIsPrefix, 0, _namespaceAIIsIndex);
1059        _namespaceAIIsPrefix = namespaceAIIsPrefix;
1060
1061        final String JavaDoc[] namespaceAIIsNamespaceName = new String JavaDoc[_namespaceAIIsIndex * 2];
1062        System.arraycopy(_namespaceAIIsNamespaceName, 0, namespaceAIIsNamespaceName, 0, _namespaceAIIsIndex);
1063        _namespaceAIIsNamespaceName = namespaceAIIsNamespaceName;
1064
1065        final int[] namespaceAIIsPrefixIndex = new int[_namespaceAIIsIndex * 2];
1066        System.arraycopy(_namespaceAIIsPrefixIndex, 0, namespaceAIIsPrefixIndex, 0, _namespaceAIIsIndex);
1067        _namespaceAIIsPrefixIndex = namespaceAIIsPrefixIndex;
1068    }
1069    
1070    protected final void processEIIWithNamespaces(boolean hasAttributes) throws FastInfosetException, IOException JavaDoc {
1071        if (++_prefixTable._declarationId == Integer.MAX_VALUE) {
1072            _prefixTable.clearDeclarationIds();
1073        }
1074
1075        _currentNamespaceAIIsStart = _namespaceAIIsIndex;
1076        String JavaDoc prefix = "", namespaceName = "";
1077        int b = read();
1078        while ((b & EncodingConstants.NAMESPACE_ATTRIBUTE_MASK) == EncodingConstants.NAMESPACE_ATTRIBUTE) {
1079            if (_namespaceAIIsIndex == _namespaceAIIsPrefix.length) {
1080                resizeNamespaceAIIs();
1081            }
1082            
1083            switch (b & EncodingConstants.NAMESPACE_ATTRIBUTE_PREFIX_NAME_MASK) {
1084                // no prefix, no namespace
1085
// Undeclaration of default namespace
1086
case 0:
1087                    prefix = namespaceName =
1088                            _namespaceAIIsPrefix[_namespaceAIIsIndex] =
1089                            _namespaceAIIsNamespaceName[_namespaceAIIsIndex] = "";
1090                    
1091                    _namespaceNameIndex = _prefixIndex = _namespaceAIIsPrefixIndex[_namespaceAIIsIndex++] = -1;
1092                    break;
1093                // no prefix, namespace
1094
// Declaration of default namespace
1095
case 1:
1096                    prefix = _namespaceAIIsPrefix[_namespaceAIIsIndex] = "";
1097                    namespaceName = _namespaceAIIsNamespaceName[_namespaceAIIsIndex] =
1098                            decodeIdentifyingNonEmptyStringOnFirstBitAsNamespaceName(false);
1099                           
1100                    _prefixIndex = _namespaceAIIsPrefixIndex[_namespaceAIIsIndex++] = -1;
1101                    break;
1102                // prefix, no namespace
1103
// Undeclaration of namespace
1104
case 2:
1105                    prefix = _namespaceAIIsPrefix[_namespaceAIIsIndex] =
1106                            decodeIdentifyingNonEmptyStringOnFirstBitAsPrefix(false);
1107                    namespaceName = _namespaceAIIsNamespaceName[_namespaceAIIsIndex] = "";
1108                    
1109                    _namespaceNameIndex = -1;
1110                    _namespaceAIIsPrefixIndex[_namespaceAIIsIndex++] = _prefixIndex;
1111                    break;
1112                // prefix, namespace
1113
// Declaration of prefixed namespace
1114
case 3:
1115                    prefix = _namespaceAIIsPrefix[_namespaceAIIsIndex] =
1116                            decodeIdentifyingNonEmptyStringOnFirstBitAsPrefix(true);
1117                    namespaceName = _namespaceAIIsNamespaceName[_namespaceAIIsIndex] =
1118                            decodeIdentifyingNonEmptyStringOnFirstBitAsNamespaceName(true);
1119                    
1120                    _namespaceAIIsPrefixIndex[_namespaceAIIsIndex++] = _prefixIndex;
1121                    break;
1122            }
1123            
1124            // Push namespace declarations onto the stack
1125
_prefixTable.pushScopeWithPrefixEntry(prefix, namespaceName, _prefixIndex, _namespaceNameIndex);
1126            
1127            b = read();
1128        }
1129        if (b != EncodingConstants.TERMINATOR) {
1130            throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.EIInamespaceNameNotTerminatedCorrectly"));
1131        }
1132        _currentNamespaceAIIsEnd = _namespaceAIIsIndex;
1133        
1134        b = read();
1135        switch(DecoderStateTables.EII[b]) {
1136            case DecoderStateTables.EII_NO_AIIS_INDEX_SMALL:
1137                processEII(_elementNameTable._array[b], hasAttributes);
1138                break;
1139            case DecoderStateTables.EII_INDEX_MEDIUM:
1140                processEII(processEIIIndexMedium(b), hasAttributes);
1141                break;
1142            case DecoderStateTables.EII_INDEX_LARGE:
1143                processEII(processEIIIndexLarge(b), hasAttributes);
1144                break;
1145            case DecoderStateTables.EII_LITERAL:
1146            {
1147                final QualifiedName qn = processLiteralQualifiedName(
1148                            b & EncodingConstants.LITERAL_QNAME_PREFIX_NAMESPACE_NAME_MASK);
1149                _elementNameTable.add(qn);
1150                processEII(qn, hasAttributes);
1151                break;
1152            }
1153            default:
1154                throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.IllegalStateDecodingEIIAfterAIIs"));
1155        }
1156    }
1157    
1158    protected final void processEII(QualifiedName name, boolean hasAttributes) throws FastInfosetException, IOException JavaDoc {
1159        if (_prefixTable._currentInScope[name.prefixIndex] != name.namespaceNameIndex) {
1160            throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.qnameOfEIINotInScope"));
1161        }
1162
1163        _eventType = START_ELEMENT;
1164        _qualifiedName = name;
1165
1166        if (_clearAttributes) {
1167            _attributes.clear();
1168            _clearAttributes = false;
1169        }
1170        
1171        if (hasAttributes) {
1172            processAIIs();
1173        }
1174        
1175        // Push element holder onto the stack
1176
_stackCount++;
1177        if (_stackCount == _qNameStack.length) {
1178            QualifiedName[] qNameStack = new QualifiedName[_qNameStack.length * 2];
1179            System.arraycopy(_qNameStack, 0, qNameStack, 0, _qNameStack.length);
1180            _qNameStack = qNameStack;
1181            
1182            int[] namespaceAIIsStartStack = new int[_namespaceAIIsStartStack.length * 2];
1183            System.arraycopy(_namespaceAIIsStartStack, 0, namespaceAIIsStartStack, 0, _namespaceAIIsStartStack.length);
1184            _namespaceAIIsStartStack = namespaceAIIsStartStack;
1185            
1186            int[] namespaceAIIsEndStack = new int[_namespaceAIIsEndStack.length * 2];
1187            System.arraycopy(_namespaceAIIsEndStack, 0, namespaceAIIsEndStack, 0, _namespaceAIIsEndStack.length);
1188            _namespaceAIIsEndStack = namespaceAIIsEndStack;
1189        }
1190        _qNameStack[_stackCount] = _qualifiedName;
1191        _namespaceAIIsStartStack[_stackCount] = _currentNamespaceAIIsStart;
1192        _namespaceAIIsEndStack[_stackCount] = _currentNamespaceAIIsEnd;
1193    }
1194    
1195    protected final void processAIIs() throws FastInfosetException, IOException JavaDoc {
1196        QualifiedName name;
1197        int b;
1198        String JavaDoc value;
1199        
1200        if (++_duplicateAttributeVerifier._currentIteration == Integer.MAX_VALUE) {
1201            _duplicateAttributeVerifier.clear();
1202        }
1203        
1204        _clearAttributes = true;
1205        boolean terminate = false;
1206        do {
1207            // AII qualified name
1208
b = read();
1209            switch (DecoderStateTables.AII[b]) {
1210                case DecoderStateTables.AII_INDEX_SMALL:
1211                    name = _attributeNameTable._array[b];
1212                    break;
1213                case DecoderStateTables.AII_INDEX_MEDIUM:
1214                {
1215                    final int i = (((b & EncodingConstants.INTEGER_2ND_BIT_MEDIUM_MASK) << 8) | read())
1216                            + EncodingConstants.INTEGER_2ND_BIT_SMALL_LIMIT;
1217                    name = _attributeNameTable._array[i];
1218                    break;
1219                }
1220                case DecoderStateTables.AII_INDEX_LARGE:
1221                {
1222                    final int i = (((b & EncodingConstants.INTEGER_2ND_BIT_LARGE_MASK) << 16) | (read() << 8) | read())
1223                            + EncodingConstants.INTEGER_2ND_BIT_MEDIUM_LIMIT;
1224                    name = _attributeNameTable._array[i];
1225                    break;
1226                }
1227                case DecoderStateTables.AII_LITERAL:
1228                    name = processLiteralQualifiedName(
1229                            b & EncodingConstants.LITERAL_QNAME_PREFIX_NAMESPACE_NAME_MASK);
1230                    name.createAttributeValues(_duplicateAttributeVerifier.MAP_SIZE);
1231                    _attributeNameTable.add(name);
1232                    break;
1233                case DecoderStateTables.AII_TERMINATOR_DOUBLE:
1234                    _internalState = INTERNAL_STATE_START_ELEMENT_TERMINATE;
1235                case DecoderStateTables.AII_TERMINATOR_SINGLE:
1236                    terminate = true;
1237                    // AIIs have finished break out of loop
1238
continue;
1239                default:
1240                    throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.decodingAIIs"));
1241            }
1242            
1243            // [normalized value] of AII
1244

1245            if (name.prefixIndex > 0 && _prefixTable._currentInScope[name.prefixIndex] != name.namespaceNameIndex) {
1246                throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.AIIqNameNotInScope"));
1247            }
1248
1249            _duplicateAttributeVerifier.checkForDuplicateAttribute(name.attributeHash, name.attributeId);
1250            
1251            b = read();
1252            switch(DecoderStateTables.NISTRING[b]) {
1253                case DecoderStateTables.NISTRING_UTF8_SMALL_LENGTH:
1254                    _octetBufferLength = (b & EncodingConstants.OCTET_STRING_LENGTH_5TH_BIT_SMALL_MASK) + 1;
1255                    value = decodeUtf8StringAsString();
1256                    if ((b & EncodingConstants.NISTRING_ADD_TO_TABLE_FLAG) > 0) {
1257                        _attributeValueTable.add(value);
1258                    }
1259                    
1260                    _attributes.addAttribute(name, value);
1261                    break;
1262                case DecoderStateTables.NISTRING_UTF8_MEDIUM_LENGTH:
1263                    _octetBufferLength = read() + EncodingConstants.OCTET_STRING_LENGTH_5TH_BIT_SMALL_LIMIT;
1264                    value = decodeUtf8StringAsString();
1265                    if ((b & EncodingConstants.NISTRING_ADD_TO_TABLE_FLAG) > 0) {
1266                        _attributeValueTable.add(value);
1267                    }
1268                    
1269                    _attributes.addAttribute(name, value);
1270                    break;
1271                case DecoderStateTables.NISTRING_UTF8_LARGE_LENGTH:
1272                    _octetBufferLength = ((read() << 24) |
1273                            (read() << 16) |
1274                            (read() << 8) |
1275                            read())
1276                            + EncodingConstants.OCTET_STRING_LENGTH_5TH_BIT_MEDIUM_LIMIT;
1277                    value = decodeUtf8StringAsString();
1278                    if ((b & EncodingConstants.NISTRING_ADD_TO_TABLE_FLAG) > 0) {
1279                        _attributeValueTable.add(value);
1280                    }
1281                    
1282                    _attributes.addAttribute(name, value);
1283                    break;
1284                case DecoderStateTables.NISTRING_UTF16_SMALL_LENGTH:
1285                    _octetBufferLength = (b & EncodingConstants.OCTET_STRING_LENGTH_5TH_BIT_SMALL_MASK) + 1;
1286                    value = decodeUtf16StringAsString();
1287                    if ((b & EncodingConstants.NISTRING_ADD_TO_TABLE_FLAG) > 0) {
1288                        _attributeValueTable.add(value);
1289                    }
1290                    
1291                    _attributes.addAttribute(name, value);
1292                    break;
1293                case DecoderStateTables.NISTRING_UTF16_MEDIUM_LENGTH:
1294                    _octetBufferLength = read() + EncodingConstants.OCTET_STRING_LENGTH_5TH_BIT_SMALL_LIMIT;
1295                    value = decodeUtf16StringAsString();
1296                    if ((b & EncodingConstants.NISTRING_ADD_TO_TABLE_FLAG) > 0) {
1297                        _attributeValueTable.add(value);
1298                    }
1299                    
1300                    _attributes.addAttribute(name, value);
1301                    break;
1302                case DecoderStateTables.NISTRING_UTF16_LARGE_LENGTH:
1303                    _octetBufferLength = ((read() << 24) |
1304                            (read() << 16) |
1305                            (read() << 8) |
1306                            read())
1307                            + EncodingConstants.OCTET_STRING_LENGTH_5TH_BIT_MEDIUM_LIMIT;
1308                    value = decodeUtf16StringAsString();
1309                    if ((b & EncodingConstants.NISTRING_ADD_TO_TABLE_FLAG) > 0) {
1310                        _attributeValueTable.add(value);
1311                    }
1312                    
1313                    _attributes.addAttribute(name, value);
1314                    break;
1315                case DecoderStateTables.NISTRING_RA:
1316                {
1317                    final boolean addToTable = (b & EncodingConstants.NISTRING_ADD_TO_TABLE_FLAG) > 0;
1318                    // Decode resitricted alphabet integer
1319
_identifier = (b & 0x0F) << 4;
1320                    b = read();
1321                    _identifier |= (b & 0xF0) >> 4;
1322                    
1323                    decodeOctetsOnFifthBitOfNonIdentifyingStringOnFirstBit(b);
1324                    
1325                    value = decodeRestrictedAlphabetAsString();
1326                    if (addToTable) {
1327                        _attributeValueTable.add(value);
1328                    }
1329                    
1330                    _attributes.addAttribute(name, value);
1331                    break;
1332                }
1333                case DecoderStateTables.NISTRING_EA:
1334                {
1335                    if ((b & EncodingConstants.NISTRING_ADD_TO_TABLE_FLAG) > 0) {
1336                        throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.addToTableNotSupported"));
1337                    }
1338                    
1339                    // Decode encoding algorithm integer
1340
_identifier = (b & 0x0F) << 4;
1341                    b = read();
1342                    _identifier |= (b & 0xF0) >> 4;
1343                    
1344                    decodeOctetsOnFifthBitOfNonIdentifyingStringOnFirstBit(b);
1345                    processAIIEncodingAlgorithm(name);
1346                    break;
1347                }
1348                case DecoderStateTables.NISTRING_INDEX_SMALL:
1349                    _attributes.addAttribute(name,
1350                            _attributeValueTable._array[b & EncodingConstants.INTEGER_2ND_BIT_SMALL_MASK]);
1351                    break;
1352                case DecoderStateTables.NISTRING_INDEX_MEDIUM:
1353                {
1354                    final int index = (((b & EncodingConstants.INTEGER_2ND_BIT_MEDIUM_MASK) << 8) | read())
1355                    + EncodingConstants.INTEGER_2ND_BIT_SMALL_LIMIT;
1356                    
1357                    _attributes.addAttribute(name,
1358                            _attributeValueTable._array[index]);
1359                    break;
1360                }
1361                case DecoderStateTables.NISTRING_INDEX_LARGE:
1362                {
1363                    final int index = (((b & EncodingConstants.INTEGER_2ND_BIT_LARGE_MASK) << 16) | (read() << 8) | read())
1364                    + EncodingConstants.INTEGER_2ND_BIT_MEDIUM_LIMIT;
1365                    
1366                    _attributes.addAttribute(name,
1367                            _attributeValueTable._array[index]);
1368                    break;
1369                }
1370                case DecoderStateTables.NISTRING_EMPTY:
1371                    _attributes.addAttribute(name, "");
1372                    break;
1373                default:
1374                    throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.decodingAIIValue"));
1375            }
1376            
1377        } while (!terminate);
1378        
1379        // Reset duplication attribute verfifier
1380
_duplicateAttributeVerifier._poolCurrent = _duplicateAttributeVerifier._poolHead;
1381    }
1382
1383    protected final QualifiedName processEIIIndexMedium(int b) throws FastInfosetException, IOException JavaDoc {
1384        final int i = (((b & EncodingConstants.INTEGER_3RD_BIT_MEDIUM_MASK) << 8) | read())
1385            + EncodingConstants.INTEGER_3RD_BIT_SMALL_LIMIT;
1386        return _elementNameTable._array[i];
1387    }
1388
1389    protected final QualifiedName processEIIIndexLarge(int b) throws FastInfosetException, IOException JavaDoc {
1390        int i;
1391        if ((b & EncodingConstants.INTEGER_3RD_BIT_LARGE_LARGE_FLAG) == 0x20) {
1392            // EII large index
1393
i = (((b & EncodingConstants.INTEGER_3RD_BIT_LARGE_MASK) << 16) | (read() << 8) | read())
1394                + EncodingConstants.INTEGER_3RD_BIT_MEDIUM_LIMIT;
1395        } else {
1396            // EII large large index
1397
i = (((read() & EncodingConstants.INTEGER_3RD_BIT_LARGE_LARGE_MASK) << 16) | (read() << 8) | read())
1398                + EncodingConstants.INTEGER_3RD_BIT_LARGE_LIMIT;
1399        }
1400        return _elementNameTable._array[i];
1401    }
1402    
1403    protected final QualifiedName processLiteralQualifiedName(int state) throws FastInfosetException, IOException JavaDoc {
1404        switch (state) {
1405            // no prefix, no namespace
1406
case 0:
1407                return new QualifiedName(
1408                        "",
1409                        "",
1410                        decodeIdentifyingNonEmptyStringOnFirstBit(_v.localName),
1411                        "",
1412                        0,
1413                        -1,
1414                        -1,
1415                        _identifier);
1416            // no prefix, namespace
1417
case 1:
1418                return new QualifiedName(
1419                        "",
1420                        decodeIdentifyingNonEmptyStringIndexOnFirstBitAsNamespaceName(false),
1421                        decodeIdentifyingNonEmptyStringOnFirstBit(_v.localName),
1422                        "",
1423                        0,
1424                        -1,
1425                        _namespaceNameIndex,
1426                        _identifier);
1427            // prefix, no namespace
1428
case 2:
1429                throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.qNameMissingNamespaceName"));
1430            // prefix, namespace
1431
case 3:
1432                return new QualifiedName(
1433                        decodeIdentifyingNonEmptyStringIndexOnFirstBitAsPrefix(true),
1434                        decodeIdentifyingNonEmptyStringIndexOnFirstBitAsNamespaceName(true),
1435                        decodeIdentifyingNonEmptyStringOnFirstBit(_v.localName),
1436                        "",
1437                        0,
1438                        _prefixIndex,
1439                        _namespaceNameIndex,
1440                        _identifier);
1441            default:
1442                throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.decodingEII"));
1443        }
1444    }
1445    
1446    protected final void processCommentII() throws FastInfosetException, IOException JavaDoc {
1447        _eventType = COMMENT;
1448        
1449        switch(decodeNonIdentifyingStringOnFirstBit()) {
1450            case NISTRING_STRING:
1451                if (_addToTable) {
1452                    _v.otherString.add(new CharArray(_charBuffer, 0, _charBufferLength, true));
1453                }
1454                
1455                _characters = _charBuffer;
1456                _charactersOffset = 0;
1457                _charactersLength = _charBufferLength;
1458                break;
1459            case NISTRING_ENCODING_ALGORITHM:
1460                throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.commentIIAlgorithmNotSupported"));
1461            case NISTRING_INDEX:
1462                final CharArray ca = _v.otherString.get(_integer);
1463                
1464                _characters = ca.ch;
1465                _charactersOffset = ca.start;
1466                _charactersLength = ca.length;
1467                break;
1468            case NISTRING_EMPTY_STRING:
1469                _characters = _charBuffer;
1470                _charactersOffset = 0;
1471                _charactersLength = 0;
1472                break;
1473        }
1474    }
1475    
1476    protected final void processProcessingII() throws FastInfosetException, IOException JavaDoc {
1477        _eventType = PROCESSING_INSTRUCTION;
1478        
1479        _piTarget = decodeIdentifyingNonEmptyStringOnFirstBit(_v.otherNCName);
1480        
1481        switch(decodeNonIdentifyingStringOnFirstBit()) {
1482            case NISTRING_STRING:
1483                _piData = new String JavaDoc(_charBuffer, 0, _charBufferLength);
1484                if (_addToTable) {
1485                    _v.otherString.add(new CharArrayString(_piData));
1486                }
1487                break;
1488            case NISTRING_ENCODING_ALGORITHM:
1489                throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.processingIIWithEncodingAlgorithm"));
1490            case NISTRING_INDEX:
1491                _piData = _v.otherString.get(_integer).toString();
1492                break;
1493            case NISTRING_EMPTY_STRING:
1494                _piData = "";
1495                break;
1496        }
1497    }
1498    
1499    protected final void processCIIEncodingAlgorithm() throws FastInfosetException, IOException JavaDoc {
1500        _algorithmData = _octetBuffer;
1501        _algorithmDataOffset = _octetBufferStart;
1502        _algorithmDataLength = _octetBufferLength;
1503            
1504        if (_algorithmId >= EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START) {
1505            _algorithmURI = _v.encodingAlgorithm.get(_algorithmId - EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START);
1506            if (_algorithmURI == null) {
1507                throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.URINotPresent", new Object JavaDoc[]{new Integer JavaDoc(_identifier)}));
1508            }
1509        } else if (_algorithmId > EncodingConstants.ENCODING_ALGORITHM_BUILTIN_END) {
1510            // Reserved built-in algorithms for future use
1511
// TODO should use sax property to decide if event will be
1512
// reported, allows for support through handler if required.
1513
throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.identifiers10to31Reserved"));
1514        }
1515    }
1516    
1517    protected final void processAIIEncodingAlgorithm(QualifiedName name) throws FastInfosetException, IOException JavaDoc {
1518        String JavaDoc URI = null;
1519        if (_identifier >= EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START) {
1520            URI = _v.encodingAlgorithm.get(_identifier - EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START);
1521            if (URI == null) {
1522                throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.URINotPresent", new Object JavaDoc[]{new Integer JavaDoc(_identifier)}));
1523            }
1524        } else if (_identifier >= EncodingConstants.ENCODING_ALGORITHM_BUILTIN_END) {
1525            if (_identifier == EncodingAlgorithmIndexes.CDATA) {
1526                throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.CDATAAlgorithmNotSupported"));
1527            }
1528            
1529            // Reserved built-in algorithms for future use
1530
// TODO should use sax property to decide if event will be
1531
// reported, allows for support through handler if required.
1532
throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.identifiers10to31Reserved"));
1533        }
1534        
1535        final byte[] data = new byte[_octetBufferLength];
1536        System.arraycopy(_octetBuffer, _octetBufferStart, data, 0, _octetBufferLength);
1537        _attributes.addAttributeWithAlgorithmData(name, URI, _identifier, data);
1538    }
1539
1540    protected final void convertEncodingAlgorithmDataToCharacters() throws FastInfosetException, IOException JavaDoc {
1541        StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
1542        if (_algorithmId < EncodingConstants.ENCODING_ALGORITHM_BUILTIN_END) {
1543            Object JavaDoc array = BuiltInEncodingAlgorithmFactory.table[_algorithmId].
1544                decodeFromBytes(_algorithmData, _algorithmDataOffset, _algorithmDataLength);
1545            BuiltInEncodingAlgorithmFactory.table[_algorithmId].convertToCharacters(array, buffer);
1546        } else if (_algorithmId == EncodingAlgorithmIndexes.CDATA) {
1547            _octetBufferOffset -= _octetBufferLength;
1548            decodeUtf8StringIntoCharBuffer();
1549            
1550            _characters = _charBuffer;
1551            _charactersOffset = 0;
1552            _charactersLength = _charBufferLength;
1553            return;
1554        } else if (_algorithmId >= EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START) {
1555            final EncodingAlgorithm ea = (EncodingAlgorithm)_registeredEncodingAlgorithms.get(_algorithmURI);
1556            if (ea != null) {
1557                final Object JavaDoc data = ea.decodeFromBytes(_octetBuffer, _octetBufferStart, _octetBufferLength);
1558                ea.convertToCharacters(data, buffer);
1559            } else {
1560                throw new EncodingAlgorithmException(
1561                        CommonResourceBundle.getInstance().getString("message.algorithmDataCannotBeReported"));
1562            }
1563        }
1564        
1565        _characters = new char[buffer.length()];
1566        buffer.getChars(0, buffer.length(), _characters, 0);
1567        _charactersOffset = 0;
1568        _charactersLength = _characters.length;
1569    }
1570    
1571    protected class NamespaceContextImpl implements NamespaceContext JavaDoc {
1572        public final String JavaDoc getNamespaceURI(String JavaDoc prefix) {
1573            return _prefixTable.getNamespaceFromPrefix(prefix);
1574        }
1575        
1576        public final String JavaDoc getPrefix(String JavaDoc namespaceURI) {
1577            return _prefixTable.getPrefixFromNamespace(namespaceURI);
1578        }
1579        
1580        public final Iterator JavaDoc getPrefixes(String JavaDoc namespaceURI) {
1581            return _prefixTable.getPrefixesFromNamespace(namespaceURI);
1582        }
1583    }
1584    
1585    public final String JavaDoc getNamespaceDecl(String JavaDoc prefix) {
1586        return _prefixTable.getNamespaceFromPrefix(prefix);
1587    }
1588        
1589    public final String JavaDoc getURI(String JavaDoc prefix) {
1590        return getNamespaceDecl(prefix);
1591    }
1592    
1593    public final Iterator JavaDoc getPrefixes() {
1594        return _prefixTable.getPrefixes();
1595    }
1596    
1597    public final AttributesHolder getAttributesHolder() {
1598        return _attributes;
1599    }
1600    
1601    public final void setManager(StAXManager manager) {
1602        _manager = manager;
1603    }
1604    
1605    final static String JavaDoc getEventTypeString(int eventType) {
1606        switch (eventType){
1607            case START_ELEMENT:
1608                return "START_ELEMENT";
1609            case END_ELEMENT:
1610                return "END_ELEMENT";
1611            case PROCESSING_INSTRUCTION:
1612                return "PROCESSING_INSTRUCTION";
1613            case CHARACTERS:
1614                return "CHARACTERS";
1615            case COMMENT:
1616                return "COMMENT";
1617            case START_DOCUMENT:
1618                return "START_DOCUMENT";
1619            case END_DOCUMENT:
1620                return "END_DOCUMENT";
1621            case ENTITY_REFERENCE:
1622                return "ENTITY_REFERENCE";
1623            case ATTRIBUTE:
1624                return "ATTRIBUTE";
1625            case DTD:
1626                return "DTD";
1627            case CDATA:
1628                return "CDATA";
1629        }
1630        return "UNKNOWN_EVENT_TYPE";
1631    }
1632    
1633}
1634
Popular Tags