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.logicalfile.LogicalDirectory;
6 import org.ogf.saga.logicalfile.LogicalFileFactory;
7 import org.ogf.saga.namespace.Flags;
8 import org.ogf.saga.session.Session;
9 import org.ogf.saga.session.SessionFactory;
10 import org.ogf.saga.url.URL;
11 import org.ogf.saga.url.URLFactory;
12
13 import java.util.List;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class NSLogicalFind extends AbstractCommand {
30 private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
31 private static final String OPT_LONG = "l", LONGOPT_LONG = "long";
32 private static final String OPT_NOWILDCARD = "n", LONGOPT_NOWILDCARD = "no-wildcard";
33 private static final String OPT_FILTER = "f", LONGOPT_FILTER = "filter";
34
35 public NSLogicalFind() {
36 super("jsaga-logical-find", new String[]{"URL"}, new String[]{OPT_HELP, LONGOPT_HELP});
37 }
38
39 public static void main(String[] args) throws Exception {
40 NSLogicalFind command = new NSLogicalFind();
41 CommandLine line = command.parse(args);
42 if (line.hasOption(OPT_HELP))
43 {
44 command.printHelpAndExit(null);
45 }
46 else
47 {
48
49 String arg = command.m_nonOptionValues[0];
50 URL url;
51 String pattern;
52 if (SAGAPattern.hasWildcard(arg) && !line.hasOption(OPT_NOWILDCARD)) {
53 Matcher matcher = Pattern.compile("((.*)/)*(.*/)/*").matcher(arg);
54 if (matcher.matches() && matcher.groupCount()>1) {
55 url = URLFactory.createURL(matcher.group(1));
56 pattern = matcher.group(3);
57 } else {
58 url = URLFactory.createURL(arg.substring(0, arg.lastIndexOf('/')+1));
59 pattern = arg.substring(arg.lastIndexOf('/')+1);
60 }
61 } else {
62 url = URLFactory.createURL(arg);
63 pattern = null;
64 }
65 String[] filters;
66 if (line.hasOption(OPT_FILTER)) {
67 filters = line.getOptionValues(OPT_FILTER);
68 } else {
69 filters = null;
70 }
71
72
73 Session session = SessionFactory.createSession(true);
74 LogicalDirectory dir = LogicalFileFactory.createLogicalDirectory(session, url, Flags.NONE.getValue());
75 List<URL> list = dir.find(pattern, filters, Flags.NONE.getValue());
76
77 if (line.hasOption(OPT_LONG)) {
78
79 EntryLongFormat formatter = new EntryLongFormat(dir);
80 for (URL entry : list) {
81 System.out.println(formatter.toString(entry));
82 }
83
84 dir.close();
85 } else {
86
87 dir.close();
88
89 for (URL entry : list) {
90
91 System.out.println(entry.getString());
92 }
93 }
94 System.exit(0);
95 }
96 }
97
98 protected Options createOptions() {
99 Options opt = new Options();
100 opt.addOption(OptionBuilder.withDescription("Display this help and exit")
101 .withLongOpt(LONGOPT_HELP)
102 .create(OPT_HELP));
103 opt.addOption(OptionBuilder.withDescription("Use a long listing format")
104 .isRequired(false)
105 .withLongOpt(LONGOPT_LONG)
106 .create(OPT_LONG));
107 opt.addOption(OptionBuilder.withDescription("Disable interpretation of wildcards")
108 .isRequired(false)
109 .withLongOpt(LONGOPT_NOWILDCARD)
110 .create(OPT_NOWILDCARD));
111 opt.addOption(OptionBuilder.withDescription("Comma-separated list of filters (<key>=<value>)")
112 .isRequired(false)
113 .hasArgs()
114 .withLongOpt(LONGOPT_FILTER)
115 .create(OPT_FILTER));
116 return opt;
117 }
118 }