1 7 8 package org.dom4j.io; 9 10 import java.io.BufferedReader ; 11 import java.io.CharArrayReader ; 12 import java.io.File ; 13 import java.io.FileReader ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 17 import java.io.Reader ; 18 import java.net.URL ; 19 20 import org.dom4j.Document; 21 import org.dom4j.DocumentException; 22 import org.dom4j.DocumentFactory; 23 import org.dom4j.Element; 24 import org.dom4j.ElementHandler; 25 import org.dom4j.QName; 26 27 import org.xmlpull.v1.XmlPullParser; 28 import org.xmlpull.v1.XmlPullParserException; 29 import org.xmlpull.v1.XmlPullParserFactory; 30 31 42 public class XPP3Reader { 43 44 private DocumentFactory factory; 45 46 47 private XmlPullParser xppParser; 48 49 50 private XmlPullParserFactory xppFactory; 51 52 53 private DispatchHandler dispatchHandler; 54 55 public XPP3Reader() { 56 } 57 58 public XPP3Reader(DocumentFactory factory) { 59 this.factory = factory; 60 } 61 62 79 public Document read(File file) throws DocumentException, IOException , 80 XmlPullParserException { 81 String systemID = file.getAbsolutePath(); 82 83 return read(new BufferedReader (new FileReader (file)), systemID); 84 } 85 86 103 public Document read(URL url) throws DocumentException, IOException , 104 XmlPullParserException { 105 String systemID = url.toExternalForm(); 106 107 return read(createReader(url.openStream()), systemID); 108 } 109 110 135 public Document read(String systemID) throws DocumentException, 136 IOException , XmlPullParserException { 137 if (systemID.indexOf(':') >= 0) { 138 return read(new URL (systemID)); 140 } else { 141 return read(new File (systemID)); 143 } 144 } 145 146 163 public Document read(InputStream in) throws DocumentException, IOException , 164 XmlPullParserException { 165 return read(createReader(in)); 166 } 167 168 185 public Document read(Reader reader) throws DocumentException, IOException , 186 XmlPullParserException { 187 getXPPParser().setInput(reader); 188 189 return parseDocument(); 190 } 191 192 209 public Document read(char[] text) throws DocumentException, IOException , 210 XmlPullParserException { 211 getXPPParser().setInput(new CharArrayReader (text)); 212 213 return parseDocument(); 214 } 215 216 235 public Document read(InputStream in, String systemID) 236 throws DocumentException, IOException , XmlPullParserException { 237 return read(createReader(in), systemID); 238 } 239 240 259 public Document read(Reader reader, String systemID) 260 throws DocumentException, IOException , XmlPullParserException { 261 Document document = read(reader); 262 document.setName(systemID); 263 264 return document; 265 } 266 267 public XmlPullParser getXPPParser() throws XmlPullParserException { 270 if (xppParser == null) { 271 xppParser = getXPPFactory().newPullParser(); 272 } 273 274 return xppParser; 275 } 276 277 public XmlPullParserFactory getXPPFactory() throws XmlPullParserException { 278 if (xppFactory == null) { 279 xppFactory = XmlPullParserFactory.newInstance(); 280 } 281 282 xppFactory.setNamespaceAware(true); 283 284 return xppFactory; 285 } 286 287 public void setXPPFactory(XmlPullParserFactory xPPfactory) { 288 this.xppFactory = xPPfactory; 289 } 290 291 297 public DocumentFactory getDocumentFactory() { 298 if (factory == null) { 299 factory = DocumentFactory.getInstance(); 300 } 301 302 return factory; 303 } 304 305 316 public void setDocumentFactory(DocumentFactory documentFactory) { 317 this.factory = documentFactory; 318 } 319 320 330 public void addHandler(String path, ElementHandler handler) { 331 getDispatchHandler().addHandler(path, handler); 332 } 333 334 341 public void removeHandler(String path) { 342 getDispatchHandler().removeHandler(path); 343 } 344 345 354 public void setDefaultHandler(ElementHandler handler) { 355 getDispatchHandler().setDefaultHandler(handler); 356 } 357 358 protected Document parseDocument() throws DocumentException, IOException , 361 XmlPullParserException { 362 DocumentFactory df = getDocumentFactory(); 363 Document document = df.createDocument(); 364 Element parent = null; 365 XmlPullParser pp = getXPPParser(); 366 pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 367 368 while (true) { 369 int type = pp.nextToken(); 370 371 switch (type) { 372 case XmlPullParser.PROCESSING_INSTRUCTION: { 373 String text = pp.getText(); 374 int loc = text.indexOf(" "); 375 376 if (loc >= 0) { 377 String target = text.substring(0, loc); 378 String txt = text.substring(loc + 1); 379 document.addProcessingInstruction(target, txt); 380 } else { 381 document.addProcessingInstruction(text, ""); 382 } 383 384 break; 385 } 386 387 case XmlPullParser.COMMENT: { 388 if (parent != null) { 389 parent.addComment(pp.getText()); 390 } else { 391 document.addComment(pp.getText()); 392 } 393 394 break; 395 } 396 397 case XmlPullParser.CDSECT: { 398 if (parent != null) { 399 parent.addCDATA(pp.getText()); 400 } else { 401 String msg = "Cannot have text content outside of the " 402 + "root document"; 403 throw new DocumentException(msg); 404 } 405 406 break; 407 } 408 409 case XmlPullParser.ENTITY_REF: 410 break; 411 412 case XmlPullParser.END_DOCUMENT: 413 return document; 414 415 case XmlPullParser.START_TAG: { 416 QName qname = (pp.getPrefix() == null) ? df.createQName(pp 417 .getName(), pp.getNamespace()) : df.createQName(pp 418 .getName(), pp.getPrefix(), pp.getNamespace()); 419 Element newElement = df.createElement(qname); 420 int nsStart = pp.getNamespaceCount(pp.getDepth() - 1); 421 int nsEnd = pp.getNamespaceCount(pp.getDepth()); 422 423 for (int i = nsStart; i < nsEnd; i++) { 424 if (pp.getNamespacePrefix(i) != null) { 425 newElement.addNamespace(pp.getNamespacePrefix(i), 426 pp.getNamespaceUri(i)); 427 } 428 } 429 430 for (int i = 0; i < pp.getAttributeCount(); i++) { 431 QName qa = (pp.getAttributePrefix(i) == null) ? df 432 .createQName(pp.getAttributeName(i)) : df 433 .createQName(pp.getAttributeName(i), pp 434 .getAttributePrefix(i), pp 435 .getAttributeNamespace(i)); 436 newElement.addAttribute(qa, pp.getAttributeValue(i)); 437 } 438 439 if (parent != null) { 440 parent.add(newElement); 441 } else { 442 document.add(newElement); 443 } 444 445 parent = newElement; 446 447 break; 448 } 449 450 case XmlPullParser.END_TAG: { 451 if (parent != null) { 452 parent = parent.getParent(); 453 } 454 455 break; 456 } 457 458 case XmlPullParser.TEXT: { 459 String text = pp.getText(); 460 461 if (parent != null) { 462 parent.addText(text); 463 } else { 464 String msg = "Cannot have text content outside of the " 465 + "root document"; 466 throw new DocumentException(msg); 467 } 468 469 break; 470 } 471 472 default: 473 break; 474 } 475 } 476 } 477 478 protected DispatchHandler getDispatchHandler() { 479 if (dispatchHandler == null) { 480 dispatchHandler = new DispatchHandler(); 481 } 482 483 return dispatchHandler; 484 } 485 486 protected void setDispatchHandler(DispatchHandler dispatchHandler) { 487 this.dispatchHandler = dispatchHandler; 488 } 489 490 501 protected Reader createReader(InputStream in) throws IOException { 502 return new BufferedReader (new InputStreamReader (in)); 503 } 504 } 505 506 542 | Popular Tags |