| 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.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 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | @author |
| 67 | | | |
| 68 | | | public abstract class AbstractReportReader |
| 69 | | | implements ReportReader, ValidationEventHandler |
| 70 | | | { |
| 71 | 100 | | private static final String CLASSNAME |
| 72 | | | = AbstractReportReader.class.getName(); |
| 73 | 100 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 74 | | | |
| 75 | | | private final JAXBContext mJaxbContext; |
| 76 | 100 | | private Marshaller mMarshaller = null; |
| 77 | 100 | | private Unmarshaller mUnmarshaller = null; |
| 78 | | | |
| 79 | | | AbstractReportReader (String jaxbContext) |
| 80 | | | throws JAXBException |
| 81 | 100 | | { |
| 82 | 100 | | mJaxbContext = JAXBContext.newInstance(jaxbContext, |
| 83 | | | this.getClass().getClassLoader()); |
| 84 | 100 | | } |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | |
| 89 | | | @return |
| 90 | | | @throws |
| 91 | | | |
| 92 | | | public final Marshaller getMarshaller () |
| 93 | | | throws JAXBException |
| 94 | | | { |
| 95 | 0 | | if (mMarshaller == null) |
| 96 | | | { |
| 97 | 0 | | mMarshaller = mJaxbContext.createMarshaller(); |
| 98 | 0 | | mMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, |
| 99 | | | Boolean.TRUE); |
| 100 | 0 | | mMarshaller.setEventHandler(this); |
| 101 | | | } |
| 102 | 0 | | return mMarshaller; |
| 103 | | | } |
| 104 | | | |
| 105 | | | |
| 106 | | | |
| 107 | | | |
| 108 | | | @return |
| 109 | | | @throws |
| 110 | | | |
| 111 | | | public final Unmarshaller getUnmarshaller () |
| 112 | | | throws JAXBException |
| 113 | | | { |
| 114 | 0 | | if (mUnmarshaller == null) |
| 115 | | | { |
| 116 | 0 | | mUnmarshaller = mJaxbContext.createUnmarshaller(); |
| 117 | 0 | (1) | mUnmarshaller.setValidating(true); |
| 118 | 0 | | mUnmarshaller.setEventHandler(this); |
| 119 | | | } |
| 120 | 0 | | return mUnmarshaller; |
| 121 | | | } |
| 122 | | | |
| 123 | | | |
| 124 | | | <tt></tt> |
| 125 | | | |
| 126 | | | |
| 127 | | | {@link } |
| 128 | | | {@link } |
| 129 | | | |
| 130 | | | |
| 131 | | | @param |
| 132 | | | @return |
| 133 | | | |
| 134 | | | public final Object unmarshall (InputStream in) |
| 135 | | | { |
| 136 | | | final Object result; |
| 137 | | | try |
| 138 | | | { |
| 139 | 0 | | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 140 | 0 | | factory.setNamespaceAware(true); |
| 141 | | | final XMLReader reader; |
| 142 | | | try |
| 143 | | | { |
| 144 | 0 | | reader = factory.newSAXParser().getXMLReader(); |
| 145 | | | } |
| 146 | 0 | | catch (SAXException e) |
| 147 | | | { |
| 148 | 0 | | throw new RuntimeException(e); |
| 149 | | | } |
| 150 | 0 | | catch (ParserConfigurationException e) |
| 151 | | | { |
| 152 | 0 | | throw new RuntimeException(e); |
| 153 | 0 | | } |
| 154 | 0 | | final UnmarshallerHandler un |
| 155 | | | = getUnmarshaller().getUnmarshallerHandler(); |
| 156 | 0 | | reader.setEntityResolver(new DummyEntityResolver()); |
| 157 | 0 | | reader.setContentHandler(un); |
| 158 | 0 | | reader.parse(new InputSource(in)); |
| 159 | 0 | | result = un.getResult(); |
| 160 | | | } |
| 161 | 0 | | catch (Exception e) |
| 162 | | | { |
| 163 | 0 | (2) | throw new RuntimeException("FIXME", e); |
| 164 | 0 | | } |
| 165 | 0 | | return result; |
| 166 | | | } |
| 167 | | | |
| 168 | | | {@inheritDoc} |
| 169 | | | public final void merge (Map<ResourceInfo, List<Item>> toItems) |
| 170 | | | throws JAXBException |
| 171 | | | { |
| 172 | 0 | | final Map<ResourceInfo, List<Item>> myResourceList = getItems(); |
| 173 | | | |
| 174 | 0 | | for (Entry<ResourceInfo, List<Item>> entry : myResourceList.entrySet()) |
| 175 | | | { |
| 176 | 0 | | final ResourceInfo info = entry.getKey(); |
| 177 | 0 | | List<Item> items = toItems.get(info); |
| 178 | 0 | | if (items == null) |
| 179 | | | { |
| 180 | 0 | | items = new ArrayList<Item>(); |
| 181 | 0 | | toItems.put(info, items); |
| 182 | | | } |
| 183 | 0 | | items.addAll(entry.getValue()); |
| 184 | 0 | | } |
| 185 | 0 | | } |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | |
| 190 | | | |
| 191 | | | @return |
| 192 | | | @throws |
| 193 | | | |
| 194 | | | protected abstract Map<ResourceInfo, List<Item>> getItems () |
| 195 | | | throws JAXBException; |
| 196 | | | |
| 197 | | | {@inheritDoc} |
| 198 | | | public final boolean handleEvent (ValidationEvent e) |
| 199 | | | { |
| 200 | 0 | | final ValidationEventLocator l = e.getLocator(); |
| 201 | 0 | | final StringBuilder sb = new StringBuilder(); |
| 202 | 0 | | sb.append("[ValidationEvent:"); |
| 203 | 0 | | sb.append(", message="); |
| 204 | 0 | | sb.append(e.getMessage()); |
| 205 | 0 | | sb.append(", severity="); |
| 206 | 0 | | sb.append(e.getSeverity()); |
| 207 | 0 | | sb.append(", link exception="); |
| 208 | 0 | | sb.append(e.getLinkedException()); |
| 209 | 0 | | sb.append(", message="); |
| 210 | 0 | | sb.append(l.getObject()); |
| 211 | 0 | | sb.append(", column number="); |
| 212 | 0 | | sb.append(l.getColumnNumber()); |
| 213 | 0 | | sb.append(", line number="); |
| 214 | 0 | | sb.append(l.getLineNumber()); |
| 215 | 0 | | sb.append(", node="); |
| 216 | 0 | | sb.append(l.getNode()); |
| 217 | 0 | | sb.append(", offset="); |
| 218 | 0 | | sb.append(l.getOffset()); |
| 219 | 0 | | sb.append(", URL="); |
| 220 | 0 | | sb.append(l.getURL()); |
| 221 | 0 | | sb.append(']'); |
| 222 | | | |
| 223 | 0 | | System.err.println(sb.toString()); |
| 224 | | | |
| 225 | 0 | | return false; |
| 226 | | | } |
| 227 | | | |
| 228 | | | |
| 229 | | | |
| 230 | | | |
| 231 | | | @param |
| 232 | | | @return |
| 233 | | | |
| 234 | | | protected final String normalizeFileName (String filename) |
| 235 | | | { |
| 236 | | | final String newFilename; |
| 237 | 0 | | if (filename.indexOf('$') != -1) |
| 238 | | | { |
| 239 | 0 | | newFilename = filename.substring(0, filename.indexOf('$')) |
| 240 | | | + ".java"; |
| 241 | 0 | | logger.fine("Changing resource filename from " + filename + " to " |
| 242 | | | + newFilename); |
| 243 | | | } |
| 244 | | | else |
| 245 | | | { |
| 246 | 0 | | newFilename = filename; |
| 247 | | | } |
| 248 | 0 | | return new File(newFilename).getAbsolutePath(); |
| 249 | | | } |
| 250 | | | |
| 251 | | (3) | |
| 252 | | | |
| 253 | 0 | | private static class DummyEntityResolver |
| 254 | | | implements EntityResolver |
| 255 | | | { |
| 256 | | | |
| 257 | 0 | | private static final String CLASSNAME = DummyEntityResolver.class |
| 258 | | | .getName(); |
| 259 | | | |
| 260 | | | |
| 261 | | | |
| 262 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 263 | | | |
| 264 | | | |
| 265 | | | public InputSource resolveEntity (String publicId, String systemId) |
| 266 | | | { |
| 267 | 0 | | logger.finest( |
| 268 | | | "Resolving entity " + publicId + " SYSTEM " + systemId); |
| 269 | | (4) | |
| 270 | 0 | | return new InputSource(new ByteArrayInputStream(new byte[0])); |
| 271 | | | } |
| 272 | | | |
| 273 | | | } |
| 274 | | | } |