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/02/15 14:04:12 UTC

svn commit: r1070877 - in /commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3: internal/DigesterImpl.java utils/ utils/InputSources.java xmlrules/FromXmlRulesModule.java

Author: simonetripodi
Date: Tue Feb 15 13:04:11 2011
New Revision: 1070877

URL: http://svn.apache.org/viewvc?rev=1070877&view=rev
Log:
moved utility methods to open InputSource from different kind of sources into a proper class, since they can be reused in the FromXmlRulesModule as well

Added:
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/utils/
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/utils/InputSources.java   (with props)
Modified:
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/xmlrules/FromXmlRulesModule.java

Modified: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java?rev=1070877&r1=1070876&r2=1070877&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java (original)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java Tue Feb 15 13:04:11 2011
@@ -17,6 +17,8 @@
  */
 package org.apache.commons.digester3.internal;
 
+import static org.apache.commons.digester3.utils.InputSources.*;
+
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -24,7 +26,6 @@ import java.io.Reader;
 import java.lang.reflect.InvocationTargetException;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.EmptyStackException;
 import java.util.HashMap;
@@ -1023,11 +1024,7 @@ public final class DigesterImpl implemen
      * {@inheritDoc}
      */
     public Object parse(File file) throws IOException, SAXException {
-        if (file == null) {
-            throw new IllegalArgumentException("File to parse must be not null");
-        }
-
-        return this.parse(file.toURI().toURL());
+        return this.parse(createInputSourceFromFile(file));
     }
 
     /**
@@ -1052,87 +1049,28 @@ public final class DigesterImpl implemen
      * {@inheritDoc}
      */
     public Object parse(InputStream input) throws IOException, SAXException {
-        if (input == null) {
-            throw new IllegalArgumentException("InputStream to parse must be not null");
-        }
-
-        return this.parse(new InputSource(input));
+        return this.parse(createInputSourceFromInputStream(input));
     }
 
     /**
      * {@inheritDoc}
      */
     public Object parse(Reader reader) throws IOException, SAXException {
-        if (reader == null) {
-            throw new IllegalArgumentException("Reader to parse must be not null");
-        }
-
-        return this.parse(new InputSource(reader));
+        return this.parse(createInputSourceFromReader(reader));
     }
 
     /**
      * {@inheritDoc}
      */
     public Object parse(String uri) throws IOException, SAXException {
-        if (uri == null) {
-            throw new IllegalArgumentException("String URI to parse must be not null");
-        }
-
-        return this.parse(new URL(uri));
+        return this.parse(createInputSourceFromUri(uri));
     }
 
     /**
      * {@inheritDoc}
      */
     public Object parse(URL url) throws IOException, SAXException {
-        if (url == null) {
-            throw new IllegalArgumentException("URL to parse is null");
-        }
-
-        InputSource inputSource = createInputSourceFromURL(url);
-        inputSource.setSystemId(url.toString());
-        return this.parse(inputSource);
-    }
-
-    /**
-     * Given a URL, return an InputSource that reads from that URL.
-     * <p>
-     * Ideally this function would not be needed and code could just use
-     * <code>new InputSource(entityURL)</code>. Unfortunately it appears
-     * that when the entityURL points to a file within a jar archive a
-     * caching mechanism inside the InputSource implementation causes a
-     * file-handle to the jar file to remain open. On Windows systems
-     * this then causes the jar archive file to be locked on disk
-     * ("in use") which makes it impossible to delete the jar file -
-     * and that really stuffs up "undeploy" in webapps in particular.
-     * <p>
-     * In JDK1.4 and later, Apache XercesJ is used as the xml parser.
-     * The InputSource object provided is converted into an XMLInputSource,
-     * and eventually passed to an instance of XMLDocumentScannerImpl to
-     * specify the source data to be converted into tokens for the rest
-     * of the XMLReader code to handle. XMLDocumentScannerImpl calls
-     * fEntityManager.startDocumentEntity(source), where fEntityManager
-     * is declared in ancestor class XMLScanner to be an XMLEntityManager. In
-     * that class, if the input source stream is null, then:
-     * <pre>
-     *  URL location = new URL(expandedSystemId);
-     *  URLConnection connect = location.openConnection();
-     *  if (connect instanceof HttpURLConnection) {
-     *    setHttpProperties(connect,xmlInputSource);
-     *  }
-     *  stream = connect.getInputStream();
-     * </pre>
-     * This method pretty much duplicates the standard behaviour, except
-     * that it calls URLConnection.setUseCaches(false) before opening
-     * the connection.
-     */
-    private static InputSource createInputSourceFromURL(URL url) throws MalformedURLException, IOException {
-        URLConnection connection = url.openConnection();
-        connection.setUseCaches(false);
-        InputStream stream = connection.getInputStream();
-        InputSource source = new InputSource(stream);
-        source.setSystemId(url.toExternalForm());
-        return source;
+        return this.parse(createInputSourceFromURL(url));
     }
 
     /**

Added: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/utils/InputSources.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/utils/InputSources.java?rev=1070877&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/utils/InputSources.java (added)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/utils/InputSources.java Tue Feb 15 13:04:11 2011
@@ -0,0 +1,155 @@
+/* $Id$
+ *
+ * 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 org.apache.commons.digester3.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.xml.sax.InputSource;
+
+/**
+ * Utilities class to manipulate {@code org.xml.sax.InputSource}.
+ */
+public final class InputSources {
+
+    /**
+     * This class can't be instantiated
+     */
+    private InputSources() {
+        // do nothing
+    }
+
+    /**
+     * Opens a new {@code org.xml.sax.InputSource} given the XML file.
+     *
+     * @param file The XML file
+     * @return The {@code org.xml.sax.InputSource} to read the given XML file
+     * @throws IOException if any error occurs while opening the file
+     */
+    public static InputSource createInputSourceFromFile(File file) throws IOException {
+        if (file == null) {
+            throw new IllegalArgumentException("File to parse must be not null");
+        }
+
+        return createInputSourceFromURL(file.toURI().toURL());
+    }
+
+    /**
+     * Opens a new {@code org.xml.sax.InputSource} given the URI that contains the XML to parse.
+     *
+     * @param uri The URI that contains the XML to parse
+     * @return The {@code org.xml.sax.InputSource} to read the given URI
+     * @throws IOException if any error occurs while opening the URI
+     */
+    public static InputSource createInputSourceFromUri(String uri) throws IOException {
+        if (uri == null) {
+            throw new IllegalArgumentException("String URI to parse must be not null");
+        }
+
+        return createInputSourceFromURL(new URL(uri));
+    }
+
+    /**
+     * Given a URL, return an InputSource that reads from that URL.
+     * <p>
+     * Ideally this function would not be needed and code could just use
+     * <code>new InputSource(entityURL)</code>. Unfortunately it appears
+     * that when the entityURL points to a file within a jar archive a
+     * caching mechanism inside the InputSource implementation causes a
+     * file-handle to the jar file to remain open. On Windows systems
+     * this then causes the jar archive file to be locked on disk
+     * ("in use") which makes it impossible to delete the jar file -
+     * and that really stuffs up "undeploy" in webapps in particular.
+     * <p>
+     * In JDK1.4 and later, Apache XercesJ is used as the xml parser.
+     * The InputSource object provided is converted into an XMLInputSource,
+     * and eventually passed to an instance of XMLDocumentScannerImpl to
+     * specify the source data to be converted into tokens for the rest
+     * of the XMLReader code to handle. XMLDocumentScannerImpl calls
+     * fEntityManager.startDocumentEntity(source), where fEntityManager
+     * is declared in ancestor class XMLScanner to be an XMLEntityManager. In
+     * that class, if the input source stream is null, then:
+     * <pre>
+     *  URL location = new URL(expandedSystemId);
+     *  URLConnection connect = location.openConnection();
+     *  if (connect instanceof HttpURLConnection) {
+     *    setHttpProperties(connect,xmlInputSource);
+     *  }
+     *  stream = connect.getInputStream();
+     * </pre>
+     * This method pretty much duplicates the standard behaviour, except
+     * that it calls URLConnection.setUseCaches(false) before opening
+     * the connection.
+     */
+    public static InputSource createInputSourceFromURL(URL url) throws IOException {
+        if (url == null) {
+            throw new IllegalArgumentException("Parameter 'url' must be not null");
+        }
+        URLConnection connection = url.openConnection();
+        connection.setUseCaches(false);
+        InputStream stream = connection.getInputStream();
+        InputSource source = new InputSource(stream);
+        source.setSystemId(url.toExternalForm());
+        return source;
+    }
+
+    /**
+     * Opens a new {@code org.xml.sax.InputSource} given the XML in textual form.
+     *
+     * @param xmlText The XML in textual form
+     * @return The {@code org.xml.sax.InputSource} to read the given XML in textual form
+     */
+    public static InputSource createInputSourceFromInputStream(InputStream input) {
+        if (input == null) {
+            throw new IllegalArgumentException("Parameter 'input' must be not null");
+        }
+        return new InputSource(input);
+    }
+
+    /**
+     * Opens a new {@code org.xml.sax.InputSource} given the XML in textual form.
+     *
+     * @param xmlText The XML in textual form
+     * @return The {@code org.xml.sax.InputSource} to read the given XML in textual form
+     */
+    public static InputSource createInputSourceFromString(String xmlText) {
+        if (xmlText == null) {
+            throw new IllegalArgumentException("Parameter 'xmlText' must be not null");
+        }
+        return createInputSourceFromReader(new StringReader(xmlText));
+    }
+
+    /**
+     * Opens a new {@code org.xml.sax.InputSource} given a {@code java.io.Reader}.
+     *
+     * @param xmlText The XML {@code java.io.Reader}
+     * @return The {@code org.xml.sax.InputSource} to read the given XML {@code java.io.Reader}
+     */
+    public static InputSource createInputSourceFromReader(Reader reader) {
+        if (reader == null) {
+            throw new IllegalArgumentException("Parameter 'reader' must be not null");
+        }
+        return new InputSource(reader);
+    }
+
+}

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/utils/InputSources.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/utils/InputSources.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/utils/InputSources.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/xmlrules/FromXmlRulesModule.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/xmlrules/FromXmlRulesModule.java?rev=1070877&r1=1070876&r2=1070877&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/xmlrules/FromXmlRulesModule.java (original)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/xmlrules/FromXmlRulesModule.java Tue Feb 15 13:04:11 2011
@@ -18,13 +18,22 @@
 package org.apache.commons.digester3.xmlrules;
 
 import static org.apache.commons.digester3.DigesterLoader.newLoader;
