Search in sources :

Example 1 with ForeignMetadataFormatMapping

use of edu.harvard.iq.dataverse.ForeignMetadataFormatMapping in project dataverse by IQSS.

the class ForeignMetadataImportServiceBean method importXML.

public void importXML(String xmlToParse, String foreignFormat, DatasetVersion datasetVersion) {
    StringReader reader = null;
    XMLStreamReader xmlr = null;
    ForeignMetadataFormatMapping mappingSupported = findFormatMappingByName(foreignFormat);
    if (mappingSupported == null) {
        throw new EJBException("Unknown/unsupported foreign metadata format " + foreignFormat);
    }
    try {
        reader = new StringReader(xmlToParse);
        XMLInputFactory xmlFactory = javax.xml.stream.XMLInputFactory.newInstance();
        xmlr = xmlFactory.createXMLStreamReader(reader);
        processXML(xmlr, mappingSupported, datasetVersion);
    } catch (XMLStreamException ex) {
        // Logger.getLogger("global").log(Level.SEVERE, null, ex);
        throw new EJBException("ERROR occurred while parsing XML fragment  (" + xmlToParse.substring(0, 64) + "...); ", ex);
    } finally {
        try {
            if (xmlr != null) {
                xmlr.close();
            }
        } catch (XMLStreamException ex) {
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ForeignMetadataFormatMapping(edu.harvard.iq.dataverse.ForeignMetadataFormatMapping) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) EJBException(javax.ejb.EJBException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with ForeignMetadataFormatMapping

use of edu.harvard.iq.dataverse.ForeignMetadataFormatMapping in project dataverse by IQSS.

the class ForeignMetadataImportServiceBean method importXML.

public void importXML(File xmlFile, String foreignFormat, DatasetVersion datasetVersion) {
    FileInputStream in = null;
    XMLStreamReader xmlr = null;
    // look up the foreign metadata mapping for this format:
    ForeignMetadataFormatMapping mappingSupported = findFormatMappingByName(foreignFormat);
    if (mappingSupported == null) {
        throw new EJBException("Unknown/unsupported foreign metadata format " + foreignFormat);
    }
    try {
        in = new FileInputStream(xmlFile);
        XMLInputFactory xmlFactory = javax.xml.stream.XMLInputFactory.newInstance();
        xmlr = xmlFactory.createXMLStreamReader(in);
        processXML(xmlr, mappingSupported, datasetVersion);
    } catch (FileNotFoundException ex) {
        // Logger.getLogger("global").log(Level.SEVERE, null, ex);
        throw new EJBException("ERROR occurred in mapDDI: File Not Found!");
    } catch (XMLStreamException ex) {
        // Logger.getLogger("global").log(Level.SEVERE, null, ex);
        throw new EJBException("ERROR occurred while parsing XML (file " + xmlFile.getAbsolutePath() + "); ", ex);
    } finally {
        try {
            if (xmlr != null) {
                xmlr.close();
            }
        } catch (XMLStreamException ex) {
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ForeignMetadataFormatMapping(edu.harvard.iq.dataverse.ForeignMetadataFormatMapping) XMLStreamException(javax.xml.stream.XMLStreamException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) EJBException(javax.ejb.EJBException) FileInputStream(java.io.FileInputStream) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with ForeignMetadataFormatMapping

use of edu.harvard.iq.dataverse.ForeignMetadataFormatMapping in project dataverse by IQSS.

the class ImportGenericServiceBean method processOAIDCxml.

// Helper method for importing harvested Dublin Core xml.
// Dublin Core is considered a mandatory, built in metadata format mapping.
// It is distributed as required content, in reference_data.sql.
// Note that arbitrary formatting tags are supported for the outer xml
// wrapper. -- L.A. 4.5
public DatasetDTO processOAIDCxml(String DcXmlToParse) throws XMLStreamException {
    // look up DC metadata mapping:
    ForeignMetadataFormatMapping dublinCoreMapping = findFormatMappingByName(DCTERMS);
    if (dublinCoreMapping == null) {
        throw new EJBException("Failed to find metadata mapping for " + DCTERMS);
    }
    DatasetDTO datasetDTO = this.initializeDataset();
    StringReader reader = null;
    XMLStreamReader xmlr = null;
    try {
        reader = new StringReader(DcXmlToParse);
        XMLInputFactory xmlFactory = javax.xml.stream.XMLInputFactory.newInstance();
        xmlr = xmlFactory.createXMLStreamReader(reader);
        // while (xmlr.next() == XMLStreamConstants.COMMENT); // skip pre root comments
        xmlr.nextTag();
        xmlr.require(XMLStreamConstants.START_ELEMENT, null, OAI_DC_OPENING_TAG);
        processXMLElement(xmlr, ":", OAI_DC_OPENING_TAG, dublinCoreMapping, datasetDTO);
    } catch (XMLStreamException ex) {
        throw new EJBException("ERROR occurred while parsing XML fragment  (" + DcXmlToParse.substring(0, 64) + "...); ", ex);
    }
    datasetDTO.getDatasetVersion().setVersionState(DatasetVersion.VersionState.RELEASED);
    // Our DC import handles the contents of the dc:identifier field
    // as an "other id". In the context of OAI harvesting, we expect
    // the identifier to be a global id, so we need to rearrange that:
    String identifier = getOtherIdFromDTO(datasetDTO.getDatasetVersion());
    logger.fine("Imported identifier: " + identifier);
    String globalIdentifier = reassignIdentifierAsGlobalId(identifier, datasetDTO);
    logger.fine("Detected global identifier: " + globalIdentifier);
    if (globalIdentifier == null) {
        throw new EJBException("Failed to find a global identifier in the OAI_DC XML record.");
    }
    return datasetDTO;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ForeignMetadataFormatMapping(edu.harvard.iq.dataverse.ForeignMetadataFormatMapping) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) EJBException(javax.ejb.EJBException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 4 with ForeignMetadataFormatMapping

use of edu.harvard.iq.dataverse.ForeignMetadataFormatMapping in project dataverse by IQSS.

the class ImportGenericServiceBean method importXML.

public void importXML(File xmlFile, String foreignFormat, DatasetVersion datasetVersion) {
    FileInputStream in = null;
    XMLStreamReader xmlr = null;
    // look up the foreign metadata mapping for this format:
    ForeignMetadataFormatMapping mappingSupported = findFormatMappingByName(foreignFormat);
    if (mappingSupported == null) {
        throw new EJBException("Unknown/unsupported foreign metadata format " + foreignFormat);
    }
    try {
        in = new FileInputStream(xmlFile);
        XMLInputFactory xmlFactory = javax.xml.stream.XMLInputFactory.newInstance();
        xmlr = xmlFactory.createXMLStreamReader(in);
        DatasetDTO datasetDTO = processXML(xmlr, mappingSupported);
        Gson gson = new Gson();
        String json = gson.toJson(datasetDTO.getDatasetVersion());
        logger.info("Json:\n" + json);
        JsonReader jsonReader = Json.createReader(new StringReader(json));
        JsonObject obj = jsonReader.readObject();
        DatasetVersion dv = new JsonParser(datasetFieldSvc, blockService, settingsService).parseDatasetVersion(obj, datasetVersion);
    } catch (FileNotFoundException ex) {
        // Logger.getLogger("global").log(Level.SEVERE, null, ex);
        throw new EJBException("ERROR occurred in mapDDI: File Not Found!");
    } catch (XMLStreamException ex) {
        // Logger.getLogger("global").log(Level.SEVERE, null, ex);
        throw new EJBException("ERROR occurred while parsing XML (file " + xmlFile.getAbsolutePath() + "); ", ex);
    } catch (JsonParseException ex) {
        Logger.getLogger(ImportGenericServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (xmlr != null) {
                xmlr.close();
            }
        } catch (XMLStreamException ex) {
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ForeignMetadataFormatMapping(edu.harvard.iq.dataverse.ForeignMetadataFormatMapping) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) IOException(java.io.IOException) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) FileInputStream(java.io.FileInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) EJBException(javax.ejb.EJBException) XMLInputFactory(javax.xml.stream.XMLInputFactory) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser)

Example 5 with ForeignMetadataFormatMapping

use of edu.harvard.iq.dataverse.ForeignMetadataFormatMapping in project dataverse by IQSS.

the class ImportGenericServiceBean method importXML.

public void importXML(String xmlToParse, String foreignFormat, DatasetVersion datasetVersion) {
    StringReader reader = null;
    XMLStreamReader xmlr = null;
    ForeignMetadataFormatMapping mappingSupported = findFormatMappingByName(foreignFormat);
    if (mappingSupported == null) {
        throw new EJBException("Unknown/unsupported foreign metadata format " + foreignFormat);
    }
    try {
        reader = new StringReader(xmlToParse);
        XMLInputFactory xmlFactory = javax.xml.stream.XMLInputFactory.newInstance();
        xmlr = xmlFactory.createXMLStreamReader(reader);
        DatasetDTO datasetDTO = processXML(xmlr, mappingSupported);
        Gson gson = new Gson();
        String json = gson.toJson(datasetDTO.getDatasetVersion());
        logger.fine(json);
        JsonReader jsonReader = Json.createReader(new StringReader(json));
        JsonObject obj = jsonReader.readObject();
        DatasetVersion dv = new JsonParser(datasetFieldSvc, blockService, settingsService).parseDatasetVersion(obj, datasetVersion);
    } catch (XMLStreamException ex) {
        // Logger.getLogger("global").log(Level.SEVERE, null, ex);
        throw new EJBException("ERROR occurred while parsing XML fragment  (" + xmlToParse.substring(0, 64) + "...); ", ex);
    } catch (JsonParseException ex) {
        Logger.getLogger(ImportGenericServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (xmlr != null) {
                xmlr.close();
            }
        } catch (XMLStreamException ex) {
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ForeignMetadataFormatMapping(edu.harvard.iq.dataverse.ForeignMetadataFormatMapping) Gson(com.google.gson.Gson) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) EJBException(javax.ejb.EJBException) XMLInputFactory(javax.xml.stream.XMLInputFactory) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser)

Aggregations

ForeignMetadataFormatMapping (edu.harvard.iq.dataverse.ForeignMetadataFormatMapping)5 EJBException (javax.ejb.EJBException)5 XMLInputFactory (javax.xml.stream.XMLInputFactory)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 XMLStreamReader (javax.xml.stream.XMLStreamReader)5 StringReader (java.io.StringReader)4 Gson (com.google.gson.Gson)2 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)2 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)2 JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 JsonObject (javax.json.JsonObject)2 JsonReader (javax.json.JsonReader)2