View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.opennap.net;
21  
22  import java.io.BufferedWriter;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  
26  public class OpenNapServerFileWriter implements OpenNapServerWriter {
27  
28      //--- Data Field(s) ---
29  
30      private BufferedWriter out = null;
31      private String filename;
32  
33      //--- Constructor(s) ---
34  	
35      public OpenNapServerFileWriter(String filename)
36      {
37  		this.filename = filename;
38      }
39  
40      //--- Method(s) ---
41  
42      public void close()
43      {
44  		try {
45  			if (out != null) {
46  				out.close();
47  			}
48  		}	    
49  		catch (IOException e) {
50  		}
51      }
52  
53      public void open() throws IOException
54      {
55  		out = new BufferedWriter(new FileWriter(filename, false));
56      }
57  
58      public void write(OpenNapServer s) throws IOException
59      {
60  		StringBuffer sb = new StringBuffer();
61  		append(sb, s.getHost());
62  		append(sb, s.getPort() + "");
63  		append(sb, s.getNetwork().getName());
64  		append(sb, (s.isLoginCustomized()) ? s.getNick() : "");
65  		append(sb, (s.isLoginCustomized()) ? s.getPassword() : "");
66  		append(sb, (s.isLoginCustomized()) ? s.getEmail() : "");
67  		append(sb, (s.isRedirector()) ? "true" : "false");
68  		append(sb, s.getAutoJoinChannels());
69  		append(sb, s.getLastConnect()+ "");
70  		append(sb, (s.getAutoConnect()) ? "true" : "false");
71  		append(sb, "");
72  		
73  		out.write(sb.toString());
74  		out.newLine();
75  	}
76  
77  	private static void append(StringBuffer sb, String s)
78  	{
79  		sb.append("\"");
80  		sb.append(s);
81  		sb.append("\" ");
82  	}
83  
84  }