velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1857729 - in /velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools: ./ config/ generic/
Date Thu, 18 Apr 2019 10:02:04 GMT
Author: cbrisson
Date: Thu Apr 18 10:02:04 2019
New Revision: 1857729

URL: http://svn.apache.org/viewvc?rev=1857729&view=rev
Log:
[tools/model] Minor tweaks and code cleaning in generic tools

Modified:
    velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java
    velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java
    velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/XmlUtils.java
    velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java
    velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java
    velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ValueParser.java
    velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/XmlTool.java

Modified: velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java?rev=1857729&r1=1857728&r2=1857729&view=diff
==============================================================================
--- velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java
(original)
+++ velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java
Thu Apr 18 10:02:04 2019
@@ -336,7 +336,7 @@ public class ClassUtils
      * @return method object
      * @throws SecurityException if not granted
      */
-    public static Method findMethod(Class clazz, String name, Class[] params)
+    public static Method findMethod(Class clazz, String name, Class... params)
         throws SecurityException
     {
         try
@@ -359,7 +359,7 @@ public class ClassUtils
      * @return
      * @throws SecurityException if not allowed
      */
-    public static Method findDeclaredMethod(Class clazz, String name, Class[] params)
+    public static Method findDeclaredMethod(Class clazz, String name, Class... params)
         throws SecurityException
     {
         try

Modified: velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java?rev=1857729&r1=1857728&r2=1857729&view=diff
==============================================================================
--- velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java
(original)
+++ velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ToolManager.java
Thu Apr 18 10:02:04 2019
@@ -321,7 +321,7 @@ public class ToolManager
      * @return <code>true</code> if this tool manager has request scoped tools,
      *         <code>false</code> otherwise
      */
-    protected boolean hasRequestTools()
+    public boolean hasRequestTools()
     {
         return hasTools(Scope.REQUEST);
     }
@@ -330,7 +330,7 @@ public class ToolManager
      * Get the toolbox for request scoped tools
      * @return toolbox of request scoped tools
      */
-    protected Toolbox getRequestToolbox()
+    public Toolbox getRequestToolbox()
     {
         return createToolbox(Scope.REQUEST);
     }
@@ -340,7 +340,7 @@ public class ToolManager
      * @return <code>true</code> if this tool manager has application scoped
tools,
      *         <code>false</code> otherwise
      */
-    protected boolean hasApplicationTools()
+    public boolean hasApplicationTools()
     {
         return hasTools(Scope.APPLICATION);
     }
@@ -349,7 +349,7 @@ public class ToolManager
      * Get the toolbox for application scoped tools
      * @return toolbox of application scoped tools
      */
-    protected Toolbox getApplicationToolbox()
+    public Toolbox getApplicationToolbox()
     {
         if (this.application == null && hasApplicationTools())
         {

Modified: velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/XmlUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/XmlUtils.java?rev=1857729&r1=1857728&r2=1857729&view=diff
==============================================================================
--- velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/XmlUtils.java
(original)
+++ velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/XmlUtils.java
Thu Apr 18 10:02:04 2019
@@ -35,6 +35,8 @@ import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.lang.ref.SoftReference;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Stack;
 import java.util.concurrent.LinkedBlockingDeque;
 
@@ -89,26 +91,11 @@ public final class XmlUtils
 
     private static boolean canReuseBuilders = false;
 
-    private static final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+    private static final DocumentBuilderFactory builderFactory = createDocumentBuilderFactory();
 
-    private static final ThreadLocal<DocumentBuilder> reusableBuilder
-        = new ThreadLocal<DocumentBuilder>() {
-        @Override
-        protected DocumentBuilder initialValue() {
-            try
+    public static final DocumentBuilderFactory createDocumentBuilderFactory()
             {
-                LOGGER.trace("Created a new document builder");
-                return builderFactory.newDocumentBuilder();
-            }
-            catch (ParserConfigurationException e)
-            {
-                throw new RuntimeException(e);
-            }
-        }
-    };
-
-    static
-    {
+        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
         // Namespace support is required for <os:> elements
         builderFactory.setNamespaceAware(true);
 
@@ -157,6 +144,27 @@ public final class XmlUtils
             LOGGER.info("Error parsing secure XML: ", e);
         }
 
+        return builderFactory;
+    }
+
+    private static final ThreadLocal<DocumentBuilder> reusableBuilder
+        = new ThreadLocal<DocumentBuilder>() {
+        @Override
+        protected DocumentBuilder initialValue() {
+            try
+            {
+                LOGGER.trace("Created a new document builder");
+                return builderFactory.newDocumentBuilder();
+            }
+            catch (ParserConfigurationException e)
+            {
+                throw new RuntimeException(e);
+            }
+        }
+    };
+
+    static
+    {
         try
         {
             DocumentBuilder builder = builderFactory.newDocumentBuilder();
@@ -246,7 +254,7 @@ public final class XmlUtils
 
     /**
      * Release the given document builder
-     * @param document builder
+     * @param builder document builder
      */
     private static synchronized void releaseBuilder(DocumentBuilder builder)
     {
@@ -384,18 +392,57 @@ public final class XmlUtils
         return parse(new StringReader(xml));
     }
 
-    public static NodeList search(String xpath, Node context)
+    /**
+     * Search for nodes using an XPath expression
+     * @param xpath XPath expression
+     * @param context evaluation context
+     * @return org.w3c.NodeList of found nodes
+     * @throws XPathExpressionException
+     */
+    public static NodeList search(String xpath, Node context) throws XPathExpressionException
     {
         NodeList ret = null;
-        try
-        {
             XPath xp = XPathFactory.newInstance().newXPath();
             XPathExpression exp = xp.compile(xpath);
             ret = (NodeList)exp.evaluate(context, XPathConstants.NODESET);
+        return ret;
         }
-        catch (XPathExpressionException xpe)
+
+    /**
+     * Search for nodes using an XPath expression
+     * @param xpath XPath expression
+     * @param context evaluation context
+     * @return List of found nodes
+     * @throws XPathExpressionException
+     */
+    public static List<Node> getNodes(String xpath, Node context) throws XPathExpressionException
+        {
+        List<Node> ret = new ArrayList<>();
+        NodeList lst = search(xpath, context);
+        for (int i = 0; i < lst.getLength(); ++i)
         {
-            LOGGER.error("could not process xpath expression {}", xpath, xpe);
+            ret.add(lst.item(i));
+        }
+        return ret;
+    }
+
+
+    /**
+     * Search for elements using an XPath expression
+     * @param xpath XPath expression
+     * @param context evaluation context
+     * @return List of found elements
+     * @throws XPathExpressionException
+     */
+    public static List<Element> getElements(String xpath, Node context) throws XPathExpressionException
+    {
+        List<Element> ret = new ArrayList<>();
+        NodeList lst = search(xpath, context);
+        for (int i = 0; i < lst.getLength(); ++i)
+        {
+            // will throw a ClassCastExpression if Node is not an Element,
+            // that's what we want
+            ret.add((Element)lst.item(i));
         }
         return ret;
     }

Modified: velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java?rev=1857729&r1=1857728&r2=1857729&view=diff
==============================================================================
--- velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java
(original)
+++ velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java
Thu Apr 18 10:02:04 2019
@@ -31,6 +31,18 @@ public class ConfigurationException exte
 
     private final Object source;
 
+    public ConfigurationException(String message)
+    {
+        super(message);
+        source = null;
+    }
+
+    public ConfigurationException(String message, Throwable cause)
+    {
+        super(message, cause);
+        source = null;
+    }
+
     public ConfigurationException(Data data, Throwable cause)
     {
         super(cause);

Modified: velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java?rev=1857729&r1=1857728&r2=1857729&view=diff
==============================================================================
--- velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java
(original)
+++ velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/config/ConfigurationUtils.java
Thu Apr 18 10:02:04 2019
@@ -47,6 +47,8 @@ public class ConfigurationUtils
         "/org/apache/velocity/tools/generic/tools.xml";
     public static final String VIEW_DEFAULTS_PATH =
         "/org/apache/velocity/tools/view/tools.xml";
+    public static final String MODEL_DEFAULTS_PATH =
+        "/org/apache/velocity/tools/model/tools.xml";
 
     public static final String AUTOLOADED_XML_PATH = "tools.xml";
     public static final String AUTOLOADED_PROPS_PATH = "tools.properties";

Modified: velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ValueParser.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ValueParser.java?rev=1857729&r1=1857728&r2=1857729&view=diff
==============================================================================
--- velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ValueParser.java
(original)
+++ velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ValueParser.java
Thu Apr 18 10:02:04 2019
@@ -673,7 +673,7 @@ public class ValueParser extends FormatC
      * @param subkey subkey to search for
      * @return the map of found values
      */
-    protected ValueParser getSubkey(String subkey)
+    public ValueParser getSubkey(String subkey)
     {
         if (!hasSubkeys() || subkey == null || subkey.length() == 0)
         {

Modified: velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/XmlTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/XmlTool.java?rev=1857729&r1=1857728&r2=1857729&view=diff
==============================================================================
--- velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/XmlTool.java
(original)
+++ velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/XmlTool.java
Thu Apr 18 10:02:04 2019
@@ -41,6 +41,8 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
 
+import javax.xml.xpath.XPathExpressionException;
+
 /**
  * <p>Tool for reading/navigating XML files, with XPath expressions support.</p>
  *
@@ -571,7 +573,16 @@ public class XmlTool extends SafeConfig
         List<Node> found = new ArrayList<Node>();
         for (Node n : nodes)
         {
-            NodeList lst = XmlUtils.search(xpath, n);
+            NodeList lst;
+            try
+            {
+                lst = XmlUtils.search(xpath, n);
+            }
+            catch(XPathExpressionException xpee)
+            {
+                getLog().error("could not parse XML expression '{}'", xpath, xpee);
+                return null;
+            }
             if (lst != null)
             {
                 for (int i = 0; i < lst.getLength(); ++i)



Mime
View raw message