| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 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 | package org.jcoderz.phoenix.report; |
|---|
| 34 | |
|---|
| 35 | import java.io.InputStream; |
|---|
| 36 | import java.util.Iterator; |
|---|
| 37 | import java.util.Properties; |
|---|
| 38 | import java.util.StringTokenizer; |
|---|
| 39 | import java.util.logging.Logger; |
|---|
| 40 | |
|---|
| 41 | import javax.xml.bind.JAXBContext; |
|---|
| 42 | import javax.xml.bind.JAXBException; |
|---|
| 43 | import javax.xml.bind.Unmarshaller; |
|---|
| 44 | |
|---|
| 45 | import org.jcoderz.phoenix.pmd.ruleset.jaxb.RuleType; |
|---|
| 46 | import org.jcoderz.phoenix.pmd.ruleset.jaxb.Ruleset; |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Adds the PMD ruleset description as finding type map. |
|---|
| 50 | * |
|---|
| 51 | * @author Michael Griffel |
|---|
| 52 | */ |
|---|
| 53 | public final class PmdFindingType |
|---|
| 54 | extends FindingType |
|---|
| 55 | { |
|---|
| 56 | private static final String CLASSNAME = PmdFindingType.class.getName(); |
|---|
| 57 | private static final Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 58 | |
|---|
| 59 | private static final String PMD_RULESET_JAXB_CONTEXT |
|---|
| 60 | = "org.jcoderz.phoenix.pmd.ruleset.jaxb"; |
|---|
| 61 | |
|---|
| 62 | private static final String PMD_RULESET_PROPERTIES_FILE |
|---|
| 63 | = "rulesets/rulesets.properties"; |
|---|
| 64 | |
|---|
| 65 | private final int mPriority; |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Constructor. |
|---|
| 69 | * @param symbol |
|---|
| 70 | * @param shortText |
|---|
| 71 | * @param description |
|---|
| 72 | */ |
|---|
| 73 | private PmdFindingType ( |
|---|
| 74 | String symbol, String shortText, String description, int priority) |
|---|
| 75 | { |
|---|
| 76 | super(symbol, shortText, description); |
|---|
| 77 | mPriority = priority; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * |
|---|
| 82 | */ |
|---|
| 83 | public static void initialize () |
|---|
| 84 | { |
|---|
| 85 | try |
|---|
| 86 | { |
|---|
| 87 | final Class clazz = PmdFindingType.class; |
|---|
| 88 | final Properties properties = new Properties(); |
|---|
| 89 | properties.load(clazz.getClassLoader().getResourceAsStream( |
|---|
| 90 | PMD_RULESET_PROPERTIES_FILE)); |
|---|
| 91 | |
|---|
| 92 | final String rulesets = (String) properties.get("rulesets.filenames"); |
|---|
| 93 | final StringTokenizer st = new StringTokenizer(rulesets, ","); |
|---|
| 94 | |
|---|
| 95 | final JAXBContext jaxbContext |
|---|
| 96 | = JAXBContext.newInstance(PMD_RULESET_JAXB_CONTEXT, |
|---|
| 97 | PmdFindingType.class.getClassLoader()); |
|---|
| 98 | final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
|---|
| 99 | unmarshaller.setEventHandler(new PmdReportReader()); |
|---|
| 100 | |
|---|
| 101 | while (st.hasMoreTokens()) |
|---|
| 102 | { |
|---|
| 103 | final String rulesetResource = st.nextToken(); |
|---|
| 104 | logger.finest("Try to unmarshalling " + rulesetResource); |
|---|
| 105 | final InputStream in = clazz.getClassLoader().getResourceAsStream( |
|---|
| 106 | rulesetResource); |
|---|
| 107 | addRulesetFindings(unmarshaller, in); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | catch (Exception e) |
|---|
| 111 | { |
|---|
| 112 | throw new RuntimeException("Cannot initialize PmdFindingTypes", e); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | private static void addRulesetFindings (Unmarshaller unmarshaller, |
|---|
| 117 | InputStream in) |
|---|
| 118 | throws JAXBException |
|---|
| 119 | { |
|---|
| 120 | final Ruleset ruleset = (Ruleset) unmarshaller.unmarshal(in); |
|---|
| 121 | for (final Iterator iterator = ruleset.getRule().iterator(); |
|---|
| 122 | iterator.hasNext();) |
|---|
| 123 | { |
|---|
| 124 | final RuleType rule = (RuleType) iterator.next(); |
|---|
| 125 | final String type = rule.getName(); |
|---|
| 126 | final String shortDescription = rule.getName(); |
|---|
| 127 | String details = null; |
|---|
| 128 | String example = null; |
|---|
| 129 | int priority = 0; |
|---|
| 130 | |
|---|
| 131 | for (final Iterator ruleTypeIterator |
|---|
| 132 | = rule.getDescriptionOrExampleOrPriority().iterator(); |
|---|
| 133 | ruleTypeIterator.hasNext();) |
|---|
| 134 | { |
|---|
| 135 | final Object element = ruleTypeIterator.next(); |
|---|
| 136 | if (element instanceof RuleType.Example) |
|---|
| 137 | { |
|---|
| 138 | final RuleType.Example e = (RuleType.Example) element; |
|---|
| 139 | example = "<pre>" + e.getValue() + "</pre>"; |
|---|
| 140 | } |
|---|
| 141 | else if (element instanceof RuleType.Description) |
|---|
| 142 | { |
|---|
| 143 | final RuleType.Description e = (RuleType.Description) element; |
|---|
| 144 | details = e.getValue().trim(); |
|---|
| 145 | } |
|---|
| 146 | else if (element instanceof RuleType.Priority) |
|---|
| 147 | { |
|---|
| 148 | final RuleType.Priority e = (RuleType.Priority) element; |
|---|
| 149 | priority = e.getValue(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | } |
|---|
| 153 | String externalLink = ""; |
|---|
| 154 | if (rule.isSetExternalInfoUrl()) |
|---|
| 155 | { |
|---|
| 156 | externalLink = "<p>Additional info can be found at this <a href='" |
|---|
| 157 | + rule.getExternalInfoUrl() + "'>" + rule.getExternalInfoUrl() |
|---|
| 158 | + " site</a>.</p>"; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | new PmdFindingType(type, shortDescription, |
|---|
| 163 | details + "<br/>" + example + externalLink, priority); |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * Returns the priority. |
|---|
| 169 | * @return the priority. |
|---|
| 170 | */ |
|---|
| 171 | public int getPriority () |
|---|
| 172 | { |
|---|
| 173 | return mPriority; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | } |
|---|