Fixed validator
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/gcubedatacatalogue-metadata-discovery@176377 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
7fe1d85ed7
commit
ed03ee906e
|
@ -5,6 +5,7 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -12,6 +13,10 @@ import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.xml.XMLConstants;
|
import javax.xml.XMLConstants;
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.transform.Source;
|
||||||
|
import javax.xml.transform.dom.DOMSource;
|
||||||
import javax.xml.transform.stream.StreamSource;
|
import javax.xml.transform.stream.StreamSource;
|
||||||
import javax.xml.validation.Schema;
|
import javax.xml.validation.Schema;
|
||||||
import javax.xml.validation.SchemaFactory;
|
import javax.xml.validation.SchemaFactory;
|
||||||
|
@ -27,6 +32,8 @@ import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatReader;
|
||||||
import org.gcube.datacatalogue.metadatadiscovery.reader.NamespaceCategoryReader;
|
import org.gcube.datacatalogue.metadatadiscovery.reader.NamespaceCategoryReader;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -182,7 +189,11 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
|
||||||
return DataCalogueMetadataFormatReader.class.getResourceAsStream(SCHEMA_FILENAME);
|
return DataCalogueMetadataFormatReader.class.getResourceAsStream(SCHEMA_FILENAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void validateAgainstXSD(StreamSource xml, StreamSource xsd) throws SAXException, IOException {
|
static URL getProfileSchemaURL() {
|
||||||
|
return DataCalogueMetadataFormatReader.class.getResource(SCHEMA_FILENAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void validateAgainstXSD(Source xml, URL xsd) throws SAXException, IOException {
|
||||||
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||||
Schema schema = factory.newSchema(xsd);
|
Schema schema = factory.newSchema(xsd);
|
||||||
Validator validator = schema.newValidator();
|
Validator validator = schema.newValidator();
|
||||||
|
@ -191,11 +202,14 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validateProfile(String xmlProfile) throws Exception {
|
public void validateProfile(String xmlProfile) throws Exception {
|
||||||
validateAgainstXSD(new StreamSource(xmlProfile), new StreamSource(getProfileSchema()));
|
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
|
Document doc = db.parse( new InputSource(new StringReader(xmlProfile)));
|
||||||
|
DOMSource source = new DOMSource(doc);
|
||||||
|
validateAgainstXSD(source, getProfileSchemaURL());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void validateProfile(InputStream xml) throws Exception {
|
public static void validateProfile(InputStream xml) throws Exception {
|
||||||
validateAgainstXSD(new StreamSource(xml), new StreamSource(getProfileSchemaInputStream()));
|
validateAgainstXSD(new StreamSource(xml), getProfileSchemaURL());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ public class TestDataCatalogueMetadataFormatReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String PROFILE_EXAMPLE_FILENAME = "profileExample.xml";
|
public static String PROFILE_EXAMPLE_FILENAME = "EmptyProfileExample.xml";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void validateAgainstProfileSchema() throws Exception {
|
public void validateAgainstProfileSchema() throws Exception {
|
||||||
|
|
Loading…
Reference in New Issue