1 6 7 package com.sun.xml.fastinfoset.stax; 8 9 import javax.xml.namespace.QName ; 10 import javax.xml.stream.events.XMLEvent; 11 import javax.xml.stream.EventFilter; 12 import javax.xml.stream.events.StartElement; 13 import javax.xml.stream.events.EndElement; 14 import javax.xml.stream.events.Characters; 15 import javax.xml.stream.XMLStreamException; 16 import javax.xml.stream.XMLEventReader; 17 import javax.xml.stream.XMLInputFactory; 18 import com.sun.xml.fastinfoset.CommonResourceBundle; 19 20 public class StAXFilteredEvent implements XMLEventReader { 21 private XMLEventReader eventReader; 22 private EventFilter _filter; 23 24 25 public StAXFilteredEvent() { 26 } 27 28 public StAXFilteredEvent(XMLEventReader reader, EventFilter filter) throws XMLStreamException 29 { 30 eventReader = reader; 31 _filter = filter; 32 } 33 34 public void setEventReader(XMLEventReader reader) { 35 eventReader = reader; 36 } 37 38 public void setFilter(EventFilter filter) { 39 _filter = filter; 40 } 41 42 public Object next() { 43 try { 44 return nextEvent(); 45 } catch (XMLStreamException e) { 46 return null; 47 } 48 } 49 50 public XMLEvent nextEvent() throws XMLStreamException 51 { 52 if (hasNext()) 53 return eventReader.nextEvent(); 54 return null; 55 } 56 57 public String getElementText() throws XMLStreamException 58 { 59 StringBuffer buffer = new StringBuffer (); 60 XMLEvent e = nextEvent(); 61 if (!e.isStartElement()) 62 throw new XMLStreamException( 63 CommonResourceBundle.getInstance().getString("message.mustBeOnSTART_ELEMENT")); 64 65 while(hasNext()) { 66 e = nextEvent(); 67 if(e.isStartElement()) 68 throw new XMLStreamException( 69 CommonResourceBundle.getInstance().getString("message.getElementTextExpectTextOnly")); 70 if(e.isCharacters()) 71 buffer.append(((Characters) e).getData()); 72 if(e.isEndElement()) 73 return buffer.toString(); 74 } 75 throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.END_ELEMENTnotFound")); 76 } 77 78 public XMLEvent nextTag() throws XMLStreamException { 79 while(hasNext()) { 80 XMLEvent e = nextEvent(); 81 if (e.isStartElement() || e.isEndElement()) 82 return e; 83 } 84 throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.startOrEndNotFound")); 85 } 86 87 88 public boolean hasNext() 89 { 90 try { 91 while(eventReader.hasNext()) { 92 if (_filter.accept(eventReader.peek())) return true; 93 eventReader.nextEvent(); 94 } 95 return false; 96 } catch (XMLStreamException e) { 97 return false; 98 } 99 } 100 101 public void remove() { 102 throw new UnsupportedOperationException (); 103 } 104 105 public XMLEvent peek() throws XMLStreamException 106 { 107 if (hasNext()) 108 return eventReader.peek(); 109 return null; 110 } 111 112 public void close() throws XMLStreamException 113 { 114 eventReader.close(); 115 } 116 117 public Object getProperty(String name) { 118 return eventReader.getProperty(name); 119 } 120 121 } 122 | Popular Tags |