java ResourceBundle editor

Status
Not open for further replies.
i know the underlying implementation of ResourceBundle is nothing more than a fileReader and a string tokenizer with a hastable for retreival. that being said, its easy to get properties from a config file. however setting properties isnt so easy.

i've done a few searches but cant seem to find anything out there that does

String s = resources.getString("keyName1");
resources.setString("keyname2", "keyValue2");
resources.save();

anyone out there have a slick subclass of a ResourceBundle?
 

Nimdok

Ars Scholae Palatinae
1,214
I came across basically the same problem you did. Seeing your post makes me wish I had gone and done some more looking. :)

I'm writing some code that might be similar to what you need. However I really am writing it right NOW. So it's kinda untested in the real world. Basically I have a Resources interface, and for now I am implementing resources as files. This is clearly awful if all you have is a bunch of Strings. Later I'd like to get an actual file format, but as I'm sure you realize that would be a lot of work.

If you'd like to swap code and ideas, send me an email or IM me:

nimdok@jhu.edu
FreshAnus

Here are the interfaces I'm using now:

code:<HR>
Code:
public abstract class Resources {
    
    public static char DELIMITER = '.';
    public static String FILE_EXTENSION = ".rcrs";<P>    public class DoesNotExistException extends IOException {
	public String getMessage() {
	    return "Resource does not exist.";
	}
    }<P>    public static Resources loadResources(File f) throws IOException {
	try {
	    FileInputStream in = new FileInputStream(f);
	    ObjectInputStream oin = new ObjectInputStream(in);
	    Object o = oin.readObject();
	    if(! (o instanceof Resources))
		throw new IOException("Not a Resources file");
	    Resources ret = (Resources)o;
	    ret.restore(f);
	    return ret;
	}
	catch(Exception e) {
	    throw new IOException(e.getMessage());
	}
    }<P>    //write Resources object to file, using Externalizable or Serializable
    public abstract void save(File f) throws IOException;<P>    //the File you were saved to, grab any information you may need from it
    public abstract void restore(File f);<P>    public abstract void createCategory(String category) throws IOException;
    public abstract void put(Resource r, String key) throws IOException;
    public abstract Resource get(String key) throws IOException;
    public abstract void remove(String key);
    public abstract DefaultTreeModel getTreeModel();
    
}<P>public interface Resource extends Serializable {<P>    public Component getEditor(ResourceNode node);<P>}<P>public class StringResource implements Resource {<P>    protected String string;<P>    public StringResource(String s) {
	string = s;
    }<P>    public String getString() {
	return string;
    }<P>    public void setString(String s) {
	this.string = s;
    }<P>    public Component getEditor(ResourceNode node) {
	return new StringResourceEditor(this, node);
    }<P>    public boolean equals(Object o) {
	if(! (o instanceof StringResource))
	    return false;
	StringResource s = (StringResource)o;
	return s.string.equals(string);
    }<P>    public String toString() {
	return "String resource: " + getString();
    }<P>}
 
Status
Not open for further replies.