Skip to content

Commit

Permalink
don't trim (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo authored Apr 28, 2023
1 parent 259b796 commit 0dd10e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
33 changes: 15 additions & 18 deletions src/main/java/org/apache/maven/shared/utils/xml/Xpp3DomBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@
* @author Kristian Rosenvold
*/
public class Xpp3DomBuilder {
private static final boolean DEFAULT_TRIM = true;

/**
* @param reader {@link Reader}
* @return the built DOM
* @throws XmlPullParserException in case of an error
*/
public static Xpp3Dom build(@WillClose @Nonnull Reader reader) throws XmlPullParserException {
return build(reader, DEFAULT_TRIM);
return build(reader, false);
}

/**
Expand All @@ -58,45 +57,49 @@ public static Xpp3Dom build(@WillClose @Nonnull Reader reader) throws XmlPullPar
* @throws XmlPullParserException in case of an error
*/
public static Xpp3Dom build(@WillClose InputStream is, @Nonnull String encoding) throws XmlPullParserException {
return build(is, encoding, DEFAULT_TRIM);
return build(is, encoding, false);
}

/**
* @param is {@link InputStream}
* @param encoding the encoding
* @param trim true/false
* @param noop vestigial argument with no effect
* @return the built DOM
* @throws XmlPullParserException in case of an error
* @deprecated use the two-arg variant
*/
public static Xpp3Dom build(@WillClose InputStream is, @Nonnull String encoding, boolean trim)
@Deprecated
public static Xpp3Dom build(@WillClose InputStream is, @Nonnull String encoding, boolean noop)
throws XmlPullParserException {
try {
Reader reader = new InputStreamReader(is, encoding);
return build(reader, trim);
return build(reader);
} catch (UnsupportedEncodingException e) {
throw new XmlPullParserException(e);
}
}

/**
* @param in {@link Reader}
* @param trim true/false
* @param noop vestigial argument with no effect
* @return the built DOM
* @throws XmlPullParserException in case of an error
* @deprecated use {#build(java.io.Reader)}
*/
public static Xpp3Dom build(@WillClose Reader in, boolean trim) throws XmlPullParserException {
@Deprecated
public static Xpp3Dom build(@WillClose Reader in, boolean noop) throws XmlPullParserException {
try (Reader reader = in) {
DocHandler docHandler = parseSax(new InputSource(reader), trim);
DocHandler docHandler = parseSax(new InputSource(reader));
reader.close();
return docHandler.result;
} catch (final IOException e) {
throw new XmlPullParserException(e);
}
}

private static DocHandler parseSax(@Nonnull InputSource inputSource, boolean trim) throws XmlPullParserException {
private static DocHandler parseSax(@Nonnull InputSource inputSource) throws XmlPullParserException {
try {
DocHandler ch = new DocHandler(trim);
DocHandler ch = new DocHandler();
XMLReader parser = createXmlReader();
parser.setContentHandler(ch);
parser.parse(inputSource);
Expand Down Expand Up @@ -147,14 +150,8 @@ private static class DocHandler extends DefaultHandler {

Xpp3Dom result = null;

private final boolean trim;

private boolean spacePreserve = false;

DocHandler(boolean trim) {
this.trim = trim;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
Expand Down Expand Up @@ -216,7 +213,7 @@ public void endElement(String uri, String localName, String qName) throws SAXExc
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String text = new String(ch, start, length);
appendToTopValue((trim && !spacePreserve) ? text.trim() : text);
appendToTopValue(text);
}

private void appendToTopValue(String toAppend) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,10 @@ public void trimming() throws Exception {
String domString = createDomString();

Xpp3Dom dom = Xpp3DomBuilder.build(new StringReader(domString), true);

assertEquals("element1value", dom.getChild("element1").getValue());

assertEquals(" element1value\n ", dom.getChild("element1").getValue());
assertEquals(" preserve space ", dom.getChild("element6").getValue());

dom = Xpp3DomBuilder.build(new StringReader(domString), false);

assertEquals(" element1value\n ", dom.getChild("element1").getValue());

assertEquals(" preserve space ", dom.getChild("element6").getValue());
}

Expand Down

0 comments on commit 0dd10e9

Please sign in to comment.