1 package com.thoughtworks.xstream.io.xml; 2 3 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 4 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 5 import com.thoughtworks.xstream.io.StreamException; 6 import com.thoughtworks.xstream.io.HierarchicalStreamDriver; 7 8 import javax.xml.stream.*; 9 import java.io.Reader ; 10 import java.io.Writer ; 11 12 18 public class StaxDriver implements HierarchicalStreamDriver { 19 20 private static boolean libraryPresent; 21 22 private QNameMap qnameMap; 23 private XMLInputFactory inputFactory; 24 private XMLOutputFactory outputFactory; 25 private boolean repairingNamespace = false; 26 27 public StaxDriver() { 28 this.qnameMap = new QNameMap(); 29 } 30 31 public StaxDriver(QNameMap qnameMap) { 32 this.qnameMap = qnameMap; 33 } 34 35 public StaxDriver(QNameMap qnameMap, boolean repairingNamespace) { 36 this.qnameMap = qnameMap; 37 this.repairingNamespace = repairingNamespace; 38 } 39 40 public HierarchicalStreamReader createReader(Reader xml) { 41 if (!libraryPresent) { 42 try { 43 Class.forName("javax.xml.stream.XMLStreamReader"); 44 } 45 catch (ClassNotFoundException e) { 46 throw new IllegalArgumentException ("StAX API is not present. Specify another driver." + 47 " For example: new XStream(new DomDriver())"); 48 } 49 libraryPresent = true; 50 } 51 try { 52 return new StaxReader(qnameMap, createParser(xml)); 53 } 54 catch (XMLStreamException e) { 55 throw new StreamException(e); 56 } 57 } 58 59 public HierarchicalStreamWriter createWriter(Writer out) { 60 try { 61 return new StaxWriter(qnameMap, getOutputFactory().createXMLStreamWriter(out), true, isRepairingNamespace()); 62 } 63 catch (XMLStreamException e) { 64 throw new StreamException(e); 65 } 66 } 67 68 public AbstractPullReader createStaxReader(XMLStreamReader in) { 69 return new StaxReader(qnameMap, in); 70 } 71 72 public StaxWriter createStaxWriter(XMLStreamWriter out, boolean writeStartEndDocument) throws XMLStreamException { 73 return new StaxWriter(qnameMap, out, writeStartEndDocument, repairingNamespace); 74 } 75 76 public StaxWriter createStaxWriter(XMLStreamWriter out) throws XMLStreamException { 77 return createStaxWriter(out, true); 78 } 79 80 81 public QNameMap getQnameMap() { 84 return qnameMap; 85 } 86 87 public void setQnameMap(QNameMap qnameMap) { 88 this.qnameMap = qnameMap; 89 } 90 91 public XMLInputFactory getInputFactory() { 92 if (inputFactory == null) { 93 inputFactory = XMLInputFactory.newInstance(); 94 } 95 return inputFactory; 96 } 97 98 public XMLOutputFactory getOutputFactory() { 99 if (outputFactory == null) { 100 outputFactory = XMLOutputFactory.newInstance(); 101 outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", isRepairingNamespace() ? Boolean.TRUE : Boolean.FALSE); 102 } 103 return outputFactory; 104 } 105 106 public boolean isRepairingNamespace() { 107 return repairingNamespace; 108 } 109 110 111 protected XMLStreamReader createParser(Reader xml) throws XMLStreamException { 114 return getInputFactory().createXMLStreamReader(xml); 115 } 116 } 117 | Popular Tags |