View Javadoc

1   // Copyright 2006-2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id: FullLogFormatter.java 3461 2008-08-07 17:43:26Z pramirez $
14  
15  package gov.nasa.pds.tools.logging;
16  
17  import java.util.List;
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.logging.Formatter;
21  import java.util.logging.Handler;
22  import java.util.logging.LogRecord;
23  import gov.nasa.pds.tools.label.Label;
24  
25  public class FullLogFormatter extends Formatter {
26  	private int numPassed;
27  	private int numFailed;
28  	private int numSkipped;
29  	private List records;
30  	private StringBuffer config;
31  	private StringBuffer parameters;
32  	private boolean headerPrinted;
33  	private static String lineFeed = System.getProperty("line.separator", "\n");
34  	private static String doubleLineFeed = System.getProperty("line.separator", "\n") + System.getProperty("line.separator", "\n");
35  	
36  	public FullLogFormatter() {
37  		numPassed = 0;
38  		numFailed = 0;
39  		numSkipped = 0;
40  		headerPrinted = false;
41  		records = new ArrayList();
42  		config = new StringBuffer();
43  		parameters = new StringBuffer("Parameter Settings:" + doubleLineFeed);
44  	}
45  
46  	/* (non-Javadoc)
47       * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
48       */
49  	public String format(LogRecord record) {
50  		ToolsLogRecord toolsRecord = (ToolsLogRecord) record;
51  		
52  		if (toolsRecord.getLevel() == ToolsLevel.CONFIGURATION) {
53  			config.append("  " + toolsRecord.getMessage() + lineFeed);
54  		} else if (toolsRecord.getLevel() == ToolsLevel.PARAMETER) {
55  			parameters.append("  " + toolsRecord.getMessage() + lineFeed);
56  		} else if (toolsRecord.getLevel() == ToolsLevel.NOTIFICATION && (Label.PASS.equals(toolsRecord.getMessage()) || 
57  				Label.SKIP.equals(toolsRecord.getMessage()) || Label.FAIL.equals(toolsRecord.getMessage()))) {
58  			return processRecords(toolsRecord);
59  		} else {
60  			records.add(toolsRecord);
61  		}
62  		
63  		return "";
64  	}
65  	
66  	private String processRecords(ToolsLogRecord record) {
67  		StringBuffer report = new StringBuffer("");
68  		
69  		if (!headerPrinted) {
70  			headerPrinted = true;
71  			report.append("PDS Validation Tool Report" + doubleLineFeed);
72  			report.append(config);
73  			report.append(lineFeed);
74  			report.append(parameters);
75  			report.append(lineFeed);
76  			report.append("Validation Details:" + lineFeed);
77  		}
78  		
79  		if (record.getMessage().equals("PASS"))
80  			numPassed++;
81  		else if (record.getMessage().equals("FAIL"))
82  			numFailed++;
83  		else
84  			numSkipped++;
85  		
86  		report.append(lineFeed + "  " + record.getMessage() + ": " + record.getFile() + lineFeed);
87  		
88  		for (Iterator i = records.iterator(); i.hasNext();) {
89  			ToolsLogRecord tlr = (ToolsLogRecord) i.next();
90  			if (tlr.getFile() != null && (record.getFile().equals(tlr.getFile()) || record.getFile().equals(tlr.getContext()))) {
91  				if (tlr.getLevel() == ToolsLevel.NOTIFICATION) {
92  					if (tlr.getContext() != null) {
93  						if ("BEGIN".equals(tlr.getMessage()))
94  							report.append("    Begin Fragment: " + tlr.getFile() + lineFeed);
95  						else if ("END".equals(tlr.getMessage()))
96  							report.append("    End Fragment: " + tlr.getFile() + lineFeed);		
97  					}
98  				} else if (tlr.getLevel() != ToolsLevel.SEVERE) {
99  					report.append("      " + tlr.getLevel().getName() + "  ");
100 					if (tlr.getLine() != -1)
101 						report.append("line " + tlr.getLine() + ": ");
102 					report.append(tlr.getMessage() + lineFeed);
103 				} else {
104 					report.append("      ERROR  ");
105 					if (tlr.getLine() != -1)
106 						report.append("line " + tlr.getLine() + ": ");
107 					report.append(tlr.getMessage() + lineFeed);
108 				}
109 			}
110 		}
111 		
112 		records = new ArrayList();
113 		return report.toString();
114 	}
115 	
116 	public String getTail(Handler handler) {
117 		StringBuffer report = new StringBuffer(doubleLineFeed + "Summary:" + doubleLineFeed);
118 		int totalFiles = numPassed + numFailed + numSkipped;
119 		int totalValidated = numPassed + numFailed;
120 		report.append("  " + totalValidated + " of " + totalFiles + " validated, " + numSkipped + " skipped" + lineFeed);
121 		report.append("  " + numPassed + " of " + totalValidated + " passed" + doubleLineFeed);
122 		report.append("End of Report" + lineFeed);
123 		return report.toString();
124 	}
125 
126 }