1   package net.sf.jhylafax;
2   
3   import static net.sf.jhylafax.JHylaFAX.i18n;
4   import java.util.ArrayList;
5   import java.util.Arrays;
6   import java.util.LinkedList;
7   import java.util.List;
8   
9   public class ArgsHandler {
10  
11  	private LinkedList<String> args;
12  	private List<String> filenames = new ArrayList<String>();
13  	private String logConfigFilename = "logging-default.properties";
14  	private List<String> numbers = new ArrayList<String>();
15  	private boolean readStdin = false;
16  	private boolean quitAfterSending;
17  	
18  	public ArgsHandler()
19  	{
20  	}
21  	
22  	private void error(String error)
23  	{
24  		System.err.println(error);
25  		System.exit(1);
26  	}
27  
28  	public void evaluate(String[] args)
29  	{
30  		this.args = new LinkedList<String>(Arrays.asList(args));
31  		
32  		while (hasNext()) {
33  			String arg = pop();
34  			if (!arg.startsWith("-")) {
35  				filenames.add(arg);
36  			}
37  			else {
38  				if ("-n".equals(arg)) {
39  					numbers.add(popArg("-n"));
40  				}
41  				else if ("-d".equals(arg)) {
42  					logConfigFilename = "logging-info.properties";
43  				}
44  				else if ("-dd".equals(arg)) {
45  					logConfigFilename = "logging-debug.properties";
46  				}
47  				else if ("--stdin".equals(arg)) {
48  					readStdin = true;
49  				}
50  				else if ("-q".equals(arg)) {
51  					quitAfterSending = true;
52  				}
53  				else if ("-h".equals(arg) | "-?".equals(arg) | "-help".equals(arg) | "--help".equals(arg)) {
54  					System.out.println("Usage: java -jar jhylafax.jar [options] [files]");
55  					System.out.println();
56  					System.out.println(" -d	            Output debug information to stderr");
57  					System.out.println(" -dd            Output more debug information to stderr");
58  					System.out.println(" -h --help      Print this help message");
59  					System.out.println(" -n number      Send fax to number");
60  					System.out.println(" -q	            Quit after sending fax");
61  					System.out.println(" --stdin        Read PostScript from stdin");
62  					System.exit(0);
63  				}
64  				else {
65  					error(i18n.tr("Invalid parameter. Use -h for help."));
66  				}
67  			}
68  		}
69  		
70  	}
71  	
72  	public String[] getFilenames()
73  	{
74  		return filenames.toArray(new String[0]);
75  	}
76  	
77  	public String getLogConfigFilename()
78  	{
79  		return logConfigFilename;
80  	}
81  	
82  	public String[] getNumbers()
83  	{
84  		return numbers.toArray(new String[0]);
85  	}
86  	
87  	public boolean getReadStdin()
88  	{
89  		return readStdin;
90  	}
91  	
92  	public boolean getQuitAfterSending() 
93  	{
94  		return quitAfterSending;
95  	}
96  	
97  	private boolean hasNext()
98  	{
99  		return !args.isEmpty();
100 	}
101 	
102 	private String pop()
103 	{
104 		return args.remove();
105 	}
106 
107 	private String popArg(String parameter)
108 	{
109 		if (!hasNext()) {
110 			error(i18n.tr("{0} parameter requires argument", parameter));
111 		}
112 		String arg = pop();
113 		if (arg.startsWith("-")) {
114 			error(i18n.tr("{0} parameter requires argument", parameter));
115 		}
116 		return arg;
117 	}
118 }