Project Report: fawkez

Packagesummary org.jcoderz.phoenix.report

org.jcoderz.phoenix.report.PmdFindingType

LineHitsNoteSource
1  /*
2   * $Id: PmdFindingType.java 128 2006-12-08 20:13:10Z 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  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  {
560    private static final String CLASSNAME = PmdFindingType.class.getName();
570    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     {
760       super(symbol, shortText, description);
770       mPriority = priority;
780    }
79  
80     /**
81      *
82      */
83     public static void initialize ()
84     {
85        try
86        {
870          final Class clazz = PmdFindingType.class;
880          final Properties properties = new Properties();
890          properties.load(clazz.getClassLoader().getResourceAsStream(
90                       PMD_RULESET_PROPERTIES_FILE));
91  
920          final String rulesets = (String) properties.get("rulesets.filenames");
930          final StringTokenizer st = new StringTokenizer(rulesets, ",");
94  
950          final JAXBContext jaxbContext
96              = JAXBContext.newInstance(PMD_RULESET_JAXB_CONTEXT,
97                  PmdFindingType.class.getClassLoader());
980          final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
990          unmarshaller.setEventHandler(new PmdReportReader());
100  
1010          while (st.hasMoreTokens())
102           {
1030             final String rulesetResource = st.nextToken();
1040             logger.finest("Try to unmarshalling " + rulesetResource);
1050             final InputStream in = clazz.getClassLoader().getResourceAsStream(
106                    rulesetResource);
1070             addRulesetFindings(unmarshaller, in);
1080          }
109        }
1100       catch (Exception e)
111        {
1120          throw new RuntimeException("Cannot initialize PmdFindingTypes", e);
1130       }
1140    }
115  
116     private static void addRulesetFindings (Unmarshaller unmarshaller,
117                                             InputStream in)
118           throws JAXBException
119     {
1200       final Ruleset ruleset = (Ruleset) unmarshaller.unmarshal(in);
1210       for (final Iterator iterator = ruleset.getRule().iterator();
1220             iterator.hasNext();)
123        {
1240          final RuleType rule = (RuleType) iterator.next();
1250          final String type = rule.getName();
1260          final String shortDescription = rule.getName();
1270          String details = null;
1280          String example = null;
1290          int priority = 0;
130  
1310          for (final Iterator ruleTypeIterator
132                   = rule.getDescriptionOrExampleOrPriority().iterator();
1330               ruleTypeIterator.hasNext();)
134           {
1350             final Object element = ruleTypeIterator.next();
1360(1)            if (element instanceof RuleType.Example)
137              {
1380                final RuleType.Example e = (RuleType.Example) element;
1390                example = "<pre>" + e.getValue() + "</pre>";
1400             }
1410             else if (element instanceof RuleType.Description)
142              {
1430                final RuleType.Description e = (RuleType.Description) element;
1440                details = e.getValue().trim();
1450             }
1460             else if (element instanceof RuleType.Priority)
147              {
1480                final RuleType.Priority e = (RuleType.Priority) element;
1490                priority = e.getValue();
150              }
151  
1520          }
1530          String externalLink = "";
1540          if (rule.isSetExternalInfoUrl())
155           {
1560             externalLink = "<p>Additional info can be found at this <a href='"
157                 + rule.getExternalInfoUrl() + "'>" + rule.getExternalInfoUrl()
158                 + " site</a>.</p>";
159           }
160  
161           
1620          new PmdFindingType(type, shortDescription,
163                    details + "<br/>" + example + externalLink, priority);
1640       }
1650    }
166  
167     /**
168      * Returns the priority.
169      * @return the priority.
170      */
171     public int getPriority ()
172     {
1730       return mPriority;
174     }
175  
176 (2)}

Findings in this File

w (1) 136 : 0 Method org.jcoderz.phoenix.report.PmdFindingType.addRulesetFindings(Unmarshaller, InputStream) uses instanceof on multiple types to arbitrate logic
w (2) 176 : 0 org.jcoderz.phoenix.report.PmdFindingType doesn't override FindingType.equals(Object)