1 23 24 package com.sun.enterprise.tools.verifier; 25 26 import java.io.OutputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.FileInputStream ; 31 import java.io.OutputStreamWriter ; 32 import java.io.IOException ; 33 import java.io.PrintWriter ; 34 import java.io.StringWriter ; 35 import java.text.SimpleDateFormat ; 36 import java.util.Date ; 37 import java.util.Enumeration ; 38 import java.util.Vector ; 39 import java.util.Locale ; 40 import java.util.logging.Level ; 41 import java.util.logging.LogRecord ; 42 import java.util.logging.Logger ; 43 44 import javax.xml.parsers.DocumentBuilder ; 45 import javax.xml.parsers.DocumentBuilderFactory ; 46 import javax.xml.transform.OutputKeys ; 47 import javax.xml.transform.Transformer ; 48 import javax.xml.transform.TransformerFactory ; 49 import javax.xml.transform.dom.DOMSource ; 50 import javax.xml.transform.stream.StreamResult ; 51 import javax.xml.transform.stream.StreamSource ; 52 53 import org.w3c.dom.Document ; 54 import org.w3c.dom.Element ; 55 import org.w3c.dom.NodeList ; 56 57 import com.sun.enterprise.logging.LogDomains; 58 import com.sun.enterprise.server.Constants; 59 import com.sun.enterprise.tools.verifier.util.VerifierConstants; 60 import com.sun.enterprise.tools.verifier.StringManagerHelper; 61 62 67 68 public class ReportHandler { 69 70 private final String TEST = "test"; private final String TEST_NAME = "test-name"; private final String TEST_DESC = "test-description"; private final String TEST_ASSERTION = "test-assertion"; private final String STATIC_VER = "static-verification"; private final String FAILED = "failed"; private final String PASSED = "passed"; private final String NOTAPPLICABLE = "not-applicable"; private final String WARNING = "warning"; 80 private final String FAILNUMBER = "failure-number"; private final String WARNINGNUMBER = "warning-number"; private final String ERRORNUMBER = "error-number"; private final String FAILCOUNT = "failure-count"; 85 private final String ERROR = "error"; private final String ERROR_NAME = "error-name"; private final String ERROR_DESC = "error-description"; private final String XSL_FILE = "textFormatForVerifierSS"; private String outputFileStr = null; 90 91 private Element rootNode = null; 92 private Document document; 93 private String textResult; private ResultManager resultMgr; 95 private FrameworkContext frameworkContext; 96 private Logger logger = LogDomains.getLogger(LogDomains.AVK_VERIFIER_LOGGER); 97 98 102 public ReportHandler(FrameworkContext frameworkContext) { 103 this.frameworkContext = frameworkContext; 104 this.resultMgr = frameworkContext.getResultManager(); 105 106 String onlyJarFile = new File (frameworkContext.getJarFileName()).getName(); 107 String outputDirName = frameworkContext.getOutputDirName(); 108 outputDirName = (outputDirName == null) ? 109 "" : outputDirName + File.separator; 110 if (frameworkContext.isUseTimeStamp()) { 111 SimpleDateFormat dateFormatter = new SimpleDateFormat ( 112 "yyyyMMddhhmmss"); outputFileStr = outputDirName + onlyJarFile + 114 dateFormatter.format(new Date ()); 115 } else 116 outputFileStr = outputDirName + onlyJarFile; 117 } 118 119 124 public void generateAllReports() throws IOException { 125 try { 126 createResultsDocument(frameworkContext.getReportLevel()); 127 writeToXmlFile(); 128 writeToTxtFile(); 129 writeToConsole(); 130 } catch (IOException e) { 131 throw e; 132 } 133 } 134 135 138 private void writeToConsole() { 139 if (frameworkContext.isUsingGui()) 140 return; 141 if (frameworkContext.isBackend()) { 142 logger.log(Level.SEVERE, textResult); 143 } else { 144 logger.log(Level.INFO, getClass().getName() + ".resultSummary", 145 new Object []{new Integer (resultMgr.getFailedCount()), 146 new Integer (resultMgr.getWarningCount()), 147 new Integer (resultMgr.getErrorCount())}); 148 } 149 if((resultMgr.getFailedCount() + resultMgr.getWarningCount() 150 + resultMgr.getErrorCount()) != 0 151 || frameworkContext.getReportLevel() == VerifierConstants.ALL) 152 logger.log(Level.INFO, getClass().getName() + 153 ".LookInResultsTestAssertions", new Object []{outputFileStr + ".txt"}); else 156 logger.log(Level.INFO, getClass().getName() + 157 ".LookInResultsTestAssertions1"); } 159 160 168 private void createResultsDocument(int reportLevel) throws IOException { 169 createDOMTree(); 170 if (reportLevel != VerifierConstants.FAIL) 171 addResultsToDocument(WARNING, resultMgr.getWarningResults()); 172 if (reportLevel == VerifierConstants.ALL) { 173 addResultsToDocument(PASSED, resultMgr.getOkayResults()); 174 addResultsToDocument(NOTAPPLICABLE, resultMgr.getNaResults()); 175 } 176 177 addResultsToDocument(FAILED, resultMgr.getFailedResults()); 178 Vector error = resultMgr.getError(); 179 if (!error.isEmpty()) { 180 for (int i = 0; i < error.size(); i++) { 181 LogRecord lr = (LogRecord ) error.get(i); 182 generateErrors(lr); 183 } 184 } 185 failureCount(); 186 } 187 188 193 private void createDOMTree() throws IOException { 194 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 195 DocumentBuilder builder = null; 196 try { 197 builder = factory.newDocumentBuilder(); 198 } catch (Exception e) { 199 IOException ioe = new IOException (e.getMessage()); 200 ioe.initCause(e); 201 throw ioe; 202 } 203 document = builder.newDocument(); 204 rootNode = document.createElement(STATIC_VER); 205 document.appendChild(rootNode); 206 } 207 208 214 private void addResultsToDocument(String status, Vector resultVector) { 215 for (int i = 0; i < resultVector.size(); i++) { 216 Enumeration en; 217 Result r = (Result) resultVector.get(i); 218 String moduleName = r.getModuleName(); 219 if (status == FAILED) { 220 en = r.getErrorDetails().elements(); 221 } else if (status == WARNING) { 222 en = r.getWarningDetails().elements(); 223 } else if (status == PASSED) 224 en = r.getGoodDetails().elements(); 225 else 226 en = r.getNaDetails().elements(); 227 createNode(moduleName, status); 228 addToDocument(moduleName, status, r, en); 229 } 230 } 231 232 237 private void generateErrors(LogRecord record) { 238 Element errorNode = null; 239 NodeList nodeList = document.getElementsByTagName(ERROR); 242 if (nodeList.getLength() == 0) { 243 errorNode = document.createElement(ERROR); 244 rootNode.appendChild(errorNode); 245 } else { 246 errorNode = (Element ) nodeList.item(0); } 248 Element excepName = getTextNode(ERROR_NAME, record.getMessage()); 249 errorNode.appendChild(excepName); 250 if (record.getThrown() != null) { 251 Element excepDescr = getTextNode(ERROR_DESC, 252 writeStackTraceToFile(record.getThrown())); 253 errorNode.appendChild(excepDescr); 254 } 255 } 256 257 268 private void createNode(String moduleName, String status) { 269 NodeList nodeList; 270 Element moduleNode; 271 nodeList = document.getElementsByTagName(moduleName); 272 if (nodeList.getLength() == 0) { 273 moduleNode = document.createElement(moduleName); 274 rootNode.appendChild(moduleNode); 275 } else { 276 moduleNode = (Element ) nodeList.item(0); } 278 nodeList = moduleNode.getChildNodes(); 279 Element statusNode = null; 280 281 if (nodeList.getLength() == 0) { 282 statusNode = document.createElement(status); 283 moduleNode.appendChild(statusNode); 284 } else { 285 for (int j = 0; j < nodeList.getLength(); j++) { 286 if (((Element ) nodeList.item(j)).getTagName().equals(status)) { 287 statusNode = (Element ) nodeList.item(j); 288 break; 289 } 290 } 291 if (statusNode == null) { 292 statusNode = document.createElement(status); 293 moduleNode.appendChild(statusNode); 294 } 295 } 296 } 297 298 306 private void addToDocument(String moduleName, String status, Result r, 307 Enumeration en) { 308 if (r == null) return; 309 NodeList nodeList; 310 nodeList = 312 document.getElementsByTagName(moduleName).item(0) 313 .getChildNodes(); 314 Element statusNode = null; 315 for (int j = 0; j < nodeList.getLength(); j++) { 316 if (((Element ) nodeList.item(j)).getTagName().equals(status)) { 317 statusNode = (Element ) nodeList.item(j); 318 break; 319 } 320 } 321 Element test = document.createElement(TEST); 323 Element testName = getTextNode(TEST_NAME, r.getTestName()); 324 Element testAssertion = getTextNode(TEST_ASSERTION, r.getAssertion()); 325 String string = ""; 327 while (en.hasMoreElements()) { 328 string = string + (String ) en.nextElement() + "\n"; } 330 Element testDescr = getTextNode(TEST_DESC, string); 331 test.appendChild(testName); 332 test.appendChild(testAssertion); 333 test.appendChild(testDescr); 334 statusNode.appendChild(test); 335 } 336 337 344 private Element getTextNode(String tag, String text) { 345 Element element = document.createElement(tag); 346 element.appendChild(document.createTextNode(text)); 347 return element; 348 } 349 350 355 private void writeToXmlFile() throws IOException { 356 FileOutputStream fos = null; 357 try { 358 File outputFile = extractResultsFileToTmpDir( 359 outputFileStr + ".xml"); DOMSource domSource = new DOMSource (document); 361 TransformerFactory tfactory = TransformerFactory.newInstance(); 362 Transformer transformer = tfactory.newTransformer(); 363 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); String encoding = System.getProperty("file.encoding"); 366 transformer.setOutputProperty(OutputKeys.ENCODING, encoding); 367 fos = new FileOutputStream (outputFile); 368 transformer.transform(domSource, new StreamResult (fos)); 369 } catch (Exception e) { 370 IOException ioe = new IOException (e.getMessage()); 371 ioe.initCause(e); 372 throw ioe; 373 } finally { 374 try { 375 if(fos != null) 376 fos.close(); 377 } catch (Exception e){} 378 } 379 } 380 381 386 private void writeToTxtFile() throws IOException { 387 File xslFile = getLocalizedXSLFile(); 388 389 Document dynamicDocument = document; 390 ByteArrayOutputStream output = new ByteArrayOutputStream (); 391 generateText(dynamicDocument, xslFile, output); 392 textResult = output.toString("UTF-8"); 393 394 File outputFile = extractResultsFileToTmpDir(outputFileStr + ".txt"); OutputStreamWriter fw = new OutputStreamWriter ( 397 new FileOutputStream (outputFile)); 398 fw.write(textResult); 399 fw.close(); 400 } 401 402 private File extractResultsFileToTmpDir(String jarFile) { 403 File tmpJarFile = null; 404 String fullFilename; 405 tmpJarFile = new File (jarFile); 406 fullFilename = tmpJarFile.getAbsolutePath(); 407 if (new File (fullFilename).getParent() != null) { 408 (new File (new File (fullFilename).getParent())).mkdirs(); 409 } 410 return tmpJarFile; 411 } 412 413 420 private void generateText(Document xmlResult, File stylesheet, 421 OutputStream output) 422 throws IOException { 423 FileOutputStream fos = null; 425 try { 426 StreamSource styleSource; 427 Transformer transformer; 428 TransformerFactory tFactory = TransformerFactory.newInstance(); 429 if (stylesheet != null) { 430 FileInputStream fis = new FileInputStream (stylesheet); 431 styleSource = new StreamSource (fis); 432 transformer = tFactory.newTransformer(styleSource); 433 } else { 434 transformer = tFactory.newTransformer(); 435 } 436 DOMSource source = new DOMSource (xmlResult); 437 StreamResult streamResult = new StreamResult (output); 438 transformer.transform(source, streamResult); 439 440 } catch (Exception e) { 441 IOException ioe = new IOException (e.getMessage()); 442 ioe.initCause(e); 443 throw ioe; 444 } finally { 445 try { 446 if(fos != null) 447 fos.close(); 448 } catch (Exception e) {} 449 } 450 } 451 452 private void failureCount() { 453 int failedCount = resultMgr.getFailedCount(); 454 int warningCount = resultMgr.getWarningCount(); 455 int errorCount = resultMgr.getErrorCount(); 456 457 Element failureNode = null; 458 NodeList nodeList = document.getElementsByTagName(FAILCOUNT); 459 if (nodeList.getLength() == 0) { 460 failureNode = document.createElement(FAILCOUNT); 461 rootNode.appendChild(failureNode); 462 } else { 463 failureNode = (Element ) nodeList.item(0); 464 } 465 466 nodeList = failureNode.getChildNodes(); Element failed_count = null; 468 Element warning_count = null; 469 Element error_count = null; 470 471 if (nodeList.getLength() == 0) { 472 failed_count = 473 getTextNode(FAILNUMBER, 474 new Integer (failedCount).toString()); 475 failureNode.appendChild(failed_count); 476 477 warning_count = 478 getTextNode(WARNINGNUMBER, 479 new Integer ((warningCount)).toString()); 480 failureNode.appendChild(warning_count); 481 482 error_count = 483 getTextNode(ERRORNUMBER, 484 new Integer (errorCount).toString()); 485 failureNode.appendChild(error_count); 486 } else { 487 for (int j = 0; j < nodeList.getLength(); j++) { 488 if (((Element ) nodeList.item(j)).getTagName().equals( 489 FAILNUMBER)) { 490 failed_count = (Element ) nodeList.item(j); 491 (failed_count.getFirstChild()).setNodeValue( 492 new Integer (failedCount).toString()); 493 } 494 if (((Element ) nodeList.item(j)).getTagName().equals( 495 WARNINGNUMBER)) { 496 warning_count = (Element ) nodeList.item(j); 497 (warning_count.getFirstChild()).setNodeValue( 498 new Integer (warningCount).toString()); 499 } 500 if (((Element ) nodeList.item(j)).getTagName().equals( 501 ERRORNUMBER)) { 502 error_count = (Element ) nodeList.item(j); 503 (error_count.getFirstChild()).setNodeValue( 504 new Integer (errorCount).toString()); 505 } 506 } 507 if (failed_count == null) { 508 failed_count = 509 getTextNode(FAILNUMBER, 510 new Integer (failedCount).toString()); 511 failureNode.appendChild(failed_count); 512 } 513 if (warning_count == null) { 514 warning_count = 515 getTextNode(WARNINGNUMBER, 516 new Integer (warningCount).toString()); 517 failureNode.appendChild(warning_count); 518 } 519 if (error_count == null) { 520 error_count = 521 getTextNode(ERRORNUMBER, 522 new Integer (errorCount).toString()); 523 failureNode.appendChild(error_count); 524 } 525 } 526 } 527 528 533 private String writeStackTraceToFile(Throwable e) { 534 StringWriter sw = new StringWriter (); 535 e.printStackTrace(new PrintWriter (sw)); 536 return sw.toString(); 537 } 538 539 545 private File getLocalizedXSLFile() { 546 String xslHome = System.getProperty(Constants.VERIFIER_XSL); 547 if (xslHome == null) { 548 xslHome = System.getProperty(Constants.INSTALL_ROOT) + 549 File.separator + 550 "lib" + File.separator + 552 "verifier"; } 554 Locale locale = Locale.getDefault(); 555 556 String xslFileName = xslHome + File.separator + XSL_FILE + "_" + locale.toString() + ".xsl"; File xslFile = new File (xslFileName); 559 if (xslFile.exists()) { 560 return xslFile; 561 } 562 xslFileName = xslHome + File.separator + XSL_FILE + "_" + locale.getLanguage() + ".xsl"; xslFile = new File (xslFileName); 565 if (xslFile.exists()) { 566 return xslFile; 567 } 568 xslFileName = xslHome + File.separator + XSL_FILE + ".xsl"; xslFile = new File (xslFileName); 571 return xslFile; 572 } 573 574 } 575 | Popular Tags |