18 package org.turro.configuration;
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.jdom.Content;
23 import org.jdom.Document;
24 import org.jdom.Element;
25 import org.jdom.JDOMException;
26 import org.jdom.xpath.XPath;
32 public abstract class Path {
35 protected Document
doc;
38 if(path.startsWith(
"/")) {
39 nodes = path.substring(1).split(
"/");
41 nodes = path.split(
"/");
57 public Element
getNode(
boolean create)
throws JDOMException {
58 Element curr =
doc.getRootElement();
60 for(String node :
nodes) {
61 Element tmp = (Element) XPath.selectSingleNode(curr, node);
62 if(tmp ==
null && create) {
65 if(tmp ==
null)
return null;
72 public void delNode(String path)
throws JDOMException {
73 Object obj = XPath.selectSingleNode(
doc.getRootElement(), path);
74 if(obj instanceof Content) {
75 ((Content) obj).detach();
79 public List<String>
getChildrenValue(String element, String name)
throws JDOMException {
80 ArrayList<String> list =
new ArrayList<String>();
82 for(Element cel : (List<Element>) el.getChildren(element)) {
83 list.add(cel.getAttributeValue(name));
88 public List<String[]>
getChildrenValues(String element, String names[])
throws JDOMException {
89 ArrayList<String[]> list =
new ArrayList<String[]>();
91 for(Element cel : (List<Element>) el.getChildren(element)) {
92 ArrayList<String> l =
new ArrayList<String>();
93 for(
int i = 0; i < names.length; i++) {
94 l.add(cel.getAttributeValue(names[i]));
96 list.add(l.toArray(
new String[0]));
103 return el.getAttributeValue(name);
108 ArrayList<String> l =
new ArrayList<String>();
109 for(
int i = 0; i < names.length; i++) {
110 l.add(el.getAttributeValue(names[i]));
112 return l.toArray(
new String[0]);
115 public void setChildrenValue(String element, String name, List<String> values)
throws JDOMException {
118 for(String v : values) {
119 Element nel =
new Element(element);
120 nel.setAttribute(name, v);
125 public void setChildrenValues(String element, String names[], List<String[]> values)
throws JDOMException {
128 for(String[] vs : values) {
129 Element nel =
new Element(element);
130 for(
int i = 0; i < names.length; i++) {
131 nel.setAttribute(names[i], vs[i]);
137 public void setNodeValue(String name, String value)
throws JDOMException {
139 el.setAttribute(name, value);
142 public void setNodeValues(String names[], String[] values)
throws JDOMException {
144 for(
int i = 0; i < names.length; i++) {
145 el.setAttribute(names[i], values[i]);
149 protected abstract Element
createNode(Element root, String path,
int index);
List< String[]> getChildrenValues(String element, String names[])
List< String > getChildrenValue(String element, String name)
Element getNode(boolean create)
void setChildrenValue(String element, String name, List< String > values)
void setChildrenValues(String element, String names[], List< String[]> values)
void setNodeValues(String names[], String[] values)
String getNodeValue(String name)
abstract Element createNode(Element root, String path, int index)
void setDoc(Document doc)
void setNodeValue(String name, String value)
String[] getNodeValues(String names[])
void delNode(String path)