| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.apache.creadur.whisker.out.velocity; |
| 20 | |
|
| 21 | |
import java.io.Writer; |
| 22 | |
import java.util.ArrayList; |
| 23 | |
import java.util.Collection; |
| 24 | |
import java.util.List; |
| 25 | |
|
| 26 | |
import org.apache.commons.io.IOUtils; |
| 27 | |
import org.apache.commons.logging.Log; |
| 28 | |
import org.apache.creadur.whisker.app.Configuration; |
| 29 | |
import org.apache.creadur.whisker.app.ResultWriterFactory; |
| 30 | |
import org.apache.creadur.whisker.app.analysis.LicenseAnalyst; |
| 31 | |
import org.apache.creadur.whisker.model.Descriptor; |
| 32 | |
import org.apache.creadur.whisker.scan.Directory; |
| 33 | |
import org.apache.velocity.VelocityContext; |
| 34 | |
import org.apache.velocity.app.VelocityEngine; |
| 35 | |
import org.apache.velocity.runtime.RuntimeServices; |
| 36 | |
import org.apache.velocity.runtime.log.LogChute; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public class VelocityReports implements LogChute { |
| 42 | |
|
| 43 | 2 | private static final Product[] PRODUCTS_THAT_GENERATE_TEMPLATES |
| 44 | |
= {Product.XML_TEMPLATE}; |
| 45 | |
|
| 46 | 2 | private static final Product[] PRODUCTS_THAT_VALIDATE |
| 47 | |
= {Product.MISSING_LICENSE_REPORT_TEMPLATE}; |
| 48 | |
|
| 49 | 2 | private static final Product[] PRODUCTS_THAT_REPORT_ON_DIRECTORIES |
| 50 | |
= {Product.DIRECTORIES_REPORT_TEMPLATE}; |
| 51 | |
|
| 52 | 2 | private static final Product[] PRODUCTS_THAT_GENERATE_LICENSING_MATERIALS |
| 53 | |
= {Product.LICENSE, Product.NOTICE}; |
| 54 | |
|
| 55 | |
|
| 56 | |
private final ResultWriterFactory writerFactory; |
| 57 | |
|
| 58 | |
private final VelocityEngine engine; |
| 59 | |
|
| 60 | |
private final Log log; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public VelocityReports( |
| 68 | 20 | final ResultWriterFactory writerFactory, final Log log) { |
| 69 | 20 | this.writerFactory = writerFactory; |
| 70 | 20 | this.log = log; |
| 71 | 20 | engine = new VelocityEngine(); |
| 72 | 20 | engine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this); |
| 73 | 20 | engine.setProperty(VelocityEngine.RESOURCE_LOADER, "classpath"); |
| 74 | 20 | engine.setProperty("classpath.resource.loader.class", |
| 75 | |
"org.apache.velocity.runtime.resource.loader." |
| 76 | |
+ "ClasspathResourceLoader"); |
| 77 | 20 | engine.init(); |
| 78 | 20 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | 20 | public final void init(final RuntimeServices services) { } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public final boolean isLevelEnabled(final int level) { |
| 94 | 272 | switch (level) { |
| 95 | |
case DEBUG_ID: |
| 96 | 78 | return log.isDebugEnabled(); |
| 97 | |
case TRACE_ID: |
| 98 | 20 | return log.isTraceEnabled(); |
| 99 | |
case INFO_ID: |
| 100 | 0 | return log.isInfoEnabled(); |
| 101 | |
case WARN_ID: |
| 102 | 174 | return log.isWarnEnabled(); |
| 103 | |
case ERROR_ID: |
| 104 | 0 | return log.isErrorEnabled(); |
| 105 | |
default: |
| 106 | 0 | return false; |
| 107 | |
} |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public final void log(final int level, final String message) { |
| 117 | 540 | switch (level) { |
| 118 | |
case DEBUG_ID: |
| 119 | 400 | log.debug(message); |
| 120 | 400 | break; |
| 121 | |
case TRACE_ID: |
| 122 | 140 | log.trace(message); |
| 123 | 140 | break; |
| 124 | |
case INFO_ID: |
| 125 | 0 | log.info(message); |
| 126 | 0 | break; |
| 127 | |
case WARN_ID: |
| 128 | 0 | log.warn(message); |
| 129 | 0 | break; |
| 130 | |
case ERROR_ID: |
| 131 | 0 | log.error(message); |
| 132 | 0 | break; |
| 133 | |
default: |
| 134 | 0 | log.trace(message); |
| 135 | |
} |
| 136 | 540 | } |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public final void log(final int level, |
| 146 | |
final String message, final Throwable throwable) { |
| 147 | 0 | switch (level) { |
| 148 | |
case DEBUG_ID: |
| 149 | 0 | log.debug(message, throwable); |
| 150 | 0 | break; |
| 151 | |
case TRACE_ID: |
| 152 | 0 | log.trace(message, throwable); |
| 153 | 0 | break; |
| 154 | |
case INFO_ID: |
| 155 | 0 | log.info(message, throwable); |
| 156 | 0 | break; |
| 157 | |
case WARN_ID: |
| 158 | 0 | log.warn(message, throwable); |
| 159 | 0 | break; |
| 160 | |
case ERROR_ID: |
| 161 | 0 | log.error(message, throwable); |
| 162 | 0 | break; |
| 163 | |
default: |
| 164 | 0 | log.trace(message, throwable); |
| 165 | |
} |
| 166 | 0 | } |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
public final void generate(final Descriptor work, |
| 175 | |
final Configuration configuration) throws Exception { |
| 176 | 20 | final List<Product> products = new ArrayList<Product>(); |
| 177 | 60 | for (Product product: PRODUCTS_THAT_GENERATE_LICENSING_MATERIALS) { |
| 178 | 2 | switch (product) { |
| 179 | |
case NOTICE: |
| 180 | 20 | if (!work.isNoticeRequired()) { |
| 181 | 2 | break; |
| 182 | |
} |
| 183 | |
default: |
| 184 | 38 | products.add(product); |
| 185 | |
} |
| 186 | |
|
| 187 | |
|
| 188 | |
} |
| 189 | 20 | final Product[] pruductArray = new Product[products.size()]; |
| 190 | 20 | merge(products.toArray(pruductArray), context(work, configuration)); |
| 191 | 20 | } |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
private void merge(final Product[] products, |
| 200 | |
final VelocityContext context) throws Exception { |
| 201 | 58 | for (final Product product : products) { |
| 202 | 38 | merge(product, context); |
| 203 | |
} |
| 204 | 20 | } |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
private void merge( |
| 213 | |
final Product product, final VelocityContext context) |
| 214 | |
throws Exception { |
| 215 | 38 | final Writer writer = product.writerFrom(writerFactory); |
| 216 | 38 | engine.getTemplate( |
| 217 | |
template(product.getTemplate())).merge(context, writer); |
| 218 | 38 | IOUtils.closeQuietly(writer); |
| 219 | 38 | } |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
private VelocityContext context(final Descriptor work, |
| 228 | |
final Configuration configuration) { |
| 229 | 20 | final VelocityContext context = new VelocityContext(); |
| 230 | 20 | context.put("work", work); |
| 231 | 20 | context.put("indent", new Indentation()); |
| 232 | 20 | context.put("helper", new RenderingHelper(work, configuration)); |
| 233 | 20 | return context; |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
private String template(final String name) { |
| 243 | 38 | return "org/apache/creadur/whisker/template/velocity/" |
| 244 | |
+ name.toLowerCase() + ".vm"; |
| 245 | |
} |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
public final void report( |
| 253 | |
final Collection<Directory> directories) throws Exception { |
| 254 | 0 | merge(PRODUCTS_THAT_REPORT_ON_DIRECTORIES, context(directories)); |
| 255 | 0 | } |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
private VelocityContext context( |
| 263 | |
final Collection<Directory> directories) { |
| 264 | 0 | final VelocityContext context = new VelocityContext(); |
| 265 | 0 | context.put("dirs", directories); |
| 266 | 0 | return context; |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
public final void validate( |
| 275 | |
final LicenseAnalyst analyst) throws Exception { |
| 276 | 0 | merge(PRODUCTS_THAT_VALIDATE, context(analyst)); |
| 277 | 0 | } |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
private VelocityContext context(final LicenseAnalyst analyst) { |
| 285 | 0 | final VelocityContext context = new VelocityContext(); |
| 286 | 0 | context.put("analyst", analyst); |
| 287 | 0 | return context; |
| 288 | |
} |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
public final void generateTemplate( |
| 296 | |
final Collection<Directory> withBase) throws Exception { |
| 297 | 0 | merge(PRODUCTS_THAT_GENERATE_TEMPLATES, context(withBase)); |
| 298 | 0 | } |
| 299 | |
} |