diff --git a/src/main/java/org/roda_project/commons_ip2/cli/model/exception/UnmarshallerException.java b/src/main/java/org/roda_project/commons_ip2/cli/model/exception/UnmarshallerException.java new file mode 100644 index 00000000..50803b27 --- /dev/null +++ b/src/main/java/org/roda_project/commons_ip2/cli/model/exception/UnmarshallerException.java @@ -0,0 +1,11 @@ +package org.roda_project.commons_ip2.cli.model.exception; + +/** + * @author Carlos Afonso + */ +public class UnmarshallerException extends Exception { + + public UnmarshallerException(String message) { + super(message); + } +} diff --git a/src/main/java/org/roda_project/commons_ip2/validator/EARKPyIPValidator.java b/src/main/java/org/roda_project/commons_ip2/validator/EARKPyIPValidator.java index ef18b7d7..5bdc26f0 100644 --- a/src/main/java/org/roda_project/commons_ip2/validator/EARKPyIPValidator.java +++ b/src/main/java/org/roda_project/commons_ip2/validator/EARKPyIPValidator.java @@ -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; @@ -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; @@ -259,10 +260,10 @@ private void validateComponents() throws IOException { * If some error occurs */ private void validateSubMets(final Map subMets, final boolean isZip) - throws IOException, JAXBException, SAXException { + throws IOException, UnmarshallerException { for (Map.Entry 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(); @@ -274,12 +275,10 @@ private void validateSubMets(final Map 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()) { @@ -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(); } diff --git a/src/main/java/org/roda_project/commons_ip2/validator/EARKSIPValidator.java b/src/main/java/org/roda_project/commons_ip2/validator/EARKSIPValidator.java index 4fe9df43..d8cfe741 100644 --- a/src/main/java/org/roda_project/commons_ip2/validator/EARKSIPValidator.java +++ b/src/main/java/org/roda_project/commons_ip2/validator/EARKSIPValidator.java @@ -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; @@ -46,8 +47,6 @@ import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; -import jakarta.xml.bind.JAXBException; - /** {@author João Gomes }. */ public class EARKSIPValidator { /** IP path. */ @@ -214,11 +213,11 @@ private void validateSubMets(final Map 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); @@ -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()); } @@ -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); } diff --git a/src/main/java/org/roda_project/commons_ip2/validator/common/InstatiateMets.java b/src/main/java/org/roda_project/commons_ip2/validator/common/InstatiateMets.java index 223a4df0..9ebb4908 100644 --- a/src/main/java/org/roda_project/commons_ip2/validator/common/InstatiateMets.java +++ b/src/main/java/org/roda_project/commons_ip2/validator/common/InstatiateMets.java @@ -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; @@ -27,7 +28,7 @@ public class InstatiateMets { /** * Constructor that sets the {@link InputStream}. - * + * * @param stream * {@link InputStream}. */ @@ -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())); + } } }