[jdom-interest] Parents - why do we need them?
Noam Tamim
noamt at yahoo.com
Mon Jul 2 13:32:31 PDT 2001
> >That's the whole idea of "Shared"; my target was definitely not memory
> >optimization, but the ability to change the attributes of many elements
with
> >a single call. Another approach could be to use an Attribute that uses a
> >StringBuffer for the value, and exposes it - so I can use the same
> >StringBuffer for many Attributes, and change it later.
> That might be the most graceful of the possibilities, since it would
> provide a fairly trivial way to implement copy-on-write as well, for
> those who want that. Feel like implementing an example?
Here:
===
import org.jdom.*;
import org.jdom.output.*;
public class SBAttribute extends Attribute {
protected StringBuffer value;
public SBAttribute(String name, StringBuffer value, Namespace ns) {
super(); // avoid setting the original String value.
setName(name);
setValue(value);
setNamespace(ns);
}
public SBAttribute(String name, StringBuffer value) {
this(name,value,Namespace.NO_NAMESPACE);
}
// return the actual StringBuffer used by this attribute.
public StringBuffer getBuffer() {
return value;
}
public String getValue() {
return value.toString();
}
public Attribute setValue(String value) {
return setValue(new StringBuffer(value));
}
public SBAttribute setValue(StringBuffer value) {
// here should probably come some verifications.
this.value=value;
return this;
}
}
===
And here's an example of using it:
void main() throws IOException {
Element root=new Element("root");
StringBuffer name=new StringBuffer("noam");
root.addContent(new Element("user").setAttribute(new
SBAttribute("name",name)));
root.addContent(new Element("user").setAttribute(new
SBAttribute("name",name)));
root.addContent(new Element("user").setAttribute(new
SBAttribute("name",name)));
Document doc=new Document(root);
XMLOutputter xout=new XMLOutputter(" ",true);
System.out.println("name="+name);
xout.output(doc,System.out);
name.reverse();
System.out.println("name="+name);
xout.output(doc,System.out);
}
Now, I'm really not sure this is the correct way to go; I was just throwing
an idea, to see what people think of it. I'm not even sure there's use to
it.
- Noam.
More information about the jdom-interest
mailing list