Refactored and added method 'toMetadataFormat'
This commit is contained in:
parent
e0671579da
commit
a6d91a4b7d
12
.classpath
12
.classpath
|
@ -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"/>
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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.
|
|
@ -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,20 +95,24 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
|
||||||
*
|
*
|
||||||
* @throws Exception the exception
|
* @throws Exception the exception
|
||||||
*/
|
*/
|
||||||
private void readMetadaFormats() throws Exception {
|
private void readMetadataFormats() throws Exception {
|
||||||
|
|
||||||
medataFormatDiscovery = new MetadataFormatDiscovery(scope);
|
//reading from Generic Resource for Secondary Type
|
||||||
logger.info("MedataFormatDiscovery has retrieved: " + medataFormatDiscovery.getMetadataProfiles().size()
|
if(grMetadataProfileSecondaryType!=null) {
|
||||||
+ " metadata type/s");
|
medataFormatDiscovery = new MetadataFormatDiscovery(scope, grMetadataProfileSecondaryType);
|
||||||
logger.debug("filling cache for MedataFormat");
|
|
||||||
hashMetadataFormats = new HashMap<String, MetadataFormat>(medataFormatDiscovery.getMetadataProfiles().size());
|
|
||||||
for (MetadataProfile mT : medataFormatDiscovery.getMetadataProfiles()) {
|
|
||||||
if (mT == null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
MetadataFormatReader reader = new MetadataFormatReader(scope, mT.getId());
|
logger.info("MedataFormatDiscovery has retrieved: " + medataFormatDiscovery.getMetadataProfiles().size()
|
||||||
hashMetadataFormats.put(mT.getId(), reader.getMetadataFormat());
|
+ " metadata type/s");
|
||||||
logger.debug("MetadataType id: " + mT.getId() + " cached as: " + reader.getMetadataFormat());
|
logger.debug("filling cache for MedataFormat");
|
||||||
|
hashMetadataFormats = new HashMap<String, MetadataFormat>(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
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -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
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
/resources/
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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()) {
|
||||||
|
|
Loading…
Reference in New Issue