-
+import static org.apache.commons.digester3.utils.InputSources.createInputSourceFromFile;
+import static org.apache.commons.digester3.utils.InputSources.createInputSourceFromInputStream;
+import static org.apache.commons.digester3.utils.InputSources.createInputSourceFromReader;
+import static org.apache.commons.digester3.utils.InputSources.createInputSourceFromURL;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
 import java.net.URL;
 
 import org.apache.commons.digester3.DigesterLoadingException;
 import org.apache.commons.digester3.RulesBinder;
 import org.apache.commons.digester3.RulesModule;
 import org.apache.commons.digester3.xmlrules.metaparser.XmlRulesModule;
+import org.xml.sax.InputSource;
 
 /**
  * 
@@ -35,15 +44,26 @@ public final class FromXmlRulesModule im
 
     private  static final String DIGESTER_DTD_PATH = "digester-rules.dtd";
 
-    private final URL xmlRules;
+    private final InputSource xmlRules;
 
     private URL xmlRulesDtdUrl = this.getClass().getResource(DIGESTER_DTD_PATH);
 
-    public FromXmlRulesModule(String path) {
+    /**
+     * 
+     * @param path
+     * @throws IOException
+     */
+    public FromXmlRulesModule(String path) throws IOException {
         this(path, Thread.currentThread().getContextClassLoader());
     }
 
