package edu.vt.marian.uip; import java.io.*; import java.net.*; import java.util.*; import edu.vt.marian.common.*; /** class name: parameter class description: this class implement parameter related operations uses the services of class(es): xdr designer: Junni Fan (jfan@vt.edu) implementators: Junni Fan(jfan@vt.edu) finished time: 11/29/98 known bugs: JDK version: 1.1.6 side effects: */ public class parameter { // possible return values public final static int OK = 0; public final static int ERROR = -4; public final static int INVALID_PARAMETER = -1; public final static int ERROR_IO = -2; public final static int CANNOT_HANDLE = -3; public final static int PROCESS_ERROR = -5; // this is the max possible number of elements in a vector private final static int MAX_NUMBER_ELEMENTS = 10000; // for the purpose of xdr related operation private xdr x; // only for debugging Debug debug; private String name; private String type; private Object value; private boolean valid; /** method description: parameter constructor uses the services of class(es): none input parameter(s): debug -- only for debugging name -- parameter name type -- parameter type value -- parameter value output parameter(s): none return value: none synchronization consideration: none */ public parameter(String name, String type, Object value, Debug debug) { this.debug = debug; x = new xdr( debug ); this.name = name; this.type = type; valid = true; if ( type == null ) { this.type = "NULL"; if ( value == null ) { this.value = null; return; } else { valid = false; debug.dumpTrace( "parameter(constructor from name,type,value): null type but not null value" ); return; } } if ( value == null ) { valid = false; debug.dumpTrace( "parameter(constructor from name,type,value): null value but not null type" ); return; } if ( type.equals( "OBJ_VECT" ) ) { Vector v; try { v = (Vector) value; } catch (Exception e) { valid = false; debug.dumpTrace ( "parameter(constructor from name,type,value): value is inconsistent with type" ); return; } int num_items = v.size(); Vector nv = new Vector(), v1 = null, nv1 = null; // check through the vector elements for ( int i=0; i MAX_NUMBER_ELEMENTS) { // vector has too many elements, probably sth goes wrong debug.dumpTrace("class parameter method read_obj_vect_from_stream_in_xdr, vector over sized"); return null; } Vector v = new Vector(), v1 = null; for ( int i = 0; i < num_elements; i++ ) { v1 = new Vector(); for ( int j = 0; j < 3; j++ ) { v1.addElement( new Integer(x.read_integer_from_stream( bis )) ); } v.addElement( v1 ); } return v; } catch (Exception e) { debug.dumpTrace("class parameter, method read_obj_vect_from_stream_in_xdr, error reading integer from stream"); return null; } } /** method description: read a doc_vect from the input stream uses the services of class(es): xdr input parameter(s): bis -- input stream output parameter(s): none return value: the desired doc_vect, null if error occurred synchronization consideration: none */ public Vector read_doc_vect_from_stream_in_xdr( BufferedInputStream bis ) { // error checking if ( bis == null ) { debug.dumpTrace( "parameter(read_doc_vect_from_stream_in_xdr): bis is null" ); return null; } int num_elements; try { num_elements = x.read_integer_from_stream( bis ); if ( num_elements < 0 ) { debug.dumpTrace( "parameter(read_doc_vect_from_stream_in_xdr): error when read number of elements" ); return null; } if (num_elements > MAX_NUMBER_ELEMENTS) { // vector has too many elements, probably sth goes wrong debug.dumpTrace("class parameter method read_doc_vect_from_stream_in_xdr, vector over sized"); return null; } Vector v = new Vector(), v1 = null; String s = null; for ( int i = 0; i < num_elements; i++ ) { v1 = new Vector(); for ( int j = 0; j < 3; j++ ) { v1.addElement( new Integer(x.read_integer_from_stream( bis )) ); } s = x.read_string_from_stream( bis ); if ( s == null ) { debug.dumpTrace( "parameter(read_doc_vect_from_stream_in_xdr): error in reading string" ); return null; } v1.addElement( s ); v.addElement( v1 ); } return v; } catch (Exception e) { debug.dumpTrace("class parameter, method read_doc_vect_from_stream_in_xdr, error reading integer from stream"); return null; } } /** method description: read a raw_doc_vect from the input stream uses the services of class(es): xdr input parameter(s): bis -- input stream output parameter(s): none return value: the desired raw_doc_vect, null if error occurred synchronization consideration: none */ public Vector read_raw_doc_vect_from_stream_in_xdr( BufferedInputStream bis ) { // error checking if ( bis == null ) { debug.dumpTrace( "parameter(read_raw_doc_vect_from_stream_in_xdr): bis is null" ); return null; } int num_elements; try { num_elements = x.read_integer_from_stream( bis ); } catch (Exception e) { debug.dumpTrace("class parameter, method read_raw_doc_vect_from_stream_in_xdr, error reading number elements from stream"); return null; } if ( num_elements < 0 ) { debug.dumpTrace( "parameter(read_raw_doc_vect_from_stream_in_xdr): error when read number of elements" ); return null; } if (num_elements > MAX_NUMBER_ELEMENTS) { // vector has too many elements, probably sth goes wrong debug.dumpTrace("class parameter method read_raw_doc_vect_from_stream_in_xdr, vector over sized"); return null; } Vector v = new Vector(); String s = null; for ( int i = 0; i < num_elements; i++ ) { s = x.read_string_from_stream( bis ); if ( s == null ) { debug.dumpTrace( "parameter(read_raw_doc_vect_from_stream_in_xdr): error in reading string" ); return null; } v.addElement( s ); } return v; } /** method description: read a doc_id_vect from the input stream uses the services of class(es): xdr input parameter(s): bis -- input stream output parameter(s): none return value: the desired doc_id_vect, null if error occurred synchronization consideration: none */ public Vector read_doc_id_vect_from_stream_in_xdr( BufferedInputStream bis ) { // error checking if ( bis == null ) { debug.dumpTrace( "parameter(read_doc_id_vect_from_stream_in_xdr): bis is null" ); return null; } int num_elements; try { num_elements = x.read_integer_from_stream( bis ); if ( num_elements < 0 ) { debug.dumpTrace( "parameter(read_doc_id_vect_from_stream_in_xdr): error when read number of elements" ); return null; } if (num_elements > MAX_NUMBER_ELEMENTS) { // vector has too many elements, probably sth goes wrong debug.dumpTrace("class parameter method read_doc_id_vect_from_stream_in_xdr, vector over sized"); return null; } Vector v = new Vector(), v1 = null; for ( int i = 0; i < num_elements; i++ ) { v1 = new Vector(); for ( int j = 0; j < 2; j++ ) { v1.addElement( new Integer(x.read_integer_from_stream( bis )) ); } v.addElement( v1 ); } return v; } catch (Exception e) { debug.dumpTrace("class parameter, method read_doc_id_vector_from_stream_in_xdr, error reading integer from stream"); return null; } } /** method description: write an object_vect to the output stream uses the services of class(es): xdr input parameter(s): bos -- output stream output parameter(s): none return value: OK -- if success ERROR_IO -- wrong in io operations INVALID_PARAMETER -- wrong parameter synchronization consideration: none */ private int obj_vect_to_stream_in_xdr( Vector v, BufferedOutputStream bos ) { // error checking if ( bos == null ) { debug.dumpTrace( "parameter(obj_vect_to_stream_in_xdr): bos is null" ); return INVALID_PARAMETER; } if ( v == null ) { debug.dumpTrace( "parameter(obj_vect_to_stream_in_xdr): vector is null" ); return INVALID_PARAMETER; } int num_elements = v.size(); if ( x.integer_to_stream_in_xdr( num_elements, bos ) != x.OK ) { debug.dumpTrace( "parameter(obj_vect_to_stream_in_xdr): error in writing number of elements" ); return PROCESS_ERROR; } Vector v1 = null; for ( int i = 0; i < num_elements; i++ ) { v1 = (Vector) v.elementAt(i); for ( int j = 0; j < 3; j++ ) { int elem; elem = ((Integer) v1.elementAt(j)).intValue(); if ( x.integer_to_stream_in_xdr( elem, bos ) != x.OK ) { debug.dumpTrace( "parameter(obj_vect_to_stream_in_xdr): error in writing parameter value" ); return PROCESS_ERROR; } } } return OK; } /** method description: write an doc_vect to the output stream uses the services of class(es): xdr input parameter(s): bos -- output stream output parameter(s): none return value: OK -- if success ERROR_IO -- wrong in io operations INVALID_PARAMETER -- wrong parameter synchronization consideration: none */ private int doc_vect_to_stream_in_xdr( Vector v, BufferedOutputStream bos ) { // error checking if ( bos == null ) { debug.dumpTrace( "parameter(doc_vect_to_stream_in_xdr): bos is null" ); return INVALID_PARAMETER; } if ( v == null ) { debug.dumpTrace( "parameter(doc_vect_to_stream_in_xdr): vector is null" ); return INVALID_PARAMETER; } int num_elements = v.size(); if ( x.integer_to_stream_in_xdr( num_elements, bos ) != x.OK ) { debug.dumpTrace( "parameter(doc_vect_to_stream_in_xdr): error in writing number of elements" ); return PROCESS_ERROR; } Vector v1 = null; for ( int i = 0; i < num_elements; i++ ) { v1 = (Vector) v.elementAt(i); for ( int j = 0; j < 3; j++ ) { int elem; elem = ((Integer) v1.elementAt(j)).intValue(); if ( x.integer_to_stream_in_xdr( elem, bos ) != x.OK ) { debug.dumpTrace( "parameter(doc_vect_to_stream_in_xdr): error in writing integers" ); return PROCESS_ERROR; } } if ( x.string_to_stream_in_xdr( (String) v1.elementAt(3), bos ) != x.OK ) { debug.dumpTrace( "parameter(doc_vect_to_stream_in_xdr): error in writing string" ); return PROCESS_ERROR; } } return OK; } /** method description: write a raw_doc_vect to the output stream uses the services of class(es): xdr input parameter(s): bos -- output stream output parameter(s): none return value: OK -- if success ERROR_IO -- wrong in io operations INVALID_PARAMETER -- wrong parameter synchronization consideration: none */ private int raw_doc_vect_to_stream_in_xdr( Vector v, BufferedOutputStream bos ) { // error checking if ( bos == null ) { debug.dumpTrace( "parameter(raw_doc_vect_to_stream_in_xdr): bos is null" ); return INVALID_PARAMETER; } if ( v == null ) { debug.dumpTrace( "parameter(doc_vect_to_stream_in_xdr): vector is null" ); return INVALID_PARAMETER; } int num_elements = v.size(); if ( x.integer_to_stream_in_xdr( num_elements, bos ) != x.OK ) { debug.dumpTrace( "parameter(raw_doc_vect_to_stream_in_xdr): error in writing number of elements" ); return PROCESS_ERROR; } for ( int i = 0; i < num_elements; i++ ) { if ( x.string_to_stream_in_xdr( (String) v.elementAt(i), bos ) != x.OK ) { debug.dumpTrace( "parameter(raw_doc_vect_to_stream_in_xdr): error in writing string" ); return PROCESS_ERROR; } } return OK; } /** method description: write an doc_id_vect to the output stream uses the services of class(es): xdr input parameter(s): bos -- output stream output parameter(s): none return value: OK -- if success ERROR_IO -- wrong in io operations INVALID_PARAMETER -- wrong parameter synchronization consideration: none */ private int doc_id_vect_to_stream_in_xdr( Vector v, BufferedOutputStream bos ) { // error checking if ( bos == null ) { debug.dumpTrace( "parameter(doc_id_vect_to_stream_in_xdr): bos is null" ); return INVALID_PARAMETER; } if ( v == null ) { debug.dumpTrace( "parameter(doc_id_vect_to_stream_in_xdr): vector is null" ); return INVALID_PARAMETER; } int num_elements = v.size(); if ( x.integer_to_stream_in_xdr( num_elements, bos ) != x.OK ) { debug.dumpTrace( "parameter(doc_id_vect_to_stream_in_xdr): error in writing number of elements" ); return PROCESS_ERROR; } Vector v1 = null; for ( int i = 0; i < num_elements; i++ ) { v1 = (Vector) v.elementAt(i); for ( int j = 0; j < 2; j++ ) { int elem; elem = ((Integer) v1.elementAt(j)).intValue(); if ( x.integer_to_stream_in_xdr( elem, bos ) != x.OK ) { debug.dumpTrace( "parameter(doc_id_vect_to_stream_in_xdr): error in writing parameter value" ); return PROCESS_ERROR; } } } return OK; } /** method description: tell whether or not this object is a valid parameter uses the services of class(es): none input parameter(s): none output parameter(s): none return value: true -- this is a valid parameter false -- this is not a valid parameter synchronization consideration: none */ public boolean is_valid() { return valid; } }