freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [07/13] incubator-freemarker git commit: DataModel changes and Significant Node check has been changed, Return a list of empty node if null in @@previous and @@next
Date Sun, 08 Jan 2017 21:20:26 GMT
DataModel changes and Significant Node check has been changed, Return a list of empty node
if null in @@previous and @@next


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/fb1f3bfc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/fb1f3bfc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/fb1f3bfc

Branch: refs/heads/2.3-gae
Commit: fb1f3bfca67ab89594b147946ae348f47797d442
Parents: 0ad0188
Author: Pradeep <pradeep_murugesan@trimble.com>
Authored: Thu Dec 10 13:41:58 2015 +0530
Committer: Pradeep <pradeep_murugesan@trimble.com>
Committed: Mon Jan 11 19:20:43 2016 +0530

----------------------------------------------------------------------
 build.xml                                       |  6 +--
 .../java/freemarker/ext/dom/ElementModel.java   | 30 ++++++++---
 src/test/java/freemarker/core/SiblingTest.java  | 53 ++++++++++++++++++--
 .../freemarker/core/siblingDataModel.xml        |  5 ++
 4 files changed, 80 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fb1f3bfc/build.xml
----------------------------------------------------------------------
diff --git a/build.xml b/build.xml
index 2d1caba..9b0ec6a 100644
--- a/build.xml
+++ b/build.xml
@@ -397,7 +397,7 @@
   <target name="test" depends="compileTest" description="Run test cases">
     <mkdir dir="build/junit-reports" />
     <ivy:cachepath conf="run.test" pathid="ivy.dep.run.test" />
-    <junit haltonfailure="on" fork="true" forkmode="once">
+    <junit haltonfailure="on" fork="true" forkmode="once" showoutput="yes">
       <classpath>
         <pathelement path="build/test-classes" />
         <pathelement path="build/classes" />
@@ -407,11 +407,11 @@
       <formatter type="xml" />
       <batchtest todir="build/junit-reports">
         <fileset dir="src/test/java">
-          <include name="**/*Test.java" />
-          <include name="**/*TestSuite.java" />
+          <include name="**/SiblingTest.java" />
           <exclude name="**/Abstract*.java" />
         </fileset>
       </batchtest>
+        <formatter type="brief" usefile="false"/>
     </junit>
   </target>
   

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fb1f3bfc/src/main/java/freemarker/ext/dom/ElementModel.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/dom/ElementModel.java b/src/main/java/freemarker/ext/dom/ElementModel.java
index 41844d7..11d1c04 100644
--- a/src/main/java/freemarker/ext/dom/ElementModel.java
+++ b/src/main/java/freemarker/ext/dom/ElementModel.java
@@ -33,8 +33,10 @@ import freemarker.template.TemplateScalarModel;
 import freemarker.template.TemplateSequenceModel;
 import freemarker.template.utility.StringUtil;
 
