1
2
3
4 package org.kuali.student.common.mojo;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10 import java.util.Set;
11
12 import org.apache.maven.plugin.AbstractMojo;
13 import org.apache.maven.project.MavenProject;
14 import org.kuali.student.contract.model.ServiceContractModel;
15 import org.kuali.student.contract.model.impl.ServiceContractModelCache;
16 import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
17 import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
18
19
20
21
22
23
24
25
26 public abstract class AbstractKSMojo extends AbstractMojo {
27
28
29
30
31 private List<String> sourceDirs;
32
33 public AbstractKSMojo() {
34 super();
35 }
36
37 public List<String> getSourceDirs() {
38 return sourceDirs;
39 }
40
41 public void setSourceDirs(List<String> sourceDirs) {
42 this.sourceDirs = sourceDirs;
43 }
44
45 protected final ServiceContractModel getModel() {
46
47 Set<String> modelSourceDirectories = new LinkedHashSet<String>();
48
49 MavenProject project = (MavenProject) getPluginContext().get("project");
50
51
52
53
54 if (sourceDirs == null) {
55 modelSourceDirectories.add(project.getBuild().getSourceDirectory());
56 } else {
57 modelSourceDirectories.addAll(sourceDirs);
58 }
59 if (modelSourceDirectories.size() == 0)
60 throw new RuntimeException("No Source Directories are defined");
61
62 ServiceContractModel instance = new ServiceContractModelQDoxLoader(
63 new ArrayList<String>(modelSourceDirectories));
64 return new ServiceContractModelCache(instance);
65 }
66
67 protected final boolean validate(ServiceContractModel model) {
68 Collection<String> errors = new ServiceContractModelValidator(model)
69 .validate();
70 if (errors.size() > 0) {
71
72 StringBuilder buf = new StringBuilder();
73 buf.append(errors.size()).append(
74 " errors found while validating the data.");
75 int cnt = 0;
76 for (String msg : errors) {
77 cnt++;
78 buf.append("\n");
79 buf.append("*error*").append(cnt).append(":").append(msg);
80 }
81
82 buf.append(errors.size()).append(
83 " errors found while validating the data.");
84 return false;
85 }
86 return true;
87 }
88
89 }