1 38 39 40 package com.sun.xml.fastinfoset.stax; 41 42 import javax.xml.namespace.QName ; 43 import javax.xml.stream.XMLStreamConstants; 44 import javax.xml.stream.XMLStreamException; 45 import javax.xml.stream.XMLStreamReader; 46 import org.xml.sax.ContentHandler ; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.ext.LexicalHandler ; 49 import org.xml.sax.helpers.AttributesImpl ; 50 import com.sun.xml.fastinfoset.CommonResourceBundle; 51 52 public class StAX2SAXReader { 53 54 57 ContentHandler _handler; 58 59 62 LexicalHandler _lexicalHandler; 63 64 67 XMLStreamReader _reader; 68 69 public StAX2SAXReader(XMLStreamReader reader, ContentHandler handler) { 70 _handler = handler; 71 _reader = reader; 72 } 73 74 public StAX2SAXReader(XMLStreamReader reader) { 75 _reader = reader; 76 } 77 78 public void setContentHandler(ContentHandler handler) { 79 _handler = handler; 80 } 81 82 public void setLexicalHandler(LexicalHandler lexicalHandler) { 83 _lexicalHandler = lexicalHandler; 84 } 85 86 public void adapt() throws XMLStreamException, SAXException { 87 QName qname; 88 String prefix, localPart; 89 AttributesImpl attrs = new AttributesImpl (); 90 char[] buffer; 91 int nsc; 92 int nat; 93 94 _handler.startDocument(); 95 96 try { 97 98 while (_reader.hasNext()) { 99 int event = _reader.next(); 100 101 102 switch(event) { 103 case XMLStreamConstants.START_ELEMENT: { 104 nsc = _reader.getNamespaceCount(); 106 for (int i = 0; i < nsc; i++) { 107 _handler.startPrefixMapping(_reader.getNamespacePrefix(i), 108 _reader.getNamespaceURI(i)); 109 } 110 111 attrs.clear(); 113 nat = _reader.getAttributeCount(); 114 for (int i = 0; i < nat; i++) { 115 QName q = _reader.getAttributeName(i); 116 String qName = _reader.getAttributePrefix(i); 117 if (qName == null || qName == "") { 118 qName = q.getLocalPart(); 119 } else { 120 qName = qName + ":" + q.getLocalPart(); 121 } 122 attrs.addAttribute(_reader.getAttributeNamespace(i), 123 q.getLocalPart(), 124 qName, 125 _reader.getAttributeType(i), 126 _reader.getAttributeValue(i)); 127 } 128 129 qname = _reader.getName(); 131 prefix = qname.getPrefix(); 132 localPart = qname.getLocalPart(); 133 134 _handler.startElement(_reader.getNamespaceURI(), 135 localPart, 136 (prefix.length() > 0) ? 137 (prefix + ":" + localPart) : localPart, 138 attrs); 139 break; 140 } 141 case XMLStreamConstants.END_ELEMENT: { 142 qname = _reader.getName(); 144 prefix = qname.getPrefix(); 145 localPart = qname.getLocalPart(); 146 147 _handler.endElement(_reader.getNamespaceURI(), 148 localPart, 149 (prefix.length() > 0) ? 150 (prefix + ":" + localPart) : localPart); 151 152 nsc = _reader.getNamespaceCount(); 154 for (int i = 0; i < nsc; i++) { 155 _handler.endPrefixMapping(_reader.getNamespacePrefix(i)); 156 } 157 break; 158 } 159 case XMLStreamConstants.CHARACTERS: 160 _handler.characters(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength()); 161 break; 162 case XMLStreamConstants.COMMENT: 163 _lexicalHandler.comment(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength()); 164 break; 165 case XMLStreamConstants.PROCESSING_INSTRUCTION: 166 _handler.processingInstruction(_reader.getPITarget(), _reader.getPIData()); 167 break; 168 case XMLStreamConstants.END_DOCUMENT: 169 break; 170 default: 171 throw new RuntimeException (CommonResourceBundle.getInstance().getString("message.StAX2SAXReader", new Object []{new Integer (event)})); 172 } } 174 } 175 catch (XMLStreamException e) { 176 _handler.endDocument(); throw e; 178 } 179 180 _handler.endDocument(); 181 } 182 183 } 184 185 | Popular Tags |