Skip to content
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

Merged
merged 32 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
512d05e
Partial support for parsing XML namespaces
ammachado Jan 16, 2024
7b65a68
Adding namespace resolution
ammachado Jan 16, 2024
9d4c924
Missing license header
ammachado Jan 16, 2024
520a282
Adding recipes to search namespace URIs/prefixes
ammachado Jan 16, 2024
1ec44eb
Namespace shortcut methods on \'Xml.Document\'
ammachado Jan 16, 2024
f5a3621
Change implementation to rely only on attributes
ammachado Jan 16, 2024
aecc3e6
Javadocs and cleanup
ammachado Jan 16, 2024
385cb7b
Rename XmlNamespaceUtils & minor polish
timtebeek Jan 16, 2024
c88e2ae
Fix namespace search on XML hierarchy
ammachado Jan 17, 2024
546b99f
`ChangeNamespaceValue` now updates the `schemaLocation` attribute
ammachado Jan 17, 2024
5d2e2b8
Merge branch 'main' into add-namespaces-xml-tree
ammachado Jan 17, 2024
e0e9527
Merge branch 'main' into add-namespaces-xml-tree
timtebeek Jan 17, 2024
436049d
Merge branch 'main' into add-namespaces-xml-tree
ammachado Jan 27, 2024
f621398
Consider namespaces on `SemanticallyEqual`.
ammachado Jan 27, 2024
218827b
Suggestions from code review.
ammachado Jan 27, 2024
a456198
Update rewrite-xml/src/main/java/org/openrewrite/xml/ChangeNamespaceV…
ammachado Jan 28, 2024
b283aa0
Revert namespace comparison changes in `SemanticallyEqual`.
ammachado Feb 2, 2024
5c76231
Merge branch 'main' into add-namespaces-xml-tree
ammachado Feb 2, 2024
d1cffc1
Merge branch 'main' into add-namespaces-xml-tree
timtebeek Feb 20, 2024
acb7c26
Merge branch 'main' into add-namespaces-xml-tree
ammachado Feb 24, 2024
6fec0b5
Merge branch 'main' into add-namespaces-xml-tree
timtebeek Feb 24, 2024
bf3b658
Adding a Namespaces abstraction
ammachado Feb 27, 2024
ac084a1
Merge branch 'main' into add-namespaces-xml-tree
evie-lau Jun 4, 2024
326b824
Add support for wildcard and local-name()
evie-lau Jun 13, 2024
50dc26f
Merge branch 'main' into add-namespaces-xml-tree
evie-lau Jun 14, 2024
20e6212
Apply suggestions from code review
timtebeek Jun 14, 2024
69d6772
Fix `Namespaces` mutability
ammachado Jun 14, 2024
527a239
Merge branch 'main' into add-namespaces-xml-tree
timtebeek Jun 14, 2024
2a0212b
Adding an iterator implementation for `Namespaces`
ammachado Jun 14, 2024
cd2fcf3
Replace `NotNull` with OpenRewrite's `NonNull`
ammachado Jun 14, 2024
c7f84cc
Merge branch 'main' into add-namespaces-xml-tree
sambsnyd Jun 20, 2024
e4083b4
Polish.
sambsnyd Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,42 +24,38 @@
import java.util.Optional;
import java.util.stream.Collectors;

public class XmlUtils {
public class XmlNamespaceUtils {
Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Expand Down
20 changes: 10 additions & 10 deletions rewrite-xml/src/main/java/org/openrewrite/xml/tree/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of extracting the namespaces here?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 /client/conduitSelector with the http://cxf.apache.org/jaxws namespace:

<jaxws:client name="{http://cxf.apache.org/hello_world_soap_http}SoapPort" createdFromAPI="true" xmlns:jaxws="http://cxf.apache.org/jaxws">
              <jaxws:conduitSelector>
                  <bean class="org.apache.cxf.endpoint.DeferredConduitSelector"/>
              </jaxws:conduitSelector>
          </jaxws:client>

return new Document(id, sourcePath, prefixUnsafe, markers, charsetName, charsetBomMarked, checksum, fileAttributes, prolog, root, eof);
}

Expand Down Expand Up @@ -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(),
"",
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
Expand Down
Loading