diff --git a/.classpath b/.classpath index bc0c5c4..8d831c8 100644 --- a/.classpath +++ b/.classpath @@ -6,7 +6,17 @@ - + + + + + + + + + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index f9fe345..29abf99 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,4 +1,6 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 encoding/=UTF-8 diff --git a/README.md b/README.md index 7c6a7c0..f8ac8c8 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ 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 diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/DataCatalogueMetadataDiscovery.java b/src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileDiscovery.java similarity index 94% rename from src/main/java/org/gcube/common/metadataprofilediscovery/DataCatalogueMetadataDiscovery.java rename to src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileDiscovery.java index 30e9523..041556c 100644 --- a/src/main/java/org/gcube/common/metadataprofilediscovery/DataCatalogueMetadataDiscovery.java +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileDiscovery.java @@ -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) * * Sep 30, 2020 */ -public interface DataCatalogueMetadataDiscovery { +public interface MetadataProfileDiscovery { /** * Gets the list of metadata types. diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/DataCalogueMetadataFormatReader.java b/src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileReader.java similarity index 64% rename from src/main/java/org/gcube/common/metadataprofilediscovery/DataCalogueMetadataFormatReader.java rename to src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileReader.java index d3379d8..3b2a4ce 100644 --- a/src/main/java/org/gcube/common/metadataprofilediscovery/DataCalogueMetadataFormatReader.java +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileReader.java @@ -13,6 +13,9 @@ import java.util.Map; import java.util.stream.Collectors; 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.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -37,12 +40,17 @@ import org.w3c.dom.Document; import org.xml.sax.InputSource; 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"; @@ -53,15 +61,24 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis 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 */ - 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(); logger.debug("Read scope " + scopeString + " from ScopeProvider"); @@ -70,28 +87,32 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis scope = new ScopeBean(scopeString); readNamespaces(); - readMetadaFormats(); + readMetadataFormats(); } - + /** * Read metada formats. * * @throws Exception the exception */ - private void readMetadaFormats() throws Exception { - - medataFormatDiscovery = new MetadataFormatDiscovery(scope); - logger.info("MedataFormatDiscovery has retrieved: " + medataFormatDiscovery.getMetadataProfiles().size() - + " metadata type/s"); - logger.debug("filling cache for MedataFormat"); - hashMetadataFormats = new HashMap(medataFormatDiscovery.getMetadataProfiles().size()); - for (MetadataProfile mT : medataFormatDiscovery.getMetadataProfiles()) { - if (mT == null) - continue; - - MetadataFormatReader reader = new MetadataFormatReader(scope, mT.getId()); - hashMetadataFormats.put(mT.getId(), reader.getMetadataFormat()); - logger.debug("MetadataType id: " + mT.getId() + " cached as: " + reader.getMetadataFormat()); + private void readMetadataFormats() throws Exception { + + //reading from Generic Resource for Secondary Type + if(grMetadataProfileSecondaryType!=null) { + medataFormatDiscovery = new MetadataFormatDiscovery(scope, grMetadataProfileSecondaryType); + + logger.info("MedataFormatDiscovery has retrieved: " + medataFormatDiscovery.getMetadataProfiles().size() + + " metadata type/s"); + logger.debug("filling cache for MedataFormat"); + hashMetadataFormats = new HashMap(medataFormatDiscovery.getMetadataProfiles().size()); + for (MetadataProfile mT : medataFormatDiscovery.getMetadataProfiles()) { + if (mT == null) + continue; + + MetadataFormatReader reader = new MetadataFormatReader(scope, mT.getId()); + hashMetadataFormats.put(mT.getId(), reader.getMetadataFormat()); + logger.debug("MetadataType id: " + mT.getId() + " cached as: " + reader.getMetadataFormat()); + } } } @@ -120,7 +141,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis * @return the profile schema string */ public static String getProfileSchemaString() { - InputStream inputStream = DataCalogueMetadataFormatReader.getProfileSchemaInputStream(); + InputStream inputStream = MetadataProfileReader.getProfileSchemaInputStream(); 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 */ 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 */ 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 getListOfMetadataProfiles() throws Exception { if (medataFormatDiscovery == null) - readMetadaFormats(); + readMetadataFormats(); return medataFormatDiscovery.getMetadataProfiles(); } @@ -252,7 +289,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis */ public String getProfileSchema() { if (profileSchema == null) { - profileSchema = DataCalogueMetadataFormatReader.getProfileSchemaString(); + profileSchema = MetadataProfileReader.getProfileSchemaString(); } return profileSchema; @@ -271,5 +308,23 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis DOMSource source = new DOMSource(doc); 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); + } + } diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java index fe8fee0..73114a2 100644 --- a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java @@ -28,28 +28,30 @@ import org.xml.sax.InputSource; /** - * The Class MedataFormatDiscovery. + * The Class MetadataFormatDiscovery. * - * @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 MetadataFormatDiscovery { private static Logger logger = LoggerFactory.getLogger(MetadataFormatDiscovery.class); - public static final String DATA_CATALOGUE_METADATA_SECONDARY_TYPE = "DataCatalogueMetadata"; private String secondaryType; private ScopeBean scope; private List 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 */ - public MetadataFormatDiscovery(ScopeBean scope) throws Exception { + public MetadataFormatDiscovery(ScopeBean scope, String secondaryType) throws Exception { this.scope = scope; - this.secondaryType = DATA_CATALOGUE_METADATA_SECONDARY_TYPE; + this.secondaryType = secondaryType; this.metadataProfiles = readMetadataProfilesFromInfrastrucure(); } @@ -115,6 +117,7 @@ public class MetadataFormatDiscovery { /** * Gets the metadata type from resource. * + * @param doc the doc * @param helper the helper * @return the metadata type from resource * @throws MetadataProfileNotFoundException the application profile not found exception diff --git a/src/test/.gitignore b/src/test/.gitignore new file mode 100644 index 0000000..cb6eb2c --- /dev/null +++ b/src/test/.gitignore @@ -0,0 +1 @@ +/resources/ diff --git a/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java b/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java index ca273fd..7113efc 100644 --- a/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java @@ -28,12 +28,13 @@ public class TestDataCatalogueMetadataFormatReader { /** * Test. */ -// @Test + @Test public void test() { try { String scopeString = "/gcube/devsec/devVRE"; + String grMetadataProfileSecondaryType = "DataCatalogueMetadata"; ScopeProvider.instance.set(scopeString); - DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader(); + MetadataProfileReader reader = new MetadataProfileReader(grMetadataProfileSecondaryType); int i = 0; List categs = reader.getListOfNamespaceCategories(); @@ -63,7 +64,7 @@ public class TestDataCatalogueMetadataFormatReader { // @Test public void validateAgainstProfileSchema() throws Exception { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROFILE_EXAMPLE_FILENAME); - DataCalogueMetadataFormatReader.validateProfile(inputStream); + MetadataProfileReader.validateProfile(inputStream); } } diff --git a/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java index 2f88da4..0e39610 100644 --- a/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java @@ -21,15 +21,16 @@ public class TestMetadataFormatDiscovery { /** * Test. */ -// @Test + @Test public void test() { String scopeString = "/gcube/devsec/devVRE"; + String grMetadataProfileSecondaryType = "DataCatalogueMetadata"; final ScopeBean scope = new ScopeBean(scopeString); MetadataFormatDiscovery reader; try { ScopeProvider.instance.set(scopeString); - reader = new MetadataFormatDiscovery(scope); + reader = new MetadataFormatDiscovery(scope, grMetadataProfileSecondaryType); //System.out.println(reader.getMetadataProfiles()); for (MetadataProfile metaProfile : reader.getMetadataProfiles()) {