package mefir.j4.properties;
import java.applet.Applet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import mefir.j4.Debug;
import netscape.javascript.JSObject;
public class PropApplet implements PropObject{
public PropApplet(Applet applet) throws Exception{
this.applet = applet;
format = new SimpleDateFormat("EE, dd-MMM-yy HH:mm:ss zz", new Locale("en","US"));
format.setTimeZone(TimeZone.getTimeZone("GMT"));
doc = (JSObject) (JSObject.getWindow(applet)).getMember("document");
}
Applet applet;
JSObject doc;
String name = "mefir_j4_prefs";
long time = 1000L * 3600L * 24L * 365L * 10L;
DateFormat format;
public String load() {
try{
return this.getCookie();
}catch(Throwable t){
Debug.println(t);
return "";
}
}
public void save(String s) {
try{
this.setCookie(s);
}catch(Throwable t){
Debug.println(t);
}
}
void setCookie(String value)throws Exception{
Date d = new Date(System.currentTimeMillis() + time);
String cookie = name+"="+value+"; expires="+format.format(d);
doc.setMember("cookie", cookie);
}
String getCookies() throws Exception{
return (String) doc.getMember("cookie");
}
String getCookie()throws Exception{
String cookie = getCookies();
String search = this.name+"=";
if(cookie.length() > 0){
int off = cookie.indexOf(search);
if(off !=-1){
off+=search.length();
int end = cookie.indexOf(";",off);
if(end == -1) end = cookie.length();
return cookie.substring(off,end);
}
}
return "";
}
}