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
17
18
19
20
21
22
23
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
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
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
70 EntryLongFormat formatter = new EntryLongFormat(dir);
71 for (URL entry : list) {
72 System.out.println(formatter.toString(entry));
73 }
74
75 dir.close();
76 } else {
77
78 dir.close();
79
80 for (URL entry : list) {
81
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 }