diff --git a/src/main/java/org/codehaus/plexus/util/xml/Xpp3Dom.java b/src/main/java/org/codehaus/plexus/util/xml/Xpp3Dom.java index 0e8ddfe9..2dcd2f01 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/Xpp3Dom.java +++ b/src/main/java/org/codehaus/plexus/util/xml/Xpp3Dom.java @@ -24,7 +24,7 @@ import java.util.Map; import org.apache.maven.api.xml.XmlNode; -import org.apache.maven.internal.xml.XmlNodeImpl; +import org.apache.maven.api.xml.XmlService; import org.codehaus.plexus.util.xml.pull.XmlSerializer; /** @@ -33,26 +33,26 @@ public class Xpp3Dom implements Serializable { private static final String[] EMPTY_STRING_ARRAY = new String[0]; - public static final String CHILDREN_COMBINATION_MODE_ATTRIBUTE = XmlNode.CHILDREN_COMBINATION_MODE_ATTRIBUTE; + public static final String CHILDREN_COMBINATION_MODE_ATTRIBUTE = XmlService.CHILDREN_COMBINATION_MODE_ATTRIBUTE; - public static final String CHILDREN_COMBINATION_MERGE = XmlNode.CHILDREN_COMBINATION_MERGE; + public static final String CHILDREN_COMBINATION_MERGE = XmlService.CHILDREN_COMBINATION_MERGE; - public static final String CHILDREN_COMBINATION_APPEND = XmlNode.CHILDREN_COMBINATION_APPEND; + public static final String CHILDREN_COMBINATION_APPEND = XmlService.CHILDREN_COMBINATION_APPEND; /** * This default mode for combining children DOMs during merge means that where element names match, the process will * try to merge the element data, rather than putting the dominant and recessive elements (which share the same * element name) as siblings in the resulting DOM. */ - public static final String DEFAULT_CHILDREN_COMBINATION_MODE = XmlNode.DEFAULT_CHILDREN_COMBINATION_MODE; + public static final String DEFAULT_CHILDREN_COMBINATION_MODE = XmlService.DEFAULT_CHILDREN_COMBINATION_MODE; - public static final String SELF_COMBINATION_MODE_ATTRIBUTE = XmlNode.SELF_COMBINATION_MODE_ATTRIBUTE; + public static final String SELF_COMBINATION_MODE_ATTRIBUTE = XmlService.SELF_COMBINATION_MODE_ATTRIBUTE; - public static final String SELF_COMBINATION_OVERRIDE = XmlNode.SELF_COMBINATION_OVERRIDE; + public static final String SELF_COMBINATION_OVERRIDE = XmlService.SELF_COMBINATION_OVERRIDE; - public static final String SELF_COMBINATION_MERGE = XmlNode.SELF_COMBINATION_MERGE; + public static final String SELF_COMBINATION_MERGE = XmlService.SELF_COMBINATION_MERGE; - public static final String SELF_COMBINATION_REMOVE = XmlNode.SELF_COMBINATION_REMOVE; + public static final String SELF_COMBINATION_REMOVE = XmlService.SELF_COMBINATION_REMOVE; /** * This default mode for combining a DOM node during merge means that where element names match, the process will @@ -60,17 +60,17 @@ public class Xpp3Dom implements Serializable { * dominant one. This means that wherever the dominant element doesn't provide the value or a particular attribute, * that value or attribute will be set from the recessive DOM node. */ - public static final String DEFAULT_SELF_COMBINATION_MODE = XmlNode.DEFAULT_SELF_COMBINATION_MODE; + public static final String DEFAULT_SELF_COMBINATION_MODE = XmlService.DEFAULT_SELF_COMBINATION_MODE; - public static final String ID_COMBINATION_MODE_ATTRIBUTE = XmlNode.ID_COMBINATION_MODE_ATTRIBUTE; + public static final String ID_COMBINATION_MODE_ATTRIBUTE = XmlService.ID_COMBINATION_MODE_ATTRIBUTE; - public static final String KEYS_COMBINATION_MODE_ATTRIBUTE = XmlNode.KEYS_COMBINATION_MODE_ATTRIBUTE; + public static final String KEYS_COMBINATION_MODE_ATTRIBUTE = XmlService.KEYS_COMBINATION_MODE_ATTRIBUTE; private ChildrenTracking childrenTracking; private XmlNode dom; public Xpp3Dom(String name) { - this.dom = new XmlNodeImpl(name); + this.dom = XmlNode.newBuilder().name(name).build(); } /** @@ -79,7 +79,7 @@ public Xpp3Dom(String name) { * @param name The name of the Dom. */ public Xpp3Dom(String name, Object inputLocation) { - this.dom = new XmlNodeImpl(name, null, null, null, inputLocation); + this.dom = XmlNode.newBuilder().name(name).inputLocation(inputLocation).build(); } /** @@ -96,7 +96,8 @@ public Xpp3Dom(Xpp3Dom src) { * @param name The name of the Dom. */ public Xpp3Dom(Xpp3Dom src, String name) { - this.dom = new XmlNodeImpl(src.dom, name); + this.dom = XmlNode.newInstance( + name, src.dom.value(), src.dom.attributes(), src.dom.children(), src.dom.inputLocation()); } public Xpp3Dom(XmlNode dom) { @@ -122,7 +123,7 @@ public XmlNode getDom() { // ---------------------------------------------------------------------- public String getName() { - return dom.getName(); + return dom.name(); } // ---------------------------------------------------------------------- @@ -130,11 +131,11 @@ public String getName() { // ---------------------------------------------------------------------- public String getValue() { - return dom.getValue(); + return dom.value(); } public void setValue(String value) { - update(new XmlNodeImpl(dom.getName(), value, dom.getAttributes(), dom.getChildren(), dom.getInputLocation())); + update(XmlNode.newInstance(dom.name(), value, dom.attributes(), dom.children(), dom.inputLocation())); } // ---------------------------------------------------------------------- @@ -142,11 +143,11 @@ public void setValue(String value) { // ---------------------------------------------------------------------- public String[] getAttributeNames() { - return dom.getAttributes().keySet().toArray(EMPTY_STRING_ARRAY); + return dom.attributes().keySet().toArray(EMPTY_STRING_ARRAY); } public String getAttribute(String name) { - return dom.getAttribute(name); + return dom.attribute(name); } /** @@ -157,11 +158,10 @@ public String getAttribute(String name) { */ public boolean removeAttribute(String name) { if (name != null && !name.isEmpty()) { - Map attrs = new HashMap<>(dom.getAttributes()); + Map attrs = new HashMap<>(dom.attributes()); boolean ret = attrs.remove(name) != null; if (ret) { - update(new XmlNodeImpl( - dom.getName(), dom.getValue(), attrs, dom.getChildren(), dom.getInputLocation())); + update(XmlNode.newInstance(dom.name(), dom.value(), attrs, dom.children(), dom.inputLocation())); } return ret; } @@ -181,9 +181,9 @@ public void setAttribute(String name, String value) { if (null == name) { throw new NullPointerException("Attribute name can not be null"); } - Map attrs = new HashMap<>(dom.getAttributes()); + Map attrs = new HashMap<>(dom.attributes()); attrs.put(name, value); - update(new XmlNodeImpl(dom.getName(), dom.getValue(), attrs, dom.getChildren(), dom.getInputLocation())); + update(XmlNode.newInstance(dom.name(), dom.value(), attrs, dom.children(), dom.inputLocation())); } // ---------------------------------------------------------------------- @@ -191,46 +191,46 @@ public void setAttribute(String name, String value) { // ---------------------------------------------------------------------- public Xpp3Dom getChild(int i) { - return new Xpp3Dom(dom.getChildren().get(i), this); + return new Xpp3Dom(dom.children().get(i), this); } public Xpp3Dom getChild(String name) { - XmlNode child = dom.getChild(name); + XmlNode child = dom.child(name); return child != null ? new Xpp3Dom(child, this) : null; } public void addChild(Xpp3Dom xpp3Dom) { - List children = new ArrayList<>(dom.getChildren()); + List children = new ArrayList<>(dom.children()); children.add(xpp3Dom.dom); xpp3Dom.childrenTracking = this::replace; - update(new XmlNodeImpl(dom.getName(), dom.getValue(), dom.getAttributes(), children, dom.getInputLocation())); + update(XmlNode.newInstance(dom.name(), dom.value(), dom.attributes(), children, dom.inputLocation())); } public Xpp3Dom[] getChildren() { - return dom.getChildren().stream().map(d -> new Xpp3Dom(d, this)).toArray(Xpp3Dom[]::new); + return dom.children().stream().map(d -> new Xpp3Dom(d, this)).toArray(Xpp3Dom[]::new); } public Xpp3Dom[] getChildren(String name) { - return dom.getChildren().stream() - .filter(c -> c.getName().equals(name)) + return dom.children().stream() + .filter(c -> c.name().equals(name)) .map(d -> new Xpp3Dom(d, this)) .toArray(Xpp3Dom[]::new); } public int getChildCount() { - return dom.getChildren().size(); + return dom.children().size(); } public void removeChild(int i) { - List children = new ArrayList<>(dom.getChildren()); + List children = new ArrayList<>(dom.children()); children.remove(i); - update(new XmlNodeImpl(dom.getName(), dom.getValue(), dom.getAttributes(), children, dom.getInputLocation())); + update(XmlNode.newInstance(dom.name(), dom.value(), dom.attributes(), children, dom.inputLocation())); } public void removeChild(Xpp3Dom child) { - List children = new ArrayList<>(dom.getChildren()); + List children = new ArrayList<>(dom.children()); children.remove(child.dom); - update(new XmlNodeImpl(dom.getName(), dom.getValue(), dom.getAttributes(), children, dom.getInputLocation())); + update(XmlNode.newInstance(dom.name(), dom.value(), dom.attributes(), children, dom.inputLocation())); } // ---------------------------------------------------------------------- @@ -252,7 +252,7 @@ public void setParent(Xpp3Dom parent) {} * @return input location */ public Object getInputLocation() { - return dom.getInputLocation(); + return dom.inputLocation(); } /** @@ -260,7 +260,7 @@ public Object getInputLocation() { * @param inputLocation input location to set */ public void setInputLocation(Object inputLocation) { - update(new XmlNodeImpl(dom.getName(), dom.getValue(), dom.getAttributes(), dom.getChildren(), inputLocation)); + update(XmlNode.newInstance(dom.name(), dom.value(), dom.attributes(), dom.children(), inputLocation)); } // ---------------------------------------------------------------------- @@ -319,7 +319,7 @@ private static void mergeIntoXpp3Dom(Xpp3Dom dominant, Xpp3Dom recessive, Boolea if (recessive == null) { return; } - dominant.dom = dominant.dom.merge(recessive.dom, childMergeOverride); + dominant.dom = XmlService.merge(dominant.dom, recessive.dom, childMergeOverride); } /** @@ -407,9 +407,9 @@ private void update(XmlNode dom) { } private boolean replace(Object prevChild, Object newChild) { - List children = new ArrayList<>(dom.getChildren()); + List children = new ArrayList<>(dom.children()); children.replaceAll(d -> d == prevChild ? (XmlNode) newChild : d); - update(new XmlNodeImpl(dom.getName(), dom.getValue(), dom.getAttributes(), children, dom.getInputLocation())); + update(XmlNode.newInstance(dom.name(), dom.value(), dom.attributes(), children, dom.inputLocation())); return true; } diff --git a/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java b/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java index b75e681f..cdea1722 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java +++ b/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java @@ -49,7 +49,7 @@ public static Xpp3Dom build(InputStream is, String encoding) throws XmlPullParse public static Xpp3Dom build(InputStream is, String encoding, boolean trim) throws XmlPullParserException, IOException { try (InputStream closeMe = is) { - return new Xpp3Dom(XmlNodeBuilder.build(is, encoding, trim)); + return buildWithDeprecatedTrim(is, encoding, trim); } } @@ -63,8 +63,7 @@ public static Xpp3Dom build(Reader reader, boolean trim) throws XmlPullParserExc public static Xpp3Dom build(Reader reader, boolean trim, InputLocationBuilder locationBuilder) throws XmlPullParserException, IOException { try (Reader closeMe = reader) { - return new Xpp3Dom(XmlNodeBuilder.build( - reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null)); + return buildWithDeprecatedTrim(reader, trim, locationBuilder); } } @@ -81,8 +80,7 @@ public static Xpp3Dom build(XmlPullParser parser, boolean trim) throws XmlPullPa */ public static Xpp3Dom build(XmlPullParser parser, boolean trim, InputLocationBuilder locationBuilder) throws XmlPullParserException, IOException { - return new Xpp3Dom( - XmlNodeBuilder.build(parser, trim, locationBuilder != null ? locationBuilder::toInputLocation : null)); + return buildWithDeprecatedTrim(parser, trim, locationBuilder); } /** @@ -93,4 +91,25 @@ public static Xpp3Dom build(XmlPullParser parser, boolean trim, InputLocationBui public interface InputLocationBuilder { Object toInputLocation(XmlPullParser parser); } + + @SuppressWarnings("deprecation") + private static Xpp3Dom buildWithDeprecatedTrim(Reader reader, boolean trim, InputLocationBuilder locationBuilder) + throws XmlPullParserException, IOException { + return new Xpp3Dom( + XmlNodeBuilder.build(reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null)); + } + + @SuppressWarnings("deprecation") + private static Xpp3Dom buildWithDeprecatedTrim( + XmlPullParser parser, boolean trim, InputLocationBuilder locationBuilder) + throws XmlPullParserException, IOException { + return new Xpp3Dom( + XmlNodeBuilder.build(parser, trim, locationBuilder != null ? locationBuilder::toInputLocation : null)); + } + + @SuppressWarnings("deprecation") + private static Xpp3Dom buildWithDeprecatedTrim(InputStream is, String encoding, boolean trim) + throws XmlPullParserException, IOException { + return new Xpp3Dom(XmlNodeBuilder.build(is, encoding, trim)); + } } diff --git a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java index 44999651..ff5abd2b 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java @@ -23,7 +23,6 @@ import java.util.Map; import org.apache.maven.api.xml.XmlNode; -import org.apache.maven.internal.xml.XmlNodeImpl; import org.codehaus.plexus.util.xml.pull.XmlPullParser; import org.junit.jupiter.api.Test; @@ -227,7 +226,7 @@ void equalsIsNullSafe() throws Exception { attributes.put(null, "nullKey"); List childList = new ArrayList<>(); childList.add(null); - Xpp3Dom dom2 = new Xpp3Dom(new XmlNodeImpl(dom.getName(), null, attributes, childList, null)); + Xpp3Dom dom2 = new Xpp3Dom(XmlNode.newInstance(dom.getName(), null, attributes, childList, null)); assertNotEquals(dom, dom2); assertNotEquals(dom2, dom);