summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorManuel Bentele2021-01-30 12:00:35 +0100
committerManuel Bentele2021-01-30 12:00:35 +0100
commit4317a82d74d8b8518a5dbb6b3675aec3f912802e (patch)
treef4f449a37385a13133d429c6eaa09fb6b6709625 /src
parentRemove unused import in QemuMetaData class (diff)
downloadmaster-sync-shared-4317a82d74d8b8518a5dbb6b3675aec3f912802e.tar.gz
master-sync-shared-4317a82d74d8b8518a5dbb6b3675aec3f912802e.tar.xz
master-sync-shared-4317a82d74d8b8518a5dbb6b3675aec3f912802e.zip
Add automatic RelaxNG schema validation for Libvirt domain XML documents
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/openslx/libvirt/domain/Domain.java9
-rw-r--r--src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java11
-rw-r--r--src/main/java/org/openslx/libvirt/xml/LibvirtXmlSchemaValidator.java7
-rw-r--r--src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java10
4 files changed, 17 insertions, 20 deletions
diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java
index 35cd012..4e15ec1 100644
--- a/src/main/java/org/openslx/libvirt/domain/Domain.java
+++ b/src/main/java/org/openslx/libvirt/domain/Domain.java
@@ -33,6 +33,7 @@ import org.openslx.libvirt.domain.device.Video;
import org.openslx.libvirt.xml.LibvirtXmlDocument;
import org.openslx.libvirt.xml.LibvirtXmlDocumentException;
import org.openslx.libvirt.xml.LibvirtXmlNode;
+import org.openslx.libvirt.xml.LibvirtXmlResources;
import org.openslx.libvirt.xml.LibvirtXmlSerializationException;
import org.openslx.libvirt.xml.LibvirtXmlValidationException;
import org.w3c.dom.Node;
@@ -61,7 +62,7 @@ public class Domain extends LibvirtXmlDocument
public Domain( String xml )
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
- super( xml );
+ super( xml, LibvirtXmlResources.getLibvirtRng( "domain.rng" ) );
}
/**
@@ -76,7 +77,7 @@ public class Domain extends LibvirtXmlDocument
public Domain( File xml )
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
- super( xml );
+ super( xml, LibvirtXmlResources.getLibvirtRng( "domain.rng" ) );
}
/**
@@ -92,7 +93,7 @@ public class Domain extends LibvirtXmlDocument
public Domain( InputStream xml )
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
- super( xml );
+ super( xml, LibvirtXmlResources.getLibvirtRng( "domain.rng" ) );
}
/**
@@ -108,7 +109,7 @@ public class Domain extends LibvirtXmlDocument
public Domain( InputSource xml )
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
- super( xml );
+ super( xml, LibvirtXmlResources.getLibvirtRng( "domain.rng" ) );
}
/**
diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java
index abab162..8fe642b 100644
--- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java
+++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java
@@ -9,7 +9,6 @@ import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
@@ -66,7 +65,7 @@ public abstract class LibvirtXmlDocument implements LibvirtXmlSerializable, Libv
* @throws LibvirtXmlDocumentException error occured during setup of the XML context to read and
* write from/to a Libvirt XML file.
*/
- private void createXmlContext( Source rngSchema ) throws LibvirtXmlDocumentException
+ private void createXmlContext( InputStream rngSchema ) throws LibvirtXmlDocumentException
{
// used for XML input
try {
@@ -134,7 +133,7 @@ public abstract class LibvirtXmlDocument implements LibvirtXmlSerializable, Libv
* @throws LibvirtXmlSerializationException serialization of the XML content failed.
* @throws LibvirtXmlValidationException XML content is not a valid Libvirt XML.
*/
- public LibvirtXmlDocument( String xml, Source rngSchema )
+ public LibvirtXmlDocument( String xml, InputStream rngSchema )
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
this.createXmlContext( rngSchema );
@@ -167,7 +166,7 @@ public abstract class LibvirtXmlDocument implements LibvirtXmlSerializable, Libv
* @throws LibvirtXmlSerializationException serialization of the XML content failed.
* @throws LibvirtXmlValidationException XML content is not a valid Libvirt XML.
*/
- public LibvirtXmlDocument( File xml, Source rngSchema )
+ public LibvirtXmlDocument( File xml, InputStream rngSchema )
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
this.createXmlContext( rngSchema );
@@ -200,7 +199,7 @@ public abstract class LibvirtXmlDocument implements LibvirtXmlSerializable, Libv
* @throws LibvirtXmlSerializationException serialization of the XML content failed.
* @throws LibvirtXmlValidationException XML content is not a valid Libvirt XML.
*/
- public LibvirtXmlDocument( InputStream xml, Source rngSchema )
+ public LibvirtXmlDocument( InputStream xml, InputStream rngSchema )
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
this.createXmlContext( rngSchema );
@@ -233,7 +232,7 @@ public abstract class LibvirtXmlDocument implements LibvirtXmlSerializable, Libv
* @throws LibvirtXmlSerializationException serialization of the XML content failed.
* @throws LibvirtXmlValidationException XML content is not a valid Libvirt XML.
*/
- public LibvirtXmlDocument( InputSource xml, Source rngSchema )
+ public LibvirtXmlDocument( InputSource xml, InputStream rngSchema )
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
this.createXmlContext( rngSchema );
diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSchemaValidator.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSchemaValidator.java
index 01e8adb..e074948 100644
--- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSchemaValidator.java
+++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlSchemaValidator.java
@@ -8,7 +8,6 @@ import java.io.InputStream;
import java.io.Reader;
import javax.xml.XMLConstants;
-import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
@@ -200,7 +199,7 @@ public class LibvirtXmlSchemaValidator
* @param rngSchema
* @throws SAXException
*/
- public LibvirtXmlSchemaValidator( Source rngSchema ) throws SAXException
+ public LibvirtXmlSchemaValidator( InputStream rngSchema ) throws SAXException
{
this.createValidationContext( rngSchema );
}
@@ -212,7 +211,7 @@ public class LibvirtXmlSchemaValidator
*
* @throws SAXException Loading, creation and processing of <code>rngSchema</code> has failed.
*/
- private void createValidationContext( Source rngSchema ) throws SAXException
+ private void createValidationContext( InputStream rngSchema ) throws SAXException
{
// use hack to load specific schema factory implementation for RelaxNG schemas
System.setProperty( SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI,
@@ -224,7 +223,7 @@ public class LibvirtXmlSchemaValidator
// create schema factory to be able to create a RelaxNG schema validator
SchemaFactory factory = SchemaFactory.newInstance( XMLConstants.RELAXNG_NS_URI );
factory.setResourceResolver( schemaResolver );
- Schema schema = factory.newSchema( rngSchema );
+ Schema schema = factory.newSchema( new StreamSource( rngSchema ) );
// create the RelaxNG schema validator
this.rngSchemaValidator = schema.newValidator();
diff --git a/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java
index 1b6e5a5..75b934e 100644
--- a/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java
+++ b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java
@@ -9,11 +9,9 @@ import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.nio.charset.StandardCharsets;
-import javax.xml.transform.Source;
-import javax.xml.transform.stream.StreamSource;
-
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
@@ -30,7 +28,7 @@ class LibvirtXmlDocumentStub extends LibvirtXmlDocument
super( xml );
}
- public LibvirtXmlDocumentStub( File xml, Source rngSchema )
+ public LibvirtXmlDocumentStub( File xml, InputStream rngSchema )
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
super( xml, rngSchema );
@@ -70,8 +68,8 @@ public class LibvirtXmlDocumentTest
try {
File xmlFile = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName );
- Source rngSchemaSource = new StreamSource( LibvirtXmlResources.getLibvirtRng( rngSchemaFileName ) );
- document = new LibvirtXmlDocumentStub( xmlFile, rngSchemaSource );
+ InputStream rngSchema = LibvirtXmlResources.getLibvirtRng( rngSchemaFileName );
+ document = new LibvirtXmlDocumentStub( xmlFile, rngSchema );
} catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException e ) {
String errorMsg = new String( "Cannot prepare requested Libvirt XML file from the resources folder" );
fail( errorMsg );