Skip to content

Commit

Permalink
Added more information to unmarshalling problems #266
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosjepard authored and hmiguim committed Apr 5, 2024
1 parent fec5afe commit 10360ec
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.roda_project.commons_ip2.cli.model.exception;

/**
* @author Carlos Afonso <cafonso@keep.pt>
*/
public class UnmarshallerException extends Exception {

public UnmarshallerException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import javax.xml.parsers.ParserConfigurationException;

import org.roda_project.commons_ip2.cli.model.exception.UnmarshallerException;
import org.roda_project.commons_ip2.validator.common.InstatiateMets;
import org.roda_project.commons_ip2.validator.components.MetsValidator;
import org.roda_project.commons_ip2.validator.components.StructureValidatorImpl;
Expand Down Expand Up @@ -192,7 +193,7 @@ public boolean validate() throws IOException, NoSuchAlgorithmException {
validationReportOutputJSONPyIP.getResults().put(ConstantsCSIPspec.VALIDATION_REPORT_SPECIFICATION_CSIP0_ID,
csipStr0);
}
} catch (IOException | JAXBException | SAXException e) {
} catch (IOException | UnmarshallerException e) {
final StringBuilder message = new StringBuilder();

Throwable cause = e;
Expand Down Expand Up @@ -259,10 +260,10 @@ private void validateComponents() throws IOException {
* If some error occurs
*/
private void validateSubMets(final Map<String, InputStream> subMets, final boolean isZip)
throws IOException, JAXBException, SAXException {
throws IOException, UnmarshallerException {
for (Map.Entry<String, InputStream> entry : subMets.entrySet()) {
final InstatiateMets instatiateMets = new InstatiateMets(entry.getValue());
metsValidatorState.setMets(instatiateMets.instatiateMetsFile());
metsValidatorState.setMets(instatiateMets.instatiateMetsFile(entry.getKey()));
metsValidatorState.setIpType(metsValidatorState.getMets().getMetsHdr().getOAISPACKAGETYPE());
setupMetsValidatorState(entry.getKey(), isZip, false);
validateComponents();
Expand All @@ -274,12 +275,10 @@ private void validateSubMets(final Map<String, InputStream> subMets, final boole
*
* @throws IOException
* If some I/O error occurs
* @throws JAXBException
* If some error occurs
* @throws SAXException
* @throws UnmarshallerException
* If some error occurs
*/
private void validateRootMets() throws IOException, JAXBException, SAXException {
private void validateRootMets() throws IOException, UnmarshallerException {
final InputStream metsRootStream;
final String ipPath;
if (structureValidatorState.isZipFileFlag()) {
Expand All @@ -294,7 +293,7 @@ private void validateRootMets() throws IOException, JAXBException, SAXException
metsValidatorState.setMetsPath(earksipPath.toString());
metsValidatorState.setMetsName(ipPath);
metsValidatorState.setIsRootMets(true);
metsValidatorState.setMets(metsRoot.instatiateMetsFile());
metsValidatorState.setMets(metsRoot.instatiateMetsFile(Constants.METS_FILE));
validateComponents();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import javax.xml.parsers.ParserConfigurationException;

import org.roda_project.commons_ip2.cli.model.exception.UnmarshallerException;
import org.roda_project.commons_ip2.validator.common.InstatiateMets;
import org.roda_project.commons_ip2.validator.components.MetsValidator;
import org.roda_project.commons_ip2.validator.components.StructureValidatorImpl;
Expand Down Expand Up @@ -46,8 +47,6 @@
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import jakarta.xml.bind.JAXBException;

/** {@author João Gomes <jgomes@keep.pt>}. */
public class EARKSIPValidator {
/** IP path. */
Expand Down Expand Up @@ -214,11 +213,11 @@ private void validateSubMets(final Map<String, InputStream> subMets, final boole

final InstatiateMets instatiateMets = new InstatiateMets(entry.getValue());
try {
metsValidatorState.setMets(instatiateMets.instatiateMetsFile());
metsValidatorState.setMets(instatiateMets.instatiateMetsFile(entry.getKey()));
metsValidatorState.setIpType(metsValidatorState.getMets().getMetsHdr().getOAISPACKAGETYPE());
setupMetsValidatorState(entry.getKey(), isZip, false);
validateComponents();
} catch (IOException | JAXBException | SAXException e) {
} catch (IOException | UnmarshallerException e) {
final String message = createExceptionMessage(e, entry.getKey());
final ReporterDetails csipStr0 = new ReporterDetails(Constants.VALIDATION_REPORT_HEADER_CSIP_VERSION, message,
false, false);
Expand All @@ -242,7 +241,10 @@ private String createExceptionMessage(final Exception e, final String mets) {
final StringBuilder message = new StringBuilder();

Throwable cause = e;
if (e.getMessage() != null) {

if (e instanceof UnmarshallerException) {
message.append(e.getMessage());
} else if (e.getMessage() != null) {
message.append(Constants.OPEN_SQUARE_BRACKET).append(e.getClass().getSimpleName())
.append(Constants.CLOSE_SQUARE_BRACKET).append(Constants.EMPTY_SPACE).append(e.getMessage());
}
Expand Down Expand Up @@ -283,15 +285,15 @@ private void validateRootMets() {
metsValidatorState.setMetsName(ipPath);
metsValidatorState.setIsRootMets(true);

metsValidatorState.setMets(metsRoot.instatiateMetsFile());
metsValidatorState.setMets(metsRoot.instatiateMetsFile(Constants.METS_FILE));
metsValidatorState.setIpType(metsValidatorState.getMets().getMetsHdr().getOAISPACKAGETYPE());
validateComponents();
} catch (IOException | JAXBException | SAXException e) {
} catch (IOException | UnmarshallerException e) {
final String message = createExceptionMessage(e,
earksipPath.toString() + Constants.SEPARATOR + Constants.METS_FILE);
final ReporterDetails csipStr0 = new ReporterDetails(Constants.VALIDATION_REPORT_HEADER_CSIP_VERSION, message,
false, false);
csipStr0.setSpecification(Constants.VALIDATION_REPORT_HEADER_CSIP_VERSION);
csipStr0.setSpecification(Constants.VALIDATION_REPORT_HEADER_CSIP_VERSION + version);
ResultsUtils.addResult(validationReportOutputJson.getResults(),
ConstantsCSIPspec.VALIDATION_REPORT_SPECIFICATION_CSIP0_ID, csipStr0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.roda_project.commons_ip2.cli.model.exception.UnmarshallerException;
import org.roda_project.commons_ip2.mets_v1_12.beans.Mets;
import org.roda_project.commons_ip2.model.IPConstants;
import org.roda_project.commons_ip2.utils.METSUtils;
Expand All @@ -27,7 +28,7 @@ public class InstatiateMets {

/**
* Constructor that sets the {@link InputStream}.
*
*
* @param stream
* {@link InputStream}.
*/
Expand All @@ -39,22 +40,25 @@ public InstatiateMets(final InputStream stream) {
* Creates the {@link Mets} object from METS file.
*
* @return the {@link Mets} object.
* @throws JAXBException
* if some schema error occurs.
* @throws SAXException
* if some parse error occurs.
* @throws UnmarshallerException
* if some schema or parse error occurs.
*/
public Mets instatiateMetsFile() throws JAXBException, SAXException {
org.glassfish.jaxb.runtime.v2.JAXBContextFactory contextFactory = new org.glassfish.jaxb.runtime.v2.JAXBContextFactory();
JAXBContext jaxbContext = contextFactory.createContext(new Class[]{Mets.class}, null);
final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setResourceResolver(new ResourceResolver());
final InputStream metsSchemaInputStream = METSUtils.class
.getResourceAsStream(IPConstants.SCHEMA_METS_RELATIVE_PATH_FROM_RESOURCES);
final Source metsSchemaSource = new StreamSource(metsSchemaInputStream);
final Schema schema = factory.newSchema(metsSchemaSource);
jaxbUnmarshaller.setSchema(schema);
return (Mets) jaxbUnmarshaller.unmarshal(stream);
public Mets instatiateMetsFile(String file) throws UnmarshallerException {
try {
org.glassfish.jaxb.runtime.v2.JAXBContextFactory contextFactory = new org.glassfish.jaxb.runtime.v2.JAXBContextFactory();
JAXBContext jaxbContext = contextFactory.createContext(new Class[] {Mets.class}, null);
final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setResourceResolver(new ResourceResolver());
final InputStream metsSchemaInputStream = METSUtils.class
.getResourceAsStream(IPConstants.SCHEMA_METS_RELATIVE_PATH_FROM_RESOURCES);
final Source metsSchemaSource = new StreamSource(metsSchemaInputStream);
final Schema schema = factory.newSchema(metsSchemaSource);
jaxbUnmarshaller.setSchema(schema);
return (Mets) jaxbUnmarshaller.unmarshal(stream);
} catch (JAXBException | SAXException e) {
throw new UnmarshallerException("An error occured during the unmarshalling process on file " + file + ". "
+ (e.getMessage() != null ? e.getMessage() : e.getCause()));
}
}
}

0 comments on commit 10360ec

Please sign in to comment.