summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java
blob: 0143e4650d23cf3663450496c537a4989665834e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package org.openslx.libvirt.xml;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Editability of XML nodes based on XPath expressions.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public interface LibvirtXmlEditable
{
	/**
	 * Returns XML node selected by a XPath expression
	 * 
	 * @param expression XPath expression to select XML node.
	 * @return selected XML node.
	 */
	public Node getXmlNode( String expression );

	/**
	 * Returns XML nodes selected by a XPath expression
	 * 
	 * @param expression XPath expression to select XML nodes.
	 * @return selected XML nodes.
	 */
	public NodeList getXmlNodes( String expression );

	/**
	 * Return current XML root element.
	 * 
	 * @return current XML root element.
	 */
	public default Node getXmlElement()
	{
		return this.getXmlElement( null );
	}

	/**
	 * Returns XML element from selection by a XPath expression.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @return selected XML element.
	 */
	public Node getXmlElement( String expression );

	/**
	 * Sets an XML element selected by a XPath expression.
	 * 
	 * If the XML element selected by the given XPath expression does not exists, the XML
	 * element will be created.
	 * 
	 * @param expression XPath expression to select XML element.
	 */
	public default void setXmlElement( String expression )
	{
		this.setXmlElement( expression, null );
	}

	/**
	 * Sets a XML element selected by a XPath expression and appends child XML node.
	 * 
	 * If the XML element selected by the given XPath expression does not exists, the XML
	 * element will be created and the given XML child node is appended.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @param child XML node that will be appended to the selected XML element.
	 */
	public void setXmlElement( String expression, Node child );

	/**
	 * Returns the text value of a XML element selected by a XPath expression.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @return Text value of the selected XML element.
	 */
	public String getXmlElementValue( String expression );

	/**
	 * Sets the text value of a XML element selected by a XPath expression.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @param value text value to set selected XML element's text.
	 */
	public void setXmlElementValue( String expression, String value );

	/**
	 * Removes a XML element and all its childs selected by a XPath expression.
	 * 
	 * @param expression XPath expression to select XML element.
	 */
	public void removeXmlElement( String expression );

	/**
	 * Removes all child elements of the current XML root element.
	 */
	public default void removeXmlElementChilds()
	{
		this.removeXmlElementChilds( null );
	}

	/**
	 * Removes all child elements of a XML element selected by a XPath expression.
	 * 
	 * @param expression XPath expression to select XML element.
	 */
	public void removeXmlElementChilds( String expression );

	/**
	 * Returns the text value of a XML attribute from the current XML root element.
	 * 
	 * @param attributeName name to select XML attribute of the current XML root element.
	 * @return attribute text of the XML attribute from the current XML root element as
	 *         {@link String}.
	 */
	public default String getXmlElementAttributeValue( String attributeName )
	{
		return this.getXmlElementAttributeValue( null, attributeName );
	}

	/**
	 * Returns the binary choice of a XML attribute from the current XML root element.
	 * 
	 * If the text value of the XML attribute equals to <i>yes</i> or <i>on</i>, the returned
	 * {@link boolean} value is set to <i>true</i>. Otherwise, if the text value of the XML attribute
	 * equals to <i>no</i> or <i>off</i>, the returned {@link boolean} value is set to <i>false</i>.
	 * 
	 * @param attributeName name to select XML attribute of the current XML root element.
	 * @return attribute value of the XML attribute from the current XML root element as
	 *         {@link boolean}.
	 */
	public default boolean getXmlElementAttributeValueAsBool( String attributeName )
	{
		final String attributeValue = this.getXmlElementAttributeValue( attributeName );
		return "yes".equals( attributeValue ) || "on".equals( attributeValue ) || "true".equals( attributeValue );
	}

	/**
	 * Returns the binary choice of a XML attribute from a XML element selected by a XPath
	 * expression.
	 * 
	 * If the text value of the XML attribute equals to <i>yes</i> or <i>on</i>, the returned
	 * {@link boolean} value is set to <i>true</i>. Otherwise, if the text value of the XML attribute
	 * equals to <i>no</i> or <i>off</i>, the returned {@link boolean} value is set to <i>false</i>.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @param attributeName name to select XML attribute of the current XML root element.
	 * @return attribute value of the XML attribute from the current XML root element as
	 *         {@link boolean}.
	 */
	public default boolean getXmlElementAttributeValueAsBool( String expression, String attributeName )
	{
		final String attributeValue = this.getXmlElementAttributeValue( expression, attributeName );
		return "yes".equals( attributeValue ) || "on".equals( attributeValue );
	}

	/**
	 * Returns the text value of a XML attribute from a XML element selected by a XPath expression.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @param attributeName name to select XML attribute of the selected XML element.
	 * @return attribute text of the XML attribute from the selected XML element.
	 */
	public String getXmlElementAttributeValue( String expression, String attributeName );

	/**
	 * Sets the text value of a XML attribute from the current XML root element.
	 * 
	 * @param attributeName name to select XML attribute of the current XML root element.
	 * @param value XML attribute value for the selected XML attribute from the current XML root
	 *           element.
	 */
	public default void setXmlElementAttributeValue( String attributeName, String value )
	{
		this.setXmlElementAttributeValue( null, attributeName, value );
	}

	/**
	 * Sets the binary choice value of a XML attribute from the current XML root element.
	 * 
	 * If the binary choice value for the XML attribute equals to <i>true</i>, the text value of the
	 * selected XML attribute is set to <i>yes</i>. Otherwise, if the binary choice value for the
	 * selected XML attribute equals to <i>false</i>, the text value of the selected XML attribute is
	 * set to <i>no</i>.
	 * 
	 * @param attributeName name to select XML attribute of the selected XML element.
	 * @param value binary choice value for the selected XML attribute from the selected XML element.
	 */
	public default void setXmlElementAttributeValueYesNo( String attributeName, boolean value )
	{
		final String valueYesNo = value ? "yes" : "no";
		this.setXmlElementAttributeValue( attributeName, valueYesNo );
	}

	/**
	 * Sets the binary choice value of a XML attribute from a XML element selected by a XPath
	 * expression.
	 * 
	 * If the binary choice value for the XML attribute equals to <i>true</i>, the text value of the
	 * selected XML attribute is set to <i>yes</i>. Otherwise, if the binary choice value for the
	 * selected XML attribute equals to <i>false</i>, the text value of the selected XML attribute is
	 * set to <i>no</i>.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @param attributeName name to select XML attribute of the selected XML element.
	 * @param value binary choice value for the selected XML attribute from the selected XML element.
	 */
	public default void setXmlElementAttributeValueYesNo( String expression, String attributeName, boolean value )
	{
		final String valueYesNo = value ? "yes" : "no";
		this.setXmlElementAttributeValue( expression, attributeName, valueYesNo );
	}

	/**
	 * Sets the binary choice value of a XML attribute from the current XML root element.
	 * 
	 * If the binary choice value for the XML attribute equals to <i>true</i>, the text value of the
	 * selected XML attribute is set to <i>on</i>. Otherwise, if the binary choice value for the
	 * selected XML attribute equals to <i>false</i>, the text value of the selected XML attribute is
	 * set to <i>off</i>.
	 * 
	 * @param attributeName name to select XML attribute of the selected XML element.
	 * @param value binary choice value for the selected XML attribute from the selected XML element.
	 */
	public default void setXmlElementAttributeValueOnOff( String attributeName, boolean value )
	{
		final String valueOnOff = value ? "on" : "off";
		this.setXmlElementAttributeValue( attributeName, valueOnOff );
	}

	/**
	 * Sets the binary choice value of a XML attribute from a XML element selected by a XPath
	 * expression.
	 * 
	 * If the binary choice value for the XML attribute equals to <i>true</i>, the text value of the
	 * selected XML attribute is set to <i>on</i>. Otherwise, if the binary choice value for the
	 * selected XML attribute equals to <i>false</i>, the text value of the selected XML attribute is
	 * set to <i>off</i>.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @param attributeName name to select XML attribute of the selected XML element.
	 * @param value binary choice value for the selected XML attribute from the selected XML element.
	 */
	public default void setXmlElementAttributeValueOnOff( String expression, String attributeName, boolean value )
	{
		final String valueOnOff = value ? "on" : "off";
		this.setXmlElementAttributeValue( expression, attributeName, valueOnOff );
	}

	/**
	 * Sets the text value of a XML attribute from a XML element selected by a XPath expression.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @param attributeName name to select XML attribute of the selected XML element.
	 * @param value XML attribute value for the selected XML attribute from the selected XML element.
	 */
	public void setXmlElementAttributeValue( String expression, String attributeName, String value );

	/**
	 * Removes an XML attribute from the current XML root element.
	 * 
	 * @param attributeName name of the attribute which should be deleted.
	 */
	public default void removeXmlElementAttribute( String attributeName )
	{
		this.removeXmlElementAttribute( null, attributeName );
	}

	/**
	 * Removes an XML attribute from a XML element selected by a XPath expression.
	 * 
	 * @param expression XPath expression to select XML element.
	 * @param attributeName name of the attribute which should be deleted.
	 */
	public void removeXmlElementAttribute( String expression, String attributeName );

	/**
	 * Removes all XML attributes from a XML element selected by a XPath expression.
	 * 
	 * @param expression XPath expression to select XML element.
	 */
	public void removeXmlElementAttributes( String expression );

}