KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > OMStAXWrapper


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.om.impl.llom;
17
18 import org.apache.axis2.om.*;
19 import org.apache.axis2.om.impl.llom.exception.OMStreamingException;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.xml.namespace.NamespaceContext JavaDoc;
24 import javax.xml.namespace.QName JavaDoc;
25 import javax.xml.stream.Location;
26 import javax.xml.stream.XMLStreamConstants;
27 import javax.xml.stream.XMLStreamException;
28 import javax.xml.stream.XMLStreamReader;
29 import java.util.Iterator JavaDoc;
30 import java.util.Stack JavaDoc;
31
32 /**
33  * Note - This class also implements the streaming constants interface
34  * to get access to the StAX constants
35  */

36 public class OMStAXWrapper implements XMLStreamReader, XMLStreamConstants {
37     /**
38      * Field log
39      */

40     private Log log = LogFactory.getLog(getClass());
41
42     /**
43      * Field navigator
44      */

45     private OMNavigator navigator;
46
47     /**
48      * Field builder
49      */

50     private OMXMLParserWrapper builder;
51
52     /**
53      * Field parser
54      */

55     private XMLStreamReader parser;
56
57     /**
58      * Field rootNode
59      */

60     private OMNode rootNode;
61
62     /**
63      * Field isFirst
64      */

65     private boolean isFirst = true;
66
67     // navigable means the output should be taken from the navigator
68
// as soon as the navigator returns a null navigable will be reset
69
// to false and the subsequent events will be taken from the builder
70
// or the parser directly
71

72     /**
73      * Field NAVIGABLE
74      */

75     private static final short NAVIGABLE = 0;
76     private static final short SWITCH_AT_NEXT = 1;
77     private static final short COMPLETED = 2;
78     private static final short SWITCHED = 3;
79     private static final short DOCUMENT_COMPLETE = 4;
80
81
82     /**
83      * Field state
84      */

85     private short state;
86
87     /**
88      * Field currentEvent
89      * Default set to START_DOCUMENT
90      */

91     private int currentEvent = START_DOCUMENT;
92
93     // SwitchingAllowed is set to false by default
94
// this means that unless the user explicitly states
95
// that he wants things not to be cached, everything will
96
// be cached
97

98     /**
99      * Field switchingAllowed
100      */

101     boolean switchingAllowed = false;
102
103     /**
104      * Field elementStack
105      */

106     private Stack JavaDoc elementStack = new Stack JavaDoc();
107
108     // keeps the next event. The parser actually keeps one step ahead to
109
// detect the end of navigation. (at the end of the stream the navigator
110
// returns a null
111

112     /**
113      * Field nextNode
114      */

115     private OMNode nextNode = null;
116
117     // holder for the current node. Needs this to generate events from the current node
118

119     /**
120      * Field currentNode
121      */

122     private OMNode currentNode = null;
123
124     // needs this to refer to the last known node
125

126     /**
127      * Field lastNode
128      */

129     private OMNode lastNode = null;
130
131     /**
132      * Method setAllowSwitching
133      *
134      * @param b
135      */

136     public void setAllowSwitching(boolean b) {
137         this.switchingAllowed = b;
138     }
139
140     /**
141      * Method isAllowSwitching
142      *
143      * @return
144      */

145     public boolean isAllowSwitching() {
146         return switchingAllowed;
147     }
148
149     /**
150      * When constructing the OMStaxWrapper, the creator must produce the
151      * builder (an instance of the OMXMLparserWrapper of the input) and the
152      * Element Node to start parsing. The wrapper wil parse(proceed) until
153      * the end of the given element. hence care should be taken to pass the
154      * root element if the entire document is needed
155      *
156      * @param builder
157      * @param startNode
158      */

159     OMStAXWrapper(OMXMLParserWrapper builder, OMElement startNode) {
160         this(builder, startNode, false);
161     }
162
163     /**
164      * Constructor OMStAXWrapper
165      *
166      * @param builder
167      * @param startNode
168      * @param cache
169      */

170     OMStAXWrapper(OMXMLParserWrapper builder, OMElement startNode,
171                   boolean cache) {
172
173         // create a navigator
174
this.navigator = new OMNavigator(startNode);
175         this.builder = builder;
176         this.rootNode = startNode;
177
178         // initaite the next and current nodes
179
// Note - navigator is written in such a way that it first
180
// returns the starting node at the first call to it
181
currentNode = navigator.next();
182         updateNextNode();
183         switchingAllowed = !cache;
184     }
185
186     /**
187      * @return
188      * @see javax.xml.stream.XMLStreamReader#getPrefix()
189      */

190     public String JavaDoc getPrefix() {
191         String JavaDoc returnStr = null;
192         if (parser != null) {
193             returnStr = parser.getPrefix();
194         } else {
195             if ((currentEvent == START_ELEMENT)
196                     || (currentEvent == END_ELEMENT)) {
197                 OMNamespace ns = ((OMElement) lastNode).getNamespace();
198                 returnStr = (ns == null)
199                         ? null
200                         : ns.getPrefix();
201             }
202         }
203         return returnStr;
204     }
205
206     /**
207      * @return
208      * @see javax.xml.stream.XMLStreamReader#getNamespaceURI()
209      */

210     public String JavaDoc getNamespaceURI() {
211         String JavaDoc returnStr = null;
212         if (parser != null) {
213             returnStr = parser.getNamespaceURI();
214         } else {
215             if ((currentEvent == START_ELEMENT)
216                     || (currentEvent == END_ELEMENT)
217                     || (currentEvent == NAMESPACE)) {
218                 OMNamespace ns = ((OMElement) lastNode).getNamespace();
219                 returnStr = (ns == null)
220                         ? null
221                         : ns.getName();
222             }
223         }
224         return returnStr;
225     }
226
227     /**
228      * @return
229      * @see javax.xml.stream.XMLStreamReader#hasName()
230      */

231     public boolean hasName() {
232         if (parser != null) {
233             return parser.hasName();
234         } else {
235             return ((currentEvent == START_ELEMENT)
236                                || (currentEvent == END_ELEMENT));
237         }
238     }
239
240     /**
241      * @return
242      * @see javax.xml.stream.XMLStreamReader#getLocalName()
243      */

244     public String JavaDoc getLocalName() {
245         String JavaDoc returnStr = null;
246         if (parser != null) {
247             returnStr = parser.getLocalName();
248         } else {
249             if ((currentEvent == START_ELEMENT)
250                     || (currentEvent == END_ELEMENT)
251                     || (currentEvent == ENTITY_REFERENCE)) {
252                 returnStr = ((OMElement) lastNode).getLocalName();
253             }
254         }
255         return returnStr;
256     }
257
258     /**
259      * @return
260      * @see javax.xml.stream.XMLStreamReader#getName()
261      */

262     public QName JavaDoc getName() {
263         QName JavaDoc returnName = null;
264         if (parser != null) {
265             returnName = parser.getName();
266         } else {
267             if ((currentEvent == START_ELEMENT)
268                     || (currentEvent == END_ELEMENT)) {
269                 returnName = getQName((OMElement) lastNode);
270             }
271         }
272         return returnName;
273     }
274
275     /**
276      * @return
277      * @see javax.xml.stream.XMLStreamReader#hasText()
278      */

279     public boolean hasText() {
280         return ((currentEvent == CHARACTERS) || (currentEvent == DTD)
281                            || (currentEvent == ENTITY_REFERENCE)
282                            || (currentEvent == COMMENT) || (currentEvent == SPACE));
283     }
284
285     /**
286      * @return
287      * @see javax.xml.stream.XMLStreamReader#getTextLength()
288      */

289     public int getTextLength() {
290         int returnLength = 0;
291         if (parser != null) {
292             returnLength = parser.getTextLength();
293         } else {
294             OMText textNode = (OMText) lastNode;
295             returnLength = textNode.getText().length();
296         }
297         return returnLength;
298     }
299
300     /**
301      * @return
302      * @see javax.xml.stream.XMLStreamReader#getTextStart()
303      */

304     public int getTextStart() {
305         int returnLength = 0;
306         if (parser != null) {
307             returnLength = parser.getTextStart();
308         }
309
310         // Note - this has no relevant method in the OM
311
return returnLength;
312     }
313
314     /**
315      * @param i
316      * @param chars
317      * @param i1
318      * @param i2
319      * @return
320      * @throws XMLStreamException
321      * @see javax.xml.stream.XMLStreamReader#getTextCharacters(int, char[], int, int)
322      */

323     public int getTextCharacters(int i, char[] chars, int i1, int i2)
324             throws XMLStreamException {
325         int returnLength = 0;
326         if (hasText()) {
327             if (parser != null) {
328                 try {
329                     returnLength = parser.getTextCharacters(i, chars, i1, i2);
330                 } catch (XMLStreamException e) {
331                     throw new OMStreamingException(e);
332                 }
333             }
334
335             // Note - this has no relevant method in the OM
336
}
337         return returnLength;
338     }
339
340     /**
341      * @return
342      * @see javax.xml.stream.XMLStreamReader#getTextCharacters()
343      */

344     public char[] getTextCharacters() {
345         char[] returnArray = null;
346         if (parser != null) {
347             returnArray = parser.getTextCharacters();
348         } else {
349             if (hasText()) {
350                 OMText textNode = (OMText) lastNode;
351                 String JavaDoc str = textNode.getText();
352                 returnArray = str.toCharArray();
353             }
354         }
355         return returnArray;
356     }
357
358     /**
359      * @return
360      * @see javax.xml.stream.XMLStreamReader#getText()
361      */

362     public String JavaDoc getText() {
363         String JavaDoc returnString = null;
364         if (parser != null) {
365             returnString = parser.getText();
366         } else {
367             if (hasText()) {
368                 OMText textNode = (OMText) lastNode;
369                 returnString = textNode.getText();
370             }
371         }
372         return returnString;
373     }
374
375     /**
376      * @return
377      * @see javax.xml.stream.XMLStreamReader#getEventType()
378      */

379
380     // todo this should be improved
381
public int getEventType() {
382         return currentEvent;
383     }
384
385     /**
386      * @param i
387      * @return
388      * @see javax.xml.stream.XMLStreamReader#getNamespaceURI
389      */

390     public String JavaDoc getNamespaceURI(int i) {
391         String JavaDoc returnString = null;
392         if (parser != null) {
393             returnString = parser.getNamespaceURI(i);
394         } else {
395             if (isStartElement() || isEndElement()
396                     || (currentEvent == NAMESPACE)) {
397                 OMNamespace ns = (OMNamespace) getItemFromIterator(
398                         ((OMElement) lastNode).getAllDeclaredNamespaces(), i);
399                 returnString = (ns == null)
400                         ? null
401                         : ns.getName();
402             }
403         }
404         return returnString;
405     }
406
407     /**
408      * @param i
409      * @return
410      * @see javax.xml.stream.XMLStreamReader#getNamespacePrefix
411      */

412     public String JavaDoc getNamespacePrefix(int i) {
413         String JavaDoc returnString = null;
414         if (parser != null) {
415             returnString = parser.getNamespacePrefix(i);
416         } else {
417             if (isStartElement() || isEndElement()
418                     || (currentEvent == NAMESPACE)) {
419                 OMNamespace ns = (OMNamespace) getItemFromIterator(
420                         ((OMElement) lastNode).getAllDeclaredNamespaces(), i);
421                 returnString = (ns == null)
422                         ? null
423                         : ns.getPrefix();
424             }
425         }
426         return returnString;
427     }
428
429     /**
430      * @return
431      * @see javax.xml.stream.XMLStreamReader#getNamespaceCount()
432      */

433     public int getNamespaceCount() {
434         int returnCount = 0;
435         if (parser != null) {
436             returnCount = parser.getNamespaceCount();
437         } else {
438             if (isStartElement() || isEndElement()
439                     || (currentEvent == NAMESPACE)) {
440                 returnCount =
441                 getCount(((OMElement) lastNode).getAllDeclaredNamespaces());
442             }
443         }
444         return returnCount;
445     }
446
447     /**
448      * @param i
449      * @return
450      * @see javax.xml.stream.XMLStreamReader#isAttributeSpecified
451      */

452     public boolean isAttributeSpecified(int i) {
453         boolean returnValue = false;
454         if (parser != null) {
455             returnValue = parser.isAttributeSpecified(i);
456         } else {
457             if (isStartElement() || (currentEvent == ATTRIBUTE)) {
458
459                 // theres nothing to be returned here
460
} else {
461                 throw new IllegalStateException JavaDoc(
462                         "attribute type accessed in illegal event!");
463             }
464         }
465         return returnValue;
466     }
467
468     /**
469      * @param i
470      * @return
471      * @see javax.xml.stream.XMLStreamReader#getAttributeValue
472      */

473     public String JavaDoc getAttributeValue(int i) {
474         String JavaDoc returnString = null;
475         if (parser != null) {
476             returnString = parser.getAttributeValue(i);
477         } else {
478             if (isStartElement() || (currentEvent == ATTRIBUTE)) {
479                 OMAttribute attrib = getAttribute((OMElement) lastNode, i);
480                 if (attrib != null) {
481                     returnString = attrib.getValue();
482                 }
483             } else {
484                 throw new IllegalStateException JavaDoc(
485                         "attribute type accessed in illegal event!");
486             }
487         }
488         return returnString;
489     }
490
491     /**
492      * @param i
493      * @return
494      * @see javax.xml.stream.XMLStreamReader#getAttributeType
495      */

496     public String JavaDoc getAttributeType(int i) {
497         String JavaDoc returnString = null;
498         if (parser != null) {
499             returnString = parser.getAttributeType(i);
500         } else {
501             if (isStartElement() || (currentEvent == ATTRIBUTE)) {
502
503                 // todo implement this
504
} else {
505                 throw new IllegalStateException JavaDoc(
506                         "attribute type accessed in illegal event!");
507             }
508         }
509         return returnString;
510     }
511
512     /**
513      * @param i
514      * @return
515      * @see javax.xml.stream.XMLStreamReader#getAttributePrefix
516      */

517     public String JavaDoc getAttributePrefix(int i) {
518         String JavaDoc returnString = null;
519         if (parser != null) {
520             returnString = parser.getAttributePrefix(i);
521         } else {
522             if (isStartElement() || (currentEvent == ATTRIBUTE)) {
523                 OMAttribute attrib = getAttribute((OMElement) lastNode, i);
524                 if (attrib != null) {
525                     OMNamespace nameSpace = attrib.getNamespace();
526                     if (nameSpace != null) {
527                         returnString = nameSpace.getPrefix();
528                     }
529                 }
530             } else {
531                 throw new IllegalStateException JavaDoc(
532                         "attribute prefix accessed in illegal event!");
533             }
534         }
535         return returnString;
536     }
537
538     /**
539      * @param i
540      * @return
541      * @see javax.xml.stream.XMLStreamReader#getAttributeLocalName
542      */

543     public String JavaDoc getAttributeLocalName(int i) {
544         String JavaDoc returnString = null;
545         if (parser != null) {
546             returnString = parser.getAttributeLocalName(i);
547         } else {
548             if (isStartElement() || (currentEvent == ATTRIBUTE)) {
549                 OMAttribute attrib = getAttribute((OMElement) lastNode, i);
550                 if (attrib != null) {
551                     returnString = attrib.getLocalName();
552                 }
553             } else {
554                 throw new IllegalStateException JavaDoc(
555                         "attribute localName accessed in illegal event!");
556             }
557         }
558         return returnString;
559     }
560
561     /**
562      * @param i
563      * @return
564      * @see javax.xml.stream.XMLStreamReader#getAttributeNamespace
565      */

566     public String JavaDoc getAttributeNamespace(int i) {
567         String JavaDoc returnString = null;
568         if (parser != null) {
569             returnString = parser.getAttributeNamespace(i);
570         } else {
571             if (isStartElement() || (currentEvent == ATTRIBUTE)) {
572                 OMAttribute attrib = getAttribute((OMElement) lastNode, i);
573                 if (attrib != null) {
574                     OMNamespace nameSpace = attrib.getNamespace();
575                     if (nameSpace != null) {
576                         returnString = nameSpace.getName();
577                     }
578                 }
579             } else {
580                 throw new IllegalStateException JavaDoc(
581                         "attribute nameSpace accessed in illegal event!");
582             }
583         }
584         return returnString;
585     }
586
587     /**
588      * @param i
589      * @return
590      * @see javax.xml.stream.XMLStreamReader#getAttributeName
591      */

592     public QName JavaDoc getAttributeName(int i) {
593         QName JavaDoc returnQName = null;
594         if (parser != null) {
595             returnQName = parser.getAttributeName(i);
596         } else {
597             if (isStartElement() || (currentEvent == ATTRIBUTE)) {
598                 returnQName = getAttribute((OMElement) lastNode, i).getQName();
599             } else {
600                 throw new IllegalStateException JavaDoc(
601                         "attribute count accessed in illegal event!");
602             }
603         }
604         return returnQName;
605     }
606
607     /**
608      * @return
609      * @see javax.xml.stream.XMLStreamReader#getAttributeCount
610      */

611     public int getAttributeCount() {
612         int returnCount = 0;
613         if (parser != null) {
614             returnCount = parser.getAttributeCount();
615         } else {
616             if (isStartElement() || (currentEvent == ATTRIBUTE)) {
617                 OMElement elt = (OMElement) lastNode;
618                 returnCount = getCount(elt.getAttributes());
619             } else {
620                 throw new IllegalStateException JavaDoc(
621                         "attribute count accessed in illegal event!");
622             }
623         }
624         return returnCount;
625     }
626
627     // todo
628

629     /**
630      * Method getAttributeValue
631      *
632      * @param s
633      * @param s1
634      * @return
635      */

636     public String JavaDoc getAttributeValue(String JavaDoc s, String JavaDoc s1) {
637         throw new UnsupportedOperationException JavaDoc();
638     }
639
640     /**
641      * Method isWhiteSpace
642      *
643      * @return
644      */

645     public boolean isWhiteSpace() {
646         boolean b;
647         if (parser != null) {
648             b = parser.isWhiteSpace();
649         } else {
650             b = (currentEvent == SPACE);
651         }
652         return b;
653     }
654
655     /**
656      * Method isCharacters
657      *
658      * @return
659      */

660     public boolean isCharacters() {
661         boolean b;
662         if (parser != null) {
663             b = parser.isCharacters();
664         } else {
665             b = (currentEvent == CHARACTERS);
666         }
667         return b;
668     }
669
670     /**
671      * Method isEndElement
672      *
673      * @return
674      */

675     public boolean isEndElement() {
676         boolean b;
677         if (parser != null) {
678             b = parser.isEndElement();
679         } else {
680             b = (currentEvent == END_ELEMENT);
681         }
682         return b;
683     }
684
685     /**
686      * @param i
687      * @param s
688      * @param s1
689      * @throws XMLStreamException
690      * @see javax.xml.stream.XMLStreamReader#require(int, String, String)
691      */

692     public void require(int i, String JavaDoc s, String JavaDoc s1) throws XMLStreamException {
693         throw new XMLStreamException();
694     }
695
696     /**
697      * Method isStartElement
698      *
699      * @return
700      */

701     public boolean isStartElement() {
702         boolean b;
703         if (parser != null) {
704             b = parser.isStartElement();
705         } else {
706             b = (currentEvent == START_ELEMENT);
707         }
708         return b;
709     }
710
711     /**
712      * Method getNamespaceURI
713      *
714      * @param s
715      * @return
716      */

717     public String JavaDoc getNamespaceURI(String JavaDoc s) {
718         String JavaDoc returnString = null;
719         if (parser != null) {
720             returnString = parser.getNamespaceURI(s);
721         } else {
722             if (isStartElement() || isEndElement()
723                     || (currentEvent == NAMESPACE)) {
724
725                 // Nothing to do here! How to get the namespacace references
726
}
727         }
728         return returnString;
729     }
730
731     /**
732      * Method close
733      *
734      * @throws XMLStreamException
735      */

736     public void close() throws XMLStreamException {
737
738         // this doesnot mean anything with respect to the OM
739
if (parser != null) {
740             parser.close();
741         }
742     }
743
744     /**
745      * Method hasNext
746      *
747      * @return
748      * @throws XMLStreamException
749      */

750     public boolean hasNext() throws XMLStreamException {
751         return !(state == DOCUMENT_COMPLETE);
752     }
753
754     /**
755      * Not implemented yet
756      *
757      * @return
758      * @throws org.apache.axis2.om.impl.llom.exception.OMStreamingException
759      *
760      * @throws XMLStreamException
761      */

762     public int nextTag() throws XMLStreamException {
763         throw new UnsupportedOperationException JavaDoc();
764     }
765
766     /**
767      * @return
768      * @throws XMLStreamException
769      * @see javax.xml.stream.XMLStreamReader#getElementText()
770      */

771     public String JavaDoc getElementText() throws XMLStreamException {
772         String JavaDoc returnText = "";
773         if (parser != null) {
774             try {
775                 returnText = parser.getElementText();
776             } catch (XMLStreamException e) {
777                 throw new OMStreamingException(e);
778             }
779         } else {
780             if (currentNode.getType() == OMNode.ELEMENT_NODE) {
781
782                 // todo complete this
783
return null;
784             }
785         }
786         return returnText;
787     }
788
789     /**
790      * Method next
791      *
792      * @return
793      * @throws XMLStreamException
794      */

795     public int next() throws XMLStreamException {
796         switch (state) {
797             case DOCUMENT_COMPLETE:
798                 throw new XMLStreamException("End of the document reached");
799             case COMPLETED:
800                 state = DOCUMENT_COMPLETE;
801                 currentEvent = END_DOCUMENT;
802                 break;
803             case SWITCH_AT_NEXT:
804                 state = SWITCHED;
805
806                 // load the parser
807
try {
808                     parser = (XMLStreamReader) builder.getParser();
809                 } catch (Exception JavaDoc e) {
810                     throw new UnsupportedOperationException JavaDoc(
811                             "incompatible parser found!");
812                 }
813                 log.info(
814                         "Switching to the Real Stax parser to generated the future events");
815
816                 if ((currentEvent == START_DOCUMENT) && (currentEvent == parser.getEventType())){
817                     currentEvent = parser.next();
818                 } else{
819                     currentEvent = parser.getEventType();
820                 }
821                 updateCompleteStatus();
822                 break;
823             case NAVIGABLE:
824                 currentEvent = generateEvents(currentNode);
825                 updateCompleteStatus();
826                 updateLastNode();
827                 break;
828             case SWITCHED:
829                 currentEvent = parser.next();
830                 updateCompleteStatus();
831                 break;
832             default :
833                 throw new OMStreamingException("unsuppported state!");
834         }
835         return currentEvent;
836     }
837
838     /**
839      * Method getProperty
840      *
841      * @param s
842      * @return
843      * @throws IllegalArgumentException
844      */

845     public Object JavaDoc getProperty(String JavaDoc s) throws IllegalArgumentException JavaDoc {
846         throw new UnsupportedOperationException JavaDoc();
847     }
848
849     /**
850      * This is a very important method. this keeps the
851      * navigator one step ahead and pushes the navigator
852      * one event ahead. If the nextNode is null then navigable is set to false;
853      * At the same time the parser and builder are set up for the upcoming event
854      * generation
855      *
856      * @throws XMLStreamException
857      */

858     private void updateLastNode() throws XMLStreamException {
859         lastNode = currentNode;
860         currentNode = nextNode;
861         try {
862             updateNextNode();
863         } catch (Exception JavaDoc e) {
864             throw new XMLStreamException(e);
865         }
866     }
867
868     /**
869      * Method updateNextNode
870      */

871     private void updateNextNode() {
872         if (navigator.isNavigable()) {
873             nextNode = navigator.next();
874         } else {
875             if (!switchingAllowed) {
876                 if (navigator.isCompleted()) {
877                     nextNode = null;
878
879                 } else {
880                     builder.next();
881                     navigator.step();
882                     nextNode = navigator.next();
883                 }
884             } else {
885
886                 // reset caching (the default is ON so it was not needed in the
887
// earlier case!
888
builder.setCache(false);
889                 state = SWITCH_AT_NEXT;
890             }
891         }
892     }
893
894     /**
895      * Method updateCompleteStatus
896      */

897     private void updateCompleteStatus() {
898         if (state == NAVIGABLE) {
899             if (rootNode == currentNode) {
900                 if (isFirst) {
901                     isFirst = false;
902                 } else {
903                     state = COMPLETED;
904                 }
905             }
906         } else {
907             state = (currentEvent == END_DOCUMENT)
908                     ? DOCUMENT_COMPLETE
909                     : state;
910         }
911     }
912
913     /**
914      * Method getNamespaceContext
915      *
916      * @return
917      */

918     public NamespaceContext JavaDoc getNamespaceContext() {
919         throw new UnsupportedOperationException JavaDoc();
920     }
921
922     /**
923      * Method getEncoding
924      *
925      * @return
926      */

927     public String JavaDoc getEncoding() {
928         return null;
929     }
930
931     /**
932      * Method getLocation
933      *
934      * @return
935      */

936     public Location getLocation() {
937         throw new UnsupportedOperationException JavaDoc();
938     }
939
940     /**
941      * Method getVersion
942      *
943      * @return
944      */

945     public String JavaDoc getVersion() {
946 // throw new UnsupportedOperationException();
947
return "1.0"; //todo put the constant
948
}
949
950
951     /**
952      * Method isStandalone
953      *
954      * @return
955      */

956     public boolean isStandalone() {
957        return true;
958     }
959
960     /**
961      * Method standaloneSet
962      *
963      * @return
964      */

965     public boolean standaloneSet() {
966         return false;
967     }
968
969     /**
970      * Method getCharacterEncodingScheme
971      *
972      * @return
973      */

974     public String JavaDoc getCharacterEncodingScheme() {
975 // throw new UnsupportedOperationException();
976
// throw new UnsupportedOperationException();
977
return "utf-8";
978     }
979
980     /**
981      * Method getPITarget
982      *
983      * @return
984      */

985     public String JavaDoc getPITarget() {
986         throw new UnsupportedOperationException JavaDoc();
987     }
988
989     /**
990      * Method getPIData
991      *
992      * @return
993      */

994     public String JavaDoc getPIData() {
995         throw new UnsupportedOperationException JavaDoc();
996     }
997
998     /*
999      *
1000     * ################################################################
1001     * Generator methods for the OMNodes returned by the navigator
1002     * ################################################################
1003     *
1004     */

1005
1006    /**
1007     * Method generateEvents
1008     *
1009     * @param node
1010     * @return
1011     */

1012    private int generateEvents(OMNode node) {
1013        int returnEvent = 0;
1014        int nodeType = node.getType();
1015        switch (nodeType) {
1016            case OMNode.ELEMENT_NODE:
1017                OMElement element = (OMElement) node;
1018// log.info("Generating events from element {"
1019
// + element.getNamespace().getName() + '}'
1020
// + element.getLocalName() + " Generated OM tree");
1021
returnEvent = generateElementEvents(element);
1022                break;
1023            case OMNode.TEXT_NODE:
1024                returnEvent = generateTextEvents();
1025                break;
1026            case OMNode.COMMENT_NODE:
1027                returnEvent = generateCommentEvents();
1028                break;
1029            case OMNode.CDATA_SECTION_NODE:
1030                returnEvent = generateCdataEvents();
1031                break;
1032            default :
1033                break; // just ignore any other nodes
1034
}
1035        return returnEvent;
1036    }
1037
1038    /**
1039     * Method generateElementEvents
1040     *
1041     * @param elt
1042     * @return
1043     */

1044    private int generateElementEvents(OMElement elt) {
1045        int returnValue = START_ELEMENT;
1046        if (!elementStack.isEmpty() && elementStack.peek().equals(elt)) {
1047            returnValue = END_ELEMENT;
1048            elementStack.pop();
1049        } else {
1050            elementStack.push(elt);
1051        }
1052        return returnValue;
1053    }
1054
1055    /**
1056     * Method generateTextEvents
1057     *
1058     * @return
1059     */

1060    private int generateTextEvents() {
1061        return CHARACTERS;
1062    }
1063
1064    /**
1065     * Method generateCommentEvents
1066     *
1067     * @return
1068     */

1069    private int generateCommentEvents() {
1070        return COMMENT;
1071    }
1072
1073    /**
1074     * Method generateCdataEvents
1075     *
1076     * @return
1077     */

1078    private int generateCdataEvents() {
1079        return CDATA;
1080    }
1081
1082    /*
1083     * ####################################################################
1084     * Other helper methods
1085     * ####################################################################
1086     */

1087
1088    /**
1089     * helper method
1090     *
1091     * @param it
1092     * @return
1093     */

1094    private int getCount(Iterator JavaDoc it) {
1095        int count = 0;
1096        if (it != null) {
1097            while (it.hasNext()) {
1098                it.next();
1099                count++;
1100            }
1101        }
1102        return count;
1103    }
1104
1105    /**
1106     * Helper method
1107     *
1108     * @param it
1109     * @param index
1110     * @return
1111     */

1112    private Object JavaDoc getItemFromIterator(Iterator JavaDoc it, int index) {
1113        int count = 0;
1114        Object JavaDoc returnObject = null;
1115        boolean found = false;
1116        if (it != null) {
1117            while (it.hasNext()) {
1118                returnObject = it.next();
1119                if (index == count++) {
1120                    found = true;
1121                    break;
1122                }
1123            }
1124        }
1125        if (found) {
1126            return returnObject;
1127        } else {
1128            return null;
1129        }
1130    }
1131
1132    /**
1133     * Helper method
1134     *
1135     * @param element
1136     * @return
1137     */

1138    private QName JavaDoc getQName(OMElement element) {
1139        QName JavaDoc returnName;
1140        OMNamespace ns = element.getNamespace();
1141        String JavaDoc localPart = element.getLocalName();
1142        if (ns != null) {
1143            String JavaDoc prefix = ns.getPrefix();
1144            String JavaDoc uri = ns.getName();
1145            if ((prefix == null) || prefix.equals("")) {
1146                returnName = new QName JavaDoc(uri, localPart);
1147            } else {
1148                returnName = new QName JavaDoc(uri, localPart, prefix);
1149            }
1150        } else {
1151            returnName = new QName JavaDoc(localPart);
1152        }
1153        return returnName;
1154    }
1155
1156    /**
1157     * @param elt
1158     * @param index
1159     * @return
1160     */

1161    private OMAttribute getAttribute(OMElement elt, int index) {
1162        OMAttribute returnAttrib = null;
1163        if (elt != null) {
1164            returnAttrib =
1165            (OMAttribute) getItemFromIterator(elt.getAttributes(), index);
1166        }
1167        return returnAttrib;
1168    }
1169}
1170
Popular Tags