[jdom-interest] StringBufferAttribute

Noam Tamim noamt at yahoo.com
Sun Jul 8 13:48:35 PDT 2001


Hi there.

What do you guys think about this idea: extending the Attribute class to use
(and expose!) a StringBuffer object for the value.
I'm not suggesting to replace the existing Attribute, I just want comments
on the idea:

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);
}


 - Noam.







More information about the jdom-interest mailing list