Project Report: fawkez

Packagesummary org.jcoderz.phoenix.report

org.jcoderz.phoenix.report.AbstractReportReader

LineHitsNoteSource
1  /*
2   * $Id: AbstractReportReader.java 1011 2008-06-16 17:57:36Z 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.ByteArrayInputStream;
36  import java.io.File;
37  import java.io.InputStream;
38  import java.util.ArrayList;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.Map.Entry;
42  import java.util.logging.Logger;
43  
44  import javax.xml.bind.JAXBContext;
45  import javax.xml.bind.JAXBException;
46  import javax.xml.bind.Marshaller;
47  import javax.xml.bind.Unmarshaller;
48  import javax.xml.bind.UnmarshallerHandler;
49  import javax.xml.bind.ValidationEvent;
50  import javax.xml.bind.ValidationEventHandler;
51  import javax.xml.bind.ValidationEventLocator;
52  import javax.xml.parsers.ParserConfigurationException;
53  import javax.xml.parsers.SAXParserFactory;
54  
55  import org.jcoderz.phoenix.report.jaxb.Item;
56  import org.xml.sax.EntityResolver;
57  import org.xml.sax.InputSource;
58  import org.xml.sax.SAXException;
59  import org.xml.sax.XMLReader;
60  
61  /**
62   * Base Report Reader class.
63   *
64   * Every report reader must extend from this class.
65   *
66   * @author Michael Griffel
67   */
68  public abstract class AbstractReportReader
69          implements ReportReader, ValidationEventHandler
70  {
71100     private static final String CLASSNAME
72          = AbstractReportReader.class.getName();
73100     private static final Logger logger = Logger.getLogger(CLASSNAME);
74  
75      private final JAXBContext mJaxbContext;
76100     private Marshaller mMarshaller = null;
77100     private Unmarshaller mUnmarshaller = null;
78  
79      AbstractReportReader (String jaxbContext)
80              throws JAXBException
81100     {
82100         mJaxbContext = JAXBContext.newInstance(jaxbContext,
83                this.getClass().getClassLoader());
84100     }
85  
86      /**
87       * Returns a Marshaller for this report reader.
88       *
89       * @return a Marshaller for this report reader.
90       * @throws JAXBException if the creation of the marshaller failed.
91       */
92      public final Marshaller getMarshaller ()
93              throws JAXBException
94      {
950         if (mMarshaller == null)
96          {
970             mMarshaller = mJaxbContext.createMarshaller();
980             mMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
99                      Boolean.TRUE);
1000             mMarshaller.setEventHandler(this);
101          }
1020         return mMarshaller;
103      }
104  
105      /**
106       * Returns a Unmarshaller for this report reader.
107       *
108       * @return a Unmarshaller for this report reader.
109       * @throws JAXBException if the creation of the unmarshaller failed.
110       */
111      public final Unmarshaller getUnmarshaller ()
112              throws JAXBException
113      {
1140         if (mUnmarshaller == null)
115          {
1160             mUnmarshaller = mJaxbContext.createUnmarshaller();
1170(1)            mUnmarshaller.setValidating(true);
1180             mUnmarshaller.setEventHandler(this);
119          }
1200         return mUnmarshaller;
121      }
122  
123      /**
124       * Reads the XML data from the given InputStream <tt>in</tt> and unmarshal
125       * it to the corresponding JAXB object.
126       *
127       * This method uses a native {@link XMLReader} to parse the XML data using
128       * a simple {@link EntityResolver} that resolves any entity to an empty
129       * string.
130       *
131       * @param in the input stream to read the XML data from.
132       * @return the JAXB object.
133       */
134      public final Object unmarshall (InputStream in)
135      {
136          final Object result;
137          try
138          {
1390             final SAXParserFactory factory = SAXParserFactory.newInstance();
1400             factory.setNamespaceAware(true);
141              final XMLReader reader;
142              try
143              {
1440                reader = factory.newSAXParser().getXMLReader();
145              }
1460             catch (SAXException e)
147              {
1480                 throw new RuntimeException(e);
149              }
1500             catch (ParserConfigurationException e)
151              {
1520                 throw new RuntimeException(e);
1530             }
1540             final UnmarshallerHandler un
155                  = getUnmarshaller().getUnmarshallerHandler();
1560             reader.setEntityResolver(new DummyEntityResolver());
1570             reader.setContentHandler(un);
1580             reader.parse(new InputSource(in));
1590             result = un.getResult();
160          }
1610         catch (Exception e)
162          {
1630(2)            throw new RuntimeException("FIXME", e);
1640         }
1650         return result;
166      }
167  
168      /** {@inheritDoc} */
169      public final void merge (Map<ResourceInfo, List<Item>> toItems)
170              throws JAXBException
171      {
1720         final Map<ResourceInfo, List<Item>> myResourceList = getItems();
173  
1740         for (Entry<ResourceInfo, List<Item>> entry : myResourceList.entrySet())
175          {
1760             final ResourceInfo info = entry.getKey();
1770             List<Item> items = toItems.get(info);
1780             if (items == null)
179              {
1800                 items = new ArrayList<Item>();
1810                 toItems.put(info, items);
182              }
1830             items.addAll(entry.getValue());
1840         }
1850     }
186  
187      /**
188       * Returns the items of the input report as a Map of filename string
189       * and of the type Item (org.jcoderz.phoenix.report.jaxb.Item).
190       *
191       * @return the items of the input report as a List of the type Item.
192       * @throws JAXBException if an JAXB exception occures.
193       */
194      protected abstract Map<ResourceInfo, List<Item>> getItems ()
195              throws JAXBException;
196  
197      /** {@inheritDoc} */
198      public final boolean handleEvent (ValidationEvent e)
199      {
2000         final ValidationEventLocator l = e.getLocator();
2010         final StringBuilder sb = new StringBuilder();
2020         sb.append("[ValidationEvent:");
2030         sb.append(", message=");
2040         sb.append(e.getMessage());
2050         sb.append(", severity=");
2060         sb.append(e.getSeverity());
2070         sb.append(", link exception=");
2080         sb.append(e.getLinkedException());
2090         sb.append(", message=");
2100         sb.append(l.getObject());
2110         sb.append(", column number=");
2120         sb.append(l.getColumnNumber());
2130         sb.append(", line number=");
2140         sb.append(l.getLineNumber());
2150         sb.append(", node=");
2160         sb.append(l.getNode());
2170         sb.append(", offset=");
2180         sb.append(l.getOffset());
2190         sb.append(", URL=");
2200         sb.append(l.getURL());
2210         sb.append(']');
222  
2230         System.err.println(sb.toString());
224  
2250         return false;
226      }
227  
228      /**
229       * Normalize the filename (platform dependend).
230       *
231       * @param filename the filename.
232       * @return the normalized filename.
233       */
234      protected final String normalizeFileName (String filename)
235      {
236          final String newFilename;
2370         if (filename.indexOf('$') != -1)
238          {
2390             newFilename = filename.substring(0, filename.indexOf('$'))
240                      + ".java";
2410             logger.fine("Changing resource filename from " + filename + " to "
242                      + newFilename);
243          }
244          else
245          {
2460             newFilename = filename;
247          }
2480         return new File(newFilename).getAbsolutePath();
249      }
250  
251 (3)    // TODO: make public class (util package?) -
252      // see also DummyEntityResolver in taskdef package
2530     private static class DummyEntityResolver
254              implements EntityResolver
255      {
256          /** The full qualified name of this class. */
2570         private static final String CLASSNAME = DummyEntityResolver.class
258                  .getName();
259  
260  
261          /** The logger to use. */
2620         private static final Logger logger = Logger.getLogger(CLASSNAME);
263  
264  
265          public InputSource resolveEntity (String publicId, String systemId)
266          {
2670             logger.finest(
268                      "Resolving entity " + publicId + " SYSTEM " + systemId);
269 (4)            // TODO: make public class EmptyInputStream
2700             return new InputSource(new ByteArrayInputStream(new byte[0]));
271          }
272  
273      }
274  }

Findings in this File

d (1) 117 : 26 [deprecation] setValidating(boolean) in javax.xml.bind.Unmarshaller has been deprecated
i (2) 163 : 0 method org.jcoderz.phoenix.report.AbstractReportReader.unmarshall(InputStream) throws exception with static message string
i (3) 251 : 0 Comment matches to-do format '(TODO|FIXME|CHECKME)'.
i (4) 269 : 0 Comment matches to-do format '(TODO|FIXME|CHECKME)'.