View Javadoc

1   /*
2    * AbstractBean.java
3    *
4    * Created on 21. juli 2005, 15:03
5    *
6    * To change this template, choose Tools | Options and locate the template under
7    * the Source Creation and Management node. Right-click the template and choose
8    * Open. You can then make changes to the template in the Source Editor.
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      // Displays //
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          // process the source tree
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  // [FIXME] add SOAP dependencies to project.xml
71  //    public final SOAPElement toXMLSOAPElement() throws TransformerConfigurationException, ParserConfigurationException{
72  //        final Document doc = toXMLDocument();
73  //        //create a SOAPElement 
74  //        final SOAPElement parent = SOAPFactory.newInstance().createElement("dummy");
75  //        final Transformer transformer = TransformerFactory.newInstance().newTransformer();
76  //        transformer.transform(new DOMSource(doc), new DOMResult(parent));
77  //        return (SOAPElement) parent.getChildElements().next();
78  //    }
79  }