-    public FromXmlRulesModule(String path, ClassLoader classLoader) {
+    /**
+     * 
+     * @param path
+     * @param classLoader
+     * @throws IOException
+     */
+    public FromXmlRulesModule(String path, ClassLoader classLoader) throws IOException {
         if (path == null) {
             throw new DigesterLoadingException("Parameter 'path' must not be null");
         }
@@ -54,10 +74,48 @@ public final class FromXmlRulesModule im
         if (xmlRules == null) {
             throw new DigesterLoadingException(String.format("XML Rules '%s' not found on ", path));
         }
-        this.xmlRules = xmlRules;
+        this.xmlRules = createInputSourceFromURL(xmlRules);
+    }
+
+    /**
+     * 
+     * @param file
+     * @throws IOException
+     */
+    public FromXmlRulesModule(File file) throws IOException {
+        this(createInputSourceFromFile(file));
+    }
+
+    /**
+     * 
+     * @param xmlRulesURL
+     * @throws IOException
+     */
+    public FromXmlRulesModule(URL xmlRulesURL) throws IOException {
+        this(createInputSourceFromURL(xmlRulesURL));
+    }
+
+    /**
+     * 
+     * @param input
+     */
+    public FromXmlRulesModule(InputStream input) {
+        this(createInputSourceFromInputStream(input));
     }
 
-    public FromXmlRulesModule(URL xmlRules) {
+    /**
+     * 
+     * @param reader
+     */
+    public FromXmlRulesModule(Reader reader) {
+        this(createInputSourceFromReader(reader));
+    }
+
+    /**
+     * 
+     * @param xmlRules
+     */
+    public FromXmlRulesModule(InputSource xmlRules) {
         if (xmlRules == null) {
             throw new DigesterLoadingException("Parameter 'xmlRules' must not be null");
         }
@@ -91,7 +149,8 @@ public final class FromXmlRulesModule im
      */
     @Override
     public String toString() {
-        return String.format("FromXmlRulesModule[%s]", this.xmlRules);
+        return String.format("FromXmlRulesModule[%s]",
+                this.xmlRules.getSystemId() != null ? this.xmlRules.getSystemId() : this.xmlRules.toString());
     }
 
 }