1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.student.common.mojo;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.apache.commons.lang.StringEscapeUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.maven.plugin.AbstractMojo;
33 import org.apache.maven.project.MavenProject;
34 import org.kuali.student.contract.model.ServiceContractModel;
35 import org.kuali.student.contract.model.impl.ServiceContractModelCache;
36 import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
37 import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.thoughtworks.qdox.directorywalker.DirectoryScanner;
42 import com.thoughtworks.qdox.directorywalker.FileVisitor;
43 import com.thoughtworks.qdox.directorywalker.SuffixFilter;
44
45
46
47
48
49
50
51
52 public abstract class AbstractKSMojo extends AbstractMojo {
53
54 private static final Logger log = LoggerFactory.getLogger(AbstractKSMojo.class);
55
56
57
58
59 private List<String> sourceDirs;
60
61
62
63
64 private List<String>contextSourceDirs;
65
66 public AbstractKSMojo() {
67 super();
68 }
69
70 public List<String> getSourceDirs() {
71 return sourceDirs;
72 }
73
74 public void setSourceDirs(List<String> sourceDirs) {
75 this.sourceDirs = sourceDirs;
76 }
77
78
79
80
81
82
83 public List<String> getContextSourceDirs() {
84 return contextSourceDirs;
85 }
86
87
88
89
90 public void setContextSourceDirs(List<String> contextSourceDirs) {
91 this.contextSourceDirs = contextSourceDirs;
92 }
93
94
95
96 protected final Set<String>localPackages = new HashSet<String>();
97
98
99 protected final ServiceContractModel getModel() {
100
101 Set<String> modelSourceDirectories = new LinkedHashSet<String>();
102
103 MavenProject project = (MavenProject) getPluginContext().get("project");
104
105
106
107
108 if (sourceDirs == null) {
109 modelSourceDirectories.add(project.getBuild().getSourceDirectory());
110
111 } else {
112 modelSourceDirectories.addAll(sourceDirs);
113 }
114 if (modelSourceDirectories.size() == 0)
115 throw new RuntimeException("No Source Directories are defined");
116
117 for (String directory : modelSourceDirectories) {
118
119 DirectoryScanner scanner = new DirectoryScanner(new File (directory));
120 scanner.addFilter(new SuffixFilter(".java"));
121 scanner.scan(new FileVisitor() {
122 public void visitFile(File currentFile) {
123
124 String path = currentFile.getPath();
125
126 int startIndex = path.indexOf("org" + File.separator + "kuali");
127
128 if (startIndex != -1) {
129
130 String subPath = path.substring(startIndex);
131
132 String[] parts = subPath.split(StringEscapeUtils.escapeJava(File.separator));
133
134 StringBuilder packageBuilder = new StringBuilder();
135
136 for (int i = 0; i < parts.length-1; i++) {
137 if (i != 0)
138 packageBuilder.append(".");
139
140 packageBuilder.append(parts[i]);
141 }
142
143 String pkg = packageBuilder.toString();
144
145 localPackages.add(pkg);
146 }
147 }
148 });
149 }
150
151 if (contextSourceDirs != null)
152 modelSourceDirectories.addAll(contextSourceDirs);
153
154 ServiceContractModel instance = new ServiceContractModelQDoxLoader(
155 new ArrayList<String>(modelSourceDirectories));
156
157 return new ServiceContractModelCache(instance);
158 }
159
160 protected final boolean validate(ServiceContractModel model) {
161 Collection<String> errors = new ServiceContractModelValidator(model)
162 .validate();
163 if (errors.size() > 0) {
164
165 StringBuilder buf = new StringBuilder();
166 buf.append(errors.size()).append(
167 " errors found while validating the data.");
168 int cnt = 0;
169 for (String msg : errors) {
170 cnt++;
171 buf.append("\n");
172 buf.append("*error*").append(cnt).append(":").append(msg);
173 }
174
175 buf.append(errors.size()).append(
176 " errors found while validating the data.");
177 return false;
178 }
179 return true;
180 }
181
182 }