1 9 10 package org.dom4j.io; 11 12 import java.io.*; 13 import java.net.URL ; 14 import org.dom4j.Document; 15 import org.dom4j.DocumentException; 16 import org.dom4j.DocumentFactory; 17 import org.dom4j.Element; 18 import org.dom4j.ElementHandler; 19 import org.dom4j.QName; 20 import org.xmlpull.v1.XmlPullParser; 21 import org.xmlpull.v1.XmlPullParserException; 22 import org.xmlpull.v1.XmlPullParserFactory; 23 24 34 public class XPPPacketReader { 35 36 39 private DocumentFactory factory; 40 41 44 private XmlPullParser xppParser; 45 46 49 private XmlPullParserFactory xppFactory; 50 51 54 private DispatchHandler dispatchHandler; 55 56 57 public XPPPacketReader() { 58 } 59 60 public XPPPacketReader(DocumentFactory factory) { 61 this.factory = factory; 62 } 63 64 65 73 public Document read(File file) throws DocumentException, IOException, XmlPullParserException { 74 String systemID = file.getAbsolutePath(); 75 return read(new BufferedReader(new FileReader(file)), systemID); 76 } 77 78 85 public Document read(URL url) throws DocumentException, IOException, XmlPullParserException { 86 String systemID = url.toExternalForm(); 87 return read(createReader(url.openStream()), systemID); 88 } 89 90 106 public Document read(String systemID) throws DocumentException, IOException, XmlPullParserException { 107 if (systemID.indexOf(':') >= 0) { 108 return read(new URL (systemID)); 110 } 111 else { 112 return read(new File(systemID)); 114 } 115 } 116 117 124 public Document read(InputStream in) throws DocumentException, IOException, XmlPullParserException { 125 return read(createReader(in)); 126 } 127 128 135 public Document read(Reader reader) throws DocumentException, IOException, XmlPullParserException { 136 getXPPParser().setInput(reader); 137 return parseDocument(); 138 } 139 140 147 public Document read(char[] text) throws DocumentException, IOException, XmlPullParserException { 148 getXPPParser().setInput(new CharArrayReader(text)); 149 return parseDocument(); 150 } 151 152 160 public Document read(InputStream in, String systemID) throws DocumentException, IOException, XmlPullParserException { 161 return read(createReader(in), systemID); 162 } 163 164 172 public Document read(Reader reader, String systemID) throws DocumentException, IOException, XmlPullParserException { 173 Document document = read(reader); 174 document.setName(systemID); 175 return document; 176 } 177 178 179 182 public XmlPullParser getXPPParser() throws XmlPullParserException { 183 if (xppParser == null) { 184 xppParser = getXPPFactory().newPullParser(); 185 } 186 return xppParser; 187 } 188 189 public XmlPullParserFactory getXPPFactory() throws XmlPullParserException { 190 if (xppFactory == null) { 191 xppFactory = XmlPullParserFactory.newInstance(); 192 } 193 xppFactory.setNamespaceAware(true); 194 return xppFactory; 195 } 196 197 public void setXPPFactory(XmlPullParserFactory xppFactory) { 198 this.xppFactory = xppFactory; 199 } 200 201 204 public DocumentFactory getDocumentFactory() { 205 if (factory == null) { 206 factory = DocumentFactory.getInstance(); 207 } 208 return factory; 209 } 210 211 218 public void setDocumentFactory(DocumentFactory factory) { 219 this.factory = factory; 220 } 221 222 223 231 public void addHandler(String path, ElementHandler handler) { 232 getDispatchHandler().addHandler(path, handler); 233 } 234 235 241 public void removeHandler(String path) { 242 getDispatchHandler().removeHandler(path); 243 } 244 245 254 public void setDefaultHandler(ElementHandler handler) { 255 getDispatchHandler().setDefaultHandler(handler); 256 } 257 258 public Document parseDocument() throws DocumentException, IOException, XmlPullParserException { 261 DocumentFactory df = getDocumentFactory(); 262 Document document = df.createDocument(); 263 Element parent = null; 264 XmlPullParser pp = getXPPParser(); 265 int count = 0; 266 while (true) { 267 int type = -1; 268 type = pp.nextToken(); 269 switch (type) { 270 case XmlPullParser.PROCESSING_INSTRUCTION: { 271 String text = pp.getText(); 272 int loc = text.indexOf(" "); 273 if (loc >= 0) { 274 document.addProcessingInstruction(text.substring(0, loc), text.substring(loc + 1)); 275 } 276 else 277 document.addProcessingInstruction(text, ""); 278 break; 279 } 280 case XmlPullParser.COMMENT: { 281 if (parent != null) 282 parent.addComment(pp.getText()); 283 else 284 document.addComment(pp.getText()); 285 break; 286 } 287 case XmlPullParser.CDSECT: { 288 String text = pp.getText(); 289 if (parent != null) { 290 parent.addCDATA(text); 291 } 292 else { 293 if (text.trim().length() > 0) { 294 throw new DocumentException("Cannot have text content outside of the root document"); 295 } 296 } 297 break; 298 299 } 300 case XmlPullParser.ENTITY_REF: { 301 String text = pp.getText(); 302 if (parent != null) { 303 parent.addText(text); 304 } 305 else { 306 if (text.trim().length() > 0) { 307 throw new DocumentException("Cannot have an entityref outside of the root document"); 308 } 309 } 310 break; 311 } 312 case XmlPullParser.END_DOCUMENT: { 313 return document; 314 } 315 case XmlPullParser.START_TAG: { 316 QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace()) : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace()); 317 Element newElement = null; 318 if ("jabber:client".equals(qname.getNamespaceURI()) || 322 "jabber:server".equals(qname.getNamespaceURI()) || 323 "jabber:component:accept".equals(qname.getNamespaceURI())) { 324 newElement = df.createElement(pp.getName()); 325 } 326 else { 327 newElement = df.createElement(qname); 328 } 329 int nsStart = pp.getNamespaceCount(pp.getDepth() - 1); 330 int nsEnd = pp.getNamespaceCount(pp.getDepth()); 331 for (int i = nsStart; i < nsEnd; i++) 332 if (pp.getNamespacePrefix(i) != null) 333 newElement.addNamespace(pp.getNamespacePrefix(i), pp.getNamespaceUri(i)); 334 for (int i = 0; i < pp.getAttributeCount(); i++) { 335 QName qa = (pp.getAttributePrefix(i) == null) ? df.createQName(pp.getAttributeName(i)) : df.createQName(pp.getAttributeName(i), pp.getAttributePrefix(i), pp.getAttributeNamespace(i)); 336 newElement.addAttribute(qa, pp.getAttributeValue(i)); 337 } 338 if (parent != null) { 339 parent.add(newElement); 340 } 341 else { 342 document.add(newElement); 343 } 344 parent = newElement; 345 count++; 346 break; 347 } 348 case XmlPullParser.END_TAG: { 349 if (parent != null) { 350 parent = parent.getParent(); 351 } 352 count--; 353 if (count < 1) { 354 return document; 355 } 356 break; 357 } 358 case XmlPullParser.TEXT: { 359 String text = pp.getText(); 360 if (parent != null) { 361 parent.addText(text); 362 } 363 else { 364 if (text.trim().length() > 0) { 365 throw new DocumentException("Cannot have text content outside of the root document"); 366 } 367 } 368 break; 369 } 370 default: 371 { 372 ; 373 } 374 } 375 } 376 } 377 378 protected DispatchHandler getDispatchHandler() { 379 if (dispatchHandler == null) { 380 dispatchHandler = new DispatchHandler(); 381 } 382 return dispatchHandler; 383 } 384 385 protected void setDispatchHandler(DispatchHandler dispatchHandler) { 386 this.dispatchHandler = dispatchHandler; 387 } 388 389 392 protected Reader createReader(InputStream in) throws IOException { 393 return new BufferedReader(new InputStreamReader(in)); 394 } 395 } 396 397 441 | Popular Tags |