You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/03/07 17:12:56 UTC
[groovy] branch GROOVY-10495 updated (c9916f0 -> 545663d)
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a change to branch GROOVY-10495
in repository https://gitbox.apache.org/repos/asf/groovy.git.
discard c9916f0 GROOVY-10495: Invalid newlines generated by XmlTemplateEngine
new 545663d GROOVY-10495: Invalid newlines generated by XmlTemplateEngine
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (c9916f0)
\
N -- N -- N refs/heads/GROOVY-10495 (545663d)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../groovy/text/GroovyXmlTemplateEngineTest.groovy | 27 +++++++++++-----------
.../java/groovy/text/XmlTemplateEngineTest.java | 9 --------
2 files changed, 14 insertions(+), 22 deletions(-)
copy src/test/groovy/bugs/Groovy3719.groovy => subprojects/groovy-templates/src/test/groovy/groovy/text/GroovyXmlTemplateEngineTest.groovy (66%)
[groovy] 01/01: GROOVY-10495: Invalid newlines generated by XmlTemplateEngine
Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY-10495
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 545663d2d23947604f9d399348d9c8711e9df251
Author: Daniel Sun <su...@apache.org>
AuthorDate: Tue Mar 8 00:39:15 2022 +0800
GROOVY-10495: Invalid newlines generated by XmlTemplateEngine
---
.../main/groovy/groovy/text/XmlTemplateEngine.java | 45 +++++++++++++++---
.../src/spec/test/TemplateEnginesTest.groovy | 4 +-
.../groovy/text/GroovyXmlTemplateEngineTest.groovy | 37 +++++++++++++++
.../src/main/java/groovy/xml/XmlNodePrinter.java | 54 ++++++++++++++--------
4 files changed, 111 insertions(+), 29 deletions(-)
diff --git a/subprojects/groovy-templates/src/main/groovy/groovy/text/XmlTemplateEngine.java b/subprojects/groovy-templates/src/main/groovy/groovy/text/XmlTemplateEngine.java
index 68f413d..98d1e36 100644
--- a/subprojects/groovy-templates/src/main/groovy/groovy/text/XmlTemplateEngine.java
+++ b/subprojects/groovy-templates/src/main/groovy/groovy/text/XmlTemplateEngine.java
@@ -23,11 +23,12 @@ import groovy.lang.GroovyRuntimeException;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import groovy.lang.Writable;
+import groovy.namespace.QName;
import groovy.util.IndentPrinter;
import groovy.util.Node;
+import groovy.util.NodeList;
import groovy.xml.XmlNodePrinter;
import groovy.xml.XmlParser;
-import groovy.namespace.QName;
import org.apache.groovy.io.StringBuilderWriter;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.runtime.FormatHelper;
@@ -137,10 +138,10 @@ public class XmlTemplateEngine extends TemplateEngine {
}
@Override
- protected void printSimpleItem(Object value) {
- this.printLineBegin();
+ protected void printSimpleItem(Node node, Object value) {
+ this.printLineBegin(node);
out.print(escapeSpecialChars(FormatHelper.toString(value)));
- printLineEnd();
+ printLineEnd(node);
}
private String escapeSpecialChars(String s) {
@@ -186,13 +187,21 @@ public class XmlTemplateEngine extends TemplateEngine {
@Override
protected void printLineBegin() {
+ printLineBegin(null);
+ }
+
+ @Override
+ protected void printLineBegin(Node node) {
out.print("out.print(\"\"\"");
- out.printIndent();
+ if (needNewlineAndRelatedIndents(node)){
+ out.printIndent();
+ }
}
@Override
- protected void printLineEnd(String comment) {
- out.print("\\n\"\"\");");
+ protected void printLineEnd(Node node, String comment) {
+ out.print((needNewlineAndRelatedIndents(node) ? "\\n" : "") + "\"\"\");");
+
if (comment != null) {
out.print(" // ");
out.print(comment);
@@ -200,6 +209,28 @@ public class XmlTemplateEngine extends TemplateEngine {
out.print("\n");
}
+ private boolean needNewlineAndRelatedIndents(Node node) {
+ return !containsValueOrNone(node);
+ }
+
+ private boolean containsValueOrNone(Node node) {
+ if (null == node) return false;
+
+ final Object value = node.value();
+ if (!(value instanceof NodeList)) {
+ return true;
+ }
+ NodeList nodeList = (NodeList) value;
+ final int size = nodeList.size();
+ if (0 == size) {
+ return true;
+ }
+ if (1 == size && !(nodeList.get(0) instanceof Node)) {
+ return true;
+ }
+ return false;
+ }
+
@Override
protected boolean printSpecialNode(Node node) {
Object name = node.name();
diff --git a/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy b/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy
index 3999d8b..397c279 100644
--- a/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy
+++ b/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy
@@ -188,9 +188,7 @@ The conference committee.'''
assert template.toString() == '''\
<document type='letter'>
Dearest
- <foo:to xmlns:foo='baz'>
- Jochen "blackdrag" Theodorou
- </foo:to>
+ <foo:to xmlns:foo='baz'>Jochen "blackdrag" Theodorou</foo:to>
How are you today?
</document>
'''
diff --git a/subprojects/groovy-templates/src/test/groovy/groovy/text/GroovyXmlTemplateEngineTest.groovy b/subprojects/groovy-templates/src/test/groovy/groovy/text/GroovyXmlTemplateEngineTest.groovy
new file mode 100644
index 0000000..4617d18
--- /dev/null
+++ b/subprojects/groovy-templates/src/test/groovy/groovy/text/GroovyXmlTemplateEngineTest.groovy
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.text
+
+import groovy.xml.XmlParser
+import org.junit.Test
+
+class GroovyXmlTemplateEngineTest {
+ @Test
+ void testFormat() {
+ String xmlScript =
+"""<?xml version="1.0"?>
+<person>
+ <name>Daniel</name>
+</person>
+"""
+ def name1 = new XmlParser().parseText(xmlScript).name.text()
+ def name2 = new XmlParser().parseText(new XmlTemplateEngine().createTemplate(xmlScript).make().toString()).name.text()
+ assert name1 == name2
+ }
+}
\ No newline at end of file
diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlNodePrinter.java b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlNodePrinter.java
index 8dcb086..66cbabc 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlNodePrinter.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlNodePrinter.java
@@ -203,7 +203,7 @@ public class XmlNodePrinter {
Object value = node.value();
if (value instanceof List) {
printName(node, ctx, true, isListOfSimple((List) value));
- printList((List) value, ctx);
+ printList(node, ctx);
printName(node, ctx, false, isListOfSimple((List) value));
out.flush();
return;
@@ -211,7 +211,7 @@ public class XmlNodePrinter {
// treat as simple type - probably a String
printName(node, ctx, true, preserveWhitespace);
- printSimpleItemWithIndent(value);
+ printSimpleItemWithIndent(node);
printName(node, ctx, false, preserveWhitespace);
out.flush();
}
@@ -224,14 +224,26 @@ public class XmlNodePrinter {
}
protected void printLineBegin() {
+ printLineBegin(null);
+ }
+
+ protected void printLineBegin(Node node) {
out.printIndent();
}
protected void printLineEnd() {
- printLineEnd(null);
+ printLineEnd(null, null);
+ }
+
+ protected void printLineEnd(Node node) {
+ printLineEnd(node, null);
}
protected void printLineEnd(String comment) {
+ printLineEnd(null, comment);
+ }
+
+ protected void printLineEnd(Node node, String comment) {
if (comment != null) {
out.print(" <!-- ");
out.print(comment);
@@ -241,22 +253,26 @@ public class XmlNodePrinter {
out.flush();
}
- protected void printList(List list, NamespaceContext ctx) {
- out.incrementIndent();
- for (Object value : list) {
- /*
- * If the current value is a node, recurse into that node.
- */
- if (value instanceof Node) {
- print((Node) value, new NamespaceContext(ctx));
- continue;
+ protected void printList(Node node, NamespaceContext ctx) {
+ final Object v = node.value();
+ if (v instanceof List) {
+ List list = (List) v;
+ out.incrementIndent();
+ for (Object value : list) {
+ /*
+ * If the current value is a node, recurse into that node.
+ */
+ if (value instanceof Node) {
+ print((Node) value, new NamespaceContext(ctx));
+ continue;
+ }
+ printSimpleItem(node, value);
}
- printSimpleItem(value);
+ out.decrementIndent();
}
- out.decrementIndent();
}
- protected void printSimpleItem(Object value) {
+ protected void printSimpleItem(Node node, Object value) {
if (!preserveWhitespace) printLineBegin();
printEscaped(FormatHelper.toString(value), false);
if (!preserveWhitespace) printLineEnd();
@@ -270,7 +286,7 @@ public class XmlNodePrinter {
if (name == null) {
throw new NullPointerException("Name must not be null.");
}
- if (!preserve || begin) printLineBegin();
+ if (!preserve || begin) printLineBegin(begin ? null : node);
out.print("<");
if (!begin) {
out.print("/");
@@ -283,7 +299,7 @@ public class XmlNodePrinter {
printNameAttributes(node.attributes(), ctx);
}
out.print(">");
- if (!preserve || !begin) printLineEnd();
+ if (!preserve || !begin) printLineEnd(!begin ? null : node);
}
protected boolean printSpecialNode(Node node) {
@@ -363,9 +379,9 @@ public class XmlNodePrinter {
return object.toString();
}
- private void printSimpleItemWithIndent(Object value) {
+ private void printSimpleItemWithIndent(Node node) {
if (!preserveWhitespace) out.incrementIndent();
- printSimpleItem(value);
+ printSimpleItem(node, node.value());
if (!preserveWhitespace) out.decrementIndent();
}