1 29 30 package com.caucho.xml.stream; 31 32 import com.caucho.util.L10N; 33 import com.caucho.xml.stream.events.*; 34 35 import javax.xml.namespace.QName ; 36 import javax.xml.stream.XMLEventReader; 37 import javax.xml.stream.XMLStreamConstants; 38 import javax.xml.stream.XMLStreamException; 39 import javax.xml.stream.XMLStreamReader; 40 import javax.xml.stream.events.Attribute; 41 import javax.xml.stream.events.Namespace; 42 import javax.xml.stream.events.XMLEvent; 43 import java.util.HashMap ; 44 45 public class XMLEventReaderImpl implements XMLEventReader, XMLStreamConstants { 46 public static final L10N L = new L10N(XMLEventReaderImpl.class); 47 48 private final XMLStreamReader _in; 49 private XMLEvent _current = null; 50 private XMLEvent _next = null; 51 52 public XMLEventReaderImpl(XMLStreamReader in) 53 throws XMLStreamException 54 { 55 _in = in; 56 _next = getEvent(); 57 } 58 59 public void close() 60 throws XMLStreamException 61 { 62 _in.close(); 63 } 64 65 public String getElementText() 66 throws XMLStreamException 67 { 68 return _in.getElementText(); 69 } 70 71 public Object getProperty(String name) 72 throws IllegalArgumentException 73 { 74 throw new IllegalArgumentException (name); 75 } 76 77 public boolean hasNext() 78 { 79 try { 80 return _next != null || _in.hasNext(); 81 } 82 catch (XMLStreamException e) { 83 return false; 84 } 85 } 86 87 public XMLEvent nextEvent() throws XMLStreamException 88 { 89 if (_next != null) { 90 _current = _next; 91 _next = null; 92 } 93 else { 94 _in.next(); 95 _current = getEvent(); 96 } 97 98 return _current; 99 } 100 101 public XMLEvent nextTag() throws XMLStreamException 102 { 103 if (_next != null) { 104 _current = _next; 105 _next = null; 106 } 107 else { 108 _in.nextTag(); 109 _current = getEvent(); 110 } 111 112 return _current; 113 } 114 115 public XMLEvent peek() throws XMLStreamException 116 { 117 if (_next == null) { 118 _in.next(); 119 _next = getEvent(); 120 } 121 122 return _next; 123 } 124 125 public void remove() 126 { 127 throw new UnsupportedOperationException (); 128 } 129 130 public XMLEvent next() 131 { 132 try { 133 return nextEvent(); 134 } 135 catch (XMLStreamException e) { 136 return null; 137 } 138 } 139 140 private XMLEvent getEvent() 141 throws XMLStreamException 142 { 143 switch (_in.getEventType()) { 144 case ATTRIBUTE: 145 break; 148 149 case CDATA: 150 return new CharactersImpl(_in.getText(), true, false, false); 151 152 case CHARACTERS: 153 return new CharactersImpl(_in.getText(), false, false, false); 154 155 case COMMENT: 156 return new CommentImpl(_in.getText()); 157 158 case DTD: 159 break; 161 162 case END_DOCUMENT: 163 return new EndDocumentImpl(); 164 165 case END_ELEMENT: 166 return new EndElementImpl(_in.getName()); 167 168 case ENTITY_DECLARATION: 169 break; 171 172 case ENTITY_REFERENCE: 173 break; 175 176 case NAMESPACE: 177 break; 180 181 case NOTATION_DECLARATION: 182 break; 184 185 case PROCESSING_INSTRUCTION: 186 return new ProcessingInstructionImpl(_in.getPIData(), 187 _in.getPITarget()); 188 189 case SPACE: 190 return new CharactersImpl(_in.getText(), false, true, true); 191 192 case START_DOCUMENT: 193 boolean encodingSet = true; 194 String encoding = _in.getCharacterEncodingScheme(); 195 196 if (encoding == null) { 197 encoding = "utf-8"; encodingSet = false; 199 } 200 201 return new StartDocumentImpl(encodingSet, encoding, 202 null , 203 _in.getVersion(), 204 _in.isStandalone(), _in.standaloneSet()); 205 206 case START_ELEMENT: 207 HashMap <QName ,Attribute> attributes = new HashMap <QName ,Attribute>(); 208 209 for (int i = 0; i < _in.getAttributeCount(); i++) { 210 Attribute attribute = new AttributeImpl(_in.getAttributeName(i), 211 _in.getAttributeValue(i)); 212 attributes.put(_in.getAttributeName(i), attribute); 213 } 214 215 HashMap <String ,Namespace> namespaces= new HashMap <String ,Namespace>(); 216 217 for (int i = 0; i < _in.getNamespaceCount(); i++) { 218 Namespace namespace = new NamespaceImpl(_in.getNamespacePrefix(i), 219 _in.getNamespaceURI(i)); 220 namespaces.put(_in.getNamespacePrefix(i), namespace); 221 } 222 223 return new StartElementImpl(_in.getName(), attributes, namespaces, 224 _in.getNamespaceContext()); 225 } 226 227 throw new XMLStreamException("Event type = " + _in.getEventType()); 228 } 229 } 230 231 | Popular Tags |