+import java.util.ArrayList;
+
 class ElementModel extends NodeModel implements TemplateScalarModel {
-    
+    private final static ArrayList EMPTY_ARRAYLIST = new ArrayList();
     public ElementModel(Element element) {
         super(element);
     }
@@ -91,17 +93,26 @@ class ElementModel extends NodeModel implements TemplateScalarModel {
             }
             if (key.equals("@@previous")) {
                 Node previousSibling = node.getPreviousSibling();
-                while(!this.isSignificantNode(previousSibling)) {
+                while(previousSibling != null && !this.isSignificantNode(previousSibling))
{
                     previousSibling = previousSibling.getPreviousSibling();
                 }
-                return wrap(previousSibling);
+                if(previousSibling == null) {
+                    return new NodeListModel(EMPTY_ARRAYLIST, null);
+                } else {
+                    return wrap(previousSibling);
+                }
             }
             if (key.equals("@@next")) {
                 Node nextSibling = node.getNextSibling();
-                while(!this.isSignificantNode(nextSibling)) {
+                while(nextSibling != null && !this.isSignificantNode(nextSibling))
{
                     nextSibling = nextSibling.getNextSibling();
                 }
-                return wrap(nextSibling);
+                if(nextSibling == null) {
+                    return new NodeListModel(EMPTY_ARRAYLIST, null);
+                }
+                else {
+                    return wrap(nextSibling);
+                }
             }
             if (StringUtil.isXMLID(key.substring(1))) {
                 Attr att = getAttribute(key.substring(1));
@@ -122,10 +133,15 @@ class ElementModel extends NodeModel implements TemplateScalarModel
{
     }
 
     public boolean isSignificantNode(Node node) throws TemplateModelException {
-        boolean isEmpty =  node.getTextContent().trim().isEmpty();
-        boolean significantNode = !isEmpty;
+        boolean significantNode = false;
+        if(node != null) {
+            boolean isEmpty = StringUtil.isTrimmableToEmpty(node.getTextContent().toCharArray());
+            boolean isPINode = node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE;
+            significantNode = !(isEmpty || isPINode);
+        }
         return significantNode;
     }
+
     public String getAsString() throws TemplateModelException {
         NodeList nl = node.getChildNodes();
         String result = "";

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fb1f3bfc/src/test/java/freemarker/core/SiblingTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/core/SiblingTest.java b/src/test/java/freemarker/core/SiblingTest.java
index 0db4df8..afbb880 100644
--- a/src/test/java/freemarker/core/SiblingTest.java
+++ b/src/test/java/freemarker/core/SiblingTest.java
@@ -47,7 +47,7 @@ public class SiblingTest extends TemplateTest {
         }
     }
 
-    @Test
+   @Test
     public void testEmptyPreviousSibling() throws IOException, TemplateException {
         String ftl = "${doc.person.name?previousSibling}";
         assertOutput(ftl, "\n    ");
@@ -71,15 +71,60 @@ public class SiblingTest extends TemplateTest {
         assertOutput(ftl, "Chennai, India");
     }
 
+
     @Test
-    public void testSignificantNextSibling() throws IOException, TemplateException {
-        String ftl = "${doc.person.name.@@next}";
-        assertOutput(ftl, "12th August");
+    public void testNullPreviousSibling() throws IOException, TemplateException {
+        String ftl = "<#if doc.person?previousSibling??> " +
+                        "previous is not null" +
+                      "<#else>" +
+                         "previous is null" +
+                    "</#if>";
+        assertOutput(ftl, "previous is null");
+
     }
 
     @Test
     public void testSignificantPreviousSibling() throws IOException, TemplateException {
         String ftl = "${doc.person.name.@@previous}";
         assertOutput(ftl, "male");
+
+    }
+
+
+    @Test
+    public void testSignificantNextSibling() throws IOException, TemplateException {
+        String ftl = "${doc.person.name.@@next}";
+        assertOutput(ftl, "12th August");
+
     }
+
+    @Test
+    public void testNullSignificantPreviousSibling() throws IOException, TemplateException
{
+        String ftl = "<#if doc.person.phone.@@next?size == 0>" +
+                "Next is null" +
+                "<#else>" +
+                "Next is not null" +
+                "</#if>";
+        assertOutput(ftl, "Next is null");
+
+    }
+
+    @Test
+    public void testSkippingCommentNode() throws IOException, TemplateException {
+        String ftl = "${doc.person.profession.@@previous}";
+        assertOutput(ftl, "Chennai, India");
+
+    }
+
+    @Test
+    public void testpreviousSiblingforPINode() throws IOException, TemplateException {
+        String ftl = "${doc.person.profession?previousSibling?previousSibling}";
+        assertOutput(ftl, "Chennai, India");
+    }
+    /*@Test
+    public void testSkippingCdataNode() throws IOException, TemplateException {
+        String ftl = "${doc.person.phone.@@previous}";
+        assertOutput(ftl, "Chennai, India");
+    }*/
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fb1f3bfc/src/test/resources/freemarker/core/siblingDataModel.xml
----------------------------------------------------------------------
diff --git a/src/test/resources/freemarker/core/siblingDataModel.xml b/src/test/resources/freemarker/core/siblingDataModel.xml
index 13f928a..37e8795 100644
--- a/src/test/resources/freemarker/core/siblingDataModel.xml
+++ b/src/test/resources/freemarker/core/siblingDataModel.xml
@@ -3,4 +3,9 @@
     <gender>male</gender>
     <name>pradeep</name>
     <dob>12th August</dob><address>Chennai, India</address>
+    <!--This is a comment Node -->
+    <?xml-stylesheet type="text/css" href="style.css"?>
+    <profession>Software Engineer</profession>
+    <![CDATA[ <a>test<a> ]]>
+    <phone>12345678</phone>
 </person>


Mime
View raw message