2859 |
27 Oct 14 |
nicklas |
1 |
package net.sf.basedb.reggie; |
2859 |
27 Oct 14 |
nicklas |
2 |
|
3116 |
06 Feb 15 |
nicklas |
3 |
import java.util.ArrayList; |
3114 |
05 Feb 15 |
nicklas |
4 |
import java.util.Arrays; |
3116 |
06 Feb 15 |
nicklas |
5 |
import java.util.Collections; |
3114 |
05 Feb 15 |
nicklas |
6 |
import java.util.List; |
3114 |
05 Feb 15 |
nicklas |
7 |
|
2859 |
27 Oct 14 |
nicklas |
8 |
import net.sf.basedb.core.ConfigurationException; |
2859 |
27 Oct 14 |
nicklas |
9 |
import net.sf.basedb.util.Values; |
2859 |
27 Oct 14 |
nicklas |
10 |
|
3009 |
04 Dec 14 |
nicklas |
11 |
import org.jdom2.Attribute; |
3009 |
04 Dec 14 |
nicklas |
12 |
import org.jdom2.Element; |
5594 |
11 Sep 19 |
nicklas |
13 |
import org.jdom2.filter.ElementFilter; |
3009 |
04 Dec 14 |
nicklas |
14 |
import org.jdom2.xpath.XPathFactory; |
2859 |
27 Oct 14 |
nicklas |
15 |
|
2859 |
27 Oct 14 |
nicklas |
16 |
/** |
2859 |
27 Oct 14 |
nicklas |
Utility class for wokring with Reggie XML configuration file. |
2859 |
27 Oct 14 |
nicklas |
18 |
|
2859 |
27 Oct 14 |
nicklas |
@author nicklas |
2859 |
27 Oct 14 |
nicklas |
@since 2.18 |
2859 |
27 Oct 14 |
nicklas |
21 |
*/ |
2859 |
27 Oct 14 |
nicklas |
22 |
public class XmlConfig |
2859 |
27 Oct 14 |
nicklas |
23 |
{ |
2859 |
27 Oct 14 |
nicklas |
24 |
|
2859 |
27 Oct 14 |
nicklas |
25 |
private final Element root; |
2859 |
27 Oct 14 |
nicklas |
26 |
private final String id; |
2859 |
27 Oct 14 |
nicklas |
27 |
|
2859 |
27 Oct 14 |
nicklas |
28 |
/** |
2859 |
27 Oct 14 |
nicklas |
Create a new configuration item using the given XML element as the |
2859 |
27 Oct 14 |
nicklas |
root element. |
2859 |
27 Oct 14 |
nicklas |
@param root The root XML element |
2859 |
27 Oct 14 |
nicklas |
@param id The id of the root element (used in error message) |
2859 |
27 Oct 14 |
nicklas |
33 |
*/ |
2859 |
27 Oct 14 |
nicklas |
34 |
public XmlConfig(Element root, String id) |
2859 |
27 Oct 14 |
nicklas |
35 |
{ |
2859 |
27 Oct 14 |
nicklas |
36 |
this.root = root; |
2859 |
27 Oct 14 |
nicklas |
37 |
this.id = id; |
2859 |
27 Oct 14 |
nicklas |
38 |
} |
2859 |
27 Oct 14 |
nicklas |
39 |
|
2859 |
27 Oct 14 |
nicklas |
40 |
/** |
2859 |
27 Oct 14 |
nicklas |
Get the XML root element. |
2859 |
27 Oct 14 |
nicklas |
42 |
*/ |
2859 |
27 Oct 14 |
nicklas |
43 |
public Element getRoot() |
2859 |
27 Oct 14 |
nicklas |
44 |
{ |
2859 |
27 Oct 14 |
nicklas |
45 |
return root; |
2859 |
27 Oct 14 |
nicklas |
46 |
} |
2859 |
27 Oct 14 |
nicklas |
47 |
|
2859 |
27 Oct 14 |
nicklas |
48 |
/** |
2859 |
27 Oct 14 |
nicklas |
Get the value of a required configuration option. If no |
2859 |
27 Oct 14 |
nicklas |
value is found an exception is thrown. |
2859 |
27 Oct 14 |
nicklas |
@param xpath An XPath expression identifying a (single) configuration element |
3114 |
05 Feb 15 |
nicklas |
@param parameterSet A named parameter set that should be used before the unnamed parameters |
3114 |
05 Feb 15 |
nicklas |
@since 3.1 |
3114 |
05 Feb 15 |
nicklas |
54 |
*/ |
3114 |
05 Feb 15 |
nicklas |
55 |
public String getRequiredConfig(String xpath, String parameterSet) |
3114 |
05 Feb 15 |
nicklas |
56 |
{ |
3114 |
05 Feb 15 |
nicklas |
57 |
String value = getConfig(xpath, parameterSet, null); |
3114 |
05 Feb 15 |
nicklas |
58 |
if (value == null) |
3114 |
05 Feb 15 |
nicklas |
59 |
{ |
3114 |
05 Feb 15 |
nicklas |
60 |
throw new ConfigurationException("Can't find '" + xpath + "' setting for " + id + "[parameter-set: " + parameterSet + "]"); |
3114 |
05 Feb 15 |
nicklas |
61 |
} |
3114 |
05 Feb 15 |
nicklas |
62 |
return value; |
3114 |
05 Feb 15 |
nicklas |
63 |
} |
3114 |
05 Feb 15 |
nicklas |
64 |
|
3114 |
05 Feb 15 |
nicklas |
65 |
/** |
3699 |
18 Jan 16 |
nicklas |
Shortcut for {@link #getConfig(String, String, String)} with null |
3699 |
18 Jan 16 |
nicklas |
values for parameterSet and defaultValue. |
3699 |
18 Jan 16 |
nicklas |
@since 4.1 |
3699 |
18 Jan 16 |
nicklas |
69 |
*/ |
3699 |
18 Jan 16 |
nicklas |
70 |
public String getConfig(String xpath) |
3699 |
18 Jan 16 |
nicklas |
71 |
{ |
3699 |
18 Jan 16 |
nicklas |
72 |
return getConfig(xpath, null, null); |
3699 |
18 Jan 16 |
nicklas |
73 |
} |
3699 |
18 Jan 16 |
nicklas |
74 |
|
3699 |
18 Jan 16 |
nicklas |
75 |
/** |
4044 |
02 Aug 16 |
nicklas |
Searches for an ordered list of configuration values all with |
4044 |
02 Aug 16 |
nicklas |
the given root prefix. For example if prefix is 'run-archive' |
4044 |
02 Aug 16 |
nicklas |
this will look for 'run-archive-N', 'run-archive-(N+1)', and so on |
4044 |
02 Aug 16 |
nicklas |
until an empty element or no element is found (N=starting index). |
4044 |
02 Aug 16 |
nicklas |
@since 4.6 |
4044 |
02 Aug 16 |
nicklas |
81 |
*/ |
4044 |
02 Aug 16 |
nicklas |
82 |
public List<String> getConfigList(String prefix, int startAt) |
4044 |
02 Aug 16 |
nicklas |
83 |
{ |
4044 |
02 Aug 16 |
nicklas |
84 |
int index = startAt; |
4044 |
02 Aug 16 |
nicklas |
85 |
List<String> list = new ArrayList<>(); |
4044 |
02 Aug 16 |
nicklas |
86 |
do |
4044 |
02 Aug 16 |
nicklas |
87 |
{ |
4044 |
02 Aug 16 |
nicklas |
88 |
String value = getConfig(prefix + "-" + index); |
4044 |
02 Aug 16 |
nicklas |
89 |
if (value == null) break; |
4044 |
02 Aug 16 |
nicklas |
90 |
list.add(value); |
4044 |
02 Aug 16 |
nicklas |
91 |
index++; |
4044 |
02 Aug 16 |
nicklas |
92 |
} while (true); |
4044 |
02 Aug 16 |
nicklas |
93 |
|
4044 |
02 Aug 16 |
nicklas |
94 |
return list; |
4044 |
02 Aug 16 |
nicklas |
95 |
} |
4044 |
02 Aug 16 |
nicklas |
96 |
|
4044 |
02 Aug 16 |
nicklas |
97 |
|
4044 |
02 Aug 16 |
nicklas |
98 |
/** |
3114 |
05 Feb 15 |
nicklas |
Get the value of a configuration option. If no value is found the default value is used. |
3114 |
05 Feb 15 |
nicklas |
100 |
|
3114 |
05 Feb 15 |
nicklas |
@param xpath An XPath expression identifying a (single) configuration element or attribute |
3114 |
05 Feb 15 |
nicklas |
@param parameterSet A named parameter set that should be used before the unnamed parameters |
3114 |
05 Feb 15 |
nicklas |
@since 3.1 |
3114 |
05 Feb 15 |
nicklas |
104 |
*/ |
3114 |
05 Feb 15 |
nicklas |
105 |
public String getConfig(String xpath, String parameterSet, String defaultValue) |
3114 |
05 Feb 15 |
nicklas |
106 |
{ |
2859 |
27 Oct 14 |
nicklas |
107 |
String value = null; |
3605 |
16 Nov 15 |
nicklas |
108 |
if (xpath == null) return defaultValue; |
3114 |
05 Feb 15 |
nicklas |
109 |
|
3114 |
05 Feb 15 |
nicklas |
110 |
List<Object> list = XPathFactory.instance().compile(xpath).evaluate(root); |
3114 |
05 Feb 15 |
nicklas |
111 |
for (Object n : list) |
2859 |
27 Oct 14 |
nicklas |
112 |
{ |
3114 |
05 Feb 15 |
nicklas |
113 |
Element e = null; |
3114 |
05 Feb 15 |
nicklas |
114 |
String tmp = null; |
3114 |
05 Feb 15 |
nicklas |
115 |
if (n instanceof Element) |
3114 |
05 Feb 15 |
nicklas |
116 |
{ |
3114 |
05 Feb 15 |
nicklas |
117 |
e = (Element)n; |
3114 |
05 Feb 15 |
nicklas |
118 |
tmp = Values.getStringOrNull(e.getTextTrim()); |
3114 |
05 Feb 15 |
nicklas |
119 |
} |
3114 |
05 Feb 15 |
nicklas |
120 |
else if (n instanceof Attribute) |
3114 |
05 Feb 15 |
nicklas |
121 |
{ |
3114 |
05 Feb 15 |
nicklas |
122 |
Attribute a = (Attribute)n; |
3114 |
05 Feb 15 |
nicklas |
123 |
e = a.getParent(); |
3114 |
05 Feb 15 |
nicklas |
124 |
tmp = Values.getStringOrNull(a.getValue()); |
3114 |
05 Feb 15 |
nicklas |
125 |
} |
3114 |
05 Feb 15 |
nicklas |
126 |
if (e != null) |
3114 |
05 Feb 15 |
nicklas |
127 |
{ |
3114 |
05 Feb 15 |
nicklas |
128 |
String ps = e.getAttributeValue("parameter-set"); |
3114 |
05 Feb 15 |
nicklas |
129 |
if (ps == null) |
3114 |
05 Feb 15 |
nicklas |
130 |
{ |
3114 |
05 Feb 15 |
nicklas |
// Default parameter set; use this unless we find the specific parameter set |
3114 |
05 Feb 15 |
nicklas |
132 |
value = tmp; |
3114 |
05 Feb 15 |
nicklas |
133 |
if (parameterSet == null) break; |
3114 |
05 Feb 15 |
nicklas |
134 |
} |
3114 |
05 Feb 15 |
nicklas |
135 |
else if (parameterSet != null && Arrays.asList(ps.split(",")).indexOf(parameterSet) != -1) |
3114 |
05 Feb 15 |
nicklas |
136 |
{ |
6973 |
12 Jan 23 |
nicklas |
137 |
boolean extendsDefault = Values.getBoolean(e.getAttributeValue("extends-default")); |
3114 |
05 Feb 15 |
nicklas |
// We have found the parameter set we are lookup for, break and use this value |
6973 |
12 Jan 23 |
nicklas |
139 |
if (extendsDefault && value != null) |
6973 |
12 Jan 23 |
nicklas |
140 |
{ |
6973 |
12 Jan 23 |
nicklas |
141 |
value += "\n"+tmp; |
6973 |
12 Jan 23 |
nicklas |
142 |
} |
6973 |
12 Jan 23 |
nicklas |
143 |
else |
6973 |
12 Jan 23 |
nicklas |
144 |
{ |
6973 |
12 Jan 23 |
nicklas |
145 |
value = tmp; |
6973 |
12 Jan 23 |
nicklas |
146 |
} |
3114 |
05 Feb 15 |
nicklas |
147 |
break; |
3114 |
05 Feb 15 |
nicklas |
148 |
} |
3114 |
05 Feb 15 |
nicklas |
149 |
} |
2859 |
27 Oct 14 |
nicklas |
150 |
} |
2859 |
27 Oct 14 |
nicklas |
151 |
return value == null ? defaultValue : value; |
2859 |
27 Oct 14 |
nicklas |
152 |
} |
3116 |
06 Feb 15 |
nicklas |
153 |
|
3116 |
06 Feb 15 |
nicklas |
154 |
/** |
3116 |
06 Feb 15 |
nicklas |
Get all parameters in the given parameter set. |
3116 |
06 Feb 15 |
nicklas |
@since 3.1 |
3116 |
06 Feb 15 |
nicklas |
157 |
*/ |
3116 |
06 Feb 15 |
nicklas |
158 |
public List<Element> getAllInParameterSet(String parameterSet) |
3116 |
06 Feb 15 |
nicklas |
159 |
{ |
3116 |
06 Feb 15 |
nicklas |
160 |
if (parameterSet == null) return Collections.emptyList(); |
3116 |
06 Feb 15 |
nicklas |
161 |
|
3116 |
06 Feb 15 |
nicklas |
162 |
List<Object> list = XPathFactory.instance().compile("//*[@parameter-set]").evaluate(root); |
3116 |
06 Feb 15 |
nicklas |
163 |
List<Element> parameters = new ArrayList<Element>(); |
3116 |
06 Feb 15 |
nicklas |
164 |
|
3116 |
06 Feb 15 |
nicklas |
165 |
for (Object o : list) |
3116 |
06 Feb 15 |
nicklas |
166 |
{ |
3116 |
06 Feb 15 |
nicklas |
167 |
Element e = (Element)o; |
3116 |
06 Feb 15 |
nicklas |
168 |
String ps = e.getAttributeValue("parameter-set"); |
3116 |
06 Feb 15 |
nicklas |
169 |
if (Arrays.asList(ps.split(",")).indexOf(parameterSet) != -1) |
3116 |
06 Feb 15 |
nicklas |
170 |
{ |
3116 |
06 Feb 15 |
nicklas |
171 |
parameters.add(e); |
3116 |
06 Feb 15 |
nicklas |
172 |
} |
3116 |
06 Feb 15 |
nicklas |
173 |
} |
3116 |
06 Feb 15 |
nicklas |
174 |
return parameters; |
3116 |
06 Feb 15 |
nicklas |
175 |
} |
3116 |
06 Feb 15 |
nicklas |
176 |
|
5594 |
11 Sep 19 |
nicklas |
177 |
/** |
5594 |
11 Sep 19 |
nicklas |
Get the first element that matches the given XPATH expression. Returns |
5594 |
11 Sep 19 |
nicklas |
null if no element matches. |
5594 |
11 Sep 19 |
nicklas |
@since 4.23 |
5594 |
11 Sep 19 |
nicklas |
181 |
*/ |
5594 |
11 Sep 19 |
nicklas |
182 |
public Element getElement(String xpath) |
5594 |
11 Sep 19 |
nicklas |
183 |
{ |
5594 |
11 Sep 19 |
nicklas |
184 |
List<Element> list = XPathFactory.instance().compile(xpath, new ElementFilter()).evaluate(root); |
5594 |
11 Sep 19 |
nicklas |
185 |
return list.size() == 0 ? null : list.get(0); |
5594 |
11 Sep 19 |
nicklas |
186 |
} |
5594 |
11 Sep 19 |
nicklas |
187 |
|
5594 |
11 Sep 19 |
nicklas |
188 |
/** |
5594 |
11 Sep 19 |
nicklas |
Get all elements that matches the given XPATH expression. Returns |
5594 |
11 Sep 19 |
nicklas |
an empty list if no element matches. |
5594 |
11 Sep 19 |
nicklas |
@since 4.23 |
5594 |
11 Sep 19 |
nicklas |
192 |
*/ |
5594 |
11 Sep 19 |
nicklas |
193 |
public List<Element> getElements(String xpath) |
5594 |
11 Sep 19 |
nicklas |
194 |
{ |
5594 |
11 Sep 19 |
nicklas |
195 |
return XPathFactory.instance().compile(xpath, new ElementFilter()).evaluate(root); |
5594 |
11 Sep 19 |
nicklas |
196 |
} |
2859 |
27 Oct 14 |
nicklas |
197 |
} |