-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partial support for parsing XML namespaces #3925
Changes from 1 commit
512d05e
7b65a68
9d4c924
520a282
1ec44eb
f5a3621
aecc3e6
385cb7b
c88e2ae
546b99f
5d2e2b8
e0e9527
436049d
f621398
218827b
a456198
b283aa0
5c76231
d1cffc1
acb7c26
6fec0b5
bf3b658
ac084a1
326b824
50dc26f
20e6212
69d6772
527a239
2a0212b
cd2fcf3
c7f84cc
e4083b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
package org.openrewrite.xml.internal; | ||
|
||
import org.openrewrite.Cursor; | ||
import org.openrewrite.internal.lang.NonNull; | ||
import org.openrewrite.xml.tree.Xml; | ||
|
||
import java.util.Collection; | ||
|
@@ -25,42 +24,38 @@ | |
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class XmlUtils { | ||
public class XmlNamespaceUtils { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of a separate utils class, could this be methods on Namespaces instead you think? |
||
|
||
private XmlUtils() { | ||
private XmlNamespaceUtils() { | ||
} | ||
|
||
private static boolean isNamespaceAttribute(String name) { | ||
return name.startsWith("xmlns"); | ||
} | ||
|
||
@NonNull | ||
public static String getAttributeNameFor(String namespacePrefix) { | ||
return namespacePrefix.isEmpty() ? "xmlns" : "xmlns:" + namespacePrefix; | ||
} | ||
|
||
@NonNull | ||
public static String extractNamespacePrefix(String name) { | ||
if (!isNamespaceAttribute("xmlns")) { | ||
if (!isNamespaceAttribute(name)) { | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new IllegalArgumentException("Namespace attribute names must start with \"xmlns\"."); | ||
} | ||
int colon = name.indexOf(':'); | ||
return colon == -1 ? "" : name.substring(colon + 1); | ||
} | ||
|
||
@NonNull | ||
public static Map<String, String> extractNamespaces(Collection<Xml.Attribute> attributes) { | ||
return attributes.isEmpty() | ||
? Collections.emptyMap() | ||
: attributes.stream() | ||
.filter(attribute -> isNamespaceAttribute(attribute.getKeyAsString())) | ||
.collect(Collectors.toMap( | ||
attribute -> extractNamespacePrefix(attribute.getKeyAsString()), | ||
attribute -> attribute.getValue().getValue() | ||
)); | ||
.filter(attribute -> isNamespaceAttribute(attribute.getKeyAsString())) | ||
.collect(Collectors.toMap( | ||
attribute -> extractNamespacePrefix(attribute.getKeyAsString()), | ||
attribute -> attribute.getValue().getValue() | ||
)); | ||
} | ||
|
||
@NonNull | ||
public static Optional<String> findNamespacePrefix(Cursor cursor, String namespacePrefix) { | ||
String resolvedNamespace = null; | ||
while (cursor != null) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,8 @@ | |
import org.openrewrite.xml.XmlParser; | ||
import org.openrewrite.xml.XmlVisitor; | ||
import org.openrewrite.xml.internal.WithPrefix; | ||
import org.openrewrite.xml.internal.XmlNamespaceUtils; | ||
import org.openrewrite.xml.internal.XmlPrinter; | ||
import org.openrewrite.xml.internal.XmlUtils; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
|
@@ -162,7 +162,7 @@ public Document withRoot(Tag root) { | |
if (this.root == root) { | ||
return this; | ||
} | ||
Map<String, String> namespaces = XmlUtils.extractNamespaces(root.getAttributes()); | ||
Map<String, String> namespaces = XmlNamespaceUtils.extractNamespaces(root.getAttributes()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of extracting the namespaces here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we need this back in to later support namespace lookup for particular elements? For example, using the test file, and wanting to look for the XPath
|
||
return new Document(id, sourcePath, prefixUnsafe, markers, charsetName, charsetBomMarked, checksum, fileAttributes, prolog, root, eof); | ||
} | ||
|
||
|
@@ -308,14 +308,14 @@ class Tag implements Xml, Content { | |
* @return a map of namespace prefixes (without the <code>xmlns</code> prefix) to URIs for this tag. | ||
*/ | ||
public Map<String, String> getNamespaces() { | ||
return XmlUtils.extractNamespaces(attributes); | ||
return XmlNamespaceUtils.extractNamespaces(attributes); | ||
} | ||
|
||
public Tag withNamespaces(Map<String, String> namespaces) { | ||
List<Xml.Attribute> attributes = this.attributes; | ||
if (attributes.isEmpty()) { | ||
for (Map.Entry<String, String> ns : namespaces.entrySet()) { | ||
String key = XmlUtils.getAttributeNameFor(ns.getKey()); | ||
String key = XmlNamespaceUtils.getAttributeNameFor(ns.getKey()); | ||
attributes = ListUtils.concat(attributes, new Xml.Attribute( | ||
randomId(), | ||
"", | ||
|
@@ -343,7 +343,7 @@ public Tag withNamespaces(Map<String, String> namespaces) { | |
)); | ||
|
||
for (Map.Entry<String, String> ns : namespaces.entrySet()) { | ||
String key = XmlUtils.getAttributeNameFor(ns.getKey()); | ||
String key = XmlNamespaceUtils.getAttributeNameFor(ns.getKey()); | ||
if (attributeByKey.containsKey(key)) { | ||
Xml.Attribute attribute = attributeByKey.get(key); | ||
if (!ns.getValue().equals(attribute.getValueAsString())) { | ||
|
@@ -551,7 +551,7 @@ public Tag withContent(@Nullable List<? extends Content> content) { | |
* @return The namespace prefix for this tag, if any. | ||
*/ | ||
public Optional<String> getNamespacePrefix() { | ||
String namespacePrefix = XmlUtils.extractNamespacePrefix(name); | ||
String namespacePrefix = XmlNamespaceUtils.extractNamespacePrefix(name); | ||
return Optional.ofNullable(namespacePrefix.isEmpty() ? null : namespacePrefix); | ||
} | ||
|
||
|
@@ -565,12 +565,12 @@ public Optional<String> getNamespaceUri(Cursor cursor) { | |
} | ||
|
||
String namespacePrefix = maybeNamespacePrefix.get(); | ||
Map<String, String> namespaces = XmlUtils.extractNamespaces(attributes); | ||
Map<String, String> namespaces = XmlNamespaceUtils.extractNamespaces(attributes); | ||
if (namespaces.containsKey(namespacePrefix)) { | ||
return Optional.of(namespaces.get(namespacePrefix)); | ||
} | ||
|
||
return XmlUtils.findNamespacePrefix(cursor, namespacePrefix); | ||
return XmlNamespaceUtils.findNamespacePrefix(cursor, namespacePrefix); | ||
} | ||
|
||
@Override | ||
|
@@ -642,7 +642,7 @@ public String getPrefix() { | |
* @return The namespace prefix for this attribute, if any. | ||
*/ | ||
public Optional<String> getNamespacePrefix() { | ||
String namespacePrefix = XmlUtils.extractNamespacePrefix(key.getName()); | ||
String namespacePrefix = XmlNamespaceUtils.extractNamespacePrefix(key.getName()); | ||
return Optional.ofNullable(namespacePrefix.isEmpty() ? null : namespacePrefix); | ||
} | ||
|
||
|
@@ -651,7 +651,7 @@ public Optional<String> getNamespacePrefix() { | |
*/ | ||
public Optional<String> getNamespaceUri(Cursor cursor) { | ||
Optional<String> maybeNamespacePrefix = getNamespacePrefix(); | ||
return maybeNamespacePrefix.flatMap(s -> XmlUtils.findNamespacePrefix(cursor, s)); | ||
return maybeNamespacePrefix.flatMap(s -> XmlNamespaceUtils.findNamespacePrefix(cursor, s)); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could be methods on the
Namespaces
class for easier discoverability.