Skip to content

Commit

Permalink
Merge branch 'master' into java9
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipJirsak authored Sep 16, 2017
2 parents 1a3cdfa + 169612f commit 136cd0f
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .idea/dom4j.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: java
sudo: false
jdk:
- oraclejdk8
- oraclejdk7
- openjdk6
- openjdk7
after_success:
- bash <(curl -s https://codecov.io/bash)
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,5 @@ if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')
}

task wrapper(type: Wrapper) {
gradleVersion = '4.1'
gradleVersion = '4.1'
}
6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Aug 22 18:47:11 BST 2017
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-bin.zip
#Sat Sep 16 13:35:18 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-bin.zip
37 changes: 19 additions & 18 deletions src/main/java/org/dom4j/io/XMLWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.XMLFilterImpl;

import static org.dom4j.util.StringUtils.endsWithWhitespace;
import static org.dom4j.util.StringUtils.startsWithWhitespace;

/**
* <p>
* <code>XMLWriter</code> takes a DOM4J tree and formats it to a stream as
Expand Down Expand Up @@ -1036,14 +1039,16 @@ protected void writeElementContent(Element element) throws IOException {
if (!textOnly && format.isPadText()) {
// only add the PAD_TEXT if the text itself starts with
// whitespace
char firstChar = 'a';
final boolean startsWithWhitespace;
if (buff != null) {
firstChar = buff.charAt(0);
startsWithWhitespace = startsWithWhitespace(buff);
} else if (lastTextNode != null) {
firstChar = lastTextNode.getText().charAt(0);
startsWithWhitespace = startsWithWhitespace(lastTextNode.getText());
} else {
startsWithWhitespace = false;
}

if (Character.isWhitespace(firstChar)) {
if (startsWithWhitespace) {
writer.write(PAD_TEXT);
}
}
Expand All @@ -1059,15 +1064,14 @@ protected void writeElementContent(Element element) throws IOException {
if (format.isPadText()) {
// only add the PAD_TEXT if the text itself ends
// with whitespace
char lastTextChar = 'a';
final boolean endsWithWhitespace;
if (buff != null) {
lastTextChar = buff.charAt(buff.length() - 1);
} else if (lastTextNode != null) {
String txt = lastTextNode.getText();
lastTextChar = txt.charAt(txt.length() - 1);
endsWithWhitespace = endsWithWhitespace(buff);
} else {
endsWithWhitespace = endsWithWhitespace(lastTextNode.getText());
}

if (Character.isWhitespace(lastTextChar)) {
if (endsWithWhitespace) {
writer.write(PAD_TEXT);
}
}
Expand All @@ -1084,14 +1088,14 @@ protected void writeElementContent(Element element) throws IOException {
if (!textOnly && format.isPadText()) {
// only add the PAD_TEXT if the text itself starts with
// whitespace
char firstChar = 'a';
final boolean startsWithWhitespace;
if (buff != null) {
firstChar = buff.charAt(0);
startsWithWhitespace = startsWithWhitespace(buff);
} else {
firstChar = lastTextNode.getText().charAt(0);
startsWithWhitespace = startsWithWhitespace(lastTextNode.getText());
}

if (Character.isWhitespace(firstChar)) {
if (startsWithWhitespace) {
writer.write(PAD_TEXT);
}
}
Expand All @@ -1116,10 +1120,7 @@ protected void writeElementContent(Element element) throws IOException {
if ((lastTextNode != null) && format.isPadText()) {
// only add the PAD_TEXT if the text itself ends with
// whitespace
String txt = lastTextNode.getText();
char lastTextChar = txt.charAt(txt.length() - 1);

if (Character.isWhitespace(lastTextChar)) {
if (endsWithWhitespace(lastTextNode.getText())) {
writer.write(PAD_TEXT);
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/org/dom4j/util/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.dom4j.util;

/**
* Contains utilities related to strings.
*
* @author Marián Petráš
*/
public final class StringUtils {

private StringUtils() {}

/**
* Finds out if the given character sequence starts with a whitespace
* character.
*
* @return {@code true} if the given character sequence is not empty
* and starts with a whitespace character; {@code false} otherwise
* @exception NullPointerException if the given character sequence is
* {@code null}
*/
public static boolean startsWithWhitespace(final CharSequence charSeq) {
if (charSeq.length() == 0) {
return false;
}
return Character.isWhitespace(charSeq.charAt(0));
}

/**
* Finds out if the given character sequence ends with a whitespace
* character.
*
* @return {@code true} if the given character sequence is not empty
* and ends with a whitespace character; {@code false} otherwise
* @exception NullPointerException if the given character sequence is
* {@code null}
*/
public static boolean endsWithWhitespace(final CharSequence charSeq) {
if (charSeq.length() == 0) {
return false;
}
return Character.isWhitespace(charSeq.charAt(charSeq.length() - 1));
}

}
75 changes: 75 additions & 0 deletions src/test/java/org/dom4j/XMLWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package org.dom4j;

