Skip to content

Commit

Permalink
fixed issue with Attribute keys and source locations
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Nov 28, 2023
1 parent d4db728 commit 9316459
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/org/rascalmpl/library/lang/xml/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,12 @@ else if (a.getKey().equals("xmlns")) {
// remove all the namespace attributes
return !a.getKey().startsWith("xmlns");
})
.map(a -> removeNamespace(a, elem.attributes(), fullyQualify))
.collect(Collectors.toMap(a -> normalizeAttr(a.getKey()), a -> vf.string(a.getValue())));
.collect(Collectors.toMap(a -> normalizeAttr(a.getKey()), a -> vf.string(removeNamespace(a, elem.attributes(), fullyQualify))));

// we traverse again to record the source positions of each attribute
IMap Srcs = file != null ? StreamSupport.stream(elem.attributes().spliterator(), false)
.filter(a -> !a.getKey().startsWith("xmlns"))
// .map(a -> removeNamespace(a, elem.attributes(), fullyQualify))
.map(a -> vf.tuple(vf.string(normalizeAttr(removeNamespace(a, elem.attributes(), fullyQualify).getKey())), attrToLoc(a, file))) // key-value tuples for the map collector
.map(a -> vf.tuple(vf.string(normalizeAttr(removeNamespace(a, elem.attributes(), fullyQualify))), attrToLoc(a, file))) // key-value tuples for the map collector
.filter(t -> !t.get(1).equals(vf.tuple())) // remove the failed location lookups for robustness sake
.collect(vf.mapWriter())
: vf.map()
Expand Down Expand Up @@ -201,28 +199,19 @@ private static String removeNamespace(String name, boolean fullyQualify) {
return name.substring(index+1);

}
private static Attribute removeNamespace(Attribute a, Attributes otherAttributes, boolean fullyQualify) {
private static String removeNamespace(Attribute a, Attributes otherAttributes, boolean fullyQualify) {
if (fullyQualify) {
return a;
return a.getKey();
}

String key = a.getKey();
int index = key.indexOf(":");

if (index == -1) {
return a;
return a.getKey();
}

String newKey = key.substring(index+1);

if (otherAttributes.hasKey(newKey)) {
// keep disambiguation if necessary
return a;
}

Attribute newVersion = a.clone();
newVersion.setKey(newKey);
return newVersion;
return key.substring(index+1);
}

private ITuple attrToLoc(Attribute a, ISourceLocation file) {
Expand Down

0 comments on commit 9316459

Please sign in to comment.