Debian Codegraph @

src/dr/app/tools/BeastParserDoc.java

raw symbols SCIP
1 /*
2 * BeastParserDoc.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.tools;
27
28 import dr.app.beast.BeastParser;
29 import dr.app.beast.BeastVersion;
30 import dr.app.util.Arguments;
31 import dr.util.Version;
32 import dr.xml.MarkdownDocumentationHandler;
33 import dr.xml.WikiDocumentationHandler;
34 import dr.xml.XMLDocumentationHandler;
35 import dr.xml.XMLParseException;
36
37 import java.io.File;
38 import java.io.FileWriter;
39 import java.io.PrintWriter;
40
41
42 public class BeastParserDoc {
43 enum Format {
44 XML,
45 WIKI,
46 MARKDOWN
47 }
48 private final static Version version = new BeastVersion();
49
50 public final static String INDEX_HTML = "index_" + version.getVersionString() + ".html";
51 public final static String DETAIL_HTML = "detail_" + version.getVersionString() + ".html";
52 public final static String INDEX_WIKI = "index_" + version.getVersionString() + ".wiki";
53 public final static String DETAIL_WIKI = "detail_" + version.getVersionString() + ".wiki";
54 public final static String INDEX_MARKDOWN = "index_" + version.getVersionString() + ".md";
55 public final static String DETAIL_MARKDOWN = "detail_" + version.getVersionString() + ".md";
56
57
58 public final static String TITLE = "BEAST " + version.getVersionString() + " Parser Library ("
59 + version.getDateString() + ")";
60 public final static String AUTHORS = "Alexei Drummond, Marc A Suchard & Andrew Rambaut";
61 public final static String LINK1 = "http://beast.community/";
62 public final static String LINK2 = "http://github.com/beast-dev/beast-mcmc/";
63
64 public BeastParserDoc(BeastParser parser, String directory, Format format) throws java.io.IOException {
65
66 try {
67 // Create multiple directories
68 boolean success = (new File(directory)).mkdirs();
69 if (success) {
70 System.out.println("Directories: " + directory + " created");
71 }
72
73 } catch (Exception e) {//Catch exception if any
74 System.err.println("Error: " + e.getMessage());
75 }
76
77 //
78 // File file = new File(directory);
79 //
80 // if (!file.exists()) {
81 // file.mkdir();
82 // }
83 //
84 // if (!file.isDirectory()) {
85 // throw new IllegalArgumentException(directory + " is not a directory!");
86 // }
87 PrintWriter writer;
88 if (format == Format.WIKI) {
89 XMLDocumentationHandler handler = new WikiDocumentationHandler(parser);
90
91 writer = new PrintWriter(new FileWriter(new File(directory, DETAIL_WIKI)));
92 System.out.println("Building element descriptions in " + DETAIL_WIKI + " ...");
93
94 handler.outputElements(writer);
95 System.out.println("done.");
96 // writer.flush();
97 // writer.close();
98
99 // writer = new PrintWriter(new FileWriter(new File(directory, INDEX_WIKI)));
100 System.out.println("Building types table ...");
101
102 handler.outputTypes(writer);
103 System.out.println("done.");
104 writer.flush();
105 writer.close();
106
107
108 } else if (format == Format.MARKDOWN) {
109 XMLDocumentationHandler handler = new MarkdownDocumentationHandler(parser);
110
111 writer = new PrintWriter(new FileWriter(new File(directory, DETAIL_MARKDOWN)));
112 System.out.println("Building element descriptions in " + DETAIL_MARKDOWN + " ...");
113
114 handler.outputElements(writer);
115 System.out.println("done.");
116 System.out.println("Building types table ...");
117
118 handler.outputTypes(writer);
119 System.out.println("done.");
120 writer.flush();
121 writer.close();
122
123
124 } else {
125 XMLDocumentationHandler handler = new XMLDocumentationHandler(parser);
126
127 System.out.println("Generate " + INDEX_HTML + " ...");
128 writer = new PrintWriter(new FileWriter(new File(directory, INDEX_HTML)));
129
130 handler.outputIndex(writer); // generate index.html
131 System.out.println("done.");
132 writer.flush();
133 writer.close();
134
135 System.out.println("Generate " + DETAIL_HTML + " ...");
136 writer = new PrintWriter(new FileWriter(new File(directory, DETAIL_HTML)));
137
138 handler.outputElements(writer);
139 System.out.println("done.");
140
141 writer.flush();
142 writer.close();
143 }
144 }
145
146 // private final void setup() throws XMLParseException {
147 // add all the XMLObject parsers you need
148 // }
149
150 public static void printTitle() {
151
152 System.out.println("+-----------------------------------------------\\");
153 System.out.print("|");
154 int n = 47 - TITLE.length();
155 int n1 = n / 2;
156 int n2 = n1 + (n % 2);
157 for (int i = 0; i < n1; i++) { System.out.print(" "); }
158 System.out.print(TITLE);
159 for (int i = 0; i < n2; i++) { System.out.print(" "); }
160 System.out.println("||");
161 System.out.println("| " + AUTHORS + " ||");
162 System.out.println("| " + LINK1 + " ||");
163 System.out.println("| " + LINK2 + " ||");
164 System.out.println("\\-----------------------------------------------\\|");
165 System.out.println(" \\-----------------------------------------------\\");
166 System.out.println();
167 }
168
169 public static void printUsage(Arguments arguments) {
170
171 arguments.printUsage("beastdoc", "<output-directory>");
172 System.out.println();
173 System.out.println(" Example: beastdoc ./doc");
174 System.out.println();
175
176 }
177
178 //Main method
179 public static void main(String[] args) throws java.io.IOException, XMLParseException {
180
181 printTitle();
182
183 Arguments arguments = new Arguments(
184 new Arguments.Option[] {
185 new Arguments.Option("help", "option to print this message")
186 });
187
188 try {
189 arguments.parseArguments(args);
190 } catch (Arguments.ArgumentException ae) {
191 System.out.println(ae);
192 printUsage(arguments);
193 return;
194 }
195
196 if (arguments.hasOption("help")) {
197 printUsage(arguments);
198 return;
199 }
200
201 String outputDirectory = null;
202
203 String[] args2 = arguments.getLeftoverArguments();
204
205 if (args2.length > 1) {
206 System.err.println("Unknown option: " + args2[1]);
207 System.err.println();
208 printUsage(arguments);
209 return;
210 }
211
212 if (args2.length > 0) {
213 outputDirectory = args2[0];
214 }
215
216 if (outputDirectory == null) {
217 // No input file name was given so throw up a dialog box...
218 outputDirectory = System.getProperty("user.dir") + System.getProperty("file.separator") + "release"
219 + System.getProperty("file.separator") + "common" + System.getProperty("file.separator")
220 + "doc" + System.getProperty("file.separator");
221 }
222 System.out.println("Output directory : " + outputDirectory);
223 // BeastParserDoc(BeastParser parser, String directory, boolean wikiFormat)
224 new BeastParserDoc(new BeastParser(new String[] {}, null, false, false, false, version), outputDirectory, Format.MARKDOWN);
225
226 System.exit(0);
227 }
228 }
229