Arcade Village Blog

From applet to application (Part 3): getDocumentBase and getCodeBase

2019-05-28 Java programming
We are nearing the end of our journey. The applet is encapsulated in a Frame and can retrieve its parameters. We still have to tackle the last two functions: GetDocumentBase and GetCodeBase.
These two functions do not really make sense for an application, so I decided to point them to the user directory. For this, the following code is sufficient:

public class CCApplet extends Panel
{
private static final long serialVersionUID = 1L;
private String udb = null;
private Properties params = new Properties();
private CCAppletContext ca;

public CCApplet()
{
ca = new CCAppletContext(this);
}

public static String getCompleteDirPath( String srelatif )
{
String s = System.getProperty("user.dir")+System.getProperty("file.separator")+srelatif;
s = s.replace("./", "");
s = "file:"+System.getProperty("file.separator")+ s;
s = s.replace("/", System.getProperty("file.separator"));
s = s.replace("", System.getProperty("file.separator"));
return s;
}

public URL getDocumentBase()
{
try
{
return new URL(CCApplet.getCompleteDirPath(this.udb));
}
catch(Exception e)
{
System.out.println("Erreur URL : "+CCApplet.getCompleteDirPath(this.udb));
return null;
}
}

public URL getCodeBase()
{
try
{
return new URL(CCApplet.getCompleteDirPath(this.udb));
}
catch(Exception e)
{
System.out.println("Erreur URL :"+CCApplet.getCompleteDirPath(this.udb));
return null;
}
}

public void setDocumentBase(String iudb)
{
this.udb = iudb;
}

udb is a string containing the directory name. It is updated in the first lines of code of the application using the setDocumentBase function, which does not exist in the applet class.
getCodeBase and getDocumentBase will return the same path.
Example for Hebi under windows:
c:\users\christophe\hebi\
(Christophe is my username.

The applets had one last feature: they could save or retrieve their data in cookies. This is how my game applets saved the player's high score.

In the last part, we will implement the AppletCookieMgr class that simulates this possibility.