Coverage Report - org.apache.creadur.whisker.app.Whisker
 
Classes in this File Line Coverage Branch Coverage Complexity
Whisker
0%
0/36
0%
0/7
1.474
Whisker$1
0%
0/1
N/A
1.474
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  *  to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  *  with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License. 
 18  
  */
 19  
 package org.apache.creadur.whisker.app;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.io.Writer;
 24  
 import java.util.Collection;
 25  
 
 26  
 import org.apache.creadur.whisker.app.analysis.LicenseAnalyst;
 27  
 import org.apache.creadur.whisker.fromxml.JDomBuilder;
 28  
 import org.apache.creadur.whisker.model.Descriptor;
 29  
 import org.apache.creadur.whisker.scan.Directory;
 30  
 import org.apache.creadur.whisker.scan.FromFileSystem;
 31  
 import org.jdom.JDOMException;
 32  
 
 33  
 
 34  
 /**
 35  
  * 
 36  
  */
 37  0
 public class Whisker {
 38  
     
 39  
     private Act act;
 40  
     private String source;
 41  
     private StreamableResource licenseDescriptor;
 42  
     private ResultWriterFactory writerFactory;
 43  
     
 44  
     private AbstractEngine engine;
 45  
 
 46  
     /**
 47  
      * Gets the factory that builds product {@link Writer}s.
 48  
      * @return factory
 49  
      */
 50  
     public ResultWriterFactory getWriterFactory() {
 51  0
         return writerFactory;
 52  
     }
 53  
 
 54  
     /**
 55  
      * Sets the factory that builds product {@link Writer}s.
 56  
      * @param writerFactory not null
 57  
      */
 58  
     public Whisker setWriterFactory(ResultWriterFactory writerFactory) {
 59  0
         this.writerFactory = writerFactory;
 60  0
         return this;
 61  
     }
 62  
 
 63  
     /**
 64  
      * Gets the reporting engine.
 65  
      * @return not null
 66  
      */
 67  
     public AbstractEngine getEngine() {
 68  0
         return engine;
 69  
     }
 70  
 
 71  
     /**
 72  
      * Sets the reporting engine.
 73  
      * @param engine not null
 74  
      * @return this, not null
 75  
      */
 76  
     public Whisker setEngine(AbstractEngine engine) {
 77  0
         this.engine = engine;
 78  0
         return this;
 79  
     }
 80  
 
 81  
     /**
 82  
      * @return the base
 83  
      */
 84  
     public String getSource() {
 85  0
         return source;
 86  
     }
 87  
 
 88  
     /**
 89  
      * @param source the base to set
 90  
      */
 91  
     public Whisker setSource(String source) {
 92  0
         this.source = source;
 93  0
         return this;
 94  
     }
 95  
 
 96  
     /**
 97  
      * @return the licenseDescriptor
 98  
      */
 99  
     public StreamableResource getLicenseDescriptor() {
 100  0
         return licenseDescriptor;
 101  
     }
 102  
 
 103  
     /**
 104  
      * @param licenseDescriptor the licenseDescriptor to set
 105  
      */
 106  
     public Whisker setLicenseDescriptor(StreamableResource licenseDescriptor) {
 107  0
         this.licenseDescriptor = licenseDescriptor;
 108  0
         return this;
 109  
     }
 110  
 
 111  
     /**
 112  
      * @return the act
 113  
      */
 114  
     public Act getAct() {
 115  0
         return act;
 116  
     }
 117  
 
 118  
     /**
 119  
      * @param act the act to set
 120  
      */
 121  
     public Whisker setAct(Act act) {
 122  0
         this.act = act;
 123  0
         return this;
 124  
     }
 125  
     
 126  
     public Whisker act() throws Exception {
 127  0
         switch (act) {
 128  
             case REPORT:
 129  0
                 return report();
 130  
             case AUDIT:
 131  0
                 return validate();
 132  
             case TEMPLATE:
 133  0
                 return template();
 134  
             case GENERATE:
 135  
             default:
 136  0
                 return generate();
 137  
         }
 138  
     }
 139  
     
 140  
     
 141  
     
 142  
     /**
 143  
      * @return
 144  
      * @throws Exception 
 145  
      */
 146  
     private Whisker template() throws Exception {
 147  0
         engine.generateTemplate(directories(), getWriterFactory());
 148  0
         return this;
 149  
     }
 150  
 
 151  
     /**
 152  
      * @return
 153  
      */
 154  
     private Whisker report() throws Exception {
 155  0
         engine.report(directories(), getWriterFactory());
 156  0
         return this;
 157  
     }
 158  
     
 159  
     /**
 160  
      * @return
 161  
      * @throws Exception 
 162  
      */
 163  
     private Whisker generate() throws Exception {
 164  0
         engine.generate(new LicenseAnalyst().validate(load(getLicenseDescriptor())), getWriterFactory());
 165  0
         return this;
 166  
     }
 167  
 
 168  
     /**
 169  
      * @return
 170  
      * @throws Exception 
 171  
      */
 172  
     private Whisker validate() throws Exception {
 173  0
         engine.validate(new LicenseAnalyst(directories()).analyse(load(getLicenseDescriptor())), getWriterFactory());
 174  0
         return this;
 175  
     }
 176  
 
 177  
     /**
 178  
      * @return
 179  
      * @throws IOException
 180  
      */
 181  
     private Collection<Directory> directories() throws IOException {
 182  0
         return new FromFileSystem().withBase(getSource());
 183  
     }
 184  
 
 185  
     @Override
 186  
     public String toString() {
 187  0
         return "Whisker [act=" + act + ", base=" + source
 188  
                 + ", licenseDescriptor=" + licenseDescriptor + "]";
 189  
     }
 190  
     
 191  
 
 192  
     /**
 193  
      * @param resource
 194  
      * @return
 195  
      * @throws IOException 
 196  
      */
 197  
     private InputStream resourceAsStream(final StreamableResource resource) throws IOException {
 198  0
         return resource.open();
 199  
     }
 200  
     
 201  
     
 202  
     /**
 203  
      * @param resource
 204  
      * @return
 205  
      * @throws JDOMException
 206  
      * @throws IOException
 207  
      */
 208  
     private Descriptor load(final StreamableResource resource) throws Exception {
 209  0
         final InputStream resourceAsStream = resourceAsStream(resource);
 210  0
         if (resourceAsStream == null) {
 211  0
             throw new IllegalArgumentException("Cannot load " + resource);
 212  
         }
 213  0
         return new JDomBuilder().build(resourceAsStream);
 214  
     }
 215  
 }