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  
14  package gov.nasa.pds.tools.logging;
15  
16  import java.util.List;
17  import java.util.ArrayList;
18  import java.util.Iterator;
19  import java.util.logging.Formatter;
20  import java.util.logging.Handler;
21  import java.util.logging.LogRecord;
22  import gov.nasa.pds.tools.label.Label;
23  
24  public class FullLogFormatter extends Formatter {
25  	private int numPassed;
26  	private int numFailed;
27  	private int numSkipped;
28  	private List records;
29  	private StringBuffer config;
30  	private StringBuffer parameters;
31  	private boolean headerPrinted;
32  	private static String lineFeed = System.getProperty("line.separator", "\n");
33  	private static String doubleLineFeed = System.getProperty("line.separator", "\n") + System.getProperty("line.separator", "\n");
34  	
35  	public FullLogFormatter() {
36  		numPassed = 0;
37  		numFailed = 0;
38  		numSkipped = 0;
39  		headerPrinted = false;
40  		records = new ArrayList();
41  		config = new StringBuffer();
42  		parameters = new StringBuffer("Parameter Settings:" + doubleLineFeed);
43  	}
44  
45  	/* (non-Javadoc)
46       * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
47       */
48  	public String format(LogRecord record) {
49  		ToolsLogRecord toolsRecord = (ToolsLogRecord) record;
50  		
51  		if (toolsRecord.getLevel() == ToolsLevel.CONFIGURATION) {
52  			config.append("  " + toolsRecord.getMessage() + lineFeed);
53  		} else if (toolsRecord.getLevel() == ToolsLevel.PARAMETER) {
54  			parameters.append("  " + toolsRecord.getMessage() + lineFeed);
55  		} else if (toolsRecord.getLevel() == ToolsLevel.NOTIFICATION && (Label.PASS.equals(toolsRecord.getMessage()) || 
56  				Label.SKIP.equals(toolsRecord.getMessage()) || Label.FAIL.equals(toolsRecord.getMessage()))) {
57  			return processRecords(toolsRecord);
58  		} else {
59  			records.add(toolsRecord);
60  		}
61  		
62  		return "";
63  	}
64  	
65  	private String processRecords(ToolsLogRecord record) {
66  		StringBuffer report = new StringBuffer("");
67  		
68  		if (!headerPrinted) {
69  			headerPrinted = true;
70  			report.append("PDS Validation Tool Report" + doubleLineFeed);
71  			report.append(config);
72  			report.append(lineFeed);
73  			report.append(parameters);
74  			report.append(lineFeed);
75  			report.append("Validation Details:" + lineFeed);
76  		}
77  		
78  		if (record.getMessage().equals("PASS"))
79  			numPassed++;
80  		else if (record.getMessage().equals("FAIL"))
81  			numFailed++;
82  		else
83  			numSkipped++;
84  		
85  		report.append(lineFeed + "  " + record.getMessage() + ": " + record.getFile() + lineFeed);
86  		
87  		for (Iterator i = records.iterator(); i.hasNext();) {
88  			ToolsLogRecord tlr = (ToolsLogRecord) i.next();
89  			if (tlr.getFile() != null && (record.getFile().equals(tlr.getFile()) || record.getFile().equals(tlr.getContext()))) {
90  				if (tlr.getLevel() == ToolsLevel.NOTIFICATION) {
91  					if (tlr.getContext() != null) {
92  						if ("BEGIN".equals(tlr.getMessage()))
93  							report.append("    Begin Fragment: " + tlr.getFile() + lineFeed);
94  						else if ("END".equals(tlr.getMessage()))
95  							report.append("    End Fragment: " + tlr.getFile() + lineFeed);		
96  					}
97  				} else if (tlr.getLevel() != ToolsLevel.SEVERE) {
98  					report.append("      " + tlr.getLevel().getName() + "  ");
99  					if (tlr.getLine() != -1)
100 						report.append("line " + tlr.getLine() + ": ");
101 					report.append(tlr.getMessage() + lineFeed);
102 				} else {
103 					report.append("      ERROR  ");
104 					if (tlr.getLine() != -1)
105 						report.append("line " + tlr.getLine() + ": ");
106 					report.append(tlr.getMessage() + lineFeed);
107 				}
108 			}
109 		}
110 		
111 		records = new ArrayList();
112 		return report.toString();
113 	}
114 	
115 	public String getTail(Handler handler) {
116 		StringBuffer report = new StringBuffer(doubleLineFeed + "Summary:" + doubleLineFeed);
117 		int totalFiles = numPassed + numFailed + numSkipped;
118 		int totalValidated = numPassed + numFailed;
119 		report.append("  " + totalValidated + " of " + totalFiles + " validated, " + numSkipped + " skipped" + lineFeed);
120 		report.append("  " + numPassed + " of " + totalValidated + " passed" + doubleLineFeed);
121 		report.append("End of Report" + lineFeed);
122 		return report.toString();
123 	}
124 
125 }