diff options
Diffstat (limited to 'src/main/java/org/openslx/libvirt/xml')
6 files changed, 117 insertions, 65 deletions
diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java index 9382ced..f24ba4f 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.io.StringReader; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; @@ -252,16 +253,7 @@ public abstract class LibvirtXmlDocument implements LibvirtXmlSerializable, Libv @Override public void fromXml( String xml ) throws LibvirtXmlSerializationException { - try { - this.xmlDocument = this.domBuilder.parse( xml ); - this.xmlDocument.getDocumentElement().normalize(); - } catch ( SAXException e ) { - e.printStackTrace(); - } catch ( IOException e ) { - e.printStackTrace(); - } - - this.rootXmlNode = new LibvirtXmlNode( this.xmlDocument, this.xmlDocument.getDocumentElement() ); + this.fromXml( new InputSource( new StringReader( xml ) ) ); } @Override diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java index 1ddddce..40d7b86 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlEditable.java @@ -4,7 +4,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** - * Editability of XML nodes based on {@link XPath} expressions. + * Editability of XML nodes based on XPath expressions. * * @author Manuel Bentele * @version 1.0 @@ -12,17 +12,17 @@ import org.w3c.dom.NodeList; public interface LibvirtXmlEditable { /** - * Returns XML node selected by a {@link XPath} expression + * Returns XML node selected by a XPath expression * - * @param expression {@link XPath} expression to select XML node. + * @param expression XPath expression to select XML node. * @return selected XML node. */ public Node getXmlNode( String expression ); /** - * Returns XML nodes selected by a {@link XPath} expression + * Returns XML nodes selected by a XPath expression * - * @param expression {@link XPath} expression to select XML nodes. + * @param expression XPath expression to select XML nodes. * @return selected XML nodes. */ public NodeList getXmlNodes( String expression ); @@ -38,20 +38,20 @@ public interface LibvirtXmlEditable } /** - * Returns XML element from selection by a {@link XPath} expression. + * Returns XML element from selection by a XPath expression. * - * @param expression {@link XPath} expression to select XML element. + * @param expression XPath expression to select XML element. * @return selected XML element. */ public Node getXmlElement( String expression ); /** - * Sets an XML element selected by a {@link XPath} expression. + * Sets an XML element selected by a XPath expression. * - * If the XML element selected by the given {@link XPath} expression does not exists, the XML + * If the XML element selected by the given XPath expression does not exists, the XML * element will be created. * - * @param expression {@link XPath} expression to select XML element. + * @param expression XPath expression to select XML element. */ public default void setXmlElement( String expression ) { @@ -59,43 +59,43 @@ public interface LibvirtXmlEditable } /** - * Sets a XML element selected by a {@link XPath} expression and appends child XML node. + * Sets a XML element selected by a XPath expression and appends child XML node. * - * If the XML element selected by the given {@link XPath} expression does not exists, the XML + * 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 {@link XPath} expression to select XML element. + * @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 {@link XPath} expression. + * Returns the text value of a XML element selected by a XPath expression. * - * @param expression {@link XPath} expression to select XML element. + * @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 {@link XPath} expression. + * Sets the text value of a XML element selected by a XPath expression. * - * @param expression {@link XPath} expression to select XML element. + * @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 {@link XPath} expression. + * Removes a XML element and all its childs selected by a XPath expression. * - * @param expression {@link XPath} expression to select XML element. + * @param expression XPath expression to select XML element. */ public void removeXmlElement( String expression ); /** - * Removes all child elements of a XML element selected by a {@link XPath} expression. + * Removes all child elements of a XML element selected by a XPath expression. * - * @param expression {@link XPath} expression to select XML element. + * @param expression XPath expression to select XML element. */ public void removeXmlElementChilds( String expression ); @@ -128,14 +128,14 @@ public interface LibvirtXmlEditable } /** - * Returns the binary choice of a XML attribute from a XML element selected by a - * {@link XPath}expression. + * 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>. * - * @param expression {@link XPath} expression to select XML element. + * @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}. @@ -146,10 +146,9 @@ public interface LibvirtXmlEditable } /** - * Returns the text value of a XML attribute from a XML element selected by a - * {@link XPath}expression. + * Returns the text value of a XML attribute from a XML element selected by a XPath expression. * - * @param expression {@link XPath} expression to select XML element. + * @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. */ @@ -185,15 +184,15 @@ public interface LibvirtXmlEditable } /** - * Sets the binary choice value of a XML attribute from a XML element selected by a - * {@link XPath} expression. + * 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 {@link XPath} expression to select XML element. + * @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. */ @@ -204,10 +203,9 @@ public interface LibvirtXmlEditable } /** - * Sets the text value of a XML attribute from a XML element selected by a - * {@link XPath} expression. + * Sets the text value of a XML attribute from a XML element selected by a XPath expression. * - * @param expression {@link XPath} expression to select XML element. + * @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. */ @@ -224,17 +222,17 @@ public interface LibvirtXmlEditable } /** - * Removes an XML attribute from a XML element selected by a {@link XPath} expression. + * Removes an XML attribute from a XML element selected by a XPath expression. * - * @param expression {@link XPath} expression to select XML element. + * @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 {@link XPath} expression. + * Removes all XML attributes from a XML element selected by a XPath expression. * - * @param expression {@link XPath} expression to select XML element. + * @param expression XPath expression to select XML element. */ public void removeXmlElementAttributes( String expression ); diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlNode.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlNode.java index 93e28de..6d00271 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlNode.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlNode.java @@ -12,7 +12,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** - * A representation of a XML node as part of a {@link LibvirtXMLDocument}. + * A representation of a XML node as part of a {@link LibvirtXmlDocument}. * * @author Manuel Bentele * @version 1.0 @@ -20,17 +20,17 @@ import org.w3c.dom.NodeList; public class LibvirtXmlNode implements LibvirtXmlCreatable, LibvirtXmlEditable { /** - * Separation character for internal {@link XPath} expressions. + * Separation character for internal XPath expressions. */ private static final String XPATH_EXPRESSION_SEPARATOR = "/"; /** - * Current XML node selection character for internal {@link XPath} expressions. + * Current XML node selection character for internal XPath expressions. */ private static final String XPATH_EXPRESSION_CURRENT_NODE = "."; /** - * Factory to create {@link XPath} objects. + * Factory to create XPath objects. */ private XPathFactory xPathFactory = null; @@ -40,13 +40,12 @@ public class LibvirtXmlNode implements LibvirtXmlCreatable, LibvirtXmlEditable private Document xmlDocument = null; /** - * Current XML base node as XML root anchor for relative internal {@link XPath} expressions. + * Current XML base node as XML root anchor for relative internal XPath expressions. */ private Node xmlBaseNode = null; /** - * Create and initialize {@link XPath} context to define and compile custom {@link XPath} - * expressions. + * Create and initialize XPath context to define and compile custom XPath expressions. */ private void createXPathContext() { @@ -139,8 +138,7 @@ public class LibvirtXmlNode implements LibvirtXmlCreatable, LibvirtXmlEditable /** * Returns current XML base node. * - * @return current XML base node as XML root anchor of relative internal {@link XPath} - * expressions. + * @return current XML base node as XML root anchor of relative internal XPath expressions. */ public Node getXmlBaseNode() { @@ -150,8 +148,8 @@ public class LibvirtXmlNode implements LibvirtXmlCreatable, LibvirtXmlEditable /** * Sets existing XML base node for Libvirt XML node. * - * @param xmlBaseNode existing XML base node as XML root anchor for relative internal - * {@link XPath} expressions. + * @param xmlBaseNode existing XML base node as XML root anchor for relative internal XPath + * expressions. */ public void setXmlBaseNode( Node xmlBaseNode ) { @@ -317,7 +315,7 @@ public class LibvirtXmlNode implements LibvirtXmlCreatable, LibvirtXmlEditable attribute.setNodeValue( value ); } } - + @Override public void removeXmlElementAttribute( String expression, String attributeName ) { diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlResources.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlResources.java index 38c818b..a6b3e39 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlResources.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlResources.java @@ -17,6 +17,11 @@ public final class LibvirtXmlResources private static final String LIBVIRT_PREFIX_PATH = File.separator + "libvirt"; /** + * File path prefix of the absolute path to the libosinfo resource folder in a *.jar file. + */ + private static final String LIBOSINFO_PREFIX_PATH = File.separator + "libvirt" + File.separator + "libosinfo"; + + /** * File path prefix of the absolute path to the libvirt XSL resource folder in a *.jar file. */ private static final String LIBVIRT_PREFIX_PATH_XSL = LIBVIRT_PREFIX_PATH + File.separator + "xsl"; @@ -27,6 +32,29 @@ public final class LibvirtXmlResources private static final String LIBVIRT_PREFIX_PATH_RNG = LIBVIRT_PREFIX_PATH + File.separator + "rng"; /** + * File path prefix of the absolute path to the libosinfo RNG resource folder in a *.jar file. + */ + private static final String LIBOSINFO_PREFIX_PATH_RNG = LIBOSINFO_PREFIX_PATH + File.separator + "rng"; + + /** + * File path prefix of the absolute path to the libosinfo XML resource folder in a *.jar file. + */ + private static final String LIBOSINFO_PREFIX_PATH_XML = LIBOSINFO_PREFIX_PATH + File.separator + "xml"; + + /** + * Returns a Libvirt resource as stream. + * + * @param prefix file path of the Libvirt resource in the resources *.jar folder. + * @param fileName file name of the Libvirt resource in the resources *.jar folder. + * @return Libvirt resource as stream. + */ + private static InputStream getLibvirtResource( String prefix, String fileName ) + { + final String path = prefix + File.separator + fileName; + return LibvirtXmlResources.class.getResourceAsStream( path ); + } + + /** * Returns a Libvirt XSL resource as stream. * * @param libvirtXslFileName file name of the XSL resource in the resources *.jar folder. @@ -34,8 +62,7 @@ public final class LibvirtXmlResources */ public static InputStream getLibvirtXsl( String libvirtXslFileName ) { - String libvirtXslPath = LibvirtXmlResources.LIBVIRT_PREFIX_PATH_XSL + File.separator + libvirtXslFileName; - return LibvirtXmlResources.class.getResourceAsStream( libvirtXslPath ); + return LibvirtXmlResources.getLibvirtResource( LibvirtXmlResources.LIBVIRT_PREFIX_PATH_XSL, libvirtXslFileName ); } /** @@ -46,7 +73,31 @@ public final class LibvirtXmlResources */ public static InputStream getLibvirtRng( String libvirtRngFileName ) { - String libvirtRngPath = LibvirtXmlResources.LIBVIRT_PREFIX_PATH_RNG + File.separator + libvirtRngFileName; - return LibvirtXmlResources.class.getResourceAsStream( libvirtRngPath ); + return LibvirtXmlResources.getLibvirtResource( LibvirtXmlResources.LIBVIRT_PREFIX_PATH_RNG, libvirtRngFileName ); + } + + /** + * Returns a libosinfo RNG schema resource as stream. + * + * @param libosInfoRngFileName file name of the RNG schema resource in the resources *.jar + * folder. + * @return libosinfo RNG schema resource as stream. + */ + public static InputStream getLibOsInfoRng( String libosInfoRngFileName ) + { + return LibvirtXmlResources.getLibvirtResource( LibvirtXmlResources.LIBOSINFO_PREFIX_PATH_RNG, + libosInfoRngFileName ); + } + + /** + * Returns a libosinfo XML resource as stream. + * + * @param libosInfoXmlFileName file name of the XML resource in the resources *.jar folder. + * @return libosinfo XML resource as stream. + */ + public static InputStream getLibOsInfoXml( String libosInfoXmlFileName ) + { + return LibvirtXmlResources.getLibvirtResource( LibvirtXmlResources.LIBOSINFO_PREFIX_PATH_XML, + libosInfoXmlFileName ); } } diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSchemaValidator.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSchemaValidator.java index e074948..bc8f90f 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSchemaValidator.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSchemaValidator.java @@ -196,8 +196,9 @@ public class LibvirtXmlSchemaValidator /** * Creates a validator for validation of Libvirt XML documents with RelaxNG schemas. * - * @param rngSchema - * @throws SAXException + * @param rngSchema RelaxNG schema used for validation with {@link #validate(Document)}. + * + * @throws SAXException creation of a Libvirt XML validator failed. */ public LibvirtXmlSchemaValidator( InputStream rngSchema ) throws SAXException { diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSerializable.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSerializable.java index 6f11ce5..4cd0a32 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSerializable.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSerializable.java @@ -17,6 +17,8 @@ public abstract interface LibvirtXmlSerializable * Serialize Libvirt XML document from {@link String}. * * @param xml {@link String} containing XML content. + * + * @throws LibvirtXmlSerializationException serialization of Libvirt XML document failed. */ public void fromXml( String xml ) throws LibvirtXmlSerializationException; @@ -24,6 +26,8 @@ public abstract interface LibvirtXmlSerializable * Serialize Libvirt XML document from {@link File}. * * @param xml {@link File} containing XML content. + * + * @throws LibvirtXmlSerializationException serialization of Libvirt XML document failed. */ public void fromXml( File xml ) throws LibvirtXmlSerializationException; @@ -31,6 +35,8 @@ public abstract interface LibvirtXmlSerializable * Serialize Libvirt XML document from {@link InputStream}. * * @param xml {@link InputStream} providing XML content. + * + * @throws LibvirtXmlSerializationException serialization of Libvirt XML document failed. */ void fromXml( InputStream xml ) throws LibvirtXmlSerializationException; @@ -38,6 +44,8 @@ public abstract interface LibvirtXmlSerializable * Serialize Libvirt XML document from {@link InputSource}. * * @param xml {@link InputSource} providing XML content. + * + * @throws LibvirtXmlSerializationException serialization of Libvirt XML document failed. */ public void fromXml( InputSource xml ) throws LibvirtXmlSerializationException; @@ -45,6 +53,8 @@ public abstract interface LibvirtXmlSerializable * Serialize Libvirt XML document to {@link String}. * * @return XML {@link String} containing Libvirt XML document content. + * + * @throws LibvirtXmlSerializationException serialization of Libvirt XML document failed. */ public String toXml() throws LibvirtXmlSerializationException; @@ -52,6 +62,8 @@ public abstract interface LibvirtXmlSerializable * Serialize Libvirt XML document to {@link File}. * * @param xml XML {@link File} containing Libvirt XML document content. + * + * @throws LibvirtXmlSerializationException serialization of Libvirt XML document failed. */ public void toXml( File xml ) throws LibvirtXmlSerializationException; } |