|
|
|
@ -5,6 +5,7 @@ import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.StringReader;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -12,6 +13,10 @@ import java.util.Map;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
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.validation.Schema;
|
|
|
|
|
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.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -182,7 +189,11 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
|
|
|
|
|
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);
|
|
|
|
|
Schema schema = factory.newSchema(xsd);
|
|
|
|
|
Validator validator = schema.newValidator();
|
|
|
|
@ -191,11 +202,14 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
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 {
|
|
|
|
|
validateAgainstXSD(new StreamSource(xml), new StreamSource(getProfileSchemaInputStream()));
|
|
|
|
|
validateAgainstXSD(new StreamSource(xml), getProfileSchemaURL());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|