1 17 package org.apache.servicemix.jbi.jaxp; 18 19 import javax.xml.XMLConstants ; 20 import javax.xml.stream.XMLStreamConstants; 21 import javax.xml.stream.XMLStreamException; 22 import javax.xml.stream.XMLStreamReader; 23 import javax.xml.transform.sax.SAXSource ; 24 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.DTDHandler ; 28 import org.xml.sax.EntityResolver ; 29 import org.xml.sax.ErrorHandler ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXNotRecognizedException ; 33 import org.xml.sax.SAXNotSupportedException ; 34 import org.xml.sax.SAXParseException ; 35 import org.xml.sax.XMLReader ; 36 import org.xml.sax.helpers.AttributesImpl ; 37 38 public class StaxSource extends SAXSource implements XMLReader { 39 40 private XMLStreamReader streamReader; 41 42 private ContentHandler contentHandler; 43 44 public StaxSource(XMLStreamReader streamReader) { 45 this.streamReader = streamReader; 46 setInputSource(new InputSource ()); 47 } 48 49 public XMLReader getXMLReader() { 50 return this; 51 } 52 53 protected void parse() throws SAXException { 54 try { 55 contentHandler.startDocument(); 56 while (true) { 57 switch (streamReader.getEventType()) { 58 case XMLStreamConstants.ATTRIBUTE: 59 case XMLStreamConstants.CDATA: 60 break; 61 case XMLStreamConstants.CHARACTERS: 62 if (!streamReader.isWhiteSpace()) { 63 64 int length = streamReader.getTextLength(); 65 int start = streamReader.getTextStart(); 66 char[] chars = streamReader.getTextCharacters(); 67 contentHandler.characters(chars, start, length); 68 75 } 76 break; 77 case XMLStreamConstants.COMMENT: 78 case XMLStreamConstants.DTD: 79 break; 80 case XMLStreamConstants.END_DOCUMENT: 81 contentHandler.endDocument(); 82 return; 83 case XMLStreamConstants.END_ELEMENT: { 84 String uri = streamReader.getNamespaceURI(); 85 String localName = streamReader.getLocalName(); 86 String prefix = streamReader.getPrefix(); 87 String qname = prefix != null && prefix.length() > 0 ? prefix + ":" + localName : localName; 88 contentHandler.endElement(uri, localName, qname); 89 for (int i = 0; i < streamReader.getNamespaceCount(); i++) { 90 } 92 break; 93 } 94 case XMLStreamConstants.ENTITY_DECLARATION: 95 case XMLStreamConstants.ENTITY_REFERENCE: 96 case XMLStreamConstants.NAMESPACE: 97 case XMLStreamConstants.NOTATION_DECLARATION: 98 case XMLStreamConstants.PROCESSING_INSTRUCTION: 99 case XMLStreamConstants.SPACE: 100 case XMLStreamConstants.START_DOCUMENT: 101 break; 102 case XMLStreamConstants.START_ELEMENT: { 103 for (int i = 0; i < streamReader.getNamespaceCount(); i++) { 104 } 107 String uri = streamReader.getNamespaceURI(); 108 String localName = streamReader.getLocalName(); 109 String prefix = streamReader.getPrefix(); 110 String qname = prefix != null && prefix.length() > 0 ? prefix + ":" + localName : localName; 111 contentHandler.startElement(uri, localName, qname, getAttributes()); 112 break; 113 } 114 } 115 streamReader.next(); 116 } 117 } catch (XMLStreamException e) { 118 if (e.getLocation() != null) { 119 throw new SAXParseException (e.getMessage(), null, null, e.getLocation().getLineNumber(), e.getLocation() 120 .getColumnNumber(), e); 121 } else { 122 throw new SAXParseException (e.getMessage(), null, null, -1, -1, e); 123 } 124 } 125 } 126 127 protected String getQualifiedName() { 128 String prefix = streamReader.getPrefix(); 129 if (prefix != null && prefix.length() > 0) { 130 return prefix + ":" + streamReader.getLocalName(); 131 } else { 132 return streamReader.getLocalName(); 133 } 134 } 135 136 protected Attributes getAttributes() { 137 AttributesImpl attrs = new AttributesImpl (); 138 for (int i = 0; i < streamReader.getNamespaceCount(); i++) { 142 String prefix = streamReader.getNamespacePrefix(i); 143 String uri = streamReader.getNamespaceURI(i); 144 if (uri == null) { 145 uri = ""; 146 } 147 if (prefix == null || prefix.length() == 0) { 149 attrs.addAttribute(XMLConstants.DEFAULT_NS_PREFIX, 150 null, 151 XMLConstants.XMLNS_ATTRIBUTE, 152 "CDATA", 153 uri); 154 } else { 155 attrs.addAttribute(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, 156 prefix, 157 XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix, 158 "CDATA", 159 uri); 160 } 161 } 162 for (int i = 0; i < streamReader.getAttributeCount(); i++) { 163 String uri = streamReader.getAttributeNamespace(i); 164 String localName = streamReader.getAttributeLocalName(i); 165 String prefix = streamReader.getAttributePrefix(i); 166 String qName; 167 if (prefix != null && prefix.length() > 0) { 168 qName = prefix + ':' + localName; 169 } else { 170 qName = localName; 171 } 172 String type = streamReader.getAttributeType(i); 173 String value = streamReader.getAttributeValue(i); 174 if (value == null) { 175 value = ""; 176 } 177 attrs.addAttribute(uri, localName, qName, type, value); 178 } 179 return attrs; 180 } 181 182 public boolean getFeature(String name) throws SAXNotRecognizedException , SAXNotSupportedException { 183 return false; 184 } 185 186 public void setFeature(String name, boolean value) throws SAXNotRecognizedException , SAXNotSupportedException { 187 } 188 189 public Object getProperty(String name) throws SAXNotRecognizedException , SAXNotSupportedException { 190 return null; 191 } 192 193 public void setProperty(String name, Object value) throws SAXNotRecognizedException , SAXNotSupportedException { 194 } 195 196 public void setEntityResolver(EntityResolver resolver) { 197 } 198 199 public EntityResolver getEntityResolver() { 200 return null; 201 } 202 203 public void setDTDHandler(DTDHandler handler) { 204 } 205 206 public DTDHandler getDTDHandler() { 207 return null; 208 } 209 210 public void setContentHandler(ContentHandler handler) { 211 this.contentHandler = handler; 212 } 213 214 public ContentHandler getContentHandler() { 215 return this.contentHandler; 216 } 217 218 public void setErrorHandler(ErrorHandler handler) { 219 } 220 221 public ErrorHandler getErrorHandler() { 222 return null; 223 } 224 225 public void parse(InputSource input) throws SAXException { 226 StaxSource.this.parse(); 227 } 228 229 public void parse(String systemId) throws SAXException { 230 StaxSource.this.parse(); 231 } 232 233 } 234 | Popular Tags |