BrightSide Workbench Full Report + Source Code
Path.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.configuration;
19 
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;
27 
32 public abstract class Path {
33 
34  protected String nodes[];
35  protected Document doc;
36 
37  public Path(String path) {
38  if(path.startsWith("/")) {
39  nodes = path.substring(1).split("/");
40  } else {
41  nodes = path.split("/");
42  }
43  }
44 
45  public Path(String[] nodes) {
46  this.nodes = nodes;
47  }
48 
49  public Document getDoc() {
50  return doc;
51  }
52 
53  public void setDoc(Document doc) {
54  this.doc = doc;
55  }
56 
57  public Element getNode(boolean create) throws JDOMException {
58  Element curr = doc.getRootElement();
59  int count = 0;
60  for(String node : nodes) {
61  Element tmp = (Element) XPath.selectSingleNode(curr, node);
62  if(tmp == null && create) {
63  tmp = createNode(curr, node, count);
64  }
65  if(tmp == null) return null;
66  curr = tmp;
67  count++;
68  }
69  return curr;
70  }
71 
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();
76  }
77  }
78 
79  public List<String> getChildrenValue(String element, String name) throws JDOMException {
80  ArrayList<String> list = new ArrayList<String>();
81  Element el = getNode(true);
82  for(Element cel : (List<Element>) el.getChildren(element)) {
83  list.add(cel.getAttributeValue(name));
84  }
85  return list;
86  }
87 
88  public List<String[]> getChildrenValues(String element, String names[]) throws JDOMException {
89  ArrayList<String[]> list = new ArrayList<String[]>();
90  Element el = getNode(true);
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]));
95  }
96  list.add(l.toArray(new String[0]));
97  }
98  return list;
99  }
100 
101  public String getNodeValue(String name) throws JDOMException {
102  Element el = getNode(true);
103  return el.getAttributeValue(name);
104  }
105 
106  public String[] getNodeValues(String names[]) throws JDOMException {
107  Element el = getNode(true);
108  ArrayList<String> l = new ArrayList<String>();
109  for(int i = 0; i < names.length; i++) {
110  l.add(el.getAttributeValue(names[i]));
111  }
112  return l.toArray(new String[0]);
113  }
114 
115  public void setChildrenValue(String element, String name, List<String> values) throws JDOMException {
116  Element el = getNode(true);
117  el.removeContent();
118  for(String v : values) {
119  Element nel = new Element(element);
120  nel.setAttribute(name, v);
121  el.addContent(nel);
122  }
123  }
124 
125  public void setChildrenValues(String element, String names[], List<String[]> values) throws JDOMException {
126  Element el = getNode(true);
127  el.removeContent();
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]);
132  }
133  el.addContent(nel);
134  }
135  }
136 
137  public void setNodeValue(String name, String value) throws JDOMException {
138  Element el = getNode(true);
139  el.setAttribute(name, value);
140  }
141 
142  public void setNodeValues(String names[], String[] values) throws JDOMException {
143  Element el = getNode(true);
144  for(int i = 0; i < names.length; i++) {
145  el.setAttribute(names[i], values[i]);
146  }
147  }
148 
149  protected abstract Element createNode(Element root, String path, int index);
150 
151 }
List< String[]> getChildrenValues(String element, String names[])
Definition: Path.java:88
List< String > getChildrenValue(String element, String name)
Definition: Path.java:79
Element getNode(boolean create)
Definition: Path.java:57
void setChildrenValue(String element, String name, List< String > values)
Definition: Path.java:115
void setChildrenValues(String element, String names[], List< String[]> values)
Definition: Path.java:125
Path(String[] nodes)
Definition: Path.java:45
void setNodeValues(String names[], String[] values)
Definition: Path.java:142
String getNodeValue(String name)
Definition: Path.java:101
abstract Element createNode(Element root, String path, int index)
void setDoc(Document doc)
Definition: Path.java:53
void setNodeValue(String name, String value)
Definition: Path.java:137
String[] getNodeValues(String names[])
Definition: Path.java:106
void delNode(String path)
Definition: Path.java:72