You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2018/09/26 21:44:28 UTC

[2/2] freemarker git commit: Minor code cleanup in ext.dom

Minor code cleanup in ext.dom


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

Branch: refs/heads/2.3-gae
Commit: c4f84789b8a6a4719fe34d87fae1aabe4d275f23
Parents: 3d2f2b2
Author: ddekany <dd...@apache.org>
Authored: Wed Sep 26 23:44:13 2018 +0200
Committer: ddekany <dd...@apache.org>
Committed: Wed Sep 26 23:44:13 2018 +0200

----------------------------------------------------------------------
 .../java/freemarker/ext/dom/NodeListModel.java  |  7 +-
 .../ext/dom/SunInternalXalanXPathSupport.java   | 71 +++++++++-----------
 .../freemarker/ext/dom/XalanXPathSupport.java   | 68 ++++++++-----------
 3 files changed, 61 insertions(+), 85 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/freemarker/blob/c4f84789/src/main/java/freemarker/ext/dom/NodeListModel.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/dom/NodeListModel.java b/src/main/java/freemarker/ext/dom/NodeListModel.java
index ce83d8c..0d0c4e3 100644
--- a/src/main/java/freemarker/ext/dom/NodeListModel.java
+++ b/src/main/java/freemarker/ext/dom/NodeListModel.java
@@ -171,14 +171,13 @@ class NodeListModel extends SimpleSequence implements TemplateHashModel, _Unexpe
             return result;
         }
         XPathSupport xps = getXPathSupport();
