1 7 8 package org.dom4j.io; 9 10 import java.io.BufferedReader ; 11 import java.io.File ; 12 import java.io.FileReader ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.InputStreamReader ; 16 import java.io.Reader ; 17 import java.net.URL ; 18 19 import org.dom4j.Document; 20 import org.dom4j.DocumentException; 21 import org.dom4j.DocumentFactory; 22 import org.dom4j.Element; 23 import org.dom4j.ElementHandler; 24 import org.dom4j.xpp.ProxyXmlStartTag; 25 26 import org.gjt.xpp.XmlEndTag; 27 import org.gjt.xpp.XmlPullParser; 28 import org.gjt.xpp.XmlPullParserException; 29 import org.gjt.xpp.XmlPullParserFactory; 30 31 42 public class XPPReader { 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 XPPReader() { 56 } 57 58 public XPPReader(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(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 return xppFactory; 283 } 284 285 public void setXPPFactory(XmlPullParserFactory xPPFactory) { 286 this.xppFactory = xPPFactory; 287 } 288 289 295 public DocumentFactory getDocumentFactory() { 296 if (factory == null) { 297 factory = DocumentFactory.getInstance(); 298 } 299 300 return factory; 301 } 302 303 314 public void setDocumentFactory(DocumentFactory documentFactory) { 315 this.factory = documentFactory; 316 } 317 318 328 public void addHandler(String path, ElementHandler handler) { 329 getDispatchHandler().addHandler(path, handler); 330 } 331 332 339 public void removeHandler(String path) { 340 getDispatchHandler().removeHandler(path); 341 } 342 343 352 public void setDefaultHandler(ElementHandler handler) { 353 getDispatchHandler().setDefaultHandler(handler); 354 } 355 356 protected Document parseDocument() throws DocumentException, IOException , 359 XmlPullParserException { 360 Document document = getDocumentFactory().createDocument(); 361 Element parent = null; 362 XmlPullParser parser = getXPPParser(); 363 parser.setNamespaceAware(true); 364 365 ProxyXmlStartTag startTag = new ProxyXmlStartTag(); 366 XmlEndTag endTag = xppFactory.newEndTag(); 367 368 while (true) { 369 int type = parser.next(); 370 371 switch (type) { 372 case XmlPullParser.END_DOCUMENT: 373 return document; 374 375 case XmlPullParser.START_TAG: { 376 parser.readStartTag(startTag); 377 378 Element newElement = startTag.getElement(); 379 380 if (parent != null) { 381 parent.add(newElement); 382 } else { 383 document.add(newElement); 384 } 385 386 parent = newElement; 387 388 break; 389 } 390 391 case XmlPullParser.END_TAG: { 392 parser.readEndTag(endTag); 393 394 if (parent != null) { 395 parent = parent.getParent(); 396 } 397 398 break; 399 } 400 401 case XmlPullParser.CONTENT: { 402 String text = parser.readContent(); 403 404 if (parent != null) { 405 parent.addText(text); 406 } else { 407 String msg = "Cannot have text content outside of the " 408 + "root document"; 409 throw new DocumentException(msg); 410 } 411 412 break; 413 } 414 415 default: 416 throw new DocumentException("Error: unknown type: " + type); 417 } 418 } 419 } 420 421 protected DispatchHandler getDispatchHandler() { 422 if (dispatchHandler == null) { 423 dispatchHandler = new DispatchHandler(); 424 } 425 426 return dispatchHandler; 427 } 428 429 protected void setDispatchHandler(DispatchHandler dispatchHandler) { 430 this.dispatchHandler = dispatchHandler; 431 } 432 433 444 protected Reader createReader(InputStream in) throws IOException { 445 return new BufferedReader (new InputStreamReader (in)); 446 } 447 } 448 449 485 | Popular Tags |