diff options
Diffstat (limited to 'collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java')
-rwxr-xr-x | collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java | 157 |
1 files changed, 88 insertions, 69 deletions
diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java index c933ece35c..8aa7366899 100755 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java @@ -20,103 +20,124 @@ package org.apache.tuscany.sca.diagram.main; import java.io.File; +import java.io.FileWriter; import java.io.StringWriter; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; import org.apache.tuscany.sca.diagram.generator.DiagramGenerator; import org.apache.tuscany.sca.diagram.html.HTMLWrapper; -import org.apache.tuscany.sca.diagram.io.XMLReader; -import org.apache.tuscany.sca.diagram.io.XMLWriter; import org.apache.tuscany.sca.diagram.layout.CompositeEntity; import org.apache.tuscany.sca.diagram.layout.EntityBuilder; import org.w3c.dom.Document; public class Main { - private static String outFileDir; //= System.getProperty("user.dir")+"/output/"; - private static boolean isHtml = false; - private static boolean isSvg = false; + + static Options getCommandLineOptions() { + Options options = new Options(); + Option opt1 = new Option("s", "svg", false, "Generate SVG diagrams"); + options.addOption(opt1); + Option opt2 = new Option("h", "html", false, "Generate HTML documents"); + options.addOption(opt2); + + Option opt3 = new Option("o", "output", true, "Output directory"); + options.addOption(opt3); + + return options; + } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { + CommandLineParser parser = new PosixParser(); + Options options = getCommandLineOptions(); + CommandLine cli = null; + boolean help = false; + try { + cli = parser.parse(options, args); + if (cli.getArgList().size() == 0) { + help = true; + } + } catch (ParseException e) { + System.out.println(e); + help = true; + } - if (args.length == 0) { - - String help = - "" + "----------Apache Tuscany Composite Diagram Generator----------\n" - + "\n" - + "Error: You have not specified an input \"Composite XML\" file.\n" - + "\n" - + "-----------------------------Help-----------------------------\n" - + "You have 3 options:\n" - + "---1) Default: Specify \"Composite XML\" file paths separated \n" - + "--- by a space, with no arguments. This will" - + "--- generate SVG and HTML output.\n" - + "--- Eg: java -jar tuscany-composite-diagram-0.0.1.jar \n" - + "--- //a//aa//a.composite //b//bb//b.composite \n" - + "---\n" - + "---2) SVG Only: Specify \"Composite XML\" file paths separated\n" - + "--- by a space, with '-svg' argument.\n" - + "--- Eg: java -jar tuscany-composite-diagram-0.0.1.jar -svg\n" - + "--- //a//aa//a.composite //b//bb//b.composite \n" - + "---\n" - + "---3) HTML Only: Specify \"Composite XML\" file paths separated\n" - + "--- by a space, with '-html' argument.\n" - + "--- Eg: java -jar tuscany-composite-diagram-0.0.1.jar -html\n" - + "--- //a//aa//a.composite //b//bb//b.composite \n" - + "---\n"; - System.out.println(help); - } else { + // Print out the options and quit + if (help) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("<-h> <-s> <-o outputDirectory> file1 file2 ...: ", options); + return; + } - if (args[0].equalsIgnoreCase("-svg")) { - isSvg = true; - args[0] = null; - } else if (args[0].equalsIgnoreCase("-html")) { - isHtml = true; - args[0] = null; - } else { - isSvg = true; - isHtml = true; - } + boolean isHtml = false; + boolean isSvg = false; + isSvg = cli.hasOption('s'); + isHtml = cli.hasOption('h'); - for (String str : args) { + if (!isSvg && !isHtml) { + isSvg = true; + isHtml = true; + } - if (str != null) { + String outFileDir; - File in = new File(str); - outFileDir = in.getParent() + "//"; - XMLReader reader = new XMLReader(); - Document doc = reader.parseXMLFile(str);//composite1.xml composite_with_compositeService.xml"); - EntityBuilder eb = new EntityBuilder(doc); - CompositeEntity comp = eb.buildCompositeEntity(); + if (cli.hasOption('o')) { + outFileDir = cli.getOptionValue('o'); + } else { + outFileDir = "."; + } + File dir = new File(outFileDir); + String[] compositeFiles = cli.getArgs(); + generate(dir, isSvg, isHtml, compositeFiles); + } + + public static void generate(File dir, boolean isSvg, boolean isHtml, String... compositeFiles) throws Exception { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + for (String str : compositeFiles) { + + if (str != null) { - DiagramGenerator dg = new DiagramGenerator(comp, isHtml); - Document svg = dg.buildSVGDocument(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(str); - String svgContent = extractSvg(svg); + EntityBuilder eb = new EntityBuilder(doc); + CompositeEntity comp = eb.buildCompositeEntity(); - if (isSvg) { - writeSVG(svgContent, comp); - } + DiagramGenerator dg = new DiagramGenerator(comp, isHtml); + Document svg = dg.buildSVGDocument(); - if (isHtml) { - writeHTML(svgContent, comp); - } + String svgContent = extractSvg(svg); + if (isSvg) { + writeSVG(dir, svgContent, comp); } + + if (isHtml) { + writeHTML(dir, svgContent, comp); + } + } } } - private static void writeHTML(String svg, CompositeEntity comp) throws Exception { + private static void writeHTML(File dir, String svg, CompositeEntity comp) throws Exception { - File htmlFile = new File(outFileDir + comp.getName() + comp.getFileNameSuffix() + ".html"); + File htmlFile = new File(dir, comp.getName() + comp.getFileNameSuffix() + ".html"); HTMLWrapper html = new HTMLWrapper(svg, comp.getName(), htmlFile); String content = html.buildHTML(); @@ -126,16 +147,14 @@ public class Main { } - private static void writeSVG(String svg, CompositeEntity comp) throws Exception { + private static File writeSVG(File dir, String svg, CompositeEntity comp) throws Exception { String svgFileName = comp.getName() + comp.getFileNameSuffix() + ".svg"; - File svgFile = new File(outFileDir + svgFileName); - XMLWriter writer = new XMLWriter(); - writer.fileWriter(svg, svgFile); - - System.err.println("--------------SVG Output for " + comp.getName() + "--------------\n"); - System.out.println(svg); - System.err.println("-------------------------------------------------------------\n"); + File svgFile = new File(dir, svgFileName); + FileWriter fw = new FileWriter(svgFile); + fw.write(svg); + fw.close(); + return svgFile; } private static String extractSvg(Document svg) throws Exception { |