1 28 29 package com.idaremedia.antx.capture; 30 31 import java.io.File ; 32 import java.io.FileReader ; 33 import java.io.IOException ; 34 import java.io.Reader ; 35 import java.io.StringReader ; 36 import java.util.Properties ; 37 38 import org.apache.tools.ant.AntTypeDefinition; 39 import org.apache.tools.ant.BuildException; 40 import org.apache.tools.ant.ComponentHelper; 41 import org.apache.tools.ant.Project; 42 43 import com.idaremedia.antx.AntX; 44 import com.idaremedia.antx.apis.Requester; 45 import com.idaremedia.antx.ownhelpers.ConditionalInnerNameValuePair; 46 import com.idaremedia.antx.ownhelpers.ConditionalParameters; 47 import com.idaremedia.antx.parameters.IsA; 48 import com.idaremedia.antx.parameters.TransformHelper; 49 50 77 78 public final class InterpretLoggedTask extends LogsUsingTask 79 { 80 83 public InterpretLoggedTask() 84 { 85 super(AntX.capture+"InterpretLoggedTask:"); 86 } 87 88 89 90 98 public void setLogFile(String fileref) 99 { 100 require_(fileref!=null,"setLogFile- nonzro fileref"); 101 m_fileRef = fileref; 102 m_logFile = null; 103 } 104 105 106 107 119 public void setPrefix(String propertyPrefix) 120 { 121 require_(propertyPrefix!=null && propertyPrefix.length()>0, 122 "setPrefix- nonzro property prefix"); 123 if (propertyPrefix.endsWith(".")) { 124 m_propertiesPrefix = propertyPrefix; 125 } else { 126 m_propertiesPrefix = propertyPrefix+"."; 127 } 128 } 129 130 131 132 136 public final String getUpdateProperty() 137 { 138 return m_propertiesPrefix+"result"; 139 } 140 141 142 143 148 public final String getErrorCountProperty() 149 { 150 return m_propertiesPrefix+"errorcount"; 151 } 152 153 154 155 160 public final String getWarningCountProperty() 161 { 162 return m_propertiesPrefix+"warningcount"; 163 } 164 165 166 167 173 public void setResultType(String resulttype) 174 { 175 require_(resulttype!=null,"setResultType- nonzro typename"); 176 IsA isa = IsA.from(resulttype); 177 if (isa==null) { 178 String err = getAntXMsg("task.illegal.param.value",getTaskName(), 179 resulttype, "resulttype"); 180 log(err, Project.MSG_ERR); 181 throw new BuildException(err,getLocation()); 182 } 183 m_resulttype = isa; 184 } 185 186 187 188 192 public final IsA getResultType() 193 { 194 return m_resulttype; 195 } 196 197 198 199 207 public void setMaxErrors(int maxErrors) 208 { 209 require_(maxErrors>=0,"setMaxErrors- zero or more value"); 210 m_maxErrors = maxErrors; 211 } 212 213 214 215 219 public final int getMaxErrors() 220 { 221 return m_maxErrors; 222 } 223 224 225 226 234 public void setMaxWarnings(int maxWarnings) 235 { 236 require_(maxWarnings>=0,"setMaxWarnings- zero or more value"); 237 m_maxWarnings = maxWarnings; 238 } 239 240 241 242 246 public final int getMaxWarnings() 247 { 248 return m_maxWarnings; 249 } 250 251 252 253 259 public void setInterpreter(String typename) 260 { 261 require_(typename!=null,"setInterprter- nonzro typename"); 262 263 ComponentHelper ch = ComponentHelper.getComponentHelper(getProject()); 264 AntTypeDefinition atd = ch.getDefinition(typename); 265 if (atd!=null) { 266 Object impl = atd.create(getProject()); 267 if (impl instanceof LogInterpreter) { 268 m_logInterpreter = (LogInterpreter)impl; 269 } 270 } 271 if (m_logInterpreter==null) { 272 try { 273 Class c = Class.forName(typename); 274 if (LogInterpreter.class.isAssignableFrom(c)) { 275 m_logInterpreter = (LogInterpreter)c.newInstance(); 276 } 277 } catch(Exception anyX) { 278 log(anyX.getMessage(),Project.MSG_ERR); 279 } 280 } 281 if (m_logInterpreter==null) { 282 String err = getAntXMsg("task.bad.custimpl.class1",typename, 283 LogInterpreter.class.getName()); 284 log(err, Project.MSG_ERR); 285 throw new BuildException(err, getLocation()); 286 } 287 } 288 289 290 291 296 public void addConfiguredParameter(ConditionalInnerNameValuePair arg) 297 { 298 if (m_interpreterArgs==null) { 299 m_interpreterArgs = new ConditionalParameters(getProject()); 300 } 301 m_interpreterArgs.addConfiguredParameter(arg); 302 } 303 304 305 306 311 protected void verifyCanExecute_(String calr) 312 { 313 super.verifyCanExecute_(calr); 314 315 if (m_logInterpreter==null) { 316 String err = getAntXMsg("task.needs.this.attr",getTaskName(), 317 "interpreter"); 318 log(err, Project.MSG_ERR); 319 throw new BuildException(err, getLocation()); 320 } 321 322 switch(getResultType().getIndex()) { 323 case IsA.PROPERTY_INDEX: { 324 checkIfProperty_(getUpdateProperty(),false); 325 break; 326 } 327 case IsA.REFERENCE_INDEX: { 328 checkIfReference_(getUpdateProperty(),true); 329 break; 330 } 331 } 332 333 if (m_fileRef!=null) { 334 m_logFile = new File (TransformHelper.toPath(m_fileRef,getProject())); 335 } 336 } 337 338 339 340 344 private Properties getInstructionsNoNull() 345 { 346 Properties ini; 347 if (m_interpreterArgs==null) { 348 ini = new Properties (); 349 } else { 350 ini = m_interpreterArgs.copyOfSimpleKeyValues(getProject(),true); 351 } 352 return ini; 353 } 354 355 356 357 364 public void execute() 365 { 366 verifyCanExecute_("exec"); 367 368 Reader inputr = null; 369 370 if (m_logFile!=null) { 371 if (!m_logFile.exists()) { 372 getProject().setNewProperty(getUpdateProperty(),LogInterpreter.CLEAN); 373 return; 374 } 375 try { 376 inputr = new FileReader (m_logFile); 377 } catch(IOException ioX) { 378 throw new BuildException(ioX); 379 } 380 } else { 381 inputr = new StringReader (getVUTLog()); 382 } 383 384 m_logInterpreter.interpret(inputr, new ConfigWrap(this,getInstructionsNoNull())); 385 } 386 387 388 389 393 protected boolean isSourceFlexible() 394 { 395 return true; 396 } 397 398 399 private String m_fileRef; 400 private File m_logFile; 401 private String m_propertiesPrefix = "_logcheck."; 402 private int m_maxErrors = 0; 403 private int m_maxWarnings = 10; 404 private LogInterpreter m_logInterpreter; 405 private ConditionalParameters m_interpreterArgs; 406 private IsA m_resulttype= IsA.PROPERTY; 407 } 408 409 410 411 412 421 class ConfigWrap extends Requester.ForComponent implements InterpretParameters { 422 private final InterpretLoggedTask m_task; 423 private final Properties m_argv; 424 ConfigWrap(InterpretLoggedTask task, Properties argv) { 425 super(task); 426 m_task = task; 427 m_argv = argv; 428 } 429 public String getErrorCountProperty() { 430 return m_task.getErrorCountProperty(); 431 } 432 public int getMaxErrors() { 433 return m_task.getMaxErrors(); 434 } 435 public int getMaxWarnings() { 436 return m_task.getMaxWarnings(); 437 } 438 public String getUpdateProperty() { 439 return m_task.getUpdateProperty(); 440 } 441 public String getWarningCountProperty() { 442 return m_task.getWarningCountProperty(); 443 } 444 public boolean updateProperties() { 445 return true; 446 } 447 public IsA getResultType() { 448 return m_task.getResultType(); 449 } 450 public Properties getInstructions() { 451 return m_argv; 452 } 453 } 454 455 | Popular Tags |