1 package com.thoughtworks.xstream.io.xml;2 3 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;4 import com.thoughtworks.xstream.io.HierarchicalStreamReader;5 import com.thoughtworks.xstream.io.StreamException;6 import org.w3c.dom.Document ;7 import org.xml.sax.InputSource ;8 import org.xml.sax.SAXException ;9 10 import javax.xml.parsers.DocumentBuilder ;11 import javax.xml.parsers.DocumentBuilderFactory ;12 import javax.xml.parsers.FactoryConfigurationError ;13 import javax.xml.parsers.ParserConfigurationException ;14 import java.io.IOException ;15 import java.io.Reader ;16 17 public class DomDriver implements HierarchicalStreamDriver {18 19 private String encoding;20 21 public DomDriver() {22 encoding = "UTF-8";23 }24 25 public DomDriver(String encoding) {26 this.encoding = encoding;27 }28 29 public HierarchicalStreamReader createReader(Reader xml) {30 try {31 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();32 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();33 Document document = documentBuilder.parse(new InputSource (xml));34 return new DomReader(document);35 } catch (FactoryConfigurationError e) {36 throw new StreamException(e);37 } catch (ParserConfigurationException e) {38 throw new StreamException(e);39 } catch (SAXException e) {40 throw new StreamException(e);41 } catch (IOException e) {42 throw new StreamException(e);43 }44 }45 46 }47