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