1
2
3
4
5
6
7
8
9
10
11 package org.wever.jradiodj.beans;
12
13 import java.io.ByteArrayOutputStream;
14 import java.io.UnsupportedEncodingException;
15 import javax.xml.parsers.ParserConfigurationException;
16 import javax.xml.transform.OutputKeys;
17 import javax.xml.transform.Result;
18 import javax.xml.transform.Transformer;
19 import javax.xml.transform.TransformerException;
20 import javax.xml.transform.TransformerFactory;
21 import javax.xml.transform.dom.DOMSource;
22 import javax.xml.transform.stream.StreamResult;
23
24 import org.w3c.dom.Document;
25
26
27 /***
28 *
29 * @author mick
30 */
31 public abstract class AbstractBean {
32
33 /*** Creates a new instance of AbstractBean. */
34 protected AbstractBean() {
35 }
36
37
38
39 /***
40 *
41 * @throws javax.xml.parsers.ParserConfigurationException
42 * @return
43 */
44 public abstract Document toXMLDocument() throws ParserConfigurationException;
45
46 /***
47 *
48 * @throws javax.xml.parsers.ParserConfigurationException
49 * @throws javax.xml.transform.TransformerException
50 * @throws java.io.UnsupportedEncodingException
51 * @return
52 */
53 public final String toXMLString() throws
54 ParserConfigurationException,
55 TransformerException,
56 UnsupportedEncodingException {
57
58 final Document doc = toXMLDocument();
59
60 final ByteArrayOutputStream baStream = new ByteArrayOutputStream();
61 final Result res = new StreamResult(baStream);
62 final Transformer transformer = TransformerFactory.newInstance().newTransformer();
63 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
64 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
65 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
66 transformer.transform(new DOMSource(doc), res);
67 return baStream.toString("UTF-8");
68 }
69
70
71
72
73
74
75
76
77
78
79 }