package edu.vt.marian.WebGate; import java.io.*; import java.net.*; import java.util.*; import edu.vt.marian.common.*; /** Class name: name_value_pair Class description: this class represent a name value pair Author: Jianxin Zhao Finished time: ????, 1998 Known bugs: none Platform: jdk1.1.5 under UNIX */ public class name_value_pair { /** this is just for debugging */ Debug debug; /** this is the name of this object */ private String name; /** this is the value of this object */ private String value; /** this constructor will create a name value pair object, the object is empty upon creation */ public name_value_pair(Debug debug) { // first initialize data members init(); this.debug = debug; } /** this constructor will create a name value pair object with the specified name and value */ public name_value_pair(String name, String value, Debug debug) { // first initialize data members init(); this.debug = debug; set_name(name); set_value(value); } /** this constructor will create a name value pair object from a stream */ public name_value_pair(BufferedReader br, Debug debug) { // first initialize data members init(); this.debug = debug; try { // read out the name of this object from the stream String name = new String(""); int number_lines = Integer.parseInt(br.readLine()); int i; for (i = 0; i < number_lines; i++) { if (i > 0) { // we don't add "\n" at the beginning of the first // line name += "\n" + br.readLine(); } else { name += br.readLine(); } } if (i != 0) { set_name(name); } // readout the value of this object from this stream String value = new String(""); number_lines = Integer.parseInt(br.readLine()); for (i = 0; i < number_lines; i++) { if (i > 0) { // we don't add "\n" at the beginning of the first // line value += "\n" + br.readLine(); } else { value += br.readLine(); } } if (i != 0) { set_value(value); } } catch (IOException e) { debug.dumpTrace("class nvp: error read name value pair from stream"); } } /** this method will set the name of this object */ public String set_name(String name) { if (name == null) { this.name = null; return "ok"; } // name not empty this.name = new String(name); return "OK"; } /** this method will return the name of this object */ public String get_name() { if (name == null) { return null; } return new String(name); } /** this method will set the value of this object */ public String set_value(String value) { if (value == null) { this.value = null; return "ok"; } this.value = new String(value); return "OK"; } /** this method will return the value of this object */ public String get_value() { if (value == null) { return null; } return new String(value); } /** this method will print the content of this object to a stream */ public String to_stream(PrintWriter pw) { // used to count lines LinedString ms = new LinedString(debug); // write the name of this object String s = get_name(); if (s == null) { pw.println("0"); } else { pw.println(ms.count_lines(s)); pw.println(s); } // write the value of this object s = get_value(); if (s == null) { pw.println("0"); } else { pw.println(ms.count_lines(s)); pw.println(s); } return "OK"; } /** this method will initialize the data members of this object */ private void init() { debug = null; name = null; value = null; } }