| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | |
| 34 | | | |
| 35 | | | |
| 36 | | | |
| 37 | | | |
| 38 | | | |
| 39 | | | |
| 40 | | | |
| 41 | | | |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | |
| 55 | | | package org.jcoderz.phoenix.report; |
| 56 | | | |
| 57 | | | import java.io.IOException; |
| 58 | | | import java.io.PrintStream; |
| 59 | | | import java.util.List; |
| 60 | | | import java.util.logging.Level; |
| 61 | | | import java.util.logging.Logger; |
| 62 | | | |
| 63 | | | import javax.xml.bind.JAXBContext; |
| 64 | | | import javax.xml.bind.JAXBException; |
| 65 | | | import javax.xml.bind.Unmarshaller; |
| 66 | | | |
| 67 | | | import org.jcoderz.commons.util.LoggingUtils; |
| 68 | | | import org.jcoderz.phoenix.report.jaxb.Report; |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | | |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | @author |
| 77 | | | |
| 78 | 0 | | public final class Report2Console |
| 79 | | | { |
| 80 | | | |
| 81 | 0 | | private static final String CLASSNAME = Report2Console.class.getName(); |
| 82 | | | |
| 83 | | | |
| 84 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 85 | | | |
| 86 | | | private static final int SEVERITY_INDENT = 12; |
| 87 | | | private java.io.File mInputData; |
| 88 | 0 | (1) | private Level mLogLevel = Level.INFO; |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | @param |
| 94 | | | @throws |
| 95 | | | @throws |
| 96 | | | |
| 97 | | | public static void main (String[] args) |
| 98 | | | throws IOException, JAXBException |
| 99 | | | { |
| 100 | 0 | | final Report2Console engine = new Report2Console(); |
| 101 | | | |
| 102 | 0 | | engine.parseArguments(args); |
| 103 | | | |
| 104 | 0 | | Logger.getLogger("org.jcoderz.phoenix.report").setLevel(Level.FINEST); |
| 105 | 0 | | engine.process(); |
| 106 | 0 | | } |
| 107 | | | |
| 108 | | | |
| 109 | | | |
| 110 | | | @param |
| 111 | | | @throws |
| 112 | | | |
| 113 | | | public void setInputFile (java.io.File file) throws IOException |
| 114 | | | { |
| 115 | 0 | | mInputData = file.getCanonicalFile(); |
| 116 | 0 | | if (!mInputData.canRead()) |
| 117 | | | { |
| 118 | 0 | | throw new RuntimeException("Can not read report file '" |
| 119 | | | + mInputData + "'."); |
| 120 | | | } |
| 121 | 0 | | logger.config("Using report file " + mInputData + "."); |
| 122 | 0 | | } |
| 123 | | | |
| 124 | | | |
| 125 | | | |
| 126 | | | @throws |
| 127 | | | @throws |
| 128 | | | |
| 129 | | | public void process () |
| 130 | | | throws JAXBException, IOException |
| 131 | | | { |
| 132 | 0 | | final JAXBContext jaxbContext |
| 133 | | | = JAXBContext.newInstance("org.jcoderz.phoenix.report.jaxb", |
| 134 | | | this.getClass().getClassLoader()); |
| 135 | 0 | | final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
| 136 | 0 | (2) | unmarshaller.setValidating(true); |
| 137 | 0 | | final Report report = (Report) unmarshaller.unmarshal(mInputData); |
| 138 | | | |
| 139 | | | for (final org.jcoderz.phoenix.report.jaxb.File file |
| 140 | 0 | (3) | : (List<org.jcoderz.phoenix.report.jaxb.File>) report.getFile()) |
| 141 | | | { |
| 142 | | | try |
| 143 | | | { |
| 144 | 0 | | outputForFile(file); |
| 145 | | | } |
| 146 | 0 | | catch (Exception ex) |
| 147 | | | { |
| 148 | 0 | | logger.log(Level.SEVERE, |
| 149 | | | "Failed to generate report for '" + file.getName() + "'.", |
| 150 | | | ex); |
| 151 | 0 | | } |
| 152 | | | } |
| 153 | | | |
| 154 | 0 | | logger.fine("Done."); |
| 155 | 0 | | } |
| 156 | | | |
| 157 | | | private void outputForFile (org.jcoderz.phoenix.report.jaxb.File file) |
| 158 | | | { |
| 159 | 0 | | final String locatorStart |
| 160 | | | = file.getPackage() + "." + file.getClassname() + "."; |
| 161 | 0 | (4) | final String locatorFile |
| 162 | | | = "(" + file.getName().substring( |
| 163 | | (5) | file.getName().lastIndexOf("\\") +1); |
| 164 | 0 | | final String filler = " "; |
| 165 | | | for (final org.jcoderz.phoenix.report.jaxb.Item item |
| 166 | 0 | (6) | : (List<org.jcoderz.phoenix.report.jaxb.Item>) file.getItem()) |
| 167 | | | { |
| 168 | 0 | | final Severity severity = item.getSeverity(); |
| 169 | 0 | (7) | PrintStream out = System.out; |
| 170 | 0 | | if (Severity.FILTERED.equals(severity) |
| 171 | | | || Severity.OK.equals(severity)) |
| 172 | | | { |
| 173 | 0 | | continue; |
| 174 | | | } |
| 175 | 0 | | else if (Severity.INFO.compareTo(severity) < 0) |
| 176 | | | { |
| 177 | 0 | | out = System.out; |
| 178 | | | } |
| 179 | | | else |
| 180 | | | { |
| 181 | 0 | | out = System.err; |
| 182 | | | } |
| 183 | 0 | (8) | String severityString = item.getSeverity().toString(); |
| 184 | 0 | | out.println(severityString |
| 185 | | | + filler.substring(0, SEVERITY_INDENT - severityString.length()) |
| 186 | | | + ": " + item.getMessage()); |
| 187 | 0 | | out.println( |
| 188 | | (9) | " at " + locatorStart + |
| 189 | | | item.getFindingType() + locatorFile + ":" |
| 190 | | | + item.getLine() + ")"); |
| 191 | 0 | | out.flush(); |
| 192 | 0 | | } |
| 193 | 0 | | } |
| 194 | | | |
| 195 | | | private void parseArguments (String[] args) |
| 196 | | | { |
| 197 | | | try |
| 198 | | | { |
| 199 | 0 | | for (int i = 0; i < args.length; ) |
| 200 | | | { |
| 201 | 0 | | if ("-report".equals(args[i])) |
| 202 | | | { |
| 203 | 0 | | setInputFile(new java.io.File(args[i + 1])); |
| 204 | | | } |
| 205 | 0 | | else if ("-loglevel".equals(args[i])) |
| 206 | | | { |
| 207 | 0 | | setLoglevel(args[i + 1]); |
| 208 | | | } |
| 209 | | | else |
| 210 | | | { |
| 211 | 0 | | throw new IllegalArgumentException( |
| 212 | | | "Invalid argument '" + args[i] + "'"); |
| 213 | | | } |
| 214 | 0 | | i += 1 + 1 ; |
| 215 | | | } |
| 216 | | | } |
| 217 | 0 | | catch (IndexOutOfBoundsException e) |
| 218 | | | { |
| 219 | 0 | | final IllegalArgumentException ex |
| 220 | | | = new IllegalArgumentException("Missing value for " |
| 221 | | | + args[args.length - 1]); |
| 222 | 0 | | ex.initCause(e); |
| 223 | 0 | | throw ex; |
| 224 | | | } |
| 225 | 0 | | catch (Exception e) |
| 226 | | | { |
| 227 | 0 | | final IllegalArgumentException ex = new IllegalArgumentException( |
| 228 | | | "Problem with argument value for " + args[args.length - 1]); |
| 229 | 0 | | ex.initCause(e); |
| 230 | 0 | | throw ex; |
| 231 | 0 | | } |
| 232 | 0 | | } |
| 233 | | | |
| 234 | | | private void setLoglevel (String loglevel) |
| 235 | | | { |
| 236 | 0 | | mLogLevel = Level.parse(loglevel); |
| 237 | 0 | | LoggingUtils.setGlobalHandlerLogLevel(Level.ALL); |
| 238 | 0 | | logger.fine("Setting log level: " + mLogLevel); |
| 239 | 0 | | logger.setLevel(mLogLevel); |
| 240 | 0 | | } |
| 241 | | | |
| 242 | | | } |