Refactored and added method 'toMetadataFormat'

This commit is contained in:
Francesco Mangiacrapa 2020-09-30 14:10:09 +02:00
parent e0671579da
commit a6d91a4b7d
9 changed files with 119 additions and 46 deletions

View File

@ -6,7 +6,17 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="src/main/resources"/> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes> <attributes>
<attribute name="test" value="true"/> <attribute name="test" value="true"/>

View File

@ -1,4 +1,6 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
encoding//src/main/java=UTF-8 encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8 encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8 encoding/<project>=UTF-8

View File

@ -2,9 +2,9 @@
The metadata-profile-discovery library is able to build java objects parsing the 'gCube Metadata Profiles' models: The metadata-profile-discovery library is able to build java objects parsing the 'gCube Metadata Profiles' models:
- defined in the contexts (scopes) of the D4Science Infrastructure; * defined in the contexts (scopes) of the D4Science Infrastructure;
- passed as an input stream; * passed as an input stream;
## Built With ## Built With

View File

@ -11,13 +11,13 @@ import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategory;
/** /**
* The Interface DataCatalogueMetadataDiscovery. * The Interface MetadataProfileDiscovery.
* *
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* *
* Sep 30, 2020 * Sep 30, 2020
*/ */
public interface DataCatalogueMetadataDiscovery { public interface MetadataProfileDiscovery {
/** /**
* Gets the list of metadata types. * Gets the list of metadata types.

View File

@ -13,6 +13,9 @@ 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.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
@ -37,12 +40,17 @@ import org.w3c.dom.Document;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
/** /**
* The Class DataCalogueMetadataReader. * The Class MetadataProfileReader.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Jun 8, 2016 * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Sep 30, 2020
*/ */
public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDiscovery { public class MetadataProfileReader implements MetadataProfileDiscovery {
public static final String URL_OF_GCDCMETADATAPROFILEV3_XSD = "https://wiki.gcube-system.org/images_gcube/e/e8/Gcdcmetadataprofilev3.xsd";
private static String SCHEMA_FILENAME = "Gdcmetadataprofilev3.xsd"; private static String SCHEMA_FILENAME = "Gdcmetadataprofilev3.xsd";
@ -53,14 +61,23 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
private String profileSchema = null; private String profileSchema = null;
private static Logger logger = LoggerFactory.getLogger(DataCalogueMetadataFormatReader.class); private String grMetadataProfileSecondaryType;
private static Logger logger = LoggerFactory.getLogger(MetadataProfileReader.class);
/** /**
* Instantiates a new data calogue metadata format reader. * Instantiates a new metadata profile reader.
* *
* @param grMetadataProfileSecondaryType the SecondaryType that must be used to discover the "gCube Metadata Profiles" from Information System
* @throws Exception the exception * @throws Exception the exception
*/ */
public DataCalogueMetadataFormatReader() throws Exception { public MetadataProfileReader(String grMetadataProfileSecondaryType) throws Exception {
if(grMetadataProfileSecondaryType==null || grMetadataProfileSecondaryType.isEmpty())
throw new NullPointerException("Invalid input parameter");
this.grMetadataProfileSecondaryType = grMetadataProfileSecondaryType;
String scopeString = ScopeProvider.instance.get(); String scopeString = ScopeProvider.instance.get();
logger.debug("Read scope " + scopeString + " from ScopeProvider"); logger.debug("Read scope " + scopeString + " from ScopeProvider");
@ -70,7 +87,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
scope = new ScopeBean(scopeString); scope = new ScopeBean(scopeString);
readNamespaces(); readNamespaces();
readMetadaFormats(); readMetadataFormats();
} }
/** /**
@ -78,9 +95,12 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
* *
* @throws Exception the exception * @throws Exception the exception
*/ */
private void readMetadaFormats() throws Exception { private void readMetadataFormats() throws Exception {
//reading from Generic Resource for Secondary Type
if(grMetadataProfileSecondaryType!=null) {
medataFormatDiscovery = new MetadataFormatDiscovery(scope, grMetadataProfileSecondaryType);
medataFormatDiscovery = new MetadataFormatDiscovery(scope);
logger.info("MedataFormatDiscovery has retrieved: " + medataFormatDiscovery.getMetadataProfiles().size() logger.info("MedataFormatDiscovery has retrieved: " + medataFormatDiscovery.getMetadataProfiles().size()
+ " metadata type/s"); + " metadata type/s");
logger.debug("filling cache for MedataFormat"); logger.debug("filling cache for MedataFormat");
@ -94,6 +114,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
logger.debug("MetadataType id: " + mT.getId() + " cached as: " + reader.getMetadataFormat()); logger.debug("MetadataType id: " + mT.getId() + " cached as: " + reader.getMetadataFormat());
} }
} }
}
/** /**
* Read namespaces. * Read namespaces.
@ -120,7 +141,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
* @return the profile schema string * @return the profile schema string
*/ */
public static String getProfileSchemaString() { public static String getProfileSchemaString() {
InputStream inputStream = DataCalogueMetadataFormatReader.getProfileSchemaInputStream(); InputStream inputStream = MetadataProfileReader.getProfileSchemaInputStream();
return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n")); return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
} }
@ -130,7 +151,15 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
* @return the profile schema input stream * @return the profile schema input stream
*/ */
public static InputStream getProfileSchemaInputStream() { public static InputStream getProfileSchemaInputStream() {
return DataCalogueMetadataFormatReader.class.getResourceAsStream(SCHEMA_FILENAME); try {
logger.info("Trying to return the URL resource: "+URL_OF_GCDCMETADATAPROFILEV3_XSD);
return new URL(URL_OF_GCDCMETADATAPROFILEV3_XSD).openStream();
} catch (Exception e) {
logger.warn("Error on reading the URL: "+URL_OF_GCDCMETADATAPROFILEV3_XSD);
}
logger.info("Returning local resource: "+SCHEMA_FILENAME);
return MetadataProfileReader.class.getResourceAsStream(SCHEMA_FILENAME);
} }
/** /**
@ -139,7 +168,15 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
* @return the profile schema URL * @return the profile schema URL
*/ */
public static URL getProfileSchemaURL() { public static URL getProfileSchemaURL() {
return DataCalogueMetadataFormatReader.class.getResource(SCHEMA_FILENAME); try {
logger.info("Trying to return the URL object: "+URL_OF_GCDCMETADATAPROFILEV3_XSD);
return new URL(URL_OF_GCDCMETADATAPROFILEV3_XSD);
} catch (Exception e) {
logger.warn("Error on reading the URL: "+URL_OF_GCDCMETADATAPROFILEV3_XSD);
}
logger.info("Returning local URL of: "+SCHEMA_FILENAME);
return MetadataProfileReader.class.getResource(SCHEMA_FILENAME);
} }
/** /**
@ -198,7 +235,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
public List<MetadataProfile> getListOfMetadataProfiles() throws Exception { public List<MetadataProfile> getListOfMetadataProfiles() throws Exception {
if (medataFormatDiscovery == null) if (medataFormatDiscovery == null)
readMetadaFormats(); readMetadataFormats();
return medataFormatDiscovery.getMetadataProfiles(); return medataFormatDiscovery.getMetadataProfiles();
} }
@ -252,7 +289,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
*/ */
public String getProfileSchema() { public String getProfileSchema() {
if (profileSchema == null) { if (profileSchema == null) {
profileSchema = DataCalogueMetadataFormatReader.getProfileSchemaString(); profileSchema = MetadataProfileReader.getProfileSchemaString();
} }
return profileSchema; return profileSchema;
@ -272,4 +309,22 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
validateAgainstXSD(source, getProfileSchemaURL()); validateAgainstXSD(source, getProfileSchemaURL());
} }
/**
* Utility method to get a metadata format from a "gCube Metadata Profiles" model (XML-based)
*
* @param metadataProfileStream the metadata profile stream
* @return the metadata format
* @throws JAXBException the JAXB exception
*/
public MetadataFormat toMetadataFormat(InputStream metadataProfileStream) throws JAXBException{
if(metadataProfileStream==null)
throw new NullPointerException("Invalid input parameter");
JAXBContext jaxbContext = JAXBContext.newInstance(MetadataFormat.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (MetadataFormat) jaxbUnmarshaller.unmarshal(metadataProfileStream);
}
} }

View File

@ -28,28 +28,30 @@ import org.xml.sax.InputSource;
/** /**
* The Class MedataFormatDiscovery. * The Class MetadataFormatDiscovery.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Jun 8, 2016 *
* Sep 30, 2020
*/ */
public class MetadataFormatDiscovery { public class MetadataFormatDiscovery {
private static Logger logger = LoggerFactory.getLogger(MetadataFormatDiscovery.class); private static Logger logger = LoggerFactory.getLogger(MetadataFormatDiscovery.class);
public static final String DATA_CATALOGUE_METADATA_SECONDARY_TYPE = "DataCatalogueMetadata";
private String secondaryType; private String secondaryType;
private ScopeBean scope; private ScopeBean scope;
private List<MetadataProfile> metadataProfiles; private List<MetadataProfile> metadataProfiles;
/** /**
* Instantiates a new medata format reader. * Instantiates a new metadata format discovery.
* *
* @param scope - the scope to be searched * @param scope the scope
* @param secondaryType the secondary type
* @throws Exception the exception * @throws Exception the exception
*/ */
public MetadataFormatDiscovery(ScopeBean scope) throws Exception { public MetadataFormatDiscovery(ScopeBean scope, String secondaryType) throws Exception {
this.scope = scope; this.scope = scope;
this.secondaryType = DATA_CATALOGUE_METADATA_SECONDARY_TYPE; this.secondaryType = secondaryType;
this.metadataProfiles = readMetadataProfilesFromInfrastrucure(); this.metadataProfiles = readMetadataProfilesFromInfrastrucure();
} }
@ -115,6 +117,7 @@ public class MetadataFormatDiscovery {
/** /**
* Gets the metadata type from resource. * Gets the metadata type from resource.
* *
* @param doc the doc
* @param helper the helper * @param helper the helper
* @return the metadata type from resource * @return the metadata type from resource
* @throws MetadataProfileNotFoundException the application profile not found exception * @throws MetadataProfileNotFoundException the application profile not found exception

1
src/test/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/resources/

View File

@ -28,12 +28,13 @@ public class TestDataCatalogueMetadataFormatReader {
/** /**
* Test. * Test.
*/ */
// @Test @Test
public void test() { public void test() {
try { try {
String scopeString = "/gcube/devsec/devVRE"; String scopeString = "/gcube/devsec/devVRE";
String grMetadataProfileSecondaryType = "DataCatalogueMetadata";
ScopeProvider.instance.set(scopeString); ScopeProvider.instance.set(scopeString);
DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader(); MetadataProfileReader reader = new MetadataProfileReader(grMetadataProfileSecondaryType);
int i = 0; int i = 0;
List<NamespaceCategory> categs = reader.getListOfNamespaceCategories(); List<NamespaceCategory> categs = reader.getListOfNamespaceCategories();
@ -63,7 +64,7 @@ public class TestDataCatalogueMetadataFormatReader {
// @Test // @Test
public void validateAgainstProfileSchema() throws Exception { public void validateAgainstProfileSchema() throws Exception {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROFILE_EXAMPLE_FILENAME); InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROFILE_EXAMPLE_FILENAME);
DataCalogueMetadataFormatReader.validateProfile(inputStream); MetadataProfileReader.validateProfile(inputStream);
} }
} }

View File

@ -21,15 +21,16 @@ public class TestMetadataFormatDiscovery {
/** /**
* Test. * Test.
*/ */
// @Test @Test
public void test() { public void test() {
String scopeString = "/gcube/devsec/devVRE"; String scopeString = "/gcube/devsec/devVRE";
String grMetadataProfileSecondaryType = "DataCatalogueMetadata";
final ScopeBean scope = new ScopeBean(scopeString); final ScopeBean scope = new ScopeBean(scopeString);
MetadataFormatDiscovery reader; MetadataFormatDiscovery reader;
try { try {
ScopeProvider.instance.set(scopeString); ScopeProvider.instance.set(scopeString);
reader = new MetadataFormatDiscovery(scope); reader = new MetadataFormatDiscovery(scope, grMetadataProfileSecondaryType);
//System.out.println(reader.getMetadataProfiles()); //System.out.println(reader.getMetadataProfiles());
for (MetadataProfile metaProfile : reader.getMetadataProfiles()) { for (MetadataProfile metaProfile : reader.getMetadataProfiles()) {