Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
84 changes: 42 additions & 42 deletions src/main/java/org/codehaus/plexus/util/xml/Xpp3Dom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -33,44 +33,44 @@
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
* try to merge the element attributes and values, rather than overriding the recessive element completely with the
* 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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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) {
Expand All @@ -122,31 +123,31 @@ public XmlNode getDom() {
// ----------------------------------------------------------------------

public String getName() {
return dom.getName();
return dom.name();
}

// ----------------------------------------------------------------------
// Value handling
// ----------------------------------------------------------------------

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()));
}

// ----------------------------------------------------------------------
// Attribute handling
// ----------------------------------------------------------------------

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);
}

/**
Expand All @@ -157,11 +158,10 @@ public String getAttribute(String name) {
*/
public boolean removeAttribute(String name) {
if (name != null && !name.isEmpty()) {
Map<String, String> attrs = new HashMap<>(dom.getAttributes());
Map<String, String> 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;
}
Expand All @@ -181,56 +181,56 @@ public void setAttribute(String name, String value) {
if (null == name) {
throw new NullPointerException("Attribute name can not be null");
}
Map<String, String> attrs = new HashMap<>(dom.getAttributes());
Map<String, String> 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()));
}

// ----------------------------------------------------------------------
// Child handling
// ----------------------------------------------------------------------

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<XmlNode> children = new ArrayList<>(dom.getChildren());
List<XmlNode> 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<XmlNode> children = new ArrayList<>(dom.getChildren());
List<XmlNode> 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<XmlNode> children = new ArrayList<>(dom.getChildren());
List<XmlNode> 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()));
}

// ----------------------------------------------------------------------
Expand All @@ -252,15 +252,15 @@ public void setParent(Xpp3Dom parent) {}
* @return input location
*/
public Object getInputLocation() {
return dom.getInputLocation();
return dom.inputLocation();
}

/**
* @since 3.2.0
* @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));
}

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

/**
Expand Down Expand Up @@ -407,9 +407,9 @@ private void update(XmlNode dom) {
}

private boolean replace(Object prevChild, Object newChild) {
List<XmlNode> children = new ArrayList<>(dom.getChildren());
List<XmlNode> 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;
}

Expand Down
29 changes: 24 additions & 5 deletions src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

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

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

/**
Expand All @@ -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));
}
}
3 changes: 1 addition & 2 deletions src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -227,7 +226,7 @@ void equalsIsNullSafe() throws Exception {
attributes.put(null, "nullKey");
List<XmlNode> 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);
Expand Down