View Javadoc

1   package org.opensync.engine.server.adapter;
2   
3   import java.io.IOException;
4   import java.io.StringReader;
5   import java.io.StringWriter;
6   
7   import javax.xml.transform.ErrorListener;
8   import javax.xml.transform.Templates;
9   import javax.xml.transform.Transformer;
10  import javax.xml.transform.TransformerConfigurationException;
11  import javax.xml.transform.TransformerException;
12  import javax.xml.transform.TransformerFactory;
13  import javax.xml.transform.stream.StreamResult;
14  import javax.xml.transform.stream.StreamSource;
15  
16  import org.dom4j.DocumentException;
17  import org.opensync.engine.server.OpenSync;
18  import org.opensync.engine.server.Statistic;
19  import org.opensync.engine.server.View;
20  import org.opensync.engine.server.Window;
21  import org.xml.sax.SAXException;
22  
23  /***
24   * This class represents the adpater to adapt xml data source format to the OpenSync
25   * data format
26   *
27   * @version	1.0
28   * @author	SOFTMED
29   */
30  
31  public class XmlFileAdapter extends FileAdapter implements ErrorListener {
32  
33    // Receive notification of a recoverable error.
34    public void error(TransformerException exception) throws TransformerException {
35      throw exception;
36    }
37  
38    // Receive notification of a non-recoverable error.
39    public void fatalError(TransformerException exception) throws TransformerException {
40      throw exception;
41    }
42  
43    public void warning(TransformerException exception) throws TransformerException {
44      throw exception;
45    }
46    /***
47     * Construct the XmlFileAdpter
48     *
49     */
50    public XmlFileAdapter() {
51    }
52  
53    /***
54     * Adapt the the data source format to the OpenSync
55     * data format
56     *
57     * @param	xml the xml view
58     * @param	view the view
59     * @exception	Exception
60     */
61    public String adaptInputView(String xml,View view, Statistic stat, Window win)throws Exception{
62      String xslDescriptor = view.getDescriptorIn();
63      if(xslDescriptor == null || xslDescriptor.length() == 0){
64        return xml;
65      }
66      else{
67        String fileSeparator = System.getProperty("file.separator");
68        String configFolder = System.getProperty("opensync.configfolder");
69        configFolder = (configFolder == null ? "" : configFolder);
70  
71        return transform(xml,
72          OpenSync.getInstance().getFilePath("etc"+fileSeparator+configFolder+fileSeparator+"descript"+fileSeparator + xslDescriptor,true)
73        );
74      }
75    }
76  
77    /***
78     * Adapt the the data OpenSync format to the
79     * data source format
80     *
81     * @param	xml the xml view
82     * @param	view the view
83     * @exception	IOException
84     * @exception	DocumentException
85     * @exception	SAXException
86     * @exception	Exception
87     */
88    public String adaptOutputView(String xml,View view, Statistic stat)throws Exception{
89      String xslDescriptor = view.getDescriptorOut();
90      if(xslDescriptor == null || xslDescriptor.length() == 0){
91        return xml;
92      }
93      else{
94        String fileSeparator = System.getProperty("file.separator");
95        String configFolder = System.getProperty("opensync.configfolder");
96        configFolder = (configFolder == null ? "" : configFolder);
97  
98        return transform(xml,
99          OpenSync.getInstance().getFilePath("etc"+fileSeparator+configFolder+fileSeparator+"descript"+fileSeparator + xslDescriptor,true)
100       );
101     }
102   }
103 
104   /***
105    * Transform the xml view to the OpenSync view
106    *
107    * @param	xml
108    * @param	xsl
109    * @exception	TransformerConfigurationException
110    * @exception	TransformerException
111    */
112   protected String transform(String xml,String xsl)
113   throws TransformerConfigurationException,TransformerException{
114     TransformerFactory factory = TransformerFactory.newInstance();
115     Templates templates = factory.newTemplates(
116       new StreamSource(new StringReader(xml))
117     );
118     Transformer transformer = templates.newTransformer();
119     transformer.setErrorListener(this);
120     StringWriter resultWriter = new StringWriter();
121     transformer.transform(
122       new StreamSource(new StringReader(xml)),
123       new StreamResult(resultWriter)
124     );
125     return resultWriter.getBuffer().toString();
126   }
127   /***
128    * Use for debug only
129    *
130    */
131   public String toString(){
132     StringBuffer buffer = new StringBuffer();
133     buffer.append("XmlFileAdapter{\n");
134     buffer.append("}\n");
135     return buffer.toString();
136   }
137 
138 }