import org.dom4j.dom.DOMElement;
import org.dom4j.dom.DOMText;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
Expand All @@ -18,6 +20,8 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

Expand Down Expand Up @@ -502,6 +506,77 @@ public void testNullCData() {
System.out.println(doc.asXML());
}

public void testGitHubIssue26_case1() throws IOException {
final Element element = new DOMElement("foo");
element.add(new DOMElement("elem1"));
element.add(new DOMText(""));
element.add(new DOMText(""));
element.add(new DOMElement("elem2"));
testGitHubIssue26(element);
}

public void testGitHubIssue26_case2() throws IOException {
final Element element = new DOMElement("foo");
element.add(new DOMElement("elem1"));
element.add(new DOMText(""));
element.add(new DOMElement("elem2"));
testGitHubIssue26(element);
}

public void testGitHubIssue26_case3() throws IOException {
final Element element = new DOMElement("foo");
element.add(new DOMText(""));
element.add(new DOMText(""));
element.add(new DOMElement("elem"));
testGitHubIssue26(element);
}

public void testGitHubIssue26_case4() throws IOException {
final Element element = new DOMElement("foo");
element.add(new DOMText(""));
element.add(new DOMElement("elem"));
testGitHubIssue26(element);
}

public void testGitHubIssue26_case5() throws IOException {
final Element element = new DOMElement("foo");
element.add(new DOMElement("elem"));
element.add(new DOMText(""));
element.add(new DOMText(""));
testGitHubIssue26(element);
}

public void testGitHubIssue26_case6() throws IOException {
final Element element = new DOMElement("foo");
element.add(new DOMElement("elem"));
element.add(new DOMText(""));
testGitHubIssue26(element);
}

private void testGitHubIssue26(final Element element) throws IOException {
final OutputFormat format = new OutputFormat(" ", true);
format.setSuppressDeclaration(false);
format.setTrimText(true);
format.setPadText(true);
format.setNewlines(true);

new XMLWriter(new CharArrayWriter(128), format).write(element);
}

public void testGitHubIssue26_case7() throws IOException {
final Element element = new DOMElement("foo");
element.add(new DOMText(""));
element.add(new DOMElement("elem"));

final OutputFormat format = new OutputFormat(" ", true);
format.setSuppressDeclaration(false);
format.setTrimText(false);
format.setPadText(true);
format.setNewlines(true);

new XMLWriter(new CharArrayWriter(128), format).write(element);
}

protected void generateXML(ContentHandler handler) throws SAXException {
handler.startDocument();

Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/dom4j/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.dom4j.util;

import org.testng.annotations.Test;

import static org.dom4j.util.StringUtils.endsWithWhitespace;
import static org.dom4j.util.StringUtils.startsWithWhitespace;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

/**
* Test of utility class StringUtils.
*
* @author Marián Petráš
*/
public class StringUtilsTest {

@Test
public void testStartsWithWhitespace_empty() {
assertFalse(startsWithWhitespace(""));
}

@Test
public void testStartsWithWhitespace_nonEmpty() {
assertTrue (startsWithWhitespace(" "));
assertFalse(startsWithWhitespace("alpha"));
assertTrue (startsWithWhitespace(" alpha"));
assertFalse(startsWithWhitespace("alpha "));
assertTrue (startsWithWhitespace(" alpha "));
}

@Test
public void testEndsWithWhitespace_empty() {
assertFalse(endsWithWhitespace(""));
}

@Test
public void testEndsWithWhitespace_nonEmpty() {
assertTrue (endsWithWhitespace(" "));
assertFalse(endsWithWhitespace("alpha"));
assertFalse(endsWithWhitespace(" alpha"));
assertTrue (endsWithWhitespace("alpha "));
assertTrue (endsWithWhitespace(" alpha "));
}

}

0 comments on commit 136cd0f

Please sign in to comment.