You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2006/06/01 16:15:52 UTC

svn commit: r410867 - /geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/CatCommand.java

Author: jdillon
Date: Thu Jun  1 07:15:51 2006
New Revision: 410867

URL: http://svn.apache.org/viewvc?rev=410867&view=rev
Log:
Support URLs

Modified:
    geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/CatCommand.java

Modified: geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/CatCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/CatCommand.java?rev=410867&r1=410866&r2=410867&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/CatCommand.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/CatCommand.java Thu Jun  1 07:15:51 2006
@@ -31,9 +31,12 @@
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.MalformedURLException;
 
 /**
- * Concatenate and print files.
+ * Concatenate and print files and/or URLs.
  *
  * @version $Id$
  */
@@ -41,66 +44,66 @@
     extends CommandSupport
 {
     private boolean displayLineNumbers = false;
-    
+
     public CatCommand() {
         super("cat");
     }
-    
+
     protected int doExecute(final String[] args) throws Exception {
         assert args != null;
-        
+
         //
         // TODO: Optimize, move common code to CommandSupport
         //
-        
+
         IO io = getIO();
-        
+
         Options options = new Options();
-        
+
         options.addOption(OptionBuilder.withLongOpt("help")
             .withDescription("Display this help message")
             .create('h'));
-        
+
         options.addOption(OptionBuilder
             .withDescription("Number the output lines, starting at 1")
             .create('n'));
-        
+
         CommandLineParser parser = new PosixParser();
         CommandLine line = parser.parse(options, args);
-        
+
         if (line.hasOption('h')) {
-            io.out.println(getName() + " -- concatenate and print files");
+            io.out.println(getName() + " -- concatenate and print files and/or URLs");
             io.out.println();
-            
+
             HelpFormatter formatter = new HelpFormatter();
             formatter.printHelp(
                 io.out,
                 80, // width (FIXME: Should pull from gshell.columns variable)
-                getName() + " [options] [file ...]",
+                getName() + " [options] [<file|url> ...]",
                 "",
                 options,
                 4, // left pad
                 4, // desc pad
                 "",
                 false); // auto usage
-            
+
             io.out.println();
-            
+
             return Command.SUCCESS;
         }
-        
+
         if (line.hasOption('n')) {
             displayLineNumbers = true;
         }
-        
+
         cat(line.getArgs());
-        
+
         return Command.SUCCESS;
     }
-    
+
     private void cat(final String[] files) throws IOException {
         assert files != null;
-        
+
         IO io = getIO();
 
         for (String filename : files) {
@@ -113,10 +116,20 @@
             if (files.length == 1 && "-".equals(files[0])) {
                 log.info("Printing STDIN");
                 reader = new BufferedReader(io.in);
-            } else {
-                File file = new File(filename);
-                log.info("Printing file: " + file);
-                reader = new BufferedReader(new FileReader(file));
+            }
+            else {
+                // First try a URL
+                try {
+                    URL url = new URL(filename);
+                    log.info("Printing URL: " + url);
+                    reader = new BufferedReader(new InputStreamReader(url.openStream()));
+                }
+                catch (MalformedURLException ignore) {
+                    // They try a file
+                    File file = new File(filename);
+                    log.info("Printing file: " + file);
+                    reader = new BufferedReader(new FileReader(file));
+                }
             }
 
             String line;