Adjustments to PR #9 "Sibling related changes"
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/fbd6fa5f Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/fbd6fa5f Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/fbd6fa5f Branch: refs/heads/2.3-gae Commit: fbd6fa5f945db29f09d2ec3ea8d2661da5104365 Parents: ddc6a5c Author: ddekany <[email protected]> Authored: Sun Jan 8 21:59:19 2017 +0100 Committer: ddekany <[email protected]> Committed: Sun Jan 8 22:19:52 2017 +0100 ---------------------------------------------------------------------- .../java/freemarker/core/BuiltInForNodeEx.java | 9 +- .../java/freemarker/core/BuiltinVariable.java | 2 - .../core/NonExtendedNodeException.java | 64 +++++++++++++ .../java/freemarker/ext/dom/ElementModel.java | 7 +- src/main/java/freemarker/ext/dom/NodeModel.java | 6 +- .../template/TemplateNodeModelEx.java | 19 ++-- .../freemarker/template/utility/ClassUtil.java | 5 +- src/test/java/freemarker/core/SiblingTest.java | 97 ++++++++------------ .../freemarker/core/siblingDataModel.xml | 18 ++++ 9 files changed, 146 insertions(+), 81 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fbd6fa5f/src/main/java/freemarker/core/BuiltInForNodeEx.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/BuiltInForNodeEx.java b/src/main/java/freemarker/core/BuiltInForNodeEx.java index 8bfe6b6..79ba429 100644 --- a/src/main/java/freemarker/core/BuiltInForNodeEx.java +++ b/src/main/java/freemarker/core/BuiltInForNodeEx.java @@ -6,9 +6,9 @@ * 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 @@ -22,13 +22,12 @@ import freemarker.template.*; public abstract class BuiltInForNodeEx extends BuiltIn { @Override - TemplateModel _eval(Environment env) - throws TemplateException { + TemplateModel _eval(Environment env) throws TemplateException { TemplateModel model = target.eval(env); if (model instanceof TemplateNodeModelEx) { return calculateResult((TemplateNodeModelEx) model, env); } else { - throw new NonNodeException(target, model, env); + throw new NonExtendedNodeException(target, model, env); } } abstract TemplateModel calculateResult(TemplateNodeModelEx nodeModel, Environment env) http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fbd6fa5f/src/main/java/freemarker/core/BuiltinVariable.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/BuiltinVariable.java b/src/main/java/freemarker/core/BuiltinVariable.java index 1ceb64e..5b0f4d2 100644 --- a/src/main/java/freemarker/core/BuiltinVariable.java +++ b/src/main/java/freemarker/core/BuiltinVariable.java @@ -72,8 +72,6 @@ final class BuiltinVariable extends Expression { static final String URL_ESCAPING_CHARSET_CC = "urlEscapingCharset"; static final String URL_ESCAPING_CHARSET = "url_escaping_charset"; static final String NOW = "now"; - static final String PREVIOUS_SIBLING = "previous"; - static final String NEXT_SIBLING = "next"; static final String[] SPEC_VAR_NAMES = new String[] { AUTO_ESC_CC, http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fbd6fa5f/src/main/java/freemarker/core/NonExtendedNodeException.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/NonExtendedNodeException.java b/src/main/java/freemarker/core/NonExtendedNodeException.java new file mode 100644 index 0000000..3377257 --- /dev/null +++ b/src/main/java/freemarker/core/NonExtendedNodeException.java @@ -0,0 +1,64 @@ +/* + * 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 freemarker.core; + +import freemarker.template.TemplateModel; +import freemarker.template.TemplateNodeModelEx; + +/** + * Indicates that a {@link TemplateNodeModelEx} value was expected, but the value had a different type. + * + * @since 2.3.26 + */ +public class NonExtendedNodeException extends UnexpectedTypeException { + + private static final Class<?>[] EXPECTED_TYPES = new Class[] { TemplateNodeModelEx.class }; + + public NonExtendedNodeException(Environment env) { + super(env, "Expecting extended node value here"); + } + + public NonExtendedNodeException(String description, Environment env) { + super(env, description); + } + + NonExtendedNodeException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonExtendedNodeException( + Expression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "extended node", EXPECTED_TYPES, env); + } + + NonExtendedNodeException( + Expression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "extended node", EXPECTED_TYPES, tip, env); + } + + NonExtendedNodeException( + Expression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "extended node", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fbd6fa5f/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 6fa29ba..a72e55c 100644 --- a/src/main/java/freemarker/ext/dom/ElementModel.java +++ b/src/main/java/freemarker/ext/dom/ElementModel.java @@ -34,9 +34,10 @@ import freemarker.template.TemplateSequenceModel; import freemarker.template.utility.StringUtil; import java.util.ArrayList; +import java.util.Collections; class ElementModel extends NodeModel implements TemplateScalarModel { - private final static ArrayList EMPTY_ARRAYLIST = new ArrayList(); + public ElementModel(Element element) { super(element); } @@ -97,7 +98,7 @@ class ElementModel extends NodeModel implements TemplateScalarModel { previousSibling = previousSibling.getPreviousSibling(); } if(previousSibling == null) { - return new NodeListModel(EMPTY_ARRAYLIST, null); + return new NodeListModel(Collections.emptyList(), null); } else { return wrap(previousSibling); } @@ -108,7 +109,7 @@ class ElementModel extends NodeModel implements TemplateScalarModel { nextSibling = nextSibling.getNextSibling(); } if(nextSibling == null) { - return new NodeListModel(EMPTY_ARRAYLIST, null); + return new NodeListModel(Collections.emptyList(), null); } else { return wrap(nextSibling); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fbd6fa5f/src/main/java/freemarker/ext/dom/NodeModel.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/dom/NodeModel.java b/src/main/java/freemarker/ext/dom/NodeModel.java index e5ed1ed..4198e2f 100644 --- a/src/main/java/freemarker/ext/dom/NodeModel.java +++ b/src/main/java/freemarker/ext/dom/NodeModel.java @@ -76,7 +76,7 @@ import freemarker.template.TemplateSequenceModel; * then), but should be used to represent a node set of exactly 1 node. */ abstract public class NodeModel -implements TemplateNodeModel, TemplateNodeModelEx, TemplateHashModel, TemplateSequenceModel, +implements TemplateNodeModelEx, TemplateHashModel, TemplateSequenceModel, AdapterTemplateModel, WrapperTemplateModel, _UnexpectedTypeErrorExplainerTemplateModel { static private final Logger LOG = Logger.getLogger("freemarker.dom"); @@ -311,7 +311,7 @@ implements TemplateNodeModel, TemplateNodeModelEx, TemplateHashModel, TemplateSe return parent; } - public TemplateNodeModel getPreviousSibling() throws TemplateModelException { + public TemplateNodeModelEx getPreviousSibling() throws TemplateModelException { if (previousSibling == null) { Node previous = node.getPreviousSibling(); previousSibling = wrap(previous); @@ -319,7 +319,7 @@ implements TemplateNodeModel, TemplateNodeModelEx, TemplateHashModel, TemplateSe return previousSibling; } - public TemplateNodeModel getNextSibling() throws TemplateModelException { + public TemplateNodeModelEx getNextSibling() throws TemplateModelException { if (nextSibling == null) { Node next = node.getNextSibling(); nextSibling = wrap(next); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fbd6fa5f/src/main/java/freemarker/template/TemplateNodeModelEx.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/template/TemplateNodeModelEx.java b/src/main/java/freemarker/template/TemplateNodeModelEx.java index 0375048..ca7d21d 100644 --- a/src/main/java/freemarker/template/TemplateNodeModelEx.java +++ b/src/main/java/freemarker/template/TemplateNodeModelEx.java @@ -6,9 +6,9 @@ * 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 @@ -19,15 +19,22 @@ package freemarker.template; +import freemarker.ext.dom.NodeModel; + +/** + * A {@link NodeModel} that supports navigating to the previous and next sibling nodes. + * + * @since 2.3.26 + */ public interface TemplateNodeModelEx extends TemplateNodeModel { /** - * @return the immediate Previous Sibling of this node + * @return The immediate previous sibling of this node, or {@code null} if there's no such node. */ - TemplateNodeModel getPreviousSibling() throws TemplateModelException; + TemplateNodeModelEx getPreviousSibling() throws TemplateModelException; /** - * @return the immediate next Sibling of this node + * @return The immediate next sibling of this node, or {@code null} if there's no such node. */ - TemplateNodeModel getNextSibling() throws TemplateModelException; + TemplateNodeModelEx getNextSibling() throws TemplateModelException; } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fbd6fa5f/src/main/java/freemarker/template/utility/ClassUtil.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/template/utility/ClassUtil.java b/src/main/java/freemarker/template/utility/ClassUtil.java index fe8d753..7a767bd 100644 --- a/src/main/java/freemarker/template/utility/ClassUtil.java +++ b/src/main/java/freemarker/template/utility/ClassUtil.java @@ -50,6 +50,7 @@ import freemarker.template.TemplateMethodModelEx; import freemarker.template.TemplateModel; import freemarker.template.TemplateModelIterator; import freemarker.template.TemplateNodeModel; +import freemarker.template.TemplateNodeModelEx; import freemarker.template.TemplateNumberModel; import freemarker.template.TemplateScalarModel; import freemarker.template.TemplateSequenceModel; @@ -190,7 +191,9 @@ public class ClassUtil { private static void appendTemplateModelTypeName(StringBuilder sb, Set typeNamesAppended, Class cl) { int initalLength = sb.length(); - if (TemplateNodeModel.class.isAssignableFrom(cl)) { + if (TemplateNodeModelEx.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "extended node"); + } else if (TemplateNodeModel.class.isAssignableFrom(cl)) { appendTypeName(sb, typeNamesAppended, "node"); } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fbd6fa5f/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 e646782..d27b1bd 100644 --- a/src/test/java/freemarker/core/SiblingTest.java +++ b/src/test/java/freemarker/core/SiblingTest.java @@ -1,21 +1,3 @@ -package freemarker.core; - -import freemarker.ext.dom.NodeModel; -import freemarker.template.TemplateException; -import freemarker.test.TemplateTest; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.util.HashMap; -import java.util.Map; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -24,9 +6,9 @@ import java.util.Map; * 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 @@ -34,52 +16,53 @@ import java.util.Map; * specific language governing permissions and limitations * under the License. */ + +package freemarker.core; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.junit.Before; +import org.junit.Test; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import freemarker.ext.dom.NodeModel; +import freemarker.template.TemplateException; +import freemarker.test.TemplateTest; + public class SiblingTest extends TemplateTest { @Before - public void setUp() { - try { - InputSource is = new InputSource(getClass().getResourceAsStream("siblingDataModel.xml")); - addToDataModel("doc", NodeModel.parse(is) ); - } catch (Exception e) { - System.out.println("Exception while parsing the dataModel xml"); - e.printStackTrace(); - } + public void setUp() throws SAXException, IOException, ParserConfigurationException { + InputSource is = new InputSource(getClass().getResourceAsStream("siblingDataModel.xml")); + addToDataModel("doc", NodeModel.parse(is)); } @Test - public void testEmptyPreviousSibling() throws IOException, TemplateException { - String ftl = "${doc.person.name?previousSibling}"; - assertOutput(ftl, "\n "); + public void testBlankPreviousSibling() throws IOException, TemplateException { + assertOutput("${doc.person.name?previousSibling}", "\n "); } @Test - public void testNonEmptyPreviousSibling() throws IOException, TemplateException { - String ftl = "${doc.person.address?previousSibling}"; - assertOutput(ftl, "12th August"); + public void testNonBlankPreviousSibling() throws IOException, TemplateException { + assertOutput("${doc.person.address?previousSibling}", "12th August"); } @Test - public void testEmptyNextSibling() throws IOException, TemplateException { - String ftl = "${doc.person.name?nextSibling}"; - assertOutput(ftl, "\n "); + public void testBlankNextSibling() throws IOException, TemplateException { + assertOutput("${doc.person.name?nextSibling}", "\n "); } @Test - public void testNonEmptyNextSibling() throws IOException, TemplateException { - String ftl = "${doc.person.dob?nextSibling}"; - assertOutput(ftl, "Chennai, India"); + public void testNonBlankNextSibling() throws IOException, TemplateException { + assertOutput("${doc.person.dob?nextSibling}", "Chennai, India"); } - @Test 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"); + assertOutput("${doc.person?previousSibling?? ?c}", "false"); } @Test @@ -97,30 +80,22 @@ public class SiblingTest extends TemplateTest { @Test public void testNullSignificantPreviousSibling() throws IOException, TemplateException { - String ftl = "<#if doc.person.phone.@@next_significant?size == 0>" + - "Next is null" + - "<#else>" + - "Next is not null" + - "</#if>"; - assertOutput(ftl, "Next is null"); + assertOutput("${doc.person.phone.@@next_significant?size}", "0"); } @Test public void testSkippingCommentNode() throws IOException, TemplateException { - String ftl = "${doc.person.profession.@@previous_significant}"; - assertOutput(ftl, "Chennai, India"); + assertOutput("${doc.person.profession.@@previous_significant}", "Chennai, India"); } @Test - public void testSkippingEmptyCdataNode() throws IOException, TemplateException { - String ftl = "${doc.person.hobby.@@previous_significant}"; - assertOutput(ftl, "Software Engineer"); + public void testSkippingEmptyCDataNode() throws IOException, TemplateException { + assertOutput("${doc.person.hobby.@@previous_significant}", "Software Engineer"); } @Test - public void testValidCdataNode() throws IOException, TemplateException { - String ftl = "${doc.person.phone.@@previous_significant}"; - assertOutput(ftl, "\n this is a valid cdata\n "); + public void testValidCDataNode() throws IOException, TemplateException { + assertOutput("${doc.person.phone.@@previous_significant}", "\n this is a valid cdata\n "); } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fbd6fa5f/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 5a48103..bdb3e45 100644 --- a/src/test/resources/freemarker/core/siblingDataModel.xml +++ b/src/test/resources/freemarker/core/siblingDataModel.xml @@ -1,4 +1,22 @@ <?xml version="1.0"?> +<!-- + 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. +--> <person> <gender>male</gender> <name>pradeep</name>
