Skip to content

Commit

Permalink
v0.8.18
Browse files Browse the repository at this point in the history
- Enhancement of class `meico.mpm.elements.metadata.Metadata` so it can be instatiated with related resources.
  • Loading branch information
axelberndt committed Nov 4, 2020
1 parent 6d56105 commit 4857bb3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -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()`.
Expand Down
2 changes: 1 addition & 1 deletion src/meico/Meico.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/meico/mei/Mei.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<RelatedResource> 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
Expand Down
10 changes: 8 additions & 2 deletions src/meico/mpm/Mpm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +13,7 @@
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

/**
Expand Down Expand Up @@ -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<RelatedResource> 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;

Expand Down
46 changes: 36 additions & 10 deletions src/meico/mpm/elements/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
import nu.xom.Text;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

/**
* This class interfaces the metadata of the MPM instance.
* @author Axel Berndt
*/
public class Metadata extends AbstractXmlSubtree {
private ArrayList<Author> authors = new ArrayList<>(); // the list of authors
private ArrayList<Element> comments = new ArrayList<>(); // the list of comments
private ArrayList<RelatedResource> relatedResources = new ArrayList<>(); // the related resources
private final ArrayList<Author> authors = new ArrayList<>(); // the list of authors
private final ArrayList<Element> comments = new ArrayList<>(); // the list of comments
private final ArrayList<RelatedResource> relatedResources = new ArrayList<>(); // the related resources

/**
* this constructor instantiates the Metadata object from an existing xml source handed over as XOM Element
Expand All @@ -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<RelatedResource> relatedResources) throws Exception {
Element metadata = new Element("metadata", Mpm.MPM_NAMESPACE);

if (author != null)
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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<RelatedResource> relatedResources) {
Metadata metadata;
try {
metadata = new Metadata(null, null, relatedResources);
} catch (Exception e) {
e.printStackTrace();
return null;
Expand All @@ -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<RelatedResource> relatedResources) {
Metadata metadata;
try {
metadata = new Metadata(author, comment);
metadata = new Metadata(author, comment, relatedResources);
} catch (Exception e) {
e.printStackTrace();
return null;
Expand Down Expand Up @@ -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.");
}

/**
Expand Down

0 comments on commit 4857bb3

Please sign in to comment.