From 4857bb3d1442208a8fbc1e406d996c9bfc3ec9e4 Mon Sep 17 00:00:00 2001 From: axelberndt Date: Wed, 4 Nov 2020 09:18:04 +0100 Subject: [PATCH] v0.8.18 - Enhancement of class `meico.mpm.elements.metadata.Metadata` so it can be instatiated with related resources. --- history.md | 4 ++ src/meico/Meico.java | 2 +- src/meico/mei/Mei.java | 7 +-- src/meico/mpm/Mpm.java | 10 +++- src/meico/mpm/elements/metadata/Metadata.java | 46 +++++++++++++++---- 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/history.md b/history.md index 75a1c405..3578b12b 100644 --- a/history.md +++ b/history.md @@ -1,6 +1,10 @@ ### Version History +#### v0.8.18 +- Enhancement of class `meico.mpm.elements.metadata.Metadata` so it can be instatiated with related resources. + + #### v0.8.17 - Made class `meico.midi.InstrumentsDirectory` public so it can be used outside of its package. - Extended method `meico.mei.Mei.makePart()`. diff --git a/src/meico/Meico.java b/src/meico/Meico.java index 573c61e0..49059910 100644 --- a/src/meico/Meico.java +++ b/src/meico/Meico.java @@ -5,7 +5,7 @@ * @author Axel Berndt */ public class Meico { - public static final String version = "0.8.17"; + public static final String version = "0.8.18"; public static void main(String[] args) { System.out.println("meico v" + Meico.version); diff --git a/src/meico/mei/Mei.java b/src/meico/mei/Mei.java index a11dec5a..1882644e 100644 --- a/src/meico/mei/Mei.java +++ b/src/meico/mei/Mei.java @@ -869,10 +869,11 @@ private void makeMovement(Element mdiv) { Mpm mpm = Mpm.createMpm(); // generate an Mpm object if (this.file != null) { - mpm.addMetadata(Author.createAuthor("meico", null, null), "This MPM has been generated from '" + this.getFile().getName() + "' using the meico MEI converter."); - mpm.getMetadata().addRelatedResource(RelatedResource.createRelatedResource(this.file.getAbsolutePath(), "mei")); // add the mei as music reference + ArrayList relatedResources = new ArrayList<>(); + relatedResources.add(RelatedResource.createRelatedResource(this.file.getAbsolutePath(), "mei")); + mpm.addMetadata(Author.createAuthor("meico", null, null), "This MPM has been generated from '" + this.getFile().getName() + "' using the meico MEI converter.", relatedResources); } else { - mpm.addMetadata(Author.createAuthor("meico", null, null), "This MPM has been generated from MEI code using the meico MEI converter."); + mpm.addMetadata(Author.createAuthor("meico", null, null), "This MPM has been generated from MEI code using the meico MEI converter.", null); } Performance performance = Performance.createPerformance("MEI export performance"); // generate a Performance object if (performance == null) { // make sure it is not null diff --git a/src/meico/mpm/Mpm.java b/src/meico/mpm/Mpm.java index c1a99bdc..69d01e31 100644 --- a/src/meico/mpm/Mpm.java +++ b/src/meico/mpm/Mpm.java @@ -4,6 +4,7 @@ import meico.mpm.elements.metadata.Author; import meico.mpm.elements.metadata.Metadata; import meico.mpm.elements.Performance; +import meico.mpm.elements.metadata.RelatedResource; import meico.msm.AbstractMsm; import nu.xom.*; import org.xml.sax.SAXException; @@ -12,6 +13,7 @@ import java.io.*; import java.net.URL; import java.util.ArrayList; +import java.util.Collection; import java.util.LinkedList; /** @@ -187,16 +189,20 @@ private Element init() { * @param comment a string or null * @return success */ - public boolean addMetadata(Author author, String comment) { + public boolean addMetadata(Author author, String comment, Collection relatedResources) { if (this.metadata != null) { if (author != null) this.metadata.addAuthor(author); if (comment != null) this.metadata.addComment(comment); + if (relatedResources != null) { + for (RelatedResource resource : relatedResources) + this.metadata.addRelatedResource(resource); + } return true; } - this.metadata = Metadata.createMetadata(author, comment); + this.metadata = Metadata.createMetadata(author, comment, relatedResources); if (this.metadata == null) return false; diff --git a/src/meico/mpm/elements/metadata/Metadata.java b/src/meico/mpm/elements/metadata/Metadata.java index eda09469..d4a72df8 100644 --- a/src/meico/mpm/elements/metadata/Metadata.java +++ b/src/meico/mpm/elements/metadata/Metadata.java @@ -8,6 +8,7 @@ import nu.xom.Text; import java.util.ArrayList; +import java.util.Collection; import java.util.LinkedList; /** @@ -15,9 +16,9 @@ * @author Axel Berndt */ public class Metadata extends AbstractXmlSubtree { - private ArrayList authors = new ArrayList<>(); // the list of authors - private ArrayList comments = new ArrayList<>(); // the list of comments - private ArrayList relatedResources = new ArrayList<>(); // the related resources + private final ArrayList authors = new ArrayList<>(); // the list of authors + private final ArrayList comments = new ArrayList<>(); // the list of comments + private final ArrayList relatedResources = new ArrayList<>(); // the related resources /** * this constructor instantiates the Metadata object from an existing xml source handed over as XOM Element @@ -32,9 +33,10 @@ private Metadata(Element xml) throws Exception { * this constructor creates a new Metadata object from an author and/or comment * @param author an Author object or null * @param comment a String or null + * @param relatedResources * @throws Exception */ - private Metadata(Author author, String comment) throws Exception { + private Metadata(Author author, String comment, Collection relatedResources) throws Exception { Element metadata = new Element("metadata", Mpm.MPM_NAMESPACE); if (author != null) @@ -46,6 +48,13 @@ private Metadata(Author author, String comment) throws Exception { metadata.appendChild(com); } + if ((relatedResources != null) && !relatedResources.isEmpty()) { + Element relatedResourcesElt = new Element("relatedResources"); + metadata.appendChild(relatedResourcesElt); + for (RelatedResource resource : relatedResources) + relatedResourcesElt.appendChild(resource.getXml()); + } + this.parseData(metadata); } @@ -73,7 +82,7 @@ public static Metadata createMetadata(Element xml) { public static Metadata createMetadata(Author author) { Metadata metadata; try { - metadata = new Metadata(author, null); + metadata = new Metadata(author, null, null); } catch (Exception e) { e.printStackTrace(); return null; @@ -89,7 +98,23 @@ public static Metadata createMetadata(Author author) { public static Metadata createMetadata(String comment) { Metadata metadata; try { - metadata = new Metadata(null, comment); + metadata = new Metadata(null, comment, null); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + return metadata; + } + + /** + * this factory generates a Metadata object from a collection of related resources + * @param relatedResources + * @return + */ + public static Metadata createMetadata(Collection relatedResources) { + Metadata metadata; + try { + metadata = new Metadata(null, null, relatedResources); } catch (Exception e) { e.printStackTrace(); return null; @@ -101,12 +126,13 @@ public static Metadata createMetadata(String comment) { * this factory generates a Metadata object from an author and/or comment * @param author * @param comment + * @param relatedResources * @return */ - public static Metadata createMetadata(Author author, String comment) { + public static Metadata createMetadata(Author author, String comment, Collection relatedResources) { Metadata metadata; try { - metadata = new Metadata(author, comment); + metadata = new Metadata(author, comment, relatedResources); } catch (Exception e) { e.printStackTrace(); return null; @@ -151,8 +177,8 @@ protected void parseData(Element xml) throws Exception { } } - if ((this.authors.size() + this.comments.size()) == 0) - throw new Exception("Cannot generate Metadata object. It must contain at least one author or comment"); + if (((this.authors.size() + this.comments.size()) == 0) && this.relatedResources.isEmpty()) + throw new Exception("Cannot generate empty Metadata object. It must contain at least one author, comment or related resource."); } /**