Skip to content

Commit

Permalink
Option to add multiple schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioG70 authored and hmiguim committed Aug 4, 2023
1 parent 5e41e28 commit 995efc9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ To create a EARK-2 SIP have to use the following options:

* **create**, [REQUIRED] this option is for the CLI to know that is to perform the creation of a EARK-2 SIP.
* **-m** or **-metadata** [OPTIONAL] Path to a metadata file; [OPTIONAL] the type of the metadata; [OPTIONAL] the version of the metadata
* **-ms** or **metadata-schema**, [OPTIONAL] Path to the metadata schema file.

**NOTE:** if does not give the metadata type and the metadata version, the tool try to obtain this values from the file
name in the following formats (file: **ead.xml** -> result: metadata type: **EAD**; file: **ead_2002.xml** -> result:
Expand All @@ -70,15 +71,15 @@ Examples:
### Full create SIP command with long options:

```
java -jar commons-ip-cli-2.X.Y.jar create --metadata "metadata.xml;ead;2002" \
java -jar commons-ip-cli-2.X.Y.jar create --metadata "metadata.xml;ead;2002" --metadata-schema ead2002.xsd \
--representation "dataFile1.pdf,dataFolder1,dataFile2.png;Mixed;representation1" \
--sip-id sip1 --ancestors sip2 sip3 --documentation documentation1 documentationFolder -p folder2 --path folder2 --submitter-agent-name agent1 --submitter-agent-id 123
```

### Full create SIP command with short options:

```
java -jar commons-ip-cli-2.X.Y.jar create -m "metadata.xml;ead;2002" \
java -jar commons-ip-cli-2.X.Y.jar create -m "metadata.xml;ead;2002" -ms ead2002.xsd \
-r "dataFile1.pdf,dataFolder1,dataFile2.png;Mixed;representation1" \
-sid sip1 -a sip2 sip3 -doc documentation1 documentationFolder -p folder2 -sn agent1 -aid 123
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public final class CLIConstants {
*/
public static final String CLI_CREATE_OPTION_METADATA_VERSION = "-mv";

/**
* Short option metadata schema file.
*/
public static final String CLI_CREATE_OPTION_METADATA_SCHEMA = "-ms";

/**
* CLI option to add data to the representation.
*/
Expand Down Expand Up @@ -188,6 +193,16 @@ public final class CLIConstants {
*/
public static final String CLI_CREATE_LONG_OPTION_REPRESENTATION_DATA_WITHOUT_IDENT = "representation-data";

/**
* Long option metadata version without ident.
*/
public static final String CLI_CREATE_LONG_OPTION_METADATA_SCHEMA_WITHOUT_IDENT = "metadata-schema";

/**
* Short option metadata schema file without ident.
*/
public static final String CLI_CREATE_SHORT_OPTION_METADATA_SCHEMA_WITHOUT_IDENT = "ms";

/**
* Short option representation data without ident.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public CLICreator() {
metadataVersion.setRequired(false);
parameters.addOption(metadataVersion);

final Option metadataSchema = new Option(CLIConstants.CLI_CREATE_SHORT_OPTION_METADATA_SCHEMA_WITHOUT_IDENT,
CLIConstants.CLI_CREATE_LONG_OPTION_METADATA_SCHEMA_WITHOUT_IDENT, true, "Metadata Schema");
metadataSchema.setArgs(Option.UNLIMITED_VALUES);
metadataSchema.setRequired(false);
parameters.addOption(metadataSchema);

final Option representationData = new Option(CLIConstants.CLI_CREATE_SHORT_OPTION_REPRESENTATION_DATA_WITHOUT_IDENT,
CLIConstants.CLI_CREATE_LONG_OPTION_REPRESENTATION_DATA_WITHOUT_IDENT, true, "Representation Data");
representationData.setArgs(Option.UNLIMITED_VALUES);
Expand Down Expand Up @@ -182,6 +188,10 @@ public int start(final String[] args) {
.getOptionValue(CLIConstants.CLI_CREATE_LONG_OPTION_CHECKSUM_ALG) == null
? commandLine.getOptionValue(CLIConstants.CLI_CREATE_SHORT_OPTION_CHECKSUM_ALG)
: commandLine.getOptionValue(CLIConstants.CLI_CREATE_LONG_OPTION_CHECKSUM_ALG);
final String[] metadataSchema = commandLine
.getOptionValues(CLIConstants.CLI_CREATE_LONG_OPTION_METADATA_SCHEMA_WITHOUT_IDENT) == null
? commandLine.getOptionValues(CLIConstants.CLI_CREATE_SHORT_OPTION_METADATA_SCHEMA_WITHOUT_IDENT)
: commandLine.getOptionValues(CLIConstants.CLI_CREATE_LONG_OPTION_METADATA_SCHEMA_WITHOUT_IDENT);
if (!SipCreatorUtils.validateAllOptions(metadata, documentation, representation)) {
CLIUtils.printErrors(System.out,
"You have to add at least one metadata file or documentation file or representation data file");
Expand Down Expand Up @@ -212,7 +222,7 @@ public int start(final String[] args) {

try {
final Path eark2SIP = SipCreatorUtils.createEARK2SIP(metadata,
representation, targetOnly, sipID, ancestors, documentation,
representation, metadataSchema, targetOnly, sipID, ancestors, documentation,
getClass().getPackage().getImplementationVersion(), path, submitterAgentName, submitterAgentID, checkSum);
System.out.println("Created the sip in " + eark2SIP.normalize().toAbsolutePath());
} catch (IPException | InterruptedException e) {
Expand Down Expand Up @@ -250,6 +260,9 @@ private static void printUsageCreate(final PrintStream printStream) {
out.append(CLIConstants.TAB).append(CLIConstants.CLI_CREATE_OPTION_METADATA_VERSION).append(", --metadata-version")
.append(CLIConstants.DOUBLE_TAB).append("(optional) The version of the metadata")
.append(CLIConstants.END_OF_LINE);
out.append(CLIConstants.TAB).append(CLIConstants.CLI_CREATE_OPTION_METADATA_SCHEMA).append(", --metadata-schema")
.append(CLIConstants.DOUBLE_TAB).append("(optional) Path to the metadata schema file")
.append(CLIConstants.END_OF_LINE);
out.append(CLIConstants.TAB).append(CLIConstants.CLI_CREATE_OPTION_REPRESENTATION_DATA)
.append(", --representation-data").append(CLIConstants.DOUBLE_TAB)
.append("(optional) Paths to the representation data").append(CLIConstants.END_OF_LINE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package org.roda_project.commons_ip2.validator.utils;

import org.roda_project.commons_ip.utils.IPException;
import org.roda_project.commons_ip2.model.IPContentInformationType;
import org.roda_project.commons_ip2.model.IPContentType;
import org.roda_project.commons_ip2.model.IPDescriptiveMetadata;
import org.roda_project.commons_ip2.model.IPFile;
import org.roda_project.commons_ip2.model.IPRepresentation;
import org.roda_project.commons_ip2.model.MetadataType;
import org.roda_project.commons_ip2.model.SIP;
import org.roda_project.commons_ip2.model.impl.eark.EARKSIP;
import org.roda_project.commons_ip2.utils.Utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.security.MessageDigest;

import org.roda_project.commons_ip.utils.IPException;
import org.roda_project.commons_ip2.model.IPContentInformationType;
import org.roda_project.commons_ip2.model.IPContentType;
import org.roda_project.commons_ip2.model.IPDescriptiveMetadata;
import org.roda_project.commons_ip2.model.IPFile;
import org.roda_project.commons_ip2.model.IPRepresentation;
import org.roda_project.commons_ip2.model.MetadataType;
import org.roda_project.commons_ip2.model.SIP;
import org.roda_project.commons_ip2.model.impl.eark.EARKSIP;
import org.roda_project.commons_ip2.utils.Utils;

/**
* {@author João Gomes <jgomes@keep.pt>}.
Expand All @@ -45,12 +45,8 @@ private SipCreatorUtils() {
/**
* Validates the metadata options.
*
* @param metadataFile
* the metadata file arg.
* @param metadataType
* the metadata type arg.
* @param metadataVersion
* the metadata version arg.
* @param metadataInfoArray
* contains the metadata file arg, type arg and version arg.
* @return a flag if is valid or not.
*/
public static boolean validateMetadataOptions(final String[] metadataInfoArray) {
Expand All @@ -69,10 +65,8 @@ public static boolean validateMetadataOptions(final String[] metadataInfoArray)
*
* @param representationData
* the paths to the representation data.
* @param representationType
* the representation type.
* @param representationID
* the representation id.
* @param representationInfo
* contains the representation type and id.
* @return flag if is valid or not.
*/
public static boolean validateRepresentationOptions(final String[] representationData,
Expand Down Expand Up @@ -165,6 +159,7 @@ public static boolean validateChecksumAlg(final String checksumAlg) {
*
* @param metadata the path to the metadata file, its type and version.
* @param representation the paths to the representation data files or folders, the type of content in representation and the representation id.
* @param metadataSchema the path to the metadata schema file.
* @param sipID the SIP id.
* @param ancestors the ancestors of the SIP.
* @param documentation the paths to the documentation files or folders.
Expand All @@ -177,9 +172,9 @@ public static boolean validateChecksumAlg(final String checksumAlg) {
* @throws InterruptedException if some error occur.
*/
public static Path createEARK2SIP(final String[] metadata,
final String[] representation, final boolean targetOnly,
final String sipID, final String[] ancestors, final String[] documentation, final String softwareVersion,
final String path, final String submitterAgentName, final String submitterAgentID, final String checksum)
final String[] representation, String[] metadataSchema, final boolean targetOnly,
final String sipID, final String[] ancestors, final String[] documentation, final String softwareVersion,
final String path, final String submitterAgentName, final String submitterAgentID, final String checksum)
throws IPException, InterruptedException {
String id = sipID;
if (id == null) {
Expand Down Expand Up @@ -214,6 +209,11 @@ public static Path createEARK2SIP(final String[] metadata,
CLIUtils.printErrors(System.out, "Cannot add metadata to the SIP.");
}
}
if (metadataSchema != null){
for(String schema : metadataSchema){
sip.addSchema(new IPFile(Paths.get(schema)));
}
}

if (representation != null) {
try {
Expand Down

0 comments on commit 995efc9

Please sign in to comment.