You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/05/11 22:12:13 UTC

svn commit: r1102062 - /commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java

Author: simonetripodi
Date: Wed May 11 20:12:13 2011
New Revision: 1102062

URL: http://svn.apache.org/viewvc?rev=1102062&view=rev
Log:
getRoot()/parse() methods are able to magic cast returned objects

Modified:
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java

Modified: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java?rev=1102062&r1=1102061&r2=1102062&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java (original)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java Wed May 11 20:12:13 2011
@@ -1639,7 +1639,7 @@ public class Digester extends DefaultHan
      * @exception IOException if an input/output error occurs
      * @exception SAXException if a parsing exception occurs
      */
-    public Object parse(File file) throws IOException, SAXException {
+    public <T> T parse(File file) throws IOException, SAXException {
 
         if (file == null) {
             throw new IllegalArgumentException("File to parse is null");
@@ -1650,7 +1650,7 @@ public class Digester extends DefaultHan
         input.setSystemId(file.toURI().toURL().toString());
         getXMLReader().parse(input);
         cleanup();
-        return (root);
+        return (this.<T>getRoot());
 
     }   
     /**
@@ -1662,7 +1662,7 @@ public class Digester extends DefaultHan
      * @exception IOException if an input/output error occurs
      * @exception SAXException if a parsing exception occurs
      */
-    public Object parse(InputSource input) throws IOException, SAXException {
+    public <T> T parse(InputSource input) throws IOException, SAXException {
  
         if (input == null) {
             throw new IllegalArgumentException("InputSource to parse is null");
@@ -1671,7 +1671,7 @@ public class Digester extends DefaultHan
         configure();
         getXMLReader().parse(input);
         cleanup();
-        return (root);
+        return this.<T>getRoot();
 
     }
 
@@ -1685,7 +1685,7 @@ public class Digester extends DefaultHan
      * @exception IOException if an input/output error occurs
      * @exception SAXException if a parsing exception occurs
      */
-    public Object parse(InputStream input) throws IOException, SAXException {
+    public <T> T parse(InputStream input) throws IOException, SAXException {
 
         if (input == null) {
             throw new IllegalArgumentException("InputStream to parse is null");
@@ -1695,7 +1695,7 @@ public class Digester extends DefaultHan
         InputSource is = new InputSource(input);
         getXMLReader().parse(is);
         cleanup();
-        return (root);
+        return (this.<T>getRoot());
 
     }
 
@@ -1709,7 +1709,7 @@ public class Digester extends DefaultHan
      * @exception IOException if an input/output error occurs
      * @exception SAXException if a parsing exception occurs
      */
-    public Object parse(Reader reader) throws IOException, SAXException {
+    public <T> T parse(Reader reader) throws IOException, SAXException {
 
         if (reader == null) {
             throw new IllegalArgumentException("Reader to parse is null");
@@ -1719,7 +1719,7 @@ public class Digester extends DefaultHan
         InputSource is = new InputSource(reader);
         getXMLReader().parse(is);
         cleanup();
-        return (root);
+        return (this.<T>getRoot());
 
     }
 
@@ -1733,7 +1733,7 @@ public class Digester extends DefaultHan
      * @exception IOException if an input/output error occurs
      * @exception SAXException if a parsing exception occurs
      */
-    public Object parse(String uri) throws IOException, SAXException {
+    public <T> T parse(String uri) throws IOException, SAXException {
 
         if (uri == null) {
             throw new IllegalArgumentException("String URI to parse is null");
@@ -1743,7 +1743,7 @@ public class Digester extends DefaultHan
         InputSource is = createInputSourceFromURL(uri);
         getXMLReader().parse(is);
         cleanup();
-        return (root);
+        return (this.<T>getRoot());
 
     }
 
@@ -1759,7 +1759,7 @@ public class Digester extends DefaultHan
      *
      * @since 1.8
      */
-    public Object parse(URL url) throws IOException, SAXException {
+    public <T> T parse(URL url) throws IOException, SAXException {
 
         if (url == null) {
             throw new IllegalArgumentException("URL to parse is null");
@@ -1769,7 +1769,7 @@ public class Digester extends DefaultHan
         InputSource is = createInputSourceFromURL(url);
         getXMLReader().parse(is);
         cleanup();
-        return (root);
+        return (this.<T>getRoot());
 
     }
 
@@ -2900,8 +2900,8 @@ public class Digester extends DefaultHan
      * @return the root object that has been created after parsing
      *  or null if the digester has not parsed any XML yet.
      */
-    public Object getRoot() {
-        return root;
+    public <T> T getRoot() {
+        return this.<T>npeSafeCast(root);
     }
     
     /**
@@ -3167,5 +3167,25 @@ public class Digester extends DefaultHan
     public SAXException createSAXException(String message) {
         return createSAXException(message, null);
     }
-    
+
+    /**
+     * Helps casting the input object to given type,
+     * avoiding NPEs.
+     *
+     * @since 3.0
+     * @param <T> the type th einput object has to be cast.
+     * @param obj the object has to be cast.
+     * @return the casted object, if input object is not null,
+     *         null otherwise.
+     */
+    private <T> T npeSafeCast(Object obj) {
+        if (obj == null) {
+            return null;
+        }
+
+        @SuppressWarnings("unchecked")
+        T result = (T) obj;
+        return result;
+    }
+
 }