View Javadoc

1   package org.fedoracommons.funapi;
2   
3   import java.io.OutputStream;
4   import java.io.StringWriter;
5   
6   import javax.xml.parsers.DocumentBuilder;
7   import javax.xml.parsers.DocumentBuilderFactory;
8   import javax.xml.parsers.ParserConfigurationException;
9   import javax.xml.transform.Result;
10  import javax.xml.transform.Source;
11  import javax.xml.transform.Transformer;
12  import javax.xml.transform.TransformerConfigurationException;
13  import javax.xml.transform.TransformerException;
14  import javax.xml.transform.TransformerFactory;
15  import javax.xml.transform.dom.DOMSource;
16  import javax.xml.transform.stream.StreamResult;
17  
18  import org.w3c.dom.DOMImplementation;
19  import org.w3c.dom.Document;
20  import org.w3c.dom.Element;
21  
22  
23  /**
24   * Provides an UnAPI formats serialization. The serialization format is given 
25   * by the following Relax-NG schema:
26   * <pre>
27     element formats {
28       attribute id { text }?, 
29       element format {
30         attribute name { text }, 
31         attribute type { text }, 
32         attribute docs { text }?,
33       }*
34     }</pre>
35   *
36   * @author Edwin Shin
37   * @since 0.1
38   * @version $Id: FormatsSerializer.java 33 2008-10-25 19:31:36Z pangloss $
39   * @see <a href="http://unapi.info/specs/">unAPI Specs</a>
40   */
41  public class FormatsSerializer {
42      
43      private DOMImplementation impl;
44      
45      private TransformerFactory xformFactory;
46      
47      public FormatsSerializer() throws UnapiException {
48          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
49          dbFactory.setNamespaceAware(true);
50  
51          try {
52              DocumentBuilder builder = dbFactory.newDocumentBuilder();
53              impl = builder.getDOMImplementation();
54  
55              xformFactory = TransformerFactory.newInstance();
56          } catch (ParserConfigurationException e) {
57              throw new UnapiException(e.getMessage(), e);
58          }
59      }
60      
61      public void serialize(UnapiFormats formats, OutputStream out) throws UnapiException { 
62          transform(formats, new StreamResult(out));
63      }
64      
65      public Document toDocument(UnapiFormats formats) {
66          Document doc = impl.createDocument(null, "formats", null);
67          Element rootElement = doc.getDocumentElement();
68          
69          if (formats.getId() != null) {
70              rootElement.setAttribute("id", formats.getId());
71          }
72  
73          for (UnapiFormat format : formats.getFormats()) {
74              Element f = doc.createElement("format");
75              f.setAttribute("name", format.getName());
76              f.setAttribute("type", format.getType());
77              if (format.getDocs() != null) {
78                  f.setAttribute("docs", format.getDocs());
79              }
80              rootElement.appendChild(f);
81          }
82          return doc;
83      }
84      
85      public String toString(UnapiFormats formats) throws UnapiException {
86          StringWriter sw = new StringWriter();
87          transform(formats, new StreamResult(sw));
88          return sw.toString();
89      }
90      
91      private void transform(UnapiFormats formats, Result output) throws UnapiException {
92          Transformer idTransform;
93          try {
94              idTransform = xformFactory.newTransformer();
95              Source input = new DOMSource(toDocument(formats));
96              idTransform.transform(input, output);
97          } catch (TransformerConfigurationException e) {
98              throw new UnapiException(e.getMessage(), e);
99          } catch (TransformerException e) {
100             throw new UnapiException(e.getMessage(), e);
101         }
102     }
103 }