Coverage Report - org.apache.tiles.autotag.plugin.CreateDescriptorMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
CreateDescriptorMojo
83%
45/54
50%
10/20
4
 
 1  
 /*
 2  
  * $Id: CreateDescriptorMojo.java 1656976 2015-02-04 02:27:40Z nlebas $
 3  
  *
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 package org.apache.tiles.autotag.plugin;
 22  
 
 23  
 /*
 24  
  * Copyright 2001-2005 The Apache Software Foundation.
 25  
  *
 26  
  * Licensed under the Apache License, Version 2.0 (the "License");
 27  
  * you may not use this file except in compliance with the License.
 28  
  * You may obtain a copy of the License at
 29  
  *
 30  
  *      http://www.apache.org/licenses/LICENSE-2.0
 31  
  *
 32  
  * Unless required by applicable law or agreed to in writing, software
 33  
  * distributed under the License is distributed on an "AS IS" BASIS,
 34  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 35  
  * See the License for the specific language governing permissions and
 36  
  * limitations under the License.
 37  
  */
 38  
 
 39  
 import java.io.File;
 40  
 import java.io.IOException;
 41  
 import java.io.OutputStream;
 42  
 import java.io.OutputStreamWriter;
 43  
 import java.io.Writer;
 44  
 import java.util.HashSet;
 45  
 import java.util.List;
 46  
 import java.util.Set;
 47  
 
 48  
 import org.apache.maven.model.Resource;
 49  
 import org.apache.maven.plugin.AbstractMojo;
 50  
 import org.apache.maven.plugin.MojoExecutionException;
 51  
 import org.apache.maven.plugins.annotations.Component;
 52  
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 53  
 import org.apache.maven.plugins.annotations.Mojo;
 54  
 import org.apache.maven.plugins.annotations.Parameter;
 55  
 import org.apache.maven.project.MavenProject;
 56  
 import org.apache.tiles.autotag.core.QDoxTemplateSuiteFactory;
 57  
 import org.apache.tiles.autotag.model.TemplateSuite;
 58  
 import org.codehaus.plexus.util.Scanner;
 59  
 import org.sonatype.plexus.build.incremental.BuildContext;
 60  
 
 61  
 import com.thoughtworks.xstream.XStream;
 62  
 
 63  
 /**
 64  
  * Creates a descriptor for the template model in XML format.
 65  
  */
 66  
 @Mojo(name = "create-descriptor", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
 67  1
 public class CreateDescriptorMojo extends AbstractMojo {
 68  
     /**
 69  
      * Location of the file.
 70  
      */
 71  
         @Parameter(defaultValue = "${project.build.directory}/autotag-template-suite", required = true)
 72  
     File outputDirectory;
 73  
 
 74  
     /**
 75  
      * Location of the file.
 76  
      */
 77  
         @Parameter(property = "project.build.sourceDirectory", required = true)
 78  
     File sourceDirectory;
 79  
 
 80  
     /**
 81  
      * included files.
 82  
      */
 83  
         @Parameter
 84  
     Set<String> includes;
 85  
 
 86  
     /**
 87  
      * The name of the template.
 88  
      */
 89  
         @Parameter(required = true)
 90  
     String name;
 91  
 
 92  
     /**
 93  
      * The documentation of the suite.
 94  
      */
 95  
         @Parameter
 96  
     String documentation;
 97  
 
 98  
     /**
 99  
      * Excluded files.
 100  
      */
 101  
         @Parameter
 102  
     Set<String> excludes;
 103  
 
 104  
     /**
 105  
      * Name of the request class.
 106  
      */
 107  
         @Parameter(defaultValue="org.apache.tiles.request.Request", required = true)
 108  
     String requestClass;
 109  
 
 110  
         @Parameter(property = "project", required = true, readonly = true)
 111  
     MavenProject project;
 112  
 
 113  
         @Component
 114  
     BuildContext buildContext;
 115  
     
 116  
     /** {@inheritDoc} */
 117  
     public void execute() throws MojoExecutionException {
 118  
         try {
 119  1
             String[] fileNames = getSourceInclusionScanner().getIncludedFiles();
 120  1
             File dir = new File(outputDirectory, "META-INF");
 121  1
             if(!dir.exists()) {
 122  0
                     dir.mkdirs();
 123  0
                     buildContext.refresh(dir);
 124  
             }
 125  1
             File outputFile = new File(dir, "template-suite.xml");
 126  1
             boolean uptodate = outputFile.exists();
 127  1
             File[] files = new File[fileNames.length];
 128  5
             for(int i=0; i<fileNames.length; i++) {
 129  4
                     files[i] = new File(sourceDirectory, fileNames[i]);
 130  4
                     uptodate &= buildContext.isUptodate(outputFile, files[i]);
 131  
             }
 132  1
             if(!uptodate) {
 133  1
                 createDescriptor(outputFile, files);
 134  
                         }
 135  1
             addResourceDirectory(outputDirectory.getAbsolutePath());
 136  0
         } catch (IOException e) {
 137  0
             throw new MojoExecutionException("error", e);
 138  1
         }
 139  1
     }
 140  
 
 141  
         private void createDescriptor(File outputFile, File[] files)
 142  
                         throws IOException {
 143  1
                 QDoxTemplateSuiteFactory factory = new QDoxTemplateSuiteFactory(files);
 144  1
                 factory.setSuiteName(name);
 145  1
                 factory.setSuiteDocumentation(documentation);
 146  1
                 factory.setRequestClass(requestClass);
 147  1
                 TemplateSuite suite = factory.createTemplateSuite();
 148  1
                 XStream xstream = new XStream();
 149  1
                 OutputStream os = buildContext.newFileOutputStream(outputFile);
 150  1
                 Writer writer = new OutputStreamWriter(os);
 151  1
                 xstream.toXML(suite, writer);
 152  1
                 writer.close();
 153  1
                 os.close();
 154  1
         }
 155  
 
 156  
         private void addResourceDirectory(String directory) {
 157  1
                 boolean addResource = true;
 158  
                 @SuppressWarnings("unchecked")
 159  1
                 List<Resource> resources = project.getResources();
 160  1
                 for(Resource resource: resources) {
 161  0
                         if(directory.equals(resource.getDirectory())) {
 162  0
                                 addResource = false;
 163  
                         }
 164  0
                 }
 165  1
                 if(addResource) {
 166  1
                     Resource resource = new Resource();
 167  1
                     resource.setDirectory(directory);
 168  1
                     project.addResource(resource);
 169  
                 }
 170  1
         }
 171  
 
 172  
     /**
 173  
      * Creates a source inclusion scanner.
 174  
      *
 175  
      * @return The inclusion scanner.
 176  
      */
 177  
     private Scanner getSourceInclusionScanner() {
 178  1
             Scanner scanner = buildContext.newScanner( sourceDirectory );
 179  1
         if (includes == null) {
 180  1
             includes = new HashSet<String>();
 181  
         }
 182  1
         if (excludes == null) {
 183  1
             excludes = new HashSet<String>();
 184  
         }
 185  
 
 186  1
         if (includes.isEmpty()) {
 187  1
             scanner.setIncludes(new String[] {"**/*Model.java"});
 188  
         }
 189  
         else {
 190  0
                 scanner.setIncludes(includes.toArray(new String[includes.size()]));
 191  
         }
 192  1
         if (!excludes.isEmpty()) {
 193  0
                 scanner.setExcludes(excludes.toArray(new String[excludes.size()]));
 194  
         }
 195  1
         scanner.scan();
 196  1
         return scanner;
 197  
     }
 198  
 }