View Javadoc

1   package org.kuali.student.contract.mojo;
2   
3   import java.io.File;
4   import java.util.Collection;
5   import java.util.HashSet;
6   import java.util.List;
7   import java.util.Set;
8   
9   import org.apache.maven.plugin.AbstractMojo;
10  import org.apache.maven.plugin.MojoExecutionException;
11  
12  import org.kuali.student.contract.model.ServiceContractModel;
13  import org.kuali.student.contract.model.XmlType;
14  import org.kuali.student.contract.model.impl.ServiceContractModelCache;
15  import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
16  import org.kuali.student.contract.model.util.KradDictionaryCreator;
17  import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
18  
19  /**
20   * The plugin entrypoint which is used to generate dictionary files based on the contract
21   * @phase generate-sources
22   * @goal ksdictionarycreator
23   */
24  public class KSDictionaryCreatorMojo extends AbstractMojo {
25  
26      /**
27       * @parameter
28       **/
29      private List<String> sourceDirs;
30      /**
31       * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/classes"
32       */
33      private File outputDirectory;
34      /**
35       * @parameter expression=false
36       */
37      private boolean writeManual;
38      /**
39       * @parameter expression=true
40       */
41      private boolean writeGenerated;
42      /**
43       * @parameter
44       */
45      private List<String> classNames;
46  
47      public File getOutputDirectory() {
48          return outputDirectory;
49      }
50  
51      public List<String> getSourceDirs() {
52          return sourceDirs;
53      }
54  
55      public boolean isWriteManual() {
56          return writeManual;
57      }
58  
59      public boolean isWriteGenerated() {
60          return writeGenerated;
61      }
62  
63      public List<String> getClassNames() {
64          return classNames;
65      }
66  
67      public void setClassNames(List<String> classNames) {
68          this.classNames = classNames;
69      }
70  
71      public void setWriteManual(boolean writeManual) {
72          this.writeManual = writeManual;
73      }
74  
75      public void setWriteGenerated(boolean writeGenerated) {
76          this.writeGenerated = writeGenerated;
77      }
78  
79      public void setOutputDirectory(File htmlDirectory) {
80          this.outputDirectory = htmlDirectory;
81      }
82  
83      public void setSourceDirs(List<String> sourceDirs) {
84          this.sourceDirs = sourceDirs;
85      }
86  
87      private ServiceContractModel getModel() {
88          ServiceContractModel instance = new ServiceContractModelQDoxLoader(
89                  sourceDirs);
90          return new ServiceContractModelCache(instance);
91      }
92  
93      private boolean validate(ServiceContractModel model) {
94          Collection<String> errors = new ServiceContractModelValidator(model).validate();
95          if (errors.size() > 0) {
96              StringBuilder buf = new StringBuilder();
97              buf.append(errors.size()).append(" errors found while validating the data.");
98              return false;
99          }
100         return true;
101     }
102 
103     @Override
104     public void execute() throws MojoExecutionException {
105         getLog().info("generating ks-XXX-dictionary.xml files=" + this.writeManual);
106         getLog().info("generating ks-XXX-dictionary-generated.xml files=" + this.writeGenerated);
107         ServiceContractModel model = this.getModel();
108         this.validate(model);
109 
110         Set<String> lowerClasses = new HashSet();
111         for (String className : classNames) {
112             lowerClasses.add(className.toLowerCase());
113         }
114 
115         String dictionaryDirectory = this.outputDirectory.toString();
116         for (XmlType xmlType : model.getXmlTypes()) {
117             if (lowerClasses.contains(xmlType.getName().toLowerCase())) {
118                 lowerClasses.remove(xmlType.getName().toLowerCase());
119                 String xmlObject = xmlType.getName();
120                 KradDictionaryCreator writer =
121                         new KradDictionaryCreator(dictionaryDirectory,
122                         model,
123                         xmlObject,
124                         writeManual,
125                         writeGenerated);
126                 writer.write();
127             }
128         }
129         if (!lowerClasses.isEmpty()) {
130             StringBuilder buf = new StringBuilder();
131             buf.append(lowerClasses.size());
132             buf.append(" classes were not processed: ");
133             String comma = "";
134             for (String className : lowerClasses) {
135                 buf.append(comma);
136                 buf.append(className);
137                 comma = ", ";
138             }
139             throw new MojoExecutionException(buf.toString());
140         }
141     }
142 }