package edu.vt.marian.WebGate; import java.io.*; import java.net.*; import java.util.*; import edu.vt.marian.common.*; /** Class name: preference Class description: this class represent the preferences of this user, there may be many preferences in the future, the design of this class make it easy to add new preferences. Author: Jianxin Zhao Finished time: ????, 1998 Known bugs: none Platform: jdk1.1.5 under UNIX */ public class preference { /** this is just for debugging */ Debug debug; /** this vector contains all the preference informations */ private Vector pres; /** this constructor will create a preference object, the object is empty upon creation */ public preference(Debug debug) { // first initialize data members init(); this.debug = debug; } /** this constructor will create a preference object from the specified directory */ public preference(String dir, Debug debug) { // first initialize data members init(); this.debug = debug; // we only save the preference of a user to a file at this time File preference_config = new File(dir); // read preferences from the file BufferedReader br; try { FileReader fr = new FileReader(preference_config); br = new BufferedReader(fr); // first read out number of preferences int number_preference = Integer.parseInt(br.readLine()); // then read out each preference int i; name_value_pair nvp; for (i = 0; i < number_preference; i++) { nvp = new name_value_pair(br, debug); pres.addElement(nvp); } br.close(); } catch (IOException e3) { debug.dumpTrace("class preference: error reading data from " + preference_config.getAbsolutePath()); } } /** this constructor will create a preference object from the specified directory */ public preference(BufferedReader br, Debug debug) { // first initialize data members init(); this.debug = debug; try { // first read out number of preferences int number_preference = Integer.parseInt(br.readLine()); // then read out each preference int i; name_value_pair nvp; for (i = 0; i < number_preference; i++) { nvp = new name_value_pair(br, debug); pres.addElement(nvp); } } catch (IOException e3) { debug.dumpTrace("class preference: constructor 2, error reading data from file"); } } /** this constructor will create a preference object from a stream, this might be helpful for remote manage in the future. (not implemented yet) */ public preference(DataInputStream dis, Debug debug) { } /** this method will return the value of the specified preference */ public String get_value(String name) { int i; name_value_pair nvp; for (i = 0; i < pres.size(); i++) { nvp = (name_value_pair) pres.elementAt(i); if (nvp.get_name().equals(name)) { // a match is found, return it's value return nvp.get_value(); } } // no match is found return null; } /** this method will return all the preference names currently in this object */ public Vector get_names() { int i; name_value_pair nvp; Vector names = new Vector(); for (i = 0; i < pres.size(); i++) { nvp = (name_value_pair) pres.elementAt(i); names.addElement(nvp.get_name()); } return names; } /** this method will add the preference with the specified value to this object */ public String add(String name, String value) { int i; name_value_pair nvp; // first search to see if this preference exists in our current // preferences for (i = 0; i < pres.size(); i++) { nvp = (name_value_pair) pres.elementAt(i); if (nvp.get_name().equals(name)) { // already exist, cann't add it return "this preference already exists"; } } // this is a new preference, so add it nvp = new name_value_pair(name, value, debug); pres.addElement(nvp); return "OK"; } /** this method will set the specified preference to the specified value */ public String set(String name, String value) { int i; name_value_pair nvp; // first search to see if this preference exists in our current // preferences for (i = 0; i < pres.size(); i++) { nvp = (name_value_pair) pres.elementAt(i); if (nvp.get_name().equals(name)) { // exist, set it's value nvp.set_value(value); return "OK"; } } // this is a new preference, cann't set it's value directly return "no such preference"; } /** this method will delete the specified preference */ public String delete(String name) { int i; name_value_pair nvp; // first search to see if this preference exists in our current // preferences for (i = 0; i < pres.size(); i++) { nvp = (name_value_pair) pres.elementAt(i); if (nvp.get_name().equals(name)) { // exist, delete it pres.removeElementAt(i); return "OK"; } } // we couldn't found such preference return "no such preference"; } /** this method will print the content of this object to the stream, this might be helpful for remote management in the future. (not implemented yet) */ public String to_stream(DataOutputStream dos) { return null; // not implemented yet } /** this method will save the content of this object to the specified directory */ public String save(String dir) { FileOutputStream fos = null; try { fos = new FileOutputStream(dir, false); } catch (IOException e3) { debug.dumpTrace("class preference: error opening preference config file to write"); } PrintWriter pw = new PrintWriter(fos); // first write number of preferences pw.println(pres.size()); // then write out each preference int i; name_value_pair nvp = null; for (i = 0; i < pres.size(); i++) { nvp = (name_value_pair) pres.elementAt(i); nvp.to_stream(pw); } pw.flush(); pw.close(); return "OK"; } /** this method will save the content of this object to the specified directory */ public String save(PrintWriter pw) { // first write number of preferences pw.println(pres.size()); // then write out each preference int i; name_value_pair nvp = null; for (i = 0; i < pres.size(); i++) { nvp = (name_value_pair) pres.elementAt(i); nvp.to_stream(pw); } return "OK"; } /** this method will initialize the data members of this object */ private void init() { debug = null; pres = new Vector(); } }