View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.utils.cmd;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.springframework.beans.BeanWrapper;
23  
24  /**
25   * 
26   * @author Marco Ruiz
27   * @since Jul 12, 2007
28   */
29  public class OptionTemplate {
30  
31  	private static final char NO_SHORT_FLAG = '-';
32  	
33  	private char shortFlag = NO_SHORT_FLAG;
34  	private String longFlag;
35  	private String description;
36  	private String defaultValue;
37  	private String propertyName = null;
38  	
39  	private String toString;
40  	
41  	public OptionTemplate(String longFlag, String description) {
42  		this(longFlag, NO_SHORT_FLAG, longFlag, description, null);
43  	}
44  	
45  	public OptionTemplate(String longFlag, String description, String defVal) {
46  		this(longFlag, NO_SHORT_FLAG, longFlag, description, defVal);
47  	}
48  	
49  	public OptionTemplate(char shortFlag, String longFlag, String description) {
50  		this(longFlag, shortFlag, longFlag, description, null);
51  	}
52  	
53  	public OptionTemplate(char shortFlag, String longFlag, String description, String defVal) {
54  		this(longFlag, shortFlag, longFlag, description, defVal);
55  	}
56  	
57  	public OptionTemplate(String propName, String longFlag, String description, String defVal) {
58  		this(propName, NO_SHORT_FLAG, longFlag, description, defVal);
59  	}
60  	
61  	public OptionTemplate(String propName, char shortFlag, String longFlag, String description) {
62  		this(propName, shortFlag, longFlag, description, null);
63  	}
64  	
65  	public OptionTemplate(String propName, char shortFlag, String longFlag, String description, String defVal) {
66  		this.propertyName = propName;
67  		this.shortFlag = shortFlag;
68  		this.longFlag = longFlag;
69  		this.description = description;
70  		this.defaultValue = defVal;
71  		toString = createToString("\t\t");
72  	}
73  	
74  	private String createToString(String filler) {
75  		String shortDesc = (shortFlag != '-') ? "\t-" + shortFlag + ", " : "";
76  		return shortDesc + "--" + longFlag + filler + getFullDescription() + "\n";
77  	}
78  
79  	private String getFullDescription() {
80  		String defValMsg = (defaultValue != null && !defaultValue.equals("")) ? "Defaults to '" + defaultValue + "'" :  "No default value";
81  		return description + ". " + defValMsg + ".";
82  	}
83  
84  	public char getShortFlag() {
85  		return shortFlag;
86  	}
87  
88  	public String getLongFlag() {
89  		return longFlag;
90  	}
91  
92  	public String getDescription() {
93  		return description;
94  	}
95  
96  	public String getDefaultValue() {
97  		return defaultValue;
98  	}
99  
100 	public String getPropertyName() {
101 		return propertyName;
102 	}
103 	
104 	public void setObjectProperty(BeanWrapper appObjectWrapper, Object value) {
105 		if (propertyName == null) return;
106 		appObjectWrapper.setPropertyValue(propertyName, value);
107 	}
108 
109 	public String readOptionValue(String[] args) {
110 		return readOptionValue(args, defaultValue);
111 	}
112 	
113 	public String readOptionValue(String[] args, String defaultValue) {
114 		String res = readUnformattedOptionValue(args, defaultValue);
115 		if (res == null) return res;
116 		return (res.matches("^\".*\"$")) ? res.substring(1, res.length() - 1) : res;
117 	}
118 
119 	private String readUnformattedOptionValue(String[] args, String defaultValue) {
120 		String longName = "--" + longFlag;
121 		String shortName = (shortFlag != '-') ? "-" + shortFlag : null; // If empty string then it will never match
122 		
123 		for (int idx = 0; idx < args.length; ++idx) {
124 		    String currArg = args[idx];
125 			if (currArg.equals(longName) || currArg.equals(shortName)) return hasValueAssigned(args, idx) ? args[idx + 1] : "";
126 		}
127 		    	
128 		for (int idx = 0; idx < args.length; ++idx) {
129 		    String currArg = args[idx];
130 		    if (startsWith(currArg, longName, shortName)) return currArg.substring(currArg.indexOf("=") + 1);
131 		}
132 
133 		return defaultValue;
134 	}
135 
136 	public String[] removeOptionFromArray(String[] args) {
137 		List<String> results = new ArrayList<String>(); 
138 		
139 		String longName = "--" + longFlag;
140 		String shortName = (shortFlag != '-') ? "-" + shortFlag : null; // If empty string then it will never match
141 
142 		for (int idx = 0; idx < args.length; ++idx) {
143 		    String currArg = args[idx];
144 			if (currArg.equals(longName) || currArg.equals(shortName)) {
145 				if (hasValueAssigned(args, idx)) idx++;
146 			} else {
147 			    if (!startsWith(currArg, longName, shortName)) results.add(currArg);
148 			}
149 		}
150 		
151 		return results.toArray(new String[]{});
152 	}
153 	
154 	private boolean hasValueAssigned(String[] args, int index) {
155 		if (args.length <= index + 1) return false;
156 		String arg = args[index + 1];
157 		if (arg.length() == 0) return true;
158 		if (arg.charAt(0) == '-') {
159 			try {
160 				Double.parseDouble(arg);
161 			} catch (Exception e) {
162 				return false;
163 			}
164 		}
165 		return true;
166 	}
167 	
168 	private boolean startsWith(String currArg, String longName, String shortName) {
169 		return currArg.startsWith(longName + "=") || (shortName != null && currArg.startsWith(shortName + "="));
170 	}
171 	
172 	public String toString() { return toString; }
173 
174 	public String toString(int longFlagSize) {
175 		return createToString(createFiller(longFlagSize));
176 	}
177 
178 	private String createFiller(int longFlagSize) {
179 		String filler = "";
180 		for (int count = 0; count < (longFlagSize - longFlag.length()); count++) filler  += " ";
181 		return filler;
182 	}
183 
184 	public boolean equals(Object obj) {
185 		if (!(obj instanceof OptionTemplate)) return false;
186 		return ((OptionTemplate)obj).getLongFlag().equals(longFlag);
187 	}
188 
189 	public int hashCode() {
190 		return toString.hashCode(); 
191 	}
192 }