1 22 23 package org.xquark.xml.xqueryevaluator.eval; 24 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 28 import org.xml.sax.*; 29 import org.xml.sax.ext.LexicalHandler ; 30 import org.xmlpull.v1.XmlPullParser; 31 import org.xmlpull.v1.XmlPullParserException; 32 import org.xmlpull.v1.XmlPullParserFactory; 33 34 39 40 public class XPPReader implements Locator, Attributes { 41 protected XmlPullParser xpp; 42 protected boolean beforeFirstTag = true; 43 44 protected ContentHandler contentHandler = null; 45 protected ErrorHandler errorHandler = null; 46 protected LexicalHandler lexicalHandler = null; 47 protected String systemId; 48 protected InputStream input = null; 49 50 private int[] charsBounds = new int[2]; 51 52 protected static XmlPullParserFactory factory; 53 54 static { 55 try { 56 factory = XmlPullParserFactory.newInstance(); 57 factory.setNamespaceAware(true); 58 } catch (XmlPullParserException ex) {} 59 } 60 61 public XPPReader() throws SAXException { 62 if (factory == null) 63 throw new SAXException("No XmlPullParser factory available"); 64 try { 65 xpp = factory.newPullParser(); 66 } catch (XmlPullParserException ex) { 67 throw new SAXException(ex); 68 } 69 } 70 71 73 public String getURI(int index) { 74 return xpp.getAttributeNamespace(index); 75 } 76 77 public String getLocalName(int index) { 78 return xpp.getAttributeName(index); 79 } 80 81 public String getQName(int index) { 82 String prefix = xpp.getAttributePrefix(index); 83 if (prefix != null) { 84 return prefix + ':' + xpp.getAttributeName(index); 85 } else { 86 return xpp.getAttributeName(index); 87 } 88 } 89 90 public String getValue(String uri, String localName) { 91 return xpp.getAttributeValue(uri, localName); 92 } 93 94 public String getValue(String qName) { 95 return xpp.getAttributeValue(null, qName); 96 } 97 98 public String getType(int index) { 99 return xpp.getAttributeType(index); 100 } 101 102 public String getValue(int index) { 103 return xpp.getAttributeValue(index); 104 } 105 106 public int getLength() { 107 return xpp.getAttributeCount(); 108 } 109 110 public int getIndex(String uri, String localName) { 111 for (int i = 0; i < xpp.getAttributeCount(); i++) { 112 if (xpp.getAttributeNamespace(i).equals(uri) && xpp.getAttributeName(i).equals(localName)) { 113 return i; 114 } 115 } 116 return -1; 117 } 118 119 public int getIndex(String qName) { 120 for (int i = 0; i < xpp.getAttributeCount(); i++) { 121 if (xpp.getAttributeName(i).equals(qName)) { 122 return i; 123 } 124 } 125 return -1; 126 } 127 128 public String getType(String uri, String localName) { 129 int index = getIndex(uri, localName); 130 if (index == -1) 131 return null; 132 else 133 return xpp.getAttributeType(index); 134 } 135 136 public String getType(String qName) { 137 int index = getIndex(qName); 138 if (index == -1) 139 return null; 140 else 141 return xpp.getAttributeType(index); 142 } 143 144 146 public String getPublicId() { 147 return null; 148 } 149 150 public String getSystemId() { 151 return systemId; 152 } 153 154 public int getLineNumber() { 155 return xpp.getLineNumber(); 156 } 157 158 public int getColumnNumber() { 159 return xpp.getColumnNumber(); 160 } 161 162 public void setContentHandler(ContentHandler handler) { 163 this.contentHandler = handler; 164 } 165 166 public ContentHandler getContentHandler() { 167 return contentHandler; 168 } 169 170 public void setLexicalHandler(LexicalHandler handler) { 171 this.lexicalHandler = handler; 172 } 173 174 public LexicalHandler getLexicalHandler() { 175 return lexicalHandler; 176 } 177 178 public void setErrorHandler(ErrorHandler handler) { 179 this.errorHandler = handler; 180 } 181 182 public ErrorHandler getErrorHandler() { 183 return errorHandler; 184 } 185 186 public void setInput(String systemId, InputStream input) throws SAXException { 187 this.systemId = systemId; 188 try { 189 xpp.setInput(input, null); 190 } catch (XmlPullParserException ex) { 191 throw new SAXException(ex); 192 } 193 } 194 195 public boolean isAtStart() throws SAXException { 196 try { 197 return xpp.getEventType() == XmlPullParser.START_DOCUMENT; 198 } catch (XmlPullParserException ex) { 199 throw new SAXException(ex); 200 } 201 } 202 203 public boolean parseNextElement() throws SAXException, IOException { 204 try { 205 int prevCount = 0; 206 int curCount = 0; 207 int depth = 0; 208 char[] chars; 209 while (true) { 210 switch (xpp.nextToken()) { 211 case XmlPullParser.START_TAG : 212 beforeFirstTag = false; 213 depth = xpp.getDepth(); 214 prevCount = xpp.getNamespaceCount(depth - 1); 215 curCount = xpp.getNamespaceCount(depth); 216 for (int i = prevCount; i < curCount; i++) { 217 contentHandler.startPrefixMapping(xpp.getNamespacePrefix(i), xpp.getNamespaceUri(i)); 218 } 219 contentHandler.startElement(xpp.getNamespace(), xpp.getName(), null, this); 220 break; 221 case XmlPullParser.TEXT : 222 case XmlPullParser.IGNORABLE_WHITESPACE : 223 case XmlPullParser.CDSECT : 224 if (!beforeFirstTag) { 225 chars = xpp.getTextCharacters(charsBounds); 226 contentHandler.characters(chars, charsBounds[0], charsBounds[1]); 227 } 228 break; 229 case XmlPullParser.COMMENT : 230 if (lexicalHandler != null) { 231 chars = xpp.getTextCharacters(charsBounds); 232 lexicalHandler.comment(chars, charsBounds[0], charsBounds[1]); 233 } 234 break; 235 case XmlPullParser.PROCESSING_INSTRUCTION : 236 String pi = xpp.getText().trim(); 237 int index = pi.indexOf(' '); 238 if (index != -1) contentHandler.processingInstruction(pi.substring(0, index), pi.substring(index+1)); 239 else contentHandler.processingInstruction(pi, ""); 240 break; 241 case XmlPullParser.END_TAG : 242 contentHandler.endElement(xpp.getNamespace(), xpp.getName(), null); 243 depth = xpp.getDepth(); 244 prevCount = xpp.getNamespaceCount(depth - 1); 245 curCount = xpp.getNamespaceCount(depth); 246 for (int i = curCount - 1; i >= prevCount; i--) { 247 contentHandler.endPrefixMapping(xpp.getNamespacePrefix(i)); 248 } 249 return false; 250 case XmlPullParser.END_DOCUMENT : 251 return true; 252 default : 253 break; 254 } 255 } 256 } catch (XmlPullParserException ex) { 257 SAXParseException saxException = new SAXParseException("", this, ex); 258 if (errorHandler != null) 259 errorHandler.fatalError(saxException); 260 throw saxException; 261 } 262 } 263 264 } 265 | Popular Tags |