| 1 | /* |
|---|
| 2 | * $Id: Java2Html.java 816 2008-05-01 20:04:04Z amandel $ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | /* |
|---|
| 34 | * Java2HTML v0.1 alpha converts a java source code into HTML with |
|---|
| 35 | * syntax highlighting for keywords, comments, strings and chars |
|---|
| 36 | * |
|---|
| 37 | * The contents of this file are subject to the |
|---|
| 38 | * Mozilla Public License Version 1.1 (the "License"); |
|---|
| 39 | * you may not use this file except in compliance with the License. |
|---|
| 40 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
|---|
| 41 | * |
|---|
| 42 | * Software distributed under the License is distributed on an |
|---|
| 43 | * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. |
|---|
| 44 | * See the License for the specific language governing rights |
|---|
| 45 | * and limitations under the License. |
|---|
| 46 | * |
|---|
| 47 | * The Original Code is Java2HTML Converter v0.1 alpha. |
|---|
| 48 | * The Initial Developer of the Original Code is Borislav Manolov. |
|---|
| 49 | * Portions created by Borislav Manolov are Copyright (C) 2003, |
|---|
| 50 | * Borislav Manolov. All Rights Reserved. |
|---|
| 51 | * |
|---|
| 52 | * Submit bugs and comments at manfear@web.de |
|---|
| 53 | * 03/03/03 |
|---|
| 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 | * Read the report and output this to the console in a eclipse |
|---|
| 72 | * friendly way. |
|---|
| 73 | * |
|---|
| 74 | * This is a early 'prototype' version. |
|---|
| 75 | * |
|---|
| 76 | * @author Andreas Mandel |
|---|
| 77 | */ |
|---|
| 78 | public final class Report2Console |
|---|
| 79 | { |
|---|
| 80 | /** Name of this class. */ |
|---|
| 81 | private static final String CLASSNAME = Report2Console.class.getName(); |
|---|
| 82 | |
|---|
| 83 | /** The logger used for technical logging inside this class. */ |
|---|
| 84 | private static final Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 85 | |
|---|
| 86 | private static final int SEVERITY_INDENT = 12; |
|---|
| 87 | private java.io.File mInputData; |
|---|
| 88 | private Level mLogLevel = Level.INFO; |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * Main entry point. |
|---|
| 92 | * |
|---|
| 93 | * @param args The command line arguments. |
|---|
| 94 | * @throws IOException an io exception occurs. |
|---|
| 95 | * @throws JAXBException if the xml can not be parsed. |
|---|
| 96 | */ |
|---|
| 97 | public static void main (String[] args) |
|---|
| 98 | throws IOException, JAXBException |
|---|
| 99 | { |
|---|
| 100 | final Report2Console engine = new Report2Console(); |
|---|
| 101 | |
|---|
| 102 | engine.parseArguments(args); |
|---|
| 103 | // Turn on logging |
|---|
| 104 | Logger.getLogger("org.jcoderz.phoenix.report").setLevel(Level.FINEST); |
|---|
| 105 | engine.process(); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * The input file containing the jcoderz report. |
|---|
| 110 | * @param file input file containing the jcoderz report. |
|---|
| 111 | * @throws IOException if access to the file fails. |
|---|
| 112 | */ |
|---|
| 113 | public void setInputFile (java.io.File file) throws IOException |
|---|
| 114 | { |
|---|
| 115 | mInputData = file.getCanonicalFile(); |
|---|
| 116 | if (!mInputData.canRead()) |
|---|
| 117 | { |
|---|
| 118 | throw new RuntimeException("Can not read report file '" |
|---|
| 119 | + mInputData + "'."); |
|---|
| 120 | } |
|---|
| 121 | logger.config("Using report file " + mInputData + "."); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | * Starts the actual generation process. |
|---|
| 126 | * @throws JAXBException if the xmp parsing fails. |
|---|
| 127 | * @throws IOException if a IO problem occurs. |
|---|
| 128 | */ |
|---|
| 129 | public void process () |
|---|
| 130 | throws JAXBException, IOException |
|---|
| 131 | { |
|---|
| 132 | final JAXBContext jaxbContext |
|---|
| 133 | = JAXBContext.newInstance("org.jcoderz.phoenix.report.jaxb", |
|---|
| 134 | this.getClass().getClassLoader()); |
|---|
| 135 | final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
|---|
| 136 | unmarshaller.setValidating(true); |
|---|
| 137 | final Report report = (Report) unmarshaller.unmarshal(mInputData); |
|---|
| 138 | |
|---|
| 139 | for (final org.jcoderz.phoenix.report.jaxb.File file |
|---|
| 140 | : (List<org.jcoderz.phoenix.report.jaxb.File>) report.getFile()) |
|---|
| 141 | { |
|---|
| 142 | try |
|---|
| 143 | { |
|---|
| 144 | outputForFile(file); |
|---|
| 145 | } |
|---|
| 146 | catch (Exception ex) |
|---|
| 147 | { |
|---|
| 148 | logger.log(Level.SEVERE, |
|---|
| 149 | "Failed to generate report for '" + file.getName() + "'.", |
|---|
| 150 | ex); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | logger.fine("Done."); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | private void outputForFile (org.jcoderz.phoenix.report.jaxb.File file) |
|---|
| 158 | { |
|---|
| 159 | final String locatorStart |
|---|
| 160 | = file.getPackage() + "." + file.getClassname() + "."; |
|---|
| 161 | final String locatorFile |
|---|
| 162 | = "(" + file.getName().substring( |
|---|
| 163 | file.getName().lastIndexOf("\\") +1); |
|---|
| 164 | final String filler = " "; |
|---|
| 165 | for (final org.jcoderz.phoenix.report.jaxb.Item item |
|---|
| 166 | : (List<org.jcoderz.phoenix.report.jaxb.Item>) file.getItem()) |
|---|
| 167 | { |
|---|
| 168 | final Severity severity = item.getSeverity(); |
|---|
| 169 | PrintStream out = System.out; |
|---|
| 170 | if (Severity.FILTERED.equals(severity) |
|---|
| 171 | || Severity.OK.equals(severity)) |
|---|
| 172 | { |
|---|
| 173 | continue; |
|---|
| 174 | } |
|---|
| 175 | else if (Severity.INFO.compareTo(severity) < 0) |
|---|
| 176 | { |
|---|
| 177 | out = System.out; |
|---|
| 178 | } |
|---|
| 179 | else |
|---|
| 180 | { |
|---|
| 181 | out = System.err; |
|---|
| 182 | } |
|---|
| 183 | String severityString = item.getSeverity().toString(); |
|---|
| 184 | out.println(severityString |
|---|
| 185 | + filler.substring(0, SEVERITY_INDENT - severityString.length()) |
|---|
| 186 | + ": " + item.getMessage()); |
|---|
| 187 | out.println( |
|---|
| 188 | " at " + locatorStart + |
|---|
| 189 | item.getFindingType() + locatorFile + ":" |
|---|
| 190 | + item.getLine() + ")"); |
|---|
| 191 | out.flush(); |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | private void parseArguments (String[] args) |
|---|
| 196 | { |
|---|
| 197 | try |
|---|
| 198 | { |
|---|
| 199 | for (int i = 0; i < args.length; ) |
|---|
| 200 | { |
|---|
| 201 | if ("-report".equals(args[i])) |
|---|
| 202 | { |
|---|
| 203 | setInputFile(new java.io.File(args[i + 1])); |
|---|
| 204 | } |
|---|
| 205 | else if ("-loglevel".equals(args[i])) |
|---|
| 206 | { |
|---|
| 207 | setLoglevel(args[i + 1]); |
|---|
| 208 | } |
|---|
| 209 | else |
|---|
| 210 | { |
|---|
| 211 | throw new IllegalArgumentException( |
|---|
| 212 | "Invalid argument '" + args[i] + "'"); |
|---|
| 213 | } |
|---|
| 214 | i += 1 /* command */ + 1 /* argument */; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | catch (IndexOutOfBoundsException e) |
|---|
| 218 | { |
|---|
| 219 | final IllegalArgumentException ex |
|---|
| 220 | = new IllegalArgumentException("Missing value for " |
|---|
| 221 | + args[args.length - 1]); |
|---|
| 222 | ex.initCause(e); |
|---|
| 223 | throw ex; |
|---|
| 224 | } |
|---|
| 225 | catch (Exception e) |
|---|
| 226 | { |
|---|
| 227 | final IllegalArgumentException ex = new IllegalArgumentException( |
|---|
| 228 | "Problem with argument value for " + args[args.length - 1]); |
|---|
| 229 | ex.initCause(e); |
|---|
| 230 | throw ex; |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | private void setLoglevel (String loglevel) |
|---|
| 235 | { |
|---|
| 236 | mLogLevel = Level.parse(loglevel); |
|---|
| 237 | LoggingUtils.setGlobalHandlerLogLevel(Level.ALL); |
|---|
| 238 | logger.fine("Setting log level: " + mLogLevel); |
|---|
| 239 | logger.setLevel(mLogLevel); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | } |
|---|