create or replace package OCI_WWW IS
/*
 Public Types
*/
   TYPE arr_www_text IS table of varchar2(1024);
   TYPE env_var IS RECORD
   (
       var_name VARCHAR2(255),
       var_val	VARCHAR2(1024)
   );
   TYPE env_arr IS TABLE OF env_var;

/*
 Public package level variables
*/
    ContentType VARCHAR2(256);
    AuthRealm	VARCHAR2(2000);
    UserID	VARCHAR2(2000);
    UserPWD	VARCHAR2(2000);
    
/*
 Public procedures
*/
   PROCEDURE ClearAll;
   PROCEDURE SetUpEnv(env IN env_arr);
   PROCEDURE SetEnv(var_name IN VARCHAR2,var_val IN VARCHAR2);
   FUNCTION  GetEnv(var_name IN VARCHAR2) RETURN VARCHAR2;
   FUNCTION  GetEnv RETURN env_arr;
   PROCEDURE putString(str IN VARCHAR2);
   PROCEDURE GoStart;
   FUNCTION  getData(n IN NUMBER) RETURN VARCHAR2;
   FUNCTION  getNextData RETURN VARCHAR2;
   PROCEDURE putStringHdr(str IN VARCHAR2);
   FUNCTION  getData RETURN arr_www_text;
   PROCEDURE GoStartHdr;
   FUNCTION  getDataHdr(n IN NUMBER) RETURN VARCHAR2;
   FUNCTION  getNextDataHdr RETURN VARCHAR2;
   FUNCTION  getDataHdr RETURN arr_www_text;
/*   PROCEDURE putContentType(typ IN VARCHAR2);
   FUNCTION  getContentType RETURN VARCHAR2;*/
end;
/
show errors
CREATE OR REPLACE PACKAGE BODY oci_www AS
   curPos NUMBER;
   environment env_arr;
   www_headers arr_www_text;
   www_body arr_www_text;
   FUNCTION GetVarInd(var_name IN VARCHAR2)RETURN INTEGER
   IS
       ind INTEGER:=environment.FIRST;
   BEGIN
       LOOP
           EXIT WHEN ind IS NULL;
	   IF environment(ind).var_name=var_name THEN
	       RETURN ind;
	   END IF;
	   ind:=environment.NEXT(ind);
       END LOOP;
       RETURN NULL;
   END GetVarInd;
       
   PROCEDURE SetUpEnv(env IN env_arr) IS
   BEGIN
       environment:=env;
   END SetUpEnv;
   PROCEDURE SetEnv(var_name IN VARCHAR2,var_val IN VaRCHAR2) IS
       var_ind INTEGER;
   BEGIN
       var_ind:=GetVarInd(var_name);
       IF var_ind IS NULL THEN
           environment.EXTEND;
	   environment(environment.LAST).var_name:=var_name;
	   environment(environment.LAST).var_val:=var_val;
       ELSE
           environment(var_ind).var_val:=var_val;
       END IF;
   END SetEnv;
   FUNCTION GetEnv(var_name IN VARCHAR2) RETURN VARCHAR2 IS
       var_ind INTEGER:=GetVarInd(var_name);
   BEGIN
       IF var_ind IS NULL THEN
           RETURN NULL;
       END IF;
       RETURN environment(var_ind).var_val;
   END GetEnv;
   FUNCTION  GetEnv RETURN env_arr IS
   BEGIN
       RETURN environment;
   END;
   PROCEDURE GoStart IS
   BEGIN
       curPos:=www_body.FIRST;
   END;
   PROCEDURE putString(str IN VARCHAR2) IS
   BEGIN
       IF NOT str IS NULL THEN
	www_body.EXTEND;
	www_body(www_body.LAST):=str;
       END IF;
   END;
   FUNCTION getNextData RETURN VARCHAR2 IS
       a VARCHAR2(1024);
   BEGIN
       IF curPos<=www_body.LAST THEN
	   a:= www_body(curPos);
           curPos:=www_body.NEXT(curPos);
	   return a;
       END IF;
       RETURN NULL;
   END;
   FUNCTION getData(n IN NUMBER) RETURN VARCHAR2
   IS
   BEGIN
	IF www_headers.EXISTS(n) THEN
		RETURN www_body(n);
	END IF;
	RETURN NULL;
   END;
   FUNCTION getData RETURN arr_www_text
   IS
   BEGIN
       RETURN www_body;
   END;
   PROCEDURE GoStartHdr IS
   BEGIN
       curPos:=www_headers.FIRST;
   END;
   PROCEDURE putStringHdr(str IN VARCHAR2) IS
   BEGIN
       IF NOT str IS NULL THEN
	www_headers.EXTEND;
	www_headers(www_headers.LAST):=str;
       END IF;
   END;
   FUNCTION getNextDataHdr RETURN VARCHAR2 IS
       a VARCHAR2(1024);
   BEGIN
       IF curPos<=www_headers.LAST THEN
	   a:= www_headers(curPos);
           curPos:=www_headers.NEXT(curPos);
	   return a;
       END IF;
       RETURN NULL;
   END;
   FUNCTION getDataHdr(n IN NUMBER) RETURN VARCHAR2
   IS
   BEGIN
	IF www_headers.EXISTS(n) THEN
		RETURN www_headers(n);
	END IF;
	RETURN NULL;
   END;
   FUNCTION getDataHdr RETURN arr_www_text
   IS
   BEGIN
       RETURN www_headers;
   END;
   PROCEDURE ClearAll
   IS
   BEGIN
	www_headers:=arr_www_text();
	www_body:=arr_www_text();
	environment:=env_arr();
	ContentType:='text/html';
	AuthRealm:='PLSQL';
	UserID:=NULL;
	UserPWD:=NULL;
   END ClearAll;
BEGIN
    ClearAll;
END;
/
show errors
