| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.mojo; |
| 17 | |
|
| 18 | |
import java.io.File; |
| 19 | |
import java.io.IOException; |
| 20 | |
import java.net.MalformedURLException; |
| 21 | |
import java.net.URL; |
| 22 | |
import java.util.Date; |
| 23 | |
import java.util.HashSet; |
| 24 | |
import java.util.Set; |
| 25 | |
|
| 26 | |
import javax.xml.parsers.ParserConfigurationException; |
| 27 | |
import javax.xml.transform.Result; |
| 28 | |
import javax.xml.transform.Transformer; |
| 29 | |
import javax.xml.transform.TransformerConfigurationException; |
| 30 | |
import javax.xml.transform.TransformerException; |
| 31 | |
import javax.xml.transform.TransformerFactoryConfigurationError; |
| 32 | |
import javax.xml.transform.stream.StreamResult; |
| 33 | |
import javax.xml.transform.stream.StreamSource; |
| 34 | |
|
| 35 | |
import org.apache.maven.plugin.AbstractMojo; |
| 36 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 37 | |
import org.apache.maven.plugin.MojoFailureException; |
| 38 | |
import org.kuali.student.contract.ContractReader; |
| 39 | |
import org.kuali.student.contract.MessageContractReader; |
| 40 | |
import org.springframework.core.io.ClassPathResource; |
| 41 | |
import org.w3c.dom.DOMException; |
| 42 | |
import org.w3c.dom.Document; |
| 43 | |
import org.w3c.dom.NamedNodeMap; |
| 44 | |
import org.w3c.dom.Node; |
| 45 | |
import org.w3c.dom.NodeList; |
| 46 | |
import org.xml.sax.SAXException; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public class KsContractMojo extends AbstractMojo { |
| 54 | |
|
| 55 | 0 | public KsContractMojo() { |
| 56 | 0 | this.contractFile = null; |
| 57 | 0 | this.contractURL = null; |
| 58 | 0 | } |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private String[] excludeClasses; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private String packageName; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 0 | private String serviceName = "foo"; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 0 | private String namespace = "http://student.kuali.org/lum/xxx"; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
private File contractFile; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private URL contractURL; |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
private File outputDirectory; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
private File transformFile; |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
private File messageTransformFile; |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
private String jsessionId; |
| 121 | |
|
| 122 | |
public void execute() throws MojoExecutionException, MojoFailureException { |
| 123 | |
|
| 124 | |
ContractReader contract; |
| 125 | |
|
| 126 | |
try { |
| 127 | 0 | if (contractURL != null) { |
| 128 | |
|
| 129 | 0 | contract = new ContractReader(contractURL, jsessionId); |
| 130 | |
} else { |
| 131 | 0 | contract = new ContractReader(contractFile); |
| 132 | |
} |
| 133 | 0 | } catch (Exception ex) { |
| 134 | 0 | throw new MojoExecutionException("Can't parse contract", ex); |
| 135 | 0 | } |
| 136 | |
|
| 137 | 0 | StreamSource wsdlXslt=null; |
| 138 | 0 | if (transformFile == null) { |
| 139 | |
try { |
| 140 | 0 | wsdlXslt = new StreamSource(new ClassPathResource("interface.xml").getInputStream()); |
| 141 | 0 | } catch (IOException e) { |
| 142 | 0 | e.printStackTrace(); |
| 143 | 0 | } |
| 144 | |
} else { |
| 145 | 0 | wsdlXslt = new StreamSource(transformFile); |
| 146 | |
} |
| 147 | |
|
| 148 | |
try { |
| 149 | 0 | Transformer transformer = net.sf.saxon.TransformerFactoryImpl |
| 150 | |
.newInstance().newTransformer(wsdlXslt); |
| 151 | 0 | transformer.setParameter("packageName", this.getPackageName()); |
| 152 | 0 | transformer.setParameter("serviceName", this.getServiceName()); |
| 153 | 0 | transformer.setParameter("url", contract.getContractPath()); |
| 154 | 0 | transformer.setParameter("date", new Date()); |
| 155 | 0 | transformer.setParameter("user", System.getProperty("user.name")); |
| 156 | 0 | transformer.setParameter("namespace", namespace); |
| 157 | 0 | transformer.setParameter("excludeClasses", join(excludeClasses,",")); |
| 158 | 0 | String packageDir=""; |
| 159 | 0 | if(packageName!=null && !packageName.isEmpty()){ |
| 160 | 0 | packageDir = "/"+packageName.replace('.', '/'); |
| 161 | |
} |
| 162 | |
|
| 163 | 0 | File serviceDir = new File(outputDirectory+packageDir, "service"); |
| 164 | 0 | serviceDir.mkdirs(); |
| 165 | 0 | Result result = new StreamResult(new File(serviceDir, getServiceName() + ".java")); |
| 166 | 0 | System.out.println("Creating Service Interface: "+ getServiceName() + ".java"); |
| 167 | |
|
| 168 | 0 | transformer.transform(contract.getStreamSource(), result); |
| 169 | |
|
| 170 | 0 | findParams(contract.getDocument()); |
| 171 | 0 | } catch (TransformerConfigurationException tcex) { |
| 172 | 0 | getLog().error(tcex); |
| 173 | 0 | throw new MojoExecutionException("Can't initialize xslt.", tcex); |
| 174 | 0 | } catch (TransformerException tex) { |
| 175 | 0 | getLog().error(tex); |
| 176 | 0 | throw new MojoExecutionException("Can't transform contract.", tex); |
| 177 | 0 | } catch (Exception ex) { |
| 178 | 0 | throw new MojoExecutionException("Error parsing params", ex); |
| 179 | 0 | } |
| 180 | 0 | } |
| 181 | |
|
| 182 | |
private String join(String[] strings, String joinString) { |
| 183 | 0 | StringBuilder sb = new StringBuilder(""); |
| 184 | 0 | if(strings!=null){ |
| 185 | 0 | for(int i=0;i<strings.length;i++){ |
| 186 | 0 | sb.append(strings[i]); |
| 187 | 0 | if(i<strings.length-1){ |
| 188 | 0 | sb.append(joinString); |
| 189 | |
} |
| 190 | |
} |
| 191 | |
} |
| 192 | 0 | return sb.toString(); |
| 193 | |
} |
| 194 | |
|
| 195 | |
public void findParamsFromMessage(String param, URL url, |
| 196 | |
Set<URL> alreadyParsedUrlSet) { |
| 197 | 0 | if (alreadyParsedUrlSet.contains(url) || "datetime".equals(param.toLowerCase()) |
| 198 | |
|| "string".equals(param.toLowerCase()) || "boolean".equals(param.toLowerCase()) |
| 199 | |
|| "integer".equals(param.toLowerCase())) { |
| 200 | 0 | return; |
| 201 | |
} |
| 202 | |
ContractReader contract; |
| 203 | |
try { |
| 204 | 0 | contract = new MessageContractReader(url, jsessionId); |
| 205 | |
|
| 206 | 0 | if (!param.endsWith("List") && !param.endsWith("Id") |
| 207 | |
&& !param.endsWith("Key") && !param.endsWith("Type")) { |
| 208 | 0 | message2Java(contract, param); |
| 209 | |
} |
| 210 | |
|
| 211 | 0 | alreadyParsedUrlSet.add(url); |
| 212 | |
|
| 213 | |
|
| 214 | 0 | NodeList nodeList = contract.getDocument().getElementsByTagName( |
| 215 | |
"td"); |
| 216 | 0 | for (int i = 0, iCnt = nodeList.getLength(); i < iCnt; i++) { |
| 217 | 0 | Node node = nodeList.item(i); |
| 218 | 0 | NamedNodeMap nodeMap = node.getAttributes(); |
| 219 | 0 | if (nodeMap != null |
| 220 | |
&& nodeMap.getNamedItem("class") != null |
| 221 | |
&& "structType".equals(nodeMap.getNamedItem("class") |
| 222 | |
.getNodeValue())) { |
| 223 | 0 | Node anchor = node.getFirstChild(); |
| 224 | 0 | String urlString = anchor.getAttributes().getNamedItem( |
| 225 | |
"href").getNodeValue(); |
| 226 | 0 | if (!urlString.startsWith("http") |
| 227 | |
&& !urlString.startsWith("file")) { |
| 228 | 0 | urlString = contractURL.getProtocol()+"://"+contractURL.getHost() |
| 229 | |
+ urlString; |
| 230 | |
} |
| 231 | 0 | URL newUrl = new URL(urlString); |
| 232 | 0 | String newParam = anchor.getTextContent().trim(); |
| 233 | 0 | if (!newParam.startsWith("<") |
| 234 | |
&& !alreadyParsedUrlSet.contains(newUrl)) { |
| 235 | 0 | findParamsFromMessage(newParam, newUrl, |
| 236 | |
alreadyParsedUrlSet); |
| 237 | |
} |
| 238 | |
} |
| 239 | |
} |
| 240 | 0 | } catch (IOException e) { |
| 241 | 0 | getLog().warn( |
| 242 | |
"Error loading page for Type '" + param + "'" |
| 243 | |
+ e.getMessage()); |
| 244 | 0 | } catch (ParserConfigurationException e) { |
| 245 | 0 | getLog().warn( |
| 246 | |
"Error parsing page for Type '" + param + "'" |
| 247 | |
+ e.getMessage()); |
| 248 | 0 | } catch (SAXException e) { |
| 249 | 0 | getLog().warn( |
| 250 | |
"Error parsing page for Type '" + param + "'" |
| 251 | |
+ e.getMessage()); |
| 252 | 0 | } |
| 253 | 0 | } |
| 254 | |
|
| 255 | |
public void findParams(Document document) { |
| 256 | 0 | Set<URL> alreadyParsedUrlSet = new HashSet<URL>(); |
| 257 | 0 | NodeList nodeList = document.getElementsByTagName("td"); |
| 258 | 0 | for (int i = 0, iCnt = nodeList.getLength(); i < iCnt; i++) { |
| 259 | 0 | Node node = nodeList.item(i); |
| 260 | 0 | NamedNodeMap nodeMap = node.getAttributes(); |
| 261 | 0 | if (nodeMap != null |
| 262 | |
&& ("methodParamType".equals(nodeMap.getNamedItem("class") |
| 263 | |
.getNodeValue()) || "methodReturnType" |
| 264 | |
.equals(nodeMap.getNamedItem("class") |
| 265 | |
.getNodeValue()))) { |
| 266 | 0 | String param = node.getFirstChild().getTextContent().trim(); |
| 267 | 0 | if (!"None".equals(param) && !"none".equals(param) |
| 268 | |
&& !param.startsWith("<") |
| 269 | |
&& !param.endsWith("Id") && !param.endsWith("Key")) { |
| 270 | |
try { |
| 271 | 0 | String urlString = node.getFirstChild().getAttributes() |
| 272 | |
.getNamedItem("href").getNodeValue(); |
| 273 | 0 | if (!urlString.startsWith("http") |
| 274 | |
&& !urlString.startsWith("file")) { |
| 275 | 0 | urlString = contractURL.getProtocol()+"://"+contractURL.getHost() |
| 276 | |
+ urlString; |
| 277 | |
} |
| 278 | 0 | findParamsFromMessage(param, new URL(urlString), |
| 279 | |
alreadyParsedUrlSet); |
| 280 | 0 | } catch (MalformedURLException e) { |
| 281 | 0 | getLog().warn( |
| 282 | |
"Error loading page for Type '" + param + "'" |
| 283 | |
+ e.getMessage()); |
| 284 | 0 | } catch (DOMException e) { |
| 285 | 0 | getLog().warn( |
| 286 | |
"DOM Error parsing page for Type '" + param |
| 287 | |
+ "'" + e.getMessage()); |
| 288 | 0 | } |
| 289 | |
} |
| 290 | |
} |
| 291 | |
} |
| 292 | 0 | } |
| 293 | |
|
| 294 | |
private void message2Java(ContractReader contract, String param) { |
| 295 | 0 | StreamSource wsdlXslt=null; |
| 296 | 0 | String className = param.substring(0, 1).toUpperCase() + param.substring(1); |
| 297 | |
|
| 298 | 0 | if(excludeClasses!=null){ |
| 299 | 0 | for(String excludeClass:excludeClasses){ |
| 300 | 0 | if(excludeClass.equals(className)){ |
| 301 | 0 | System.out.println("Skipping DTO: " + className + ".java"); |
| 302 | 0 | return; |
| 303 | |
} |
| 304 | |
} |
| 305 | |
} |
| 306 | |
|
| 307 | 0 | if (messageTransformFile == null) { |
| 308 | |
try { |
| 309 | 0 | wsdlXslt = new StreamSource(new ClassPathResource("messageInterface.xml").getInputStream()); |
| 310 | 0 | } catch (IOException e) { |
| 311 | 0 | e.printStackTrace(); |
| 312 | 0 | } |
| 313 | |
} else { |
| 314 | 0 | wsdlXslt = new StreamSource(messageTransformFile); |
| 315 | |
} |
| 316 | |
try { |
| 317 | 0 | Transformer transformer = net.sf.saxon.TransformerFactoryImpl |
| 318 | |
.newInstance().newTransformer(wsdlXslt); |
| 319 | 0 | transformer.setParameter("packageName", this.getPackageName()); |
| 320 | 0 | transformer.setParameter("sourcePath", contract.getContractPath()); |
| 321 | 0 | transformer.setParameter("date", new Date()); |
| 322 | 0 | transformer.setParameter("user", System.getProperty("user.name")); |
| 323 | 0 | transformer.setParameter("url", contract.getContractPath()); |
| 324 | 0 | transformer.setParameter("excludeClasses", join(excludeClasses,",")); |
| 325 | 0 | String packageDir=""; |
| 326 | 0 | if(packageName!=null && !packageName.isEmpty()){ |
| 327 | 0 | packageDir = "/"+packageName.replace('.', '/'); |
| 328 | |
} |
| 329 | 0 | File dtoDirectory = new File(outputDirectory+packageDir, "dto"); |
| 330 | 0 | dtoDirectory.mkdirs(); |
| 331 | 0 | Result result = new StreamResult(new File(dtoDirectory, param |
| 332 | |
.substring(0, 1).toUpperCase() |
| 333 | |
+ param.substring(1) + ".java")); |
| 334 | 0 | System.out.println("Creating DTO: " + className + ".java"); |
| 335 | 0 | transformer.transform(contract.getStreamSource(), result); |
| 336 | 0 | } catch (TransformerConfigurationException e) { |
| 337 | |
|
| 338 | 0 | e.printStackTrace(); |
| 339 | 0 | } catch (TransformerFactoryConfigurationError e) { |
| 340 | |
|
| 341 | 0 | e.printStackTrace(); |
| 342 | 0 | } catch (TransformerException e) { |
| 343 | |
|
| 344 | 0 | e.printStackTrace(); |
| 345 | 0 | } |
| 346 | |
|
| 347 | 0 | } |
| 348 | |
|
| 349 | |
public File getContractFile() { |
| 350 | 0 | return contractFile; |
| 351 | |
} |
| 352 | |
|
| 353 | |
public void setContractFile(File contractFile) { |
| 354 | 0 | this.contractFile = contractFile; |
| 355 | 0 | } |
| 356 | |
|
| 357 | |
public URL getContractURL() { |
| 358 | 0 | return contractURL; |
| 359 | |
} |
| 360 | |
|
| 361 | |
public void setContractURL(URL contractURL) { |
| 362 | 0 | this.contractURL = contractURL; |
| 363 | 0 | } |
| 364 | |
|
| 365 | |
public File getOutputDirectory() { |
| 366 | 0 | return outputDirectory; |
| 367 | |
} |
| 368 | |
|
| 369 | |
public void setOutputDirectory(File outputDirectory) { |
| 370 | 0 | this.outputDirectory = outputDirectory; |
| 371 | 0 | } |
| 372 | |
|
| 373 | |
public File getTransformFile() { |
| 374 | 0 | return transformFile; |
| 375 | |
} |
| 376 | |
|
| 377 | |
public void setTransformFile(File transformFile) { |
| 378 | 0 | this.transformFile = transformFile; |
| 379 | 0 | } |
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
public File getMessageTransformFile() { |
| 385 | 0 | return messageTransformFile; |
| 386 | |
} |
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
public void setMessageTransformFile(File messageTransformFile) { |
| 393 | 0 | this.messageTransformFile = messageTransformFile; |
| 394 | 0 | } |
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
public String getPackageName() { |
| 400 | 0 | return packageName; |
| 401 | |
} |
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
public void setPackageName(String packageName) { |
| 407 | 0 | this.packageName = packageName; |
| 408 | 0 | } |
| 409 | |
|
| 410 | |
public String getServiceName() { |
| 411 | 0 | return serviceName; |
| 412 | |
} |
| 413 | |
|
| 414 | |
public void setServiceName(String serviceName) { |
| 415 | 0 | this.serviceName = serviceName; |
| 416 | 0 | } |
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
public String getNamespace() { |
| 422 | 0 | return namespace; |
| 423 | |
} |
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
public void setNamespace(String namespace) { |
| 429 | 0 | this.namespace = namespace; |
| 430 | 0 | } |
| 431 | |
|
| 432 | |
public String[] getExcludeClasses() { |
| 433 | 0 | return excludeClasses; |
| 434 | |
} |
| 435 | |
|
| 436 | |
public void setExcludeClasses(String[] excludeClasses) { |
| 437 | 0 | this.excludeClasses = excludeClasses; |
| 438 | 0 | } |
| 439 | |
|
| 440 | |
} |