View Javadoc

1   package org.opensync.engine.server.adapter;
2   
3   import java.io.IOException;
4   
5   import javax.xml.parsers.ParserConfigurationException;
6   
7   import org.dom4j.DocumentException;
8   import org.opensync.engine.server.OpenSync;
9   import org.opensync.engine.server.Statistic;
10  import org.opensync.engine.server.View;
11  import org.opensync.engine.server.Window;
12  import org.opensync.engine.server.protocol.WinFileProtocol;
13  import org.xml.sax.SAXException;
14  
15  public class TxtFileAdapter extends FileAdapter {
16  
17    private boolean status=true;
18  
19    public void startReadInputFile(boolean status) {
20      this.status = status;
21    }
22  
23    /***
24     * This class represents the adpater to adapt txt data source format to the OpenSync
25     * data format
26     *
27     */
28    public TxtFileAdapter() {
29    }
30  
31    /***
32     * Adapt the the data source format to the OpenSync
33     * data format
34     *
35     * @param	txt the txt view
36     * @param	view the view
37     * @exception	Exception
38     */
39    synchronized public String adaptInputView(String txt,View view, Statistic stat, Window win)throws Exception{
40      String fileSeparator = System.getProperty("file.separator");
41      String configFolder = System.getProperty("opensync.configfolder");
42      configFolder = (configFolder == null ? "" : configFolder);
43  
44      String descriptorFileName = view.getDescriptor();
45      String fullDescriptorFileName = OpenSync.getInstance().getFilePath(
46        fileSeparator+"etc"+fileSeparator+configFolder+fileSeparator+"descript"+fileSeparator + descriptorFileName  ,true
47      );
48  
49      Txt2Xml txt2xml = new Txt2Xml();
50      txt2xml.startReadInputFile(status);
51      txt2xml.readDescriptor(fullDescriptorFileName);
52      String s = txt2xml.parseTxt(txt);
53      stat.query += txt2xml.getNbRow();
54      return s;
55    }
56  
57    /***
58     * Adapt the the data OpenSync format to the
59     * data source format
60     *
61     * @param	xml the txt view
62     * @param	view the view
63     * @exception	IOException
64     * @exception	DocumentException
65     * @exception	SAXException
66     * @exception	ParserConfigurationException
67     */
68    synchronized public String adaptOutputView(String xml,View view, Statistic stat)
69    throws IOException,SAXException, ParserConfigurationException {
70  
71        String fileSeparator = System.getProperty("file.separator");
72        String configFolder = System.getProperty("opensync.configfolder");
73        configFolder = (configFolder == null ? "" : configFolder);
74  
75        String descriptorFileName = view.getDescriptor();
76  
77        String fullDescriptorFileName = OpenSync.getInstance().getFilePath(
78          fileSeparator + "etc" + fileSeparator + configFolder + fileSeparator +
79          "descript" + fileSeparator + descriptorFileName, true
80          );
81  
82        Xml2Txt xml2txt = new Xml2Txt();
83        xml2txt.readDescriptor(fullDescriptorFileName);
84        if (this.getFileConnector().getProtocol()instanceof WinFileProtocol) {
85          WinFileProtocol winFileProtocol = ( (WinFileProtocol) (this.
86            getFileConnector().getProtocol()));
87          winFileProtocol.getAFileHelper().setStarting(!xml2txt.getAppend());
88          winFileProtocol.setRemoveFile(xml2txt.getRemove_file());
89        }
90        String s = xml2txt.parseXml(xml);
91        stat.insert = xml2txt.getNbRow();
92        return s;
93  
94  
95  
96    }
97  
98  
99    /***
100    * Use for debug only
101    *
102    *
103    */
104   public String toString(){
105     StringBuffer buffer = new StringBuffer();
106     buffer.append("TxtFileAdapter{\n");
107     buffer.append("}\n");
108     return buffer.toString();
109   }
110 
111 }