View Javadoc

1   package fr.in2p3.jsaga.command;
2   
3   import fr.in2p3.jsaga.helpers.SAGAPattern;
4   import org.apache.commons.cli.*;
5   import org.ogf.saga.namespace.*;
6   import org.ogf.saga.session.Session;
7   import org.ogf.saga.session.SessionFactory;
8   import org.ogf.saga.url.URL;
9   import org.ogf.saga.url.URLFactory;
10  
11  import java.util.List;
12  import java.util.regex.Matcher;
13  import java.util.regex.Pattern;
14  
15  /* ***************************************************
16  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
17  * ***             http://cc.in2p3.fr/             ***
18  * ***************************************************
19  * File:   FileList
20  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
21  * Date:   4 avr. 2007
22  * ***************************************************
23  * Description:                                      */
24  /**
25   *
26   */
27  public class NamespaceList extends AbstractCommand {
28      private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
29      private static final String OPT_LONG = "l", LONGOPT_LONG = "long";
30      private static final String OPT_NOWILDCARD = "n", LONGOPT_NOWILDCARD = "no-wildcard";
31  
32      public NamespaceList() {
33          super("jsaga-ls", new String[]{"URL"}, new String[]{OPT_HELP, LONGOPT_HELP});
34      }
35  
36      public static void main(String[] args) throws Exception {
37          NamespaceList command = new NamespaceList();
38          CommandLine line = command.parse(args);
39          if (line.hasOption(OPT_HELP))
40          {
41              command.printHelpAndExit(null);
42          }
43          else
44          {
45              // get URL and pattern from arguments
46              String arg = command.m_nonOptionValues[0];
47              URL url;
48              String pattern;
49              if (SAGAPattern.hasWildcard(arg) && !line.hasOption(OPT_NOWILDCARD)) {
50                  Matcher matcher = Pattern.compile("((.*)/)*(.*/)/*").matcher(arg);
51                  if (matcher.matches() && matcher.groupCount()>1) {
52                      url = URLFactory.createURL(matcher.group(1));
53                      pattern = matcher.group(3);
54                  } else {
55                      url = URLFactory.createURL(arg.substring(0, arg.lastIndexOf('/')+1));
56                      pattern = arg.substring(arg.lastIndexOf('/')+1);
57                  }
58              } else {
59                  url = URLFactory.createURL(arg);
60                  pattern = null;
61              }
62  
63              // get list
64              Session session = SessionFactory.createSession(true);
65              NSDirectory dir = NSFactory.createNSDirectory(session, url, Flags.NONE.getValue());
66              List<URL> list = dir.list(pattern, Flags.NONE.getValue());
67  
68              if (line.hasOption(OPT_LONG)) {
69                  // display list
70                  EntryLongFormat formatter = new EntryLongFormat(dir);
71                  for (URL entry : list) {
72                      System.out.println(formatter.toString(entry));
73                  }
74                  // close connection
75                  dir.close();
76              } else {
77                  // close connection
78                  dir.close();
79                  // display list
80                  for (URL entry : list) {
81                      // getString() decodes the URL, while toString() does not
82                      System.out.println(entry.getString());
83                  }
84              }
85              System.exit(0);
86          }
87      }
88  
89      protected Options createOptions() {
90          Options opt = new Options();
91          opt.addOption(OptionBuilder.withDescription("Display this help and exit")
92                  .withLongOpt(LONGOPT_HELP)
93                  .create(OPT_HELP));
94          opt.addOption(OptionBuilder.withDescription("Use a long listing format")
95                  .isRequired(false)
96                  .withLongOpt(LONGOPT_LONG)
97                  .create(OPT_LONG));
98          opt.addOption(OptionBuilder.withDescription("Disable interpretation of wildcards")
99                  .isRequired(false)
100                 .withLongOpt(LONGOPT_NOWILDCARD)
101                 .create(OPT_NOWILDCARD));
102         return opt;
103     }
104 }