001 package com.hammurapi.config.runtime;
002
003 import java.io.File;
004 import java.net.MalformedURLException;
005 import java.net.URL;
006 import java.net.URLClassLoader;
007 import java.util.ArrayList;
008 import java.util.HashMap;
009 import java.util.List;
010 import java.util.Map;
011
012 import org.apache.tools.ant.BuildException;
013 import org.apache.tools.ant.Task;
014 import org.eclipse.emf.ecore.EObject;
015 import org.eclipse.emf.ecore.resource.Resource;
016
017 import com.hammurapi.render.RenderingConstants;
018 import com.hammurapi.render.RenderingException;
019 import com.hammurapi.render.ReportGenerator;
020 import com.hammurapi.util.Context;
021 import com.hammurapi.util.DefaultContext;
022
023 /**
024 * Generates HTML documentation for configuration models.
025 * @author Pavel Vlasov
026 *
027 */
028 public class ConfigDoc extends Task {
029
030 private List<URL> path = new ArrayList<URL>();
031
032 public void addPath(String pathElement) {
033 for (String pthe: pathElement.split(";")) {
034 try {
035 path.add(new URL(pthe.trim()));
036 } catch (MalformedURLException e) {
037 new BuildException(e);
038 }
039 }
040 }
041
042 private EObject toDocument;
043
044 public void setToDocument(EObject toDocument) {
045 this.toDocument = toDocument;
046 }
047
048 @Override
049 public void execute() throws BuildException {
050 long start = System.currentTimeMillis();
051
052 if (!outputDir.exists()) {
053 outputDir.mkdirs();
054 }
055
056 if (toDocument==null) {
057 ClassLoader classLoader = getClass().getClassLoader();
058 if (!path.isEmpty()) {
059 classLoader = new URLClassLoader(path.toArray(new URL[path.size()]));
060 }
061
062 Resource res = ResourceLoader.load(source, classLoader);
063 toDocument = res.getContents().get(0);
064 }
065
066 ReportGenerator generator = new ReportGenerator();
067
068 Map<String, Object> env = new HashMap<String, Object>();
069 env.put(RenderingConstants.RENDER_START, start);
070 Context context = new DefaultContext(ConfigDoc.class.getClassLoader(), null);
071
072 generator.setEnvironment(env);
073 generator.setContext(context);
074
075 try {
076 generator.generate(toDocument, outputDir, "Configuration documentation", http);
077 } catch (RenderingException e) {
078 throw new BuildException(e);
079 }
080 }
081
082 private String source;
083 private File outputDir;
084 private boolean http;
085
086 /**
087 * Source - file or URL
088 * @return
089 */
090 public String getSource() {
091 return source;
092 }
093
094 /**
095 * Source - file or URL
096 * @param source
097 */
098 public void setSource(String source) {
099 this.source = source;
100 }
101
102 /**
103 * Output directory
104 * @return
105 */
106 public File getOutputDir() {
107 return outputDir;
108 }
109
110 /**
111 * Output directory
112 * @param outputDir
113 */
114 public void setOutputDir(File outputDir) {
115 this.outputDir = outputDir;
116 }
117
118 public boolean isHttp() {
119 return http;
120 }
121
122 /**
123 * If true, outline and contents trees are rendered to use AJAX. It is useful for
124 * large models, but works only over HTTP.
125 * @param http
126 */
127 public void setHttp(boolean http) {
128 this.http = http;
129 }
130
131 /**
132 * Generates documentation from a diagram file.
133 * Command line parameters: <diagram file> <output dir> [http]
134 * @param args
135 */
136 public static void main(String[] args) throws Exception {
137 System.out.println("Command line parameters: <source file> <output dir> [http]");
138 System.out.println("Example: myconfig.hgconfig config-doc");
139
140 ConfigDoc documenter = new ConfigDoc();
141 documenter.setSource(args[0]);
142 documenter.setOutputDir(new File(args[1]));
143 documenter.setHttp(args.length>2 ? "http".equals(args[2]) : false);
144 documenter.execute();
145 }
146
147 }