package edu.basejava.util;
class Element
{
private String name;
private String text;
private Element parent;
private LinkedList<Attribute> attributes;
private LinkedList<Element> children;
public boolean isRoot()
{
return parent == null;
}
public boolean isLeaf()
{
return children == null;
}
public boolean hasChild()
{
return children != null;
}
public boolean hasAttribute()
{
return attributes != null;
}
public void addAttribute(Attribute ab)
{
if (!hasAttribute())
attributes = new LinkedList<Attribute>();
attributes.append(ab);
}
public void editAttribute(String name, String value)
{
for (Attribute ab : attributes)
if (ab.getName().equals(name))
ab.setValue(value);
}
public Attribute getAttribute(String name)
{
for (Attribute ab : attributes)
if (ab.getName().equals(name))
return ab;
return null;
}
public void delAttribute(String name)
{
this.attributes.setFirst();
for (Attribute ab : this.attributes)
if (ab.getName().equals(name))
attributes.remove(ab);
}
public void editChild(Element e)
{
for (Element cd : this.children)
if (cd.getName().equals(e.getName()))
{
cd.getParent().getChildren().remove(cd);
cd.setAttributes(e.getAttributes());
cd.setChildren(e.getChildren());
cd.setParent(e.getParent());
cd.setText(e.getText());
}
}
public void delChild(String name)
{
this.children.setFirst();
for (Element cd : this.children)
if (cd.getName().equals(name))
children.remove(cd);
}
public void addChild(Element e)
{
if (children == null)
children = new LinkedList<Element>();
e.setParent(this);
children.append(e);
}
public Element getChild(String name)
{
for (Element e : children)
{
if (e.getName().equals(e))
return e;
}
return null;
}
public Element(String name, Attribute... attributes)
{
this.name = name;
if (attributes.length != 0)
{
this.attributes = new LinkedList<Attribute>();
for (Attribute ab : attributes)
this.addAttribute(ab);
}
}
public Element(String name, String text, Attribute... attributes)
{
this.name = name;
this.text = text;
if (attributes.length != 0)
{
this.attributes = new LinkedList<Attribute>();
for (Attribute ab : attributes)
this.attributes.append(ab);
}
}
private String recurElement(Element e)
{
StringBuffer sb = new StringBuffer();
if (e != null)
{
if (e.hasAttribute())
{
sb.append(XmlSymbol
.startElement(e.getName(), e.getAttributes()));
} else
sb.append(XmlSymbol.startElement(e.getName()));
if (e.getText() != null)
sb.append(e.getText());
if (e.hasChild())
{
for (Element el : e.getChildren())
sb.append(e.recurElement(el));
}
sb.append(XmlSymbol.endElement(e.getName()));
}
return sb.toString();
}
public String toString()
{
return this.recurElement(this);
}
public static void main(String[] args)
{
Element e1 = new Element("long", new Attribute("test", "value"));
Element e2 = new Element("child", "子节点", new Attribute("id1",
"firstchild"), new Attribute("id2", "firstchild"));
Element e3 = new Element("叶子");
e1.addChild(e2);
e2.editAttribute("id2", "edit");
e2.addChild(e3);
e2.delAttribute("id1");
e2.addAttribute(new Attribute("tt", "eeer"));
e2.addAttribute(new Attribute("tt2", "eeer"));
Log.println(e3);
// Log.debug(el);
}
public LinkedList<Attribute> getAttributes()
{
return attributes;
}
public void setAttributes(LinkedList<Attribute> attributes)
{
this.attributes = attributes;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Element getParent()
{
return parent;
}
public void setParent(Element parent)
{
this.parent = parent;
}
public LinkedList<Element> getChildren()
{
return children;
}
public void setChildren(LinkedList<Element> children)
{
for (Element e : children)
e.setParent(this);
this.children = children;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
}