/* The MIT License . Copyright (c) 2005, Ghent University (UGent) . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: . The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. . THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package org.codehaus.mojo.linguinemaps; import com.oy.shared.lm.ant.TaskOptions; import com.oy.shared.lm.graph.IGraphModel; import com.oy.shared.lm.in.ANTtoGRAPH; import com.oy.shared.lm.in.DTDtoGRAPH; import com.oy.shared.lm.in.HBMtoGRAPH; import com.oy.shared.lm.in.OJBtoGRAPH; import com.oy.shared.lm.in.WSDLtoGRAPH; import com.oy.shared.lm.out.GRAPHtoDOTtoGIF; import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; import org.apache.maven.reporting.AbstractMavenReport; import org.apache.maven.reporting.AbstractMavenReportRenderer; import org.apache.maven.reporting.MavenReportException; import org.codehaus.doxia.sink.Sink; import org.codehaus.doxia.site.renderer.SiteRenderer; import java.awt.Container; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Locale; /** * Generates graphical overview of an ANT build file, an XML DTD file, * Hibernate mapping file(s), an Object-Relation Bridge mapping file or a XML * Web-Services (WSDL) file. * * @author Jurgen De Landsheer * @version 1.0.0-alpha-1 - 21 December 2005 * * @goal report * @description Maven 2 Plugin: Linguine Maps * * @see GraphViz * @see Linguine * Maps */ public class LinguineMapsPlugin extends AbstractMavenReport { /** constant */ public static final String VERSION = "1.0.0-alpha-1"; /** constant */ private static final String OUTPUTDIRECTORY = "linguine"; /** constant */ public static final String TYPE_HBM = "HBM"; /** constant */ public static final String TYPE_ANT = "ANT"; /** constant */ public static final String TYPE_DTD = "DTD"; /** constant */ public static final String TYPE_OBJ = "OBJ"; /** constant */ public static final String TYPE_WSDL = "WSDL"; /** * qualified name for the intermediate output file [from configuration in * pom] * * @parameter */ private List inputfiles = new ArrayList(); /** * reference to Maven 2 Project * * @parameter expression="${project}" * @required * @readonly */ private MavenProject project; /** * Doxia Site Renderer. * * @parameter expression="${component.org.codehaus.doxia.site.renderer.SiteRenderer}" * @required * @readonly */ private SiteRenderer siteRenderer; /** * custom Graphviz attributes for the diagram * * @parameter */ private String attributes; /** * comma separated list of colors * * @parameter */ private String colors; /** * custom Graphviz attributes for the edges * * @parameter */ private String edgeattributes; /** * comma separated list of entity names that are excluded from the diagram * * @parameter */ private String excludes; /** * font name for the diagram * * @parameter default-value="Helvetica" */ private String fontname; /** *
* GraphViz executable location. *
* ** Visualization (images) will be generated only if you install this * program and set this property to the executable dot (dot.exe on Win). *
* * @parameter alias="graphViz" expression="${graphVizExecutable}" * @required */ private String graphVizExecutable; /** * comma separated list of entity names that are included into the diagram * * @parameter */ private String includes; /** * comma separated list of qualified name for the intermediate output file * [from command line] * * @parameter expression="${inputfile}" */ private String inputfile; /** * custom Graphviz attributes for the nodes * * @parameter */ private String nodeattributes; /** * Report output directory. * * @parameter expression="${project.reporting.outputDirectory}/linguine" * @required */ private String outputDirectory; /** * qualified name for the images output file * * @parameter expression="${outputfile}" * @required */ private String outputfile; /** * title of the diagram * * @parameter */ private String title; /** * wich type of export * * @parameter expression="${type}" */ private String type; /** * "true|false"; controls amount of details in the diagram * * @parameter default-value="true" */ private boolean detailed; /** * "true|false"; forces processing of XML ENTITY * * @parameter default-value="false" */ private boolean expandentityref; /** * "true|false"; strips package names class from names * * @parameter default-value="true" */ private boolean qualifiednames; /** * "true|false"; controls page orientation * * @parameter default-value="true" */ private boolean rotated; /** * base font size for the diagram * * @parameter default-value="10" */ private int fontsize; /** * Creates a new LinguineMapsPlugin object. */ public LinguineMapsPlugin() { super(); rotated = true; detailed = true; qualifiednames = true; expandentityref = false; fontsize = 10; fontname = "Helvetica"; } /** * sets attributes property * * @param attributes The attributes to set. */ public void setAttributes(String attributes) { this.attributes = attributes; } /** * gets attributes property * * @return Returns the attributes. */ public String getAttributes() { return this.attributes; } /** * sets colors property * * @param colors The colors to set. */ public void setColors(String colors) { this.colors = colors; } /** * gets colors property * * @return Returns the colors. */ public String getColors() { return this.colors; } /** * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale) */ public String getDescription(Locale locale) { return "Maven 2 Plugin: Linguine Maps"; } /** * sets detailed property * * @param detailed The detailed to set. */ public void setDetailed(boolean detailed) { this.detailed = detailed; } /** * gets dotfile property * * @return Returns the detailed. */ public boolean isDetailed() { return this.detailed; } /** * gets dotfile property * * @return Returns the dotfile. */ public String getDotfile() { return outputfile.substring(0, outputfile.lastIndexOf('.')) + ".dot"; } /** * sets edgeattributes property * * @param edgeattributes The edgeattributes to set. */ public void setEdgeattributes(String edgeattributes) { this.edgeattributes = edgeattributes; } /** * gets edgeattributes property * * @return Returns the edgeattributes. */ public String getEdgeattributes() { return this.edgeattributes; } /** * sets excludes property * * @param excludes The excludes to set. */ public void setExcludes(String excludes) { this.excludes = excludes; } /** * gets excludes property * * @return Returns the excludes. */ public String getExcludes() { return this.excludes; } /** * sets expandentityref property * * @param expandentityref The expandentityref to set. */ public void setExpandentityref(boolean expandentityref) { this.expandentityref = expandentityref; } /** * gets expandentityref property * * @return Returns the expandentityref. */ public boolean isExpandentityref() { return this.expandentityref; } /** * sets fontname property * * @param fontname The fontname to set. */ public void setFontname(String fontname) { this.fontname = fontname; } /** * gets fontname property * * @return Returns the fontname. */ public String getFontname() { return this.fontname; } /** * sets fontsize property * * @param fontsize The fontsize to set. */ public void setFontsize(int fontsize) { this.fontsize = fontsize; } /** * gets fontsize property * * @return Returns the fontsize. */ public int getFontsize() { return this.fontsize; } /** * sets graphVizExecutable property * * @param exefile The graphVizExecutable to set. */ public void setGraphVizExecutable(String exefile) { this.graphVizExecutable = exefile; } /** * gets graphVizExecutable property * * @return Returns the graphVizExecutable. */ public String getGraphVizExecutable() { return this.graphVizExecutable; } /** * sets includes property * * @param includes The includes to set. */ public void setIncludes(String includes) { this.includes = includes; } /** * gets includes property * * @return Returns the includes. */ public String getIncludes() { return this.includes; } /** * sets inputfiles property * * @param inputfiles The inputfiles to set. */ public void setInputfiles(List inputfiles) { this.inputfiles = inputfiles; } /** * gets inputfiles property * * @return Returns the inputfiles. */ public List getInputfiles() { // when called via command line if (inputfile != null) { String[] ifs = inputfile.split(","); for (int i = 0; i < ifs.length; i++) { if (!inputfiles.contains(ifs[i])) { inputfiles.add(ifs[i]); } } } return this.inputfiles; } /** * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale) */ public String getName(Locale locale) { return "Linguine Maps"; } /** * sets nodeattributes property * * @param nodeattributes The nodeattributes to set. */ public void setNodeattributes(String nodeattributes) { this.nodeattributes = nodeattributes; } /** * gets nodeattributes property * * @return Returns the nodeattributes. */ public String getNodeattributes() { return this.nodeattributes; } /** * sets outputDirectory property * * @param outputDirectory The outputDirectory to set. */ public void setOutputDirectory(String outputDirectory) { this.outputDirectory = outputDirectory; } /** * @see org.apache.maven.reporting.MavenReport#getOutputName() */ public String getOutputName() { return OUTPUTDIRECTORY + "/" + getOutputfile() + ".index"; } /** * sets outputfile property * * @param outputfile The outputfile to set. */ public void setOutputfile(String outputfile) { this.outputfile = outputfile; } /** * gets outputfile property * * @return Returns the outputfile. */ public String getOutputfile() { return this.outputfile; } /** * sets maven project * * @param project The project to set. */ public void setProject(MavenProject project) { this.project = project; } /** * sets qualifiednames property * * @param qualifiednames The qualifiednames to set. */ public void setQualifiednames(boolean qualifiednames) { this.qualifiednames = qualifiednames; } /** * gets qualifiednames property * * @return Returns the qualifiednames. */ public boolean isQualifiednames() { return this.qualifiednames; } /** * sets rotated property * * @param rotated The rotated to set. */ public void setRotated(boolean rotated) { this.rotated = rotated; } /** * gets rotated property * * @return Returns the rotated. */ public boolean isRotated() { return this.rotated; } /** * sets site renderer * * @param siteRenderer The siteRenderer to set. */ public void setSiteRenderer(SiteRenderer siteRenderer) { this.siteRenderer = siteRenderer; } /** * sets title property * * @param title The title to set. */ public void setTitle(String title) { this.title = title; } /** * gets title property * * @return Returns the title. */ public String getTitle() { return (this.title == null) ? (type + " scheme") : this.title; } /** * sets type property * * @param type The type to set. */ public void setType(String type) { this.type = type; } /** * gets type property * * @return Returns the type. */ public String getType() { return this.type; } /** * gets version * * @return version */ public String getVersion() { return VERSION; } /** * adds an input/resource file location * * @param inputfile input/resource file location */ public void addInputfile(String inputfile) { inputfiles.add(inputfile); } /** * @see org.apache.maven.plugin.Mojo#execute() */ public void execute() throws MojoExecutionException { try { new File(getOutputDirectory()).mkdirs(); run(prepareOptions(getOutputDirectory())); } catch (Exception e) { e.printStackTrace(); } } /** * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory() */ protected String getOutputDirectory() { return outputDirectory + "/"; } /** * @see org.apache.maven.reporting.AbstractMavenReport#getProject() */ protected MavenProject getProject() { return project; } /** * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer() */ protected SiteRenderer getSiteRenderer() { return siteRenderer; } /** * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale) */ protected void executeReport(Locale locale) throws MavenReportException { try { new File(getOutputDirectory()).mkdirs(); run(prepareOptions(getOutputDirectory())); new LiguineMapsSiteRenderer(getSink(), getProject().getModel()).render(); } catch (Exception e) { e.printStackTrace(); } } /** * write info to console */ protected void info() { System.out.println("type: " + getType()); System.out.println("resources: " + getInputfiles()); System.out.println("writing to " + getOutputDirectory() + "/" + getOutputfile() + " and " + getOutputDirectory() + "/" + getDotfile()); System.out.println("site: " + getOutputName() + ".html"); System.out.println("using GraphViz executable: " + getGraphVizExecutable()); } /** * creates TaskOptions object from the settings from this object * * @param outputdir * * @return TaskOptions * * @throws NullPointerException incorrect number of input files */ private TaskOptions prepareOptions(String outputdir) { if (getInputfiles().size() == 0) { throw new NullPointerException(); } if (!type.equals(TYPE_HBM) && (inputfiles.size() > 1)) { throw new NullPointerException(); } TaskOptions options = new TaskOptions(); options.outFile = outputdir + getOutputfile(); options.dotFile = outputdir + getDotfile(); options.attr = this.attributes; if (this.title == null) { if (getType().equals(TYPE_ANT)) { options.caption = "ANT build file"; } if (getType().equals(TYPE_DTD)) { options.caption = "XML DTD file"; } if (getType().equals(TYPE_HBM)) { options.caption = "Hibernate mapping file"; } if (getType().equals(TYPE_OBJ)) { options.caption = "Object-Relation Bridge mapping file"; } if (getType().equals(TYPE_WSDL)) { options.caption = "XML Web-Services (WSDL) file"; } } else { options.caption = this.title; } options.caption = this.title; options.colors = this.colors; options.detailed = this.detailed; options.edgeAttr = this.edgeattributes; options.excludes = this.excludes; options.exeFile = this.graphVizExecutable; options.expandEntityRef = this.expandentityref; options.fontName = this.fontname; options.fontSize = this.fontsize; options.includes = this.includes; if (inputfiles.size() == 1) { options.inFile = inputfiles.get(0).toString(); } String[] tmp = new String[inputfiles.size()]; for (int i = 0; i < inputfiles.size(); i++) tmp[i] = inputfiles.get(i).toString(); options.inFileSet = tmp; options.nodeAttr = this.nodeattributes; options.qualifiedNames = this.qualifiednames; options.rotated = this.rotated; return options; } /** * run task with given TaskOptions * * @param options * * @throws Exception on exception * @throws NullPointerException incorrect type */ private void run(TaskOptions options) throws Exception { info(); if (getType().equals(TYPE_ANT)) { IGraphModel graph = ANTtoGRAPH.load(options); GRAPHtoDOTtoGIF.transform(graph, options.dotFile, options.outFile, options.exeFile); return; } if (getType().equals(TYPE_DTD)) { IGraphModel graph = DTDtoGRAPH.load(options); GRAPHtoDOTtoGIF.transform(graph, options.dotFile, options.outFile, options.exeFile); return; } if (getType().equals(TYPE_HBM)) { IGraphModel graph = HBMtoGRAPH.load(options); GRAPHtoDOTtoGIF.transform(graph, options.dotFile, options.outFile, options.exeFile); return; } if (getType().equals(TYPE_OBJ)) { IGraphModel graph = OJBtoGRAPH.load(options); GRAPHtoDOTtoGIF.transform(graph, options.dotFile, options.outFile, options.exeFile); return; } if (getType().equals(TYPE_WSDL)) { IGraphModel graph = WSDLtoGRAPH.load(options); GRAPHtoDOTtoGIF.transform(graph, options.dotFile, options.outFile, options.exeFile); return; } throw new NullPointerException(); } /** * DOC_ME * * @author Jurgen De Landsheer * @version 1.0.0-alpha-1 - 21 December 2005 */ class LiguineMapsSiteRenderer extends AbstractMavenReportRenderer { /** DOC_ME */ protected Model model; /** DOC_ME */ private int maxw = 700; /** * Creates a new LiguineMapsSiteRenderer object. * * @param sink DOC_ME * @param model DOC_ME */ public LiguineMapsSiteRenderer(Sink sink, Model model) { super(sink); this.model = model; } /** * @see org.apache.maven.reporting.AbstractMavenReportRenderer#getTitle() */ public String getTitle() { return getName(Locale.getDefault()); } /** * @see org.apache.maven.reporting.AbstractMavenReportRenderer#renderBody() */ protected void renderBody() { startSection(getName(Locale.getDefault())); sink.rawText("