diff options
Diffstat (limited to 'src/main/java/org/openslx/libvirt/xml')
-rw-r--r-- | src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java | 66 | ||||
-rw-r--r-- | src/main/java/org/openslx/libvirt/xml/LibvirtXmlNode.java | 17 |
2 files changed, 61 insertions, 22 deletions
diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java index 40d7b86..5834e58 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java @@ -93,6 +93,14 @@ public interface LibvirtXmlEditable 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. @@ -114,9 +122,9 @@ public interface LibvirtXmlEditable /** * 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>, 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>, the returned {@link boolean} value is set to <i>false</i>. + * 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 @@ -124,16 +132,17 @@ public interface LibvirtXmlEditable */ public default boolean getXmlElementAttributeValueAsBool( String attributeName ) { - return "yes".equals( this.getXmlElementAttributeValue( attributeName ) ); + final String attributeValue = this.getXmlElementAttributeValue( attributeName ); + return "yes".equals( attributeValue ) || "on".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>, 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>, the returned {@link boolean} value is set to <i>false</i>. + * 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. @@ -142,7 +151,8 @@ public interface LibvirtXmlEditable */ public default boolean getXmlElementAttributeValueAsBool( String expression, String attributeName ) { - return "yes".equals( this.getXmlElementAttributeValue( expression, attributeName ) ); + final String attributeValue = this.getXmlElementAttributeValue( expression, attributeName ); + return "yes".equals( attributeValue ) || "on".equals( attributeValue ); } /** @@ -177,7 +187,7 @@ public interface LibvirtXmlEditable * @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 setXmlElementAttributeValue( String attributeName, boolean value ) + public default void setXmlElementAttributeValueYesNo( String attributeName, boolean value ) { final String valueYesNo = value ? "yes" : "no"; this.setXmlElementAttributeValue( attributeName, valueYesNo ); @@ -196,13 +206,49 @@ public interface LibvirtXmlEditable * @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 setXmlElementAttributeValue( String expression, String attributeName, boolean value ) + 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. diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlNode.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlNode.java index 6d00271..2110f7a 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlNode.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlNode.java @@ -184,9 +184,7 @@ public class LibvirtXmlNode implements LibvirtXmlCreatable, LibvirtXmlEditable { String completeExpression = null; - if ( expression == null ) { - completeExpression = XPATH_EXPRESSION_CURRENT_NODE; - } else if ( expression.isEmpty() ) { + if ( expression == null || expression.isEmpty() ) { completeExpression = XPATH_EXPRESSION_CURRENT_NODE; } else { completeExpression = XPATH_EXPRESSION_CURRENT_NODE + XPATH_EXPRESSION_SEPARATOR + expression; @@ -271,8 +269,9 @@ public class LibvirtXmlNode implements LibvirtXmlCreatable, LibvirtXmlEditable Node node = this.getXmlElement( expression ); if ( node != null ) { - for ( int i = 0; i < node.getChildNodes().getLength(); i++ ) { - Node child = node.getChildNodes().item( 0 ); + final NodeList childs = node.getChildNodes(); + while ( childs.getLength() > 0 ) { + Node child = childs.item( 0 ); node.removeChild( child ); } } @@ -281,13 +280,7 @@ public class LibvirtXmlNode implements LibvirtXmlCreatable, LibvirtXmlEditable @Override public String getXmlElementAttributeValue( String expression, String attributeName ) { - Node node = null; - - if ( expression != null && !expression.isEmpty() ) { - node = this.getXmlElement( expression ); - } else { - node = this.xmlBaseNode; - } + Node node = this.getXmlElement( expression ); if ( node == null ) { return null; |