1 4 package com.openedit.util; 5 6 import java.io.File ; 7 import java.io.FileReader ; 8 import java.io.FileWriter ; 9 import java.io.InputStream ; 10 import java.io.InputStreamReader ; 11 import java.io.Reader ; 12 import java.io.Writer ; 13 14 import org.dom4j.Document; 15 import org.dom4j.DocumentHelper; 16 import org.dom4j.Element; 17 import org.dom4j.io.SAXReader; 18 import org.dom4j.io.XMLWriter; 19 import org.dom4j.util.PerThreadSingleton; 20 21 import com.openedit.OpenEditRuntimeException; 22 import com.openedit.config.XMLConfiguration; 23 24 28 public class XmlUtil 29 { 30 protected PerThreadSingleton fieldReaderPool; 31 protected XmlWriterPool fieldWriterPool; 32 33 public Element getXml(File inFile, String inEncode ) 34 { 35 try 36 { 37 return getXml(new FileReader (inFile), inEncode); 38 } 39 catch (Exception ex) 40 { 41 throw new RuntimeException (ex); 42 } 43 } 44 45 public Element getXml(InputStream inXmlReader, String inEncoding) 46 { 47 try 48 { 49 return getXml(new InputStreamReader (inXmlReader,inEncoding), inEncoding ); 50 } 51 catch (Exception ex) 52 { 53 throw new RuntimeException (ex); 54 } 55 } 56 public Element getXml(Reader inXmlReader, String inEncode) 57 { 58 SAXReader reader = getReader(); 59 try 60 { 61 reader.setEncoding(inEncode); 62 Document document = reader.read(inXmlReader); 63 Element root = document.getRootElement(); 64 return root; 65 } 66 catch ( Exception ex) 67 { 68 throw new OpenEditRuntimeException(ex.getMessage(), ex); 69 } 70 finally 71 { 72 FileUtils.safeClose(inXmlReader); 73 } 74 } 75 public void saveXml(Element inRoot, Writer inWriter, String inEncoding) 76 { 77 if( inRoot.getDocument() != null) 78 { 79 saveXml(inRoot.getDocument(), inWriter, inEncoding); 80 return; 81 } 82 try 83 { 84 XMLWriter writer = getWriter(inEncoding); 85 writer.setWriter(inWriter); 86 writer.write(inRoot); 87 } 88 catch ( Exception ex) 89 { 90 throw new RuntimeException (ex.getMessage(), ex); 91 } 92 finally 93 { 94 FileUtils.safeClose(inWriter); 95 } 96 } 97 98 public void saveXml(Document inRoot, Writer inWriter, String inEncoding) 99 { 100 try 101 { 102 XMLWriter writer = getWriter(inEncoding); 103 writer.setWriter(inWriter); 104 writer.write(inRoot); 105 } 106 catch ( Exception ex) 107 { 108 throw new RuntimeException (ex.getMessage(), ex); 109 } 110 finally 111 { 112 FileUtils.safeClose(inWriter); 113 } 114 } 115 116 public void saveXmlConfiguration(XMLConfiguration inConfig, File inFile) 117 { 118 Document doc = DocumentHelper.createDocument(); 119 Element root = doc.addElement(inConfig.getName()); 120 inConfig.appendXml(inConfig,root); 121 saveXml( doc,inFile); 122 } 123 124 128 public void saveXml(Document inStockQuoteDocument, File inFile) 129 { 130 try 131 { 132 saveXml(inStockQuoteDocument, new FileWriter (inFile), "UTF-8"); 133 } 134 catch ( Exception ex ) 135 { 136 throw new RuntimeException (ex); 137 } 138 } 139 public String xmlEscape(String inCode) 140 { 141 return URLUtilities.xmlEscape(inCode); 142 } 143 144 public SAXReader getReader() 145 { 146 return (SAXReader)getReaderPool().instance(); 147 } 148 public XMLWriter getWriter(String inEncoding) 149 { 150 return getWriterPool().instance(inEncoding); 151 } 152 153 public PerThreadSingleton getReaderPool() 154 { 155 if (fieldReaderPool == null) 156 { 157 fieldReaderPool = new PerThreadSingleton(); 158 fieldReaderPool.setSingletonClassName(SAXReader.class.getName()); 159 } 160 return fieldReaderPool; 161 } 162 163 public XmlWriterPool getWriterPool() 164 { 165 if (fieldWriterPool == null) 166 { 167 fieldWriterPool = new XmlWriterPool(); 168 } 169 return fieldWriterPool; 170 } 171 172 173 } 174 | Popular Tags |