Skip to content

Commit 957b278

Browse files
committed
v0.11.5
- Rework of method `meico.mei.Mei.resolveCopyofs()`. This version is more stable, efficient, covers more of the complicated cases, and the code is more readable.
1 parent cd66c6d commit 957b278

File tree

4 files changed

+133
-407
lines changed

4 files changed

+133
-407
lines changed

history.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33

44
#### v0.11.5
5-
- Rework of the new functionality in `meico.mei.Mei.resolveCopyofs()` from update v0.11.4. This version is more stable, efficient, covers more complicated cases, and is more readable.
5+
- Rework of method `meico.mei.Mei.resolveCopyofs()`. This version is more stable, efficient, covers more of the complicated cases, and the code is more readable.
66

77

8-
### v0.11.4
8+
#### v0.11.4
99
- Handled a series of potential NullPointerExceptions in class `meico.mei.Mei2MusicXmlConverter`.
1010
- Added method `isChildOf()` to class `meico.mei.Helper`.
1111
- Some further minor tweaks in class `meico.mei.Helper`.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package meico.mei;
2+
3+
import nu.xom.Attribute;
4+
import nu.xom.Element;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
9+
/**
10+
* This is a helper class for method Mei.resolveCopyofs(). It collects all
11+
* attributes in the specified subtree and sorts them into HashMaps depending
12+
* on whether they are IDs, copyof/sameas or other references.
13+
*/
14+
public class AttributesWithIds {
15+
private final Element root;
16+
private HashMap<Attribute, String> copyofs = new HashMap<>();
17+
private HashMap<String, Attribute> ids = new HashMap<>();
18+
private HashMap<String, ArrayList<Attribute>> references = new HashMap<>();
19+
20+
/**
21+
* constructor
22+
* @param root
23+
*/
24+
public AttributesWithIds(Element root) {
25+
assert root != null;
26+
this.root = root;
27+
28+
this.root.query("descendant-or-self::*").forEach(node -> {
29+
Element element = (Element) node;
30+
for (int a = 0; a < element.getAttributeCount(); ++a) {
31+
Attribute attribute = element.getAttribute(a);
32+
33+
if ((attribute.getType() == Attribute.Type.ID) || (attribute.getLocalName().equals("id"))) {
34+
this.ids.put(attribute.getValue(), attribute);
35+
continue;
36+
}
37+
if (attribute.getLocalName().equals("copyof") || attribute.getLocalName().equals("sameas")) {
38+
this.copyofs.put(attribute, attribute.getValue().substring(1)); // get the ID string without the #
39+
continue;
40+
}
41+
if (attribute.getValue().startsWith("#")) {
42+
String id = attribute.getValue().substring(1);
43+
ArrayList<Attribute> attList = this.references.computeIfAbsent(id, k -> new ArrayList<>());
44+
attList.add(attribute);
45+
}
46+
}
47+
});
48+
// System.out.println("copyof set size " + this.copyofs.size());
49+
// System.out.println("id set size " + this.ids.size());
50+
// System.out.println("reference set size " + this.references.size());
51+
}
52+
53+
/**
54+
* getter for the copyofs HashMap
55+
* @return
56+
*/
57+
public HashMap<Attribute, String> getCopyofs() {
58+
return this.copyofs;
59+
}
60+
61+
public void updateCopyofs() {
62+
this.copyofs = new HashMap<>();
63+
this.root.query("descendant-or-self::*").forEach(node -> {
64+
Element element = (Element) node;
65+
for (int a = 0; a < element.getAttributeCount(); ++a) {
66+
Attribute attribute = element.getAttribute(a);
67+
68+
if (attribute.getLocalName().equals("copyof") || attribute.getLocalName().equals("sameas")) {
69+
this.copyofs.put(attribute, attribute.getValue().substring(1)); // get the ID string without the #
70+
continue;
71+
}
72+
}
73+
});
74+
}
75+
76+
/**
77+
* find element by ID
78+
* @param id
79+
* @return
80+
*/
81+
public Element getElementById(String id) {
82+
Attribute attribute = this.ids.get(id);
83+
return (attribute == null) ? null : (Element) attribute.getParent();
84+
}
85+
}

src/meico/mei/ElementsByReferenceIds.java

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)