1 38 39 40 package com.sun.xml.fastinfoset.stax.events; 41 42 import javax.xml.stream.XMLStreamConstants; 43 import javax.xml.stream.XMLEventFactory; 44 import javax.xml.stream.XMLStreamReader; 45 import javax.xml.stream.XMLStreamException; 46 import javax.xml.stream.events.*; 47 import javax.xml.stream.util.XMLEventAllocator; 48 import javax.xml.stream.util.XMLEventConsumer; 49 import javax.xml.XMLConstants ; 50 import javax.xml.namespace.QName ; 51 import com.sun.xml.fastinfoset.CommonResourceBundle; 52 53 61 public class StAXEventAllocator implements XMLEventAllocator { 62 StartElementEvent startElement = new StartElementEvent(); 63 EndElementEvent endElement = new EndElementEvent(); 64 CharactersEvent characters = new CharactersEvent(); 65 CharactersEvent cData = new CharactersEvent("",true); 66 CharactersEvent space = new CharactersEvent(); 67 CommentEvent comment = new CommentEvent(); 68 EntityReferenceEvent entity = new EntityReferenceEvent(); 69 ProcessingInstructionEvent pi = new ProcessingInstructionEvent(); 70 StartDocumentEvent startDoc = new StartDocumentEvent(); 71 EndDocumentEvent endDoc = new EndDocumentEvent(); 72 DTDEvent dtd = new DTDEvent(); 73 74 75 public StAXEventAllocator() { 76 } 77 public XMLEventAllocator newInstance() { 78 return new StAXEventAllocator(); 79 } 80 81 88 public XMLEvent allocate(XMLStreamReader streamReader) throws XMLStreamException { 89 if(streamReader == null ) 90 throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.nullReader")); 91 return getXMLEvent(streamReader); 92 } 93 94 101 public void allocate(XMLStreamReader streamReader, XMLEventConsumer consumer) throws XMLStreamException { 102 consumer.add(getXMLEvent(streamReader)); 103 104 } 105 107 108 XMLEvent getXMLEvent(XMLStreamReader reader){ 109 EventBase event = null; 110 int eventType = reader.getEventType(); 111 112 switch(eventType){ 113 114 case XMLEvent.START_ELEMENT: 115 { 116 startElement.reset(); 117 startElement.setName(new QName (reader.getNamespaceURI(), 118 reader.getLocalName(), reader.getPrefix())); 119 120 addAttributes(startElement,reader); 121 addNamespaces(startElement, reader); 122 event = startElement; 125 break; 126 } 127 case XMLEvent.END_ELEMENT: 128 { 129 endElement.reset(); 130 endElement.setName(new QName (reader.getNamespaceURI(), 131 reader.getLocalName(),reader.getPrefix())); 132 addNamespaces(endElement,reader); 133 event = endElement ; 134 break; 135 } 136 case XMLEvent.PROCESSING_INSTRUCTION: 137 { 138 pi.setTarget(reader.getPITarget()); 139 pi.setData(reader.getPIData()); 140 event = pi; 141 break; 142 } 143 case XMLEvent.CHARACTERS: 144 { 145 characters.setData(reader.getText()); 146 event = characters; 147 158 break; 159 } 160 case XMLEvent.COMMENT: 161 { 162 comment.setText(reader.getText()); 163 event = comment; 164 break; 165 } 166 case XMLEvent.START_DOCUMENT: 167 { 168 startDoc.reset(); 169 String encoding = reader.getEncoding(); 170 String version = reader.getVersion(); 171 if (encoding != null) 172 startDoc.setEncoding(encoding); 173 if (version != null) 174 startDoc.setVersion(version); 175 startDoc.setStandalone(reader.isStandalone()); 176 if(reader.getCharacterEncodingScheme() != null){ 177 startDoc.setDeclaredEncoding(true); 178 }else{ 179 startDoc.setDeclaredEncoding(false); 180 } 181 event = startDoc ; 182 break; 183 } 184 case XMLEvent.END_DOCUMENT:{ 185 event = endDoc; 186 break; 187 } 188 case XMLEvent.ENTITY_REFERENCE:{ 189 entity.setName(reader.getLocalName()); 190 entity.setDeclaration(new EntityDeclarationImpl(reader.getLocalName(),reader.getText())); 191 event = entity; 192 break; 193 194 } 195 case XMLEvent.ATTRIBUTE:{ 196 event = null ; 197 break; 198 } 199 case XMLEvent.DTD:{ 200 dtd.setDTD(reader.getText()); 201 event = dtd; 202 break; 203 } 204 case XMLEvent.CDATA:{ 205 cData.setData(reader.getText()); 206 event = cData; 207 break; 208 } 209 case XMLEvent.SPACE:{ 210 space.setData(reader.getText()); 211 space.setSpace(true); 212 event = space; 213 break; 214 } 215 } 216 event.setLocation(reader.getLocation()); 217 return event ; 218 } 219 220 protected void addAttributes(StartElementEvent event,XMLStreamReader reader){ 222 AttributeBase attr = null; 223 for(int i=0; i<reader.getAttributeCount() ;i++){ 224 attr = new AttributeBase(reader.getAttributeName(i), reader.getAttributeValue(i)); 225 attr.setAttributeType(reader.getAttributeType(i)); 226 attr.setSpecified(reader.isAttributeSpecified(i)); 227 event.addAttribute(attr); 228 } 229 } 230 231 protected void addNamespaces(StartElementEvent event,XMLStreamReader reader){ 233 Namespace namespace = null; 234 for(int i=0; i<reader.getNamespaceCount(); i++){ 235 namespace = new NamespaceBase(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); 236 event.addNamespace(namespace); 237 } 238 } 239 240 protected void addNamespaces(EndElementEvent event,XMLStreamReader reader){ 241 Namespace namespace = null; 242 for(int i=0; i<reader.getNamespaceCount(); i++){ 243 namespace = new NamespaceBase(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); 244 event.addNamespace(namespace); 245 } 246 } 247 248 } 249 | Popular Tags |