KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > util > XmlUtil


1 /*
2  * Created on May 19, 2005
3  */

4 package com.openedit.util;
5
6 import java.io.File JavaDoc;
7 import java.io.FileReader JavaDoc;
8 import java.io.FileWriter JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.Reader JavaDoc;
12 import java.io.Writer JavaDoc;
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 /**
25  * @author cburkey
26  *
27  */

28 public class XmlUtil
29 {
30     protected PerThreadSingleton fieldReaderPool;
31     protected XmlWriterPool fieldWriterPool;
32
33     public Element getXml(File JavaDoc inFile, String JavaDoc inEncode )
34     {
35         try
36         {
37             return getXml(new FileReader JavaDoc(inFile), inEncode);
38         }
39         catch (Exception JavaDoc ex)
40         {
41             throw new RuntimeException JavaDoc(ex);
42         }
43     }
44     
45     public Element getXml(InputStream JavaDoc inXmlReader, String JavaDoc inEncoding)
46     {
47         try
48         {
49             return getXml(new InputStreamReader JavaDoc(inXmlReader,inEncoding), inEncoding );
50         }
51         catch (Exception JavaDoc ex)
52         {
53             throw new RuntimeException JavaDoc(ex);
54         }
55     }
56     public Element getXml(Reader JavaDoc inXmlReader, String JavaDoc 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 JavaDoc 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 JavaDoc inWriter, String JavaDoc 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 JavaDoc ex)
89         {
90             throw new RuntimeException JavaDoc(ex.getMessage(), ex);
91         }
92         finally
93         {
94             FileUtils.safeClose(inWriter);
95         }
96     }
97     
98     public void saveXml(Document inRoot, Writer JavaDoc inWriter, String JavaDoc inEncoding)
99     {
100         try
101         {
102             XMLWriter writer = getWriter(inEncoding);
103             writer.setWriter(inWriter);
104             writer.write(inRoot);
105         }
106         catch ( Exception JavaDoc ex)
107         {
108             throw new RuntimeException JavaDoc(ex.getMessage(), ex);
109         }
110         finally
111         {
112             FileUtils.safeClose(inWriter);
113         }
114     }
115     
116     public void saveXmlConfiguration(XMLConfiguration inConfig, File JavaDoc 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     /**
125      * @param inStockQuoteDocument
126      * @param inFile
127      */

128     public void saveXml(Document inStockQuoteDocument, File JavaDoc inFile)
129     {
130         try
131         {
132             saveXml(inStockQuoteDocument, new FileWriter JavaDoc(inFile), "UTF-8");
133         }
134         catch ( Exception JavaDoc ex )
135         {
136             throw new RuntimeException JavaDoc(ex);
137         }
138     }
139     public String JavaDoc xmlEscape(String JavaDoc 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 JavaDoc 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