DefaultJDOMExternalizer question

to make DefaultJDOMExternalizer work,one must make the field public,the question is if that field is composite object(not a simple type as int or string) then to make DefaultJDOMExternalizer work for it,have i make all its field public also or it is enough to make it public in the parent object

6 comments

If DefaultJDOMExternalizer finds a public field which itself implements JDOMExternalizable, then it calls writeExternal on this object. Now it is the responsibility of that object to implement writeExternal correctly. If you want to use DefaultJDOMExternalizer there too, then all fields have to be public again.

0

ok,thanks ,this serve as a begining
the question is if my public field is a list of files ,can i utilise DefaultJDOMExternalizer functionality here?
if not,can u provide a quick example
thanks

0

DefaultJDOMExternalizer doesn't know anything about lists or files. The only non-primitive fields are String, Color, or a class implementing JDOMExternalizable itself. If you have anything else (like a list of files) then you have to implement writeExternal yourself.

For a start you could try something like this:

 files = new ArrayList();

    public void readExternal(Element element) throws InvalidDataException {
        for (Element child : (List) element.getChildren("file")) {
            files.add(new File(child.getAttributeValue("path")));
        }
    }

    public void writeExternal(Element element) throws WriteExternalException {
        for (File file : files) {
            Element child = new Element("file");
            child.setAttribute("path", file.getAbsolutePath());
            element.addContent(child);
        }
    }
}]]>

0

ok,am i allowed to change the local name of the passed element upob writeExternal ie. element.setName() or the name is mentained by idea to track different plugins
thanks

0

No, changing the passed in element is definitively calling for trouble. It's the callers responsibility to name this element appropriately and to find it again on read.

0

Please sign in to leave a comment.