001 /* 002 * Copyright 2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 1.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl1.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.student.datadictionary.mojo; 017 018 import java.io.File; 019 import java.io.FileNotFoundException; 020 import java.io.FileOutputStream; 021 import java.io.OutputStream; 022 import java.io.PrintStream; 023 import java.net.MalformedURLException; 024 import java.net.URL; 025 import java.net.URLClassLoader; 026 import java.util.ArrayList; 027 import java.util.LinkedHashSet; 028 import java.util.List; 029 import java.util.Map; 030 import java.util.Set; 031 import org.apache.maven.artifact.DependencyResolutionRequiredException; 032 import org.apache.maven.plugin.AbstractMojo; 033 import org.apache.maven.plugin.MojoExecutionException; 034 import org.apache.maven.project.MavenProject; 035 import org.kuali.rice.krad.datadictionary.DataObjectEntry; 036 import org.kuali.student.datadictionary.util.DictionaryFormatter; 037 import org.kuali.student.datadictionary.util.DictionaryTesterHelper; 038 039 /** 040 * Mojo for generating a formatted view of the data dictionary 041 * @goal ksdictionarydoc 042 * @phase site 043 * @requiresDependencyResolution test 044 */ 045 public class KSDictionaryDocMojo extends AbstractMojo { 046 047 /** 048 * @parameter expression="${project}" 049 * @required 050 * @readonly 051 */ 052 private MavenProject project; 053 /** 054 * @parameter 055 **/ 056 private List<String> inputFiles; 057 /** 058 * @parameter 059 **/ 060 private List<String> supportFiles = new ArrayList(); 061 /** 062 * @parameter expression="${htmlDirectory}" default-value="${project.build.directory}/site/services/dictionarydocs" 063 */ 064 private File htmlDirectory; 065 066 public void setHtmlDirectory(File htmlDirectory) { 067 this.htmlDirectory = htmlDirectory; 068 } 069 070 public File getHtmlDirectory() { 071 return htmlDirectory; 072 } 073 074 public List<String> getInputFiles() { 075 return inputFiles; 076 } 077 078 public MavenProject getProject() { 079 return project; 080 } 081 082 public void setInputFiles(List<String> inputFiles) { 083 this.inputFiles = inputFiles; 084 } 085 086 public List<String> getSupportFiles() { 087 return supportFiles; 088 } 089 090 public void setSupportFiles(List<String> supportFiles) { 091 this.supportFiles = supportFiles; 092 } 093 094 @Override 095 public void execute() 096 throws MojoExecutionException { 097 this.getLog().info("generating dictionary documentation"); 098 // add the current projects classpath to the plugin so the springbean 099 // loader can find the xml files and lasses that it needs to can be run 100 // against the current project's files 101 if (project != null) { 102 this.getLog().info("adding current project's classpath to plugin class loader"); 103 List runtimeClasspathElements; 104 try { 105 runtimeClasspathElements = project.getRuntimeClasspathElements(); 106 } catch (DependencyResolutionRequiredException ex) { 107 throw new MojoExecutionException("got error", ex); 108 } 109 URL[] runtimeUrls = new URL[runtimeClasspathElements.size()]; 110 for (int i = 0; i < runtimeClasspathElements.size(); i++) { 111 String element = (String) runtimeClasspathElements.get(i); 112 try { 113 runtimeUrls[i] = new File(element).toURI().toURL(); 114 } catch (MalformedURLException ex) { 115 throw new MojoExecutionException(element, ex); 116 } 117 } 118 URLClassLoader newLoader = new URLClassLoader(runtimeUrls, 119 Thread.currentThread().getContextClassLoader()); 120 Thread.currentThread().setContextClassLoader(newLoader); 121 } 122 123 124 //System.out.println ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ()); 125 if (!htmlDirectory.exists()) { 126 if (!htmlDirectory.mkdirs()) { 127 // throw new MojoExecutionException("Could not create directory " 128 throw new IllegalArgumentException("Could not create directory " 129 + this.htmlDirectory.getPath()); 130 } 131 } 132 133 Set<String> inpFiles = new LinkedHashSet(this.inputFiles.size()); 134 for (String dictFileName : this.inputFiles) { 135 if (dictFileName.endsWith(".xml")) { 136 inpFiles.add(dictFileName); 137 } 138 } 139 140 Set<String> configFiles = new LinkedHashSet(this.inputFiles.size() + supportFiles.size()); 141 configFiles.addAll(inpFiles); 142 configFiles.addAll(this.supportFiles); 143 144 String outputDir = this.htmlDirectory.getAbsolutePath(); 145 DictionaryTesterHelper tester = new DictionaryTesterHelper(outputDir, configFiles); 146 List<String> outputFileNames = tester.doTest(); 147 148 // write out the index file 149 String indexFileName = this.htmlDirectory.getPath() + "/" + "index.html"; 150 File indexFile = new File(indexFileName); 151 OutputStream outputStream; 152 try { 153 outputStream = new FileOutputStream(indexFile, false); 154 } catch (FileNotFoundException ex) { 155 // throw new MojoExecutionException(indexFileName, ex); 156 throw new IllegalArgumentException(indexFileName, ex); 157 } 158 PrintStream out = new PrintStream(outputStream); 159 DictionaryFormatter.writeHeader(out, "Data Dictionary Index"); 160 out.println("<h1>Data Dictionary Index</h1>"); 161 String endUL = ""; 162 Map<String, DataObjectEntry> beansOfType = tester.getDataObjectEntryBeans(); 163 for (String beanId : beansOfType.keySet()) { 164 String outputFileName = beanId + ".html"; 165 out.println("<li><a href=\"" + outputFileName + "\">" + beanId + "</a>"); 166 } 167 out.println("</ul>"); 168 DictionaryFormatter.writeFooter(out); 169 this.getLog().info("finished generating dictionary documentation"); 170 } 171 }