-        if (xps != null) {
-            Object context = (size == 0) ? null : rawNodeList(); 
-            return xps.executeQuery(context, key);
-        } else {
+        if (xps == null) {
             throw new TemplateModelException(
                     "Can't try to resolve the XML query key, because no XPath support is available. "
                     + "This is either malformed or an XPath expression: " + key);
         }
+        Object context = (size == 0) ? null : rawNodeList();
+        return xps.executeQuery(context, key);
     }
     
     private List rawNodeList() throws TemplateModelException {

http://git-wip-us.apache.org/repos/asf/freemarker/blob/c4f84789/src/main/java/freemarker/ext/dom/SunInternalXalanXPathSupport.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/dom/SunInternalXalanXPathSupport.java b/src/main/java/freemarker/ext/dom/SunInternalXalanXPathSupport.java
index 98035f3..e184ac9 100644
--- a/src/main/java/freemarker/ext/dom/SunInternalXalanXPathSupport.java
+++ b/src/main/java/freemarker/ext/dom/SunInternalXalanXPathSupport.java
@@ -45,47 +45,38 @@ import freemarker.template.TemplateModel;
 import freemarker.template.TemplateModelException;
 
 /**
- * This is just the XalanXPathSupport class using the sun internal
- * package names
+ * XPath support implemented on the internal Xalan that is packed into Java under {@code com.sun} packages. This
+ * won't be accessible if Java 9 module access rules are enforced (like if the application is started with
+ * {@code java --illegal-access=deny}), because then accessing {@code com.sun} packages is banned. In such case
+ * {@link XalanXPathSupport} can be used, which however needs the normal Apache Xalan to be present.
  */
-
 class SunInternalXalanXPathSupport implements XPathSupport {
-    
+
     private XPathContext xpathContext = new XPathContext();
         
-    private static final String ERRMSG_RECOMMEND_JAXEN
-            = "(Note that there is no such restriction if you "
-                    + "configure FreeMarker to use Jaxen instead of Xalan.)";
-
-    private static final String ERRMSG_EMPTY_NODE_SET
-            = "Cannot perform an XPath query against an empty node set." + ERRMSG_RECOMMEND_JAXEN;
-    
     synchronized public TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
         if (!(context instanceof Node)) {
-            if (context != null) {
-                if (isNodeList(context)) {
-                    int cnt = ((List) context).size();
-                    if (cnt != 0) {
-                        throw new TemplateModelException(
-                                "Cannot perform an XPath query against a node set of " + cnt
-                                + " nodes. Expecting a single node." + ERRMSG_RECOMMEND_JAXEN);
-                    } else {
-                        throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
-                    }
-                } else {
-                    throw new TemplateModelException(
-                            "Cannot perform an XPath query against a " + context.getClass().getName()
-                            + ". Expecting a single org.w3c.dom.Node.");
-                }
+            if (context == null || isNodeList(context)) {
+                int cnt = context != null ? ((List) context).size() : 0;
+                throw new TemplateModelException(
+                        (cnt != 0
+                                ? "Xalan can't perform an XPath query against a Node Set (contains " + cnt
+                                        + " node(s)). Expecting a single Node."
+                                : "Xalan can't perform an XPath query against an empty Node Set."
+                        )
+                        + " (There's no such restriction if you configure FreeMarker to use Jaxen for XPath.)");
             } else {
-                throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
+                throw new TemplateModelException(
+                        "Can't perform an XPath query against a " + context.getClass().getName()
+                                + ". Expecting a single org.w3c.dom.Node.");
             }
         }
+
         Node node = (Node) context;
         try {
-            XPath xpath = new XPath(xpathQuery, null, customPrefixResolver, XPath.SELECT, null);
+            XPath xpath = new XPath(xpathQuery, null, CUSTOM_PREFIX_RESOLVER, XPath.SELECT, null);
             int ctxtNode = xpathContext.getDTMHandleFromNode(node);
-            XObject xresult = xpath.execute(xpathContext, ctxtNode, customPrefixResolver);
+            XObject xresult = xpath.execute(xpathContext, ctxtNode, CUSTOM_PREFIX_RESOLVER);
             if (xresult instanceof XNodeSet) {
                 NodeListModel result = new NodeListModel(node);
                 result.xpathSupport = this;
@@ -117,7 +108,7 @@ class SunInternalXalanXPathSupport implements XPathSupport {
         }
     }
     
-    private static PrefixResolver customPrefixResolver = new PrefixResolver() {
+    private static final PrefixResolver CUSTOM_PREFIX_RESOLVER = new PrefixResolver() {
         
         public String getNamespaceForPrefix(String prefix, Node node) {
             return getNamespaceForPrefix(prefix);
@@ -143,17 +134,17 @@ class SunInternalXalanXPathSupport implements XPathSupport {
      * Used for generating more intelligent error messages.
      */
     private static boolean isNodeList(Object context) {
-        if (context instanceof List) {
-            List ls = (List) context;
-            int ln = ls.size();
-            for (int i = 0; i < ln; i++) {
-                if (!(ls.get(i) instanceof Node)) {
-                    return false;
-                }
-            }
-            return true;
-        } else {
+        if (!(context instanceof List)) {
             return false;
         }
+
+        List ls = (List) context;
+        int ln = ls.size();
+        for (int i = 0; i < ln; i++) {
+            if (!(ls.get(i) instanceof Node)) {
+                return false;
+            }
+        }
+        return true;
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/freemarker/blob/c4f84789/src/main/java/freemarker/ext/dom/XalanXPathSupport.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/dom/XalanXPathSupport.java b/src/main/java/freemarker/ext/dom/XalanXPathSupport.java
index 2a17813..59ec81c 100644
--- a/src/main/java/freemarker/ext/dom/XalanXPathSupport.java
+++ b/src/main/java/freemarker/ext/dom/XalanXPathSupport.java
@@ -44,48 +44,34 @@ import freemarker.template.TemplateModel;
 import freemarker.template.TemplateModelException;
 
 /**
- * Some glue code that bridges the Xalan XPath stuff (that is built into the JDK 1.4.x)
- * with FreeMarker TemplateModel semantics
+ * XPath support implemented on Apache Xalan.
  */
-
 class XalanXPathSupport implements XPathSupport {
     
     private XPathContext xpathContext = new XPathContext();
         
-    /* I don't recommend Jaxen...
-    private static final String ERRMSG_RECOMMEND_JAXEN
-            = "(Note that there is no such restriction if you "
-                    + "configure FreeMarker to use Jaxen instead of Xalan.)";
-    */
-    private static final String ERRMSG_EMPTY_NODE_SET
-            = "Cannot perform an XPath query against an empty node set."; /* " + ERRMSG_RECOMMEND_JAXEN;*/
-    
     synchronized public TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
         if (!(context instanceof Node)) {
-            if (context != null) {
-                if (isNodeList(context)) {
-                    int cnt = ((List) context).size();
-                    if (cnt != 0) {
-                        throw new TemplateModelException(
-                                "Cannot perform an XPath query against a node set of " + cnt
-                                + " nodes. Expecting a single node."/* " + ERRMSG_RECOMMEND_JAXEN*/);
-                    } else {
-                        throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
-                    }
-                } else {
-                    throw new TemplateModelException(
-                            "Cannot perform an XPath query against a " + context.getClass().getName()
-                            + ". Expecting a single org.w3c.dom.Node.");
-                }
+            if (context == null || isNodeList(context)) {
+                int cnt = context != null ? ((List) context).size() : 0;
+                throw new TemplateModelException(
+                        (cnt != 0
+                                ? "Xalan can't perform an XPath query against a Node Set (contains " + cnt
+                                        + " node(s)). Expecting a single Node."
+                                : "Xalan can't perform an XPath query against an empty Node Set."
+                        )
+                                + " (There's no such restriction if you configure FreeMarker to use Jaxen for XPath.)");
             } else {
-                throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
+                throw new TemplateModelException(
+                        "Can't perform an XPath query against a " + context.getClass().getName()
+                                + ". Expecting a single org.w3c.dom.Node.");
             }
         }
         Node node = (Node) context;
         try {
-            XPath xpath = new XPath(xpathQuery, null, customPrefixResolver, XPath.SELECT, null);
+            XPath xpath = new XPath(xpathQuery, null, CUSTOM_PREFIX_RESOLVER, XPath.SELECT, null);
             int ctxtNode = xpathContext.getDTMHandleFromNode(node);
-            XObject xresult = xpath.execute(xpathContext, ctxtNode, customPrefixResolver);
+            XObject xresult = xpath.execute(xpathContext, ctxtNode, CUSTOM_PREFIX_RESOLVER);
             if (xresult instanceof XNodeSet) {
                 NodeListModel result = new NodeListModel(node);
                 result.xpathSupport = this;
@@ -117,7 +103,7 @@ class XalanXPathSupport implements XPathSupport {
         }
     }
     
-    private static PrefixResolver customPrefixResolver = new PrefixResolver() {
+    private static final PrefixResolver CUSTOM_PREFIX_RESOLVER = new PrefixResolver() {
         
         public String getNamespaceForPrefix(String prefix, Node node) {
             return getNamespaceForPrefix(prefix);
@@ -138,22 +124,22 @@ class XalanXPathSupport implements XPathSupport {
             return false;
         }
     };
-    
+
     /**
      * Used for generating more intelligent error messages.
      */
     private static boolean isNodeList(Object context) {
-        if (context instanceof List) {
-            List ls = (List) context;
-            int ln = ls.size();
-            for (int i = 0; i < ln; i++) {
-                if (!(ls.get(i) instanceof Node)) {
-                    return false;
-                }
-            }
-            return true;
-        } else {
+        if (!(context instanceof List)) {
             return false;
         }
+
+        List ls = (List) context;
+        int ln = ls.size();
+        for (int i = 0; i < ln; i++) {
+            if (!(ls.get(i) instanceof Node)) {
+                return false;
+            }
+        }
+        return true;
     }
 }
\ No newline at end of file