|
| 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 | +} |
0 commit comments