1 38 39 package com.sun.xml.fastinfoset.stax.events; 40 41 import javax.xml.stream.XMLStreamConstants; 42 import javax.xml.stream.XMLEventFactory; 43 import javax.xml.stream.XMLStreamReader; 44 import javax.xml.stream.XMLStreamException; 45 import javax.xml.stream.events.*; 46 import javax.xml.stream.util.XMLEventAllocator; 47 import javax.xml.stream.util.XMLEventConsumer; 48 import javax.xml.XMLConstants ; 49 import javax.xml.namespace.QName ; 50 import com.sun.xml.fastinfoset.CommonResourceBundle; 51 52 67 public class StAXEventAllocatorBase implements XMLEventAllocator { 68 XMLEventFactory factory; 69 70 71 public StAXEventAllocatorBase() { 72 if (System.getProperty("javax.xml.stream.XMLEventFactory")==null) { 73 System.setProperty("javax.xml.stream.XMLEventFactory", 74 "com.sun.xml.fastinfoset.stax.StAXEventFactory"); 75 } 76 factory = XMLEventFactory.newInstance(); 77 } 78 79 81 85 public XMLEventAllocator newInstance() { 86 return new StAXEventAllocatorBase(); 87 } 88 89 96 public XMLEvent allocate(XMLStreamReader streamReader) throws XMLStreamException { 97 if(streamReader == null ) 98 throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.nullReader")); 99 return getXMLEvent(streamReader); 100 } 101 102 109 public void allocate(XMLStreamReader streamReader, XMLEventConsumer consumer) throws XMLStreamException { 110 consumer.add(getXMLEvent(streamReader)); 111 112 } 113 115 116 XMLEvent getXMLEvent(XMLStreamReader reader){ 117 XMLEvent event = null; 118 int eventType = reader.getEventType(); 120 factory.setLocation(reader.getLocation()); 122 switch(eventType){ 123 124 case XMLEvent.START_ELEMENT: 125 { 126 StartElementEvent startElement = (StartElementEvent)factory.createStartElement(reader.getPrefix(), 127 reader.getNamespaceURI(), reader.getLocalName()); 128 129 addAttributes(startElement,reader); 130 addNamespaces(startElement, reader); 131 event = startElement; 134 break; 135 } 136 case XMLEvent.END_ELEMENT: 137 { 138 EndElementEvent endElement = (EndElementEvent)factory.createEndElement( 139 reader.getPrefix(), reader.getNamespaceURI(), reader.getLocalName()); 140 addNamespaces(endElement,reader); 141 event = endElement ; 142 break; 143 } 144 case XMLEvent.PROCESSING_INSTRUCTION: 145 { 146 event = factory.createProcessingInstruction(reader.getPITarget(),reader.getPIData()); 147 break; 148 } 149 case XMLEvent.CHARACTERS: 150 { 151 if (reader.isWhiteSpace()) 152 event = factory.createSpace(reader.getText()); 153 else 154 event = factory.createCharacters(reader.getText()); 155 156 break; 157 } 158 case XMLEvent.COMMENT: 159 { 160 event = factory.createComment(reader.getText()); 161 break; 162 } 163 case XMLEvent.START_DOCUMENT: 164 { 165 StartDocumentEvent docEvent = (StartDocumentEvent)factory.createStartDocument( 166 reader.getVersion(), reader.getEncoding(), reader.isStandalone()); 167 if(reader.getCharacterEncodingScheme() != null){ 168 docEvent.setDeclaredEncoding(true); 169 }else{ 170 docEvent.setDeclaredEncoding(false); 171 } 172 event = docEvent ; 173 break; 174 } 175 case XMLEvent.END_DOCUMENT:{ 176 EndDocumentEvent endDocumentEvent = new EndDocumentEvent() ; 177 event = endDocumentEvent ; 178 break; 179 } 180 case XMLEvent.ENTITY_REFERENCE:{ 181 event = factory.createEntityReference(reader.getLocalName(), 182 new EntityDeclarationImpl(reader.getLocalName(),reader.getText())); 183 break; 184 185 } 186 case XMLEvent.ATTRIBUTE:{ 187 event = null ; 188 break; 189 } 190 case XMLEvent.DTD:{ 191 event = factory.createDTD(reader.getText()); 192 break; 193 } 194 case XMLEvent.CDATA:{ 195 event = factory.createCData(reader.getText()); 196 break; 197 } 198 case XMLEvent.SPACE:{ 199 event = factory.createSpace(reader.getText()); 200 break; 201 } 202 } 203 return event ; 204 } 205 206 protected void addAttributes(StartElementEvent event,XMLStreamReader streamReader){ 208 AttributeBase attr = null; 209 for(int i=0; i<streamReader.getAttributeCount() ;i++){ 210 attr = (AttributeBase)factory.createAttribute(streamReader.getAttributeName(i), 211 streamReader.getAttributeValue(i)); 212 attr.setAttributeType(streamReader.getAttributeType(i)); 213 attr.setSpecified(streamReader.isAttributeSpecified(i)); 214 event.addAttribute(attr); 215 } 216 } 217 218 protected void addNamespaces(StartElementEvent event,XMLStreamReader streamReader){ 220 Namespace namespace = null; 221 for(int i=0; i<streamReader.getNamespaceCount(); i++){ 222 namespace = factory.createNamespace(streamReader.getNamespacePrefix(i), 223 streamReader.getNamespaceURI(i)); 224 event.addNamespace(namespace); 225 } 226 } 227 protected void addNamespaces(EndElementEvent event,XMLStreamReader streamReader){ 228 Namespace namespace = null; 229 for(int i=0; i<streamReader.getNamespaceCount(); i++){ 230 namespace = factory.createNamespace(streamReader.getNamespacePrefix(i), 231 streamReader.getNamespaceURI(i)); 232 event.addNamespace(namespace); 233 } 234 } 235 236 237 } 238 | Popular Tags |