View Javadoc
1 /* 2 Bloof - visualize the evolution of your software project 3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de> 4 http://bloof.sf.net/ 5 6 This program is free software; you can redistribute it and/or modify it 7 under the terms of the GNU General Public License. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License along with 15 this program; if not, write to the Free Software Foundation, Inc., 16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 18 $RCSfile: CommandParser.java,v $ 19 Created on $Date: 2003/09/10 07:17:53 $ 20 */ 21 package net.sf.bloof.script; 22 23 import java.text.ParseException; 24 import java.text.SimpleDateFormat; 25 import java.util.Date; 26 import java.util.HashMap; 27 import java.util.Iterator; 28 import java.util.StringTokenizer; 29 30 import net.sf.bloof.Bloof; 31 import net.sf.bloof.db.DbAccess; 32 import net.sf.bloof.db.DefaultDbAccess; 33 import net.sf.bloof.metrics.DeveloperGroup; 34 import net.sf.bloof.metrics.FileGroup; 35 import net.sf.bloof.metrics.Metric; 36 import net.sf.bloof.metrics.MetricParameter; 37 import net.sf.bloof.metrics.MetricRegistry; 38 import net.sf.bloof.metrics.SqlMetric; 39 import net.sf.bloof.metrics.TimeInterval; 40 import net.sf.bloof.scm.ScmAccess; 41 import net.sf.bloof.scm.cvsplugin.CvsAccess; 42 import net.sf.bloof.scm.cvsplugin.CvsConnectionMethod; 43 import net.sf.bloof.scm.cvsplugin.InvalidRepositoryLocationException; 44 import net.sf.bloof.scm.cvsplugin.LoginDetails; 45 import net.sf.bloof.scm.cvsplugin.RepositoryLocation; 46 import net.sf.bloof.script.events.ScriptAction; 47 import net.sf.bloof.script.intl.Messages; 48 import net.sf.bloof.script.intl.Text; 49 50 /*** 51 * Parser for user commands. This class returns a ShellAction according to 52 * the command 53 * @author Lukasz Pekacki <pekacki@users.sourceforge.net> 54 * @version $Id: CommandParser.java,v 1.2 2003/09/10 07:17:53 pekacki Exp $ 55 */ 56 public class CommandParser { 57 58 private static String[] createStringArrayOfKommaSeperatedList(String aString) { 59 StringTokenizer st = new StringTokenizer(aString, ","); 60 String[] sArray = new String[st.countTokens()]; 61 int index = 0; 62 while (st.hasMoreTokens()) { 63 sArray[index] = st.nextToken(); 64 index++; 65 } 66 return sArray; 67 } 68 69 /*** 70 * @param commandParams 71 * @return 72 */ 73 private static MetricParameter getTimeLineMetricParams(String commandParams) 74 throws ParseException, CommandException { 75 HashMap params = parseNameValuePairs(commandParams); 76 FileGroup fg = null; 77 String fileListString = (String) params.get(FILES); 78 if (fileListString != null) { 79 String[] files = createStringArrayOfKommaSeperatedList(fileListString); 80 fg = new FileGroup(files); 81 fg.setName(ScriptController.createNewListIdent("file group")); 82 } 83 DeveloperGroup dg = null; 84 String develListString = (String) params.get(DEVELOPERS); 85 if (develListString != null) { 86 String[] devels = createStringArrayOfKommaSeperatedList(develListString); 87 String name = ScriptController.createNewListIdent("developer group"); 88 dg = new DeveloperGroup(devels, name); 89 } 90 TimeInterval tInter = null; 91 String timeIntervalString = (String) params.get(TIME); 92 if (timeIntervalString != null) { 93 String[] toFrom = createStringArrayOfKommaSeperatedList(timeIntervalString); 94 Date from = sDateFormatter.parse(toFrom[0]); 95 if (toFrom.length == 2) { 96 Date to = sDateFormatter.parse(toFrom[1]); 97 tInter = new TimeInterval(from, to); 98 } else { 99 tInter = new TimeInterval(from); 100 } 101 tInter.setName(ScriptController.createNewListIdent("time interval")); 102 } 103 MetricParameter mp = new MetricParameter(dg, tInter, fg); 104 return mp; 105 } 106 public static ScriptAction parse(String aCommandString) throws CommandException { 107 if (aCommandString == null || aCommandString.length() < 1) { 108 return ScriptAction.NOOP; 109 } 110 StringTokenizer st = new StringTokenizer(aCommandString); 111 String command = st.nextToken(); 112 int fromIndex = aCommandString.indexOf(command) + command.length() + 1; 113 String commandParams = ""; 114 if (aCommandString.length() > fromIndex) { 115 commandParams = aCommandString.substring(fromIndex); 116 } 117 if (command.equalsIgnoreCase(EXIT)) { 118 return ScriptAction.EXIT; 119 } 120 if (command.equalsIgnoreCase(HELP) || command.equalsIgnoreCase(HELP_SHORT)) { 121 String helpMessage = Messages.getString(Text.HELP_TEXT); 122 return new ScriptAction( 123 ScriptAction.SHOW_USER_INFO, 124 ScriptAction.PARAM_OBJECT, 125 helpMessage); 126 } 127 if (command.equalsIgnoreCase(RUN)) { 128 Metric m = parseRunMetric(commandParams); 129 return new ScriptAction(ScriptAction.EXECUTE_METRIC, ScriptAction.PARAM_OBJECT, m); 130 } 131 if (command.equalsIgnoreCase(RESULTS)) { 132 return ScriptAction.LIST_RESULTS; 133 } 134 if (command.equalsIgnoreCase(METRICS)) { 135 return ScriptAction.LIST_METRICS; 136 } 137 if (command.equalsIgnoreCase(EXPORT)) { 138 HashMap mParams = parseNameValuePairs(commandParams); 139 String resultName = (String) mParams.get(RESULT); 140 String fileName = (String) mParams.get(FILE); 141 HashMap exportParams = new HashMap(); 142 exportParams.put(ScriptController.PARAM_FILE_NAME, fileName); 143 exportParams.put(ScriptController.PARAM_RESULT, resultName); 144 return new ScriptAction(ScriptAction.EXECUTE_EXPORT_RESULT, exportParams); 145 } 146 if (command.equalsIgnoreCase(IMPORT)) { 147 HashMap h; 148 try { 149 h = parseImport(commandParams); 150 } catch (InvalidRepositoryLocationException e) { 151 throw new CommandException("Could not parse import string." + e.toString()); 152 } 153 return new ScriptAction(ScriptAction.EXECUTE_IMPORT_PROJECT, h); 154 } 155 if (command.equalsIgnoreCase(OPEN)) { 156 HashMap h; 157 try { 158 h = parseOpen(commandParams); 159 } catch (InvalidRepositoryLocationException e) { 160 throw new CommandException("Could not parse import string." + e.toString()); 161 } 162 return new ScriptAction(ScriptAction.EXECUTE_OPEN_PROJECT, h); 163 } 164 if (command.equalsIgnoreCase(UPDATE)) { 165 return ScriptAction.EXECUTE_UPDATE_PROJECT; 166 } 167 throw new CommandException("Command not known."); 168 } 169 170 private static DbAccess parseDb(String aString) throws CommandException { 171 HashMap nameValues = parseNameValuePairs(aString); 172 String dbType = (String) nameValues.get(DB); 173 if (dbType == null) { 174 throw new CommandException("No dbType specified!"); 175 } 176 if (dbType.equalsIgnoreCase(DB_INTERNAL)) { 177 return Bloof.INTERNAL_DATABASE; 178 } else { 179 String dbUrl = (String) nameValues.get(DB_URL); 180 if (dbUrl == null) { 181 throw new CommandException("No dburl specified."); 182 } 183 String dbUser = (String) nameValues.get(DB_USER); 184 if (dbUser == null) { 185 throw new CommandException("No dbuser specified."); 186 } 187 String dbPassword = (String) nameValues.get(DB_PASSWORD); 188 if (dbType.equalsIgnoreCase(DB_MCKOI)) { 189 return new DefaultDbAccess(DB_NAME, dbUser, dbPassword, dbUrl); 190 } else if (dbType.equalsIgnoreCase(DB_POSTGRES)) { 191 return new DefaultDbAccess(DB_NAME, dbUser, dbPassword, dbUrl); 192 } else { 193 throw new CommandException("No such database type known:" + dbType); 194 } 195 196 } 197 } 198 199 private static HashMap parseOpen(String aImportString) 200 throws InvalidRepositoryLocationException, CommandException { 201 HashMap h = new HashMap(); 202 DbAccess db = parseDb(aImportString); 203 h.put(ScriptController.PARAM_DB_ACCESS, db); 204 return h; 205 } 206 207 private static HashMap parseImport(String aImportString) 208 throws InvalidRepositoryLocationException, CommandException { 209 HashMap h = new HashMap(); 210 ScmAccess scm = parseScm(aImportString); 211 DbAccess db = parseDb(aImportString); 212 h.put(ScriptController.PARAM_SCM_ACCESS, scm); 213 h.put(ScriptController.PARAM_DB_ACCESS, db); 214 return h; 215 } 216 217 private static HashMap parseNameValuePairs(String aString) throws CommandException { 218 StringTokenizer st = new StringTokenizer(aString); 219 HashMap h = new HashMap(); 220 String nameValue = ""; 221 boolean firstTime = true; 222 while (st.hasMoreElements()) { 223 if (firstTime) { 224 nameValue = st.nextToken(); 225 firstTime = false; 226 } 227 int eqIndex = nameValue.indexOf('='); 228 if (eqIndex < 0) { 229 throw new CommandException("No name=value pair found!"); 230 } 231 String name = nameValue.substring(0, eqIndex).toLowerCase(); 232 String value = nameValue.substring(eqIndex + 1); 233 boolean nextPairFound = false; 234 while (st.hasMoreElements() && !nextPairFound) { 235 String token = st.nextToken(); 236 int eIndex = token.indexOf('='); 237 if (eIndex < 0) { 238 value += " " + token; 239 } else { 240 nameValue = token; 241 nextPairFound = true; 242 } 243 } 244 h.put(name, value); 245 if (nextPairFound) { 246 eqIndex = nameValue.indexOf('='); 247 if (eqIndex < 0) { 248 throw new CommandException("No name=value pair found!"); 249 } 250 name = nameValue.substring(0, eqIndex).toLowerCase(); 251 value = nameValue.substring(eqIndex + 1); 252 h.put(name, value); 253 } 254 } 255 return h; 256 } 257 258 /*** 259 * @param commandParams 260 * @return 261 */ 262 private static Metric parseRunMetric(String commandParams) throws CommandException { 263 HashMap nameValues = parseNameValuePairs(commandParams); 264 String userMetricString = (String) nameValues.get(METRIC); 265 if (userMetricString == null) { 266 throw new CommandException("Metric type not specified in: " + commandParams); 267 } 268 if (userMetricString.equalsIgnoreCase(SQL)) { 269 String sqlString = (String) nameValues.get(SQL_QUERY); 270 if (sqlString == null) { 271 throw new CommandException("SQL query not specified in: " + commandParams); 272 } 273 MetricParameter mp = new MetricParameter(sqlString); 274 return new SqlMetric(mp); 275 276 } else { 277 for (Iterator iter = MetricRegistry.getAvailableMetricNames(); iter.hasNext();) { 278 String metricName = (String) iter.next(); 279 if (metricName.equalsIgnoreCase(userMetricString)) { 280 MetricParameter mp = new MetricParameter(null,null,null); 281 try { 282 mp = getTimeLineMetricParams(commandParams); 283 } catch (ParseException e) { 284 throw new CommandException( 285 "Error parsing date parameter in: " + commandParams); 286 } 287 Metric m = MetricRegistry.createMetric(metricName); 288 m.setupMetric(mp); 289 return m; 290 } 291 } 292 293 } 294 throw new CommandException("Unknown metric type: " + userMetricString); 295 } 296 297 private static ScmAccess parseScm(String aString) 298 throws CommandException, InvalidRepositoryLocationException { 299 HashMap nameValues = parseNameValuePairs(aString); 300 String cvsRoot = (String) nameValues.get(CVSROOT); 301 if (cvsRoot == null) { 302 throw new CommandException("No cvsroot specified."); 303 } 304 String cvsModule = (String) nameValues.get(CVS_MODULE); 305 if (cvsModule == null) { 306 throw new CommandException("No cvsmodule specified."); 307 } 308 String cvsPassword = (String) nameValues.get(CVS_PASSWORD); 309 String cvsSsh = (String) nameValues.get(CVS_SSH); 310 RepositoryLocation rLoc = new RepositoryLocation(cvsRoot); 311 if (cvsSsh != null && cvsSsh.equalsIgnoreCase("true")) { 312 rLoc.setConnectionMethod(CvsConnectionMethod.SSH); 313 } 314 LoginDetails lDetail = new LoginDetails(rLoc.getUserName(), cvsPassword); 315 return new CvsAccess(rLoc, lDetail, cvsModule, CVS_ACCESS_NAME); 316 } 317 private static final String EXIT = "exit", 318 HELP = "help", 319 HELP_SHORT = "?", 320 IMPORT = "import", 321 RUN = "run", 322 EXPORT = "export", 323 OPEN = "open", 324 UPDATE = "update"; 325 private static final String DB = "db", 326 DB_INTERNAL = "internal", 327 DB_MCKOI = "mckoi", 328 DB_POSTGRES = "postgres", 329 DB_URL = "dburl", 330 DB_USER = "dbuser", 331 DB_PASSWORD = "dbpassword", 332 DB_NAME = "database", 333 CVSROOT = "cvsroot", 334 CVS_PASSWORD = "cvspassword", 335 CVS_MODULE = "cvsmodule", 336 CVS_SSH = "sshserver", 337 CVS_ACCESS_NAME = "cvs access", 338 SQL = "sql", 339 SQL_QUERY = "sql_query", 340 METRIC = "metric", 341 METRICS = "metrics", 342 FILES = "files", 343 DEVELOPERS = "developers", 344 TIME = "time", 345 RESULTS = "results", 346 RESULT = "result", 347 FILE = "file"; 348 private static SimpleDateFormat sDateFormatter = 349 new SimpleDateFormat(Messages.getString(Text.DATE_FORMAT)); 350 351 352 }

This page was automatically generated by Maven