root/trunk/src/java/org/jcoderz/phoenix/report/JcoderzReport.java

Revision 1454, 6.5 kB (checked in by amandel, 3 years ago)

Support for a 'global' finding that is not related to any source.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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 */
33package org.jcoderz.phoenix.report;
34
35import java.io.File;
36import java.io.OutputStream;
37import java.util.Collections;
38import java.util.Iterator;
39import java.util.List;
40import java.util.Map;
41import java.util.Map.Entry;
42import java.util.logging.Logger;
43
44import javax.xml.bind.JAXBException;
45
46import org.jcoderz.phoenix.report.jaxb.Item;
47import org.jcoderz.phoenix.report.jaxb.ObjectFactory;
48import org.jcoderz.phoenix.report.jaxb.Report;
49
50/**
51 * This class implements writing of <code>jcoderz-report.xml</code> files.
52 *
53 * @author Michael Griffel
54 */
55public final class JcoderzReport
56   extends AbstractReportReader
57{
58    /** JAXB context path. */
59    public static final String JCODERZ_JAXB_CONTEXT_PATH
60       = "org.jcoderz.phoenix.report.jaxb";
61
62
63    private static final String CLASSNAME = JcoderzReport.class.getName();
64
65    private static final Logger logger = Logger.getLogger(CLASSNAME);
66
67    private final Report mReport = new ObjectFactory().createReport();
68
69    /** The report level. */
70    private ReportLevel mLevel = ReportLevel.PROD;
71
72
73   JcoderzReport ()
74         throws JAXBException
75   {
76      super(JCODERZ_JAXB_CONTEXT_PATH);
77   }
78
79
80   /** {@inheritDoc} */
81   public Map getItems ()
82      throws JAXBException
83   {
84      throw new NoSuchMethodError();
85   }
86
87
88   /**
89    * Sets the report level.
90    *
91    * @param level The level of the report
92    */
93   public void setLevel (ReportLevel level)
94   {
95      mLevel = level;
96   }
97
98
99   /** {@inheritDoc} */
100   public void parse (File f)
101      throws JAXBException
102   {
103      // TODO
104      throw new RuntimeException("Method not implemented. (TODO)");
105   }
106
107
108   /**
109    * Writes the report to the specified stream by using JAXB.
110    *
111    * @param out where to write the jCoderZ report to.
112    * @param items the file items. The items are a Map of ResourceInfo and
113    *    a List of the type jCoderZ Item (org.jcoderz.phoenix.report.jaxb.Item)
114    * @throws JAXBException for JAXB errors
115    */
116   public void write (OutputStream out, Map<ResourceInfo, List<Item>> items)
117      throws JAXBException
118   {
119      addItems(mLevel, items);
120      writeReport(out);
121   }
122
123
124   /**
125    * Just add items of a specific level to the report, do not write the file
126    * to persistent storage yet.
127    *
128    * @param level The level under which to add the items.
129    * @param items A map of items.
130    * @throws JAXBException When the JAXB file representation can not
131    *      be created.
132    */
133   public void addItems (ReportLevel level, Map<ResourceInfo, List<Item>> items)
134      throws JAXBException
135   {
136      final Map<ResourceInfo, List<Item>> files = items;
137
138      for (Entry<ResourceInfo, List<Item>> entry : files.entrySet())
139      {
140         final ResourceInfo info = entry.getKey();
141         final List<Item> itemList = entry.getValue();
142         final org.jcoderz.phoenix.report.jaxb.File f
143            = new org.jcoderz.phoenix.report.jaxb.ObjectFactory().createFile();
144         if (info != null)
145         {
146             f.setName(info.getResourceName());
147             f.setClassname(info.getClassname());
148             f.setPackage(info.getPackage());
149             f.setSrcDir(info.getSourcDir());
150             f.setLoc(info.getLinesOfCode());
151         }
152         f.setLevel(level);
153         f.getItem().addAll(itemList);
154
155         mReport.getFile().add(f);
156      }
157   }
158
159
160   /**
161    * Write the report to persistent storage.
162    *
163    * @param out The output stream to write the report to.
164    * @throws JAXBException In case a marshalling exception occurs.
165    */
166   public void writeReport (OutputStream out)
167      throws JAXBException
168   {
169      getMarshaller().marshal(mReport, out);
170   }
171
172
173   /**
174    * Sets the project's home folder.
175    *
176    * @param s the home folder
177    */
178   public void setProjectHome (String s)
179   {
180      mReport.setProjectHome(s);
181   }
182
183
184   /**
185    * Gets the project's home folder.
186    *
187    * @return the home folder
188    */
189   public String getProjectHome ()
190   {
191      return mReport.getProjectHome();
192   }
193
194
195   /**
196    * Sets the project name.
197    *
198    * @param s the name of the project
199    */
200   public void setProjectName (String s)
201   {
202      mReport.setName(s);
203   }
204
205
206   /**
207    * Gets the project name.
208    *
209    * @return the name of the project
210    */
211   public String getProjectName ()
212   {
213      return mReport.getName();
214   }
215
216   public void addSystemLevelIssue (String message, Throwable e,
217       ResourceInfo res)
218   {
219       try
220       {
221           final Item item = new ObjectFactory().createItem();
222           item.setMessage(message);
223           item.setSeverity(Severity.ERROR);
224           item.setFindingType(SystemFindingType.SYS_ERROR.getSymbol());
225           item.setOrigin(Origin.SYSTEM);
226           addItems(ReportLevel.PROD, Collections.singletonMap(res,
227               Collections.singletonList(item)));
228       }
229       catch (JAXBException ex)
230       {
231           // give up.. it is about time
232           throw new RuntimeException(
233               "Failed to add detail for " + e, ex);
234       }
235   }
236
237}
Note: See TracBrowser for help on using the browser.