From 197b8082810e456fbc79fbc9057868a8996a3192 Mon Sep 17 00:00:00 2001 From: rfeng Date: Thu, 8 Sep 2011 00:45:19 +0000 Subject: Further clean-up of the code git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1166477 13f79535-47bb-0310-9956-ffa450edef68 --- .../CompositeDiagramGeneratorUsingBatik/pom.xml | 12 +- .../sca/diagram/generator/DiagramGenerator.java | 34 ++--- .../apache/tuscany/sca/diagram/io/XMLReader.java | 42 ------ .../apache/tuscany/sca/diagram/io/XMLWriter.java | 42 ------ .../org/apache/tuscany/sca/diagram/main/Main.java | 157 ++++++++++++--------- .../diagram/layout/DiagramGeneratorTestCase.java | 33 +++++ .../CompositeDiagramShellPlugin/pom.xml | 149 +++++++++---------- .../sca/diagram/shell/DrawShellCommand.java | 8 +- 8 files changed, 231 insertions(+), 246 deletions(-) delete mode 100755 collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/io/XMLReader.java delete mode 100755 collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/io/XMLWriter.java create mode 100644 collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/test/java/org/apache/tuscany/sca/diagram/layout/DiagramGeneratorTestCase.java (limited to 'collaboration') diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/pom.xml b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/pom.xml index 9882f288ed..e38c9badb6 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/pom.xml +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/pom.xml @@ -21,10 +21,15 @@ 4.0.0 org.apache.tuscany.sca tuscany-composite-diagram - 0.0.1 + 2.0-SNAPSHOT Apache Tuscany SCA Composite Diagram Generator Application + + commons-cli + commons-cli + 1.2 + org.apache.xmlgraphics batik-anim @@ -110,11 +115,12 @@ install ${basedir}/target - ${artifactId}-${version} + ${project.artifactId}-${project.version} org.apache.maven.plugins maven-compiler-plugin + 2.3.2 1.5 1.5 @@ -122,7 +128,9 @@ + org.apache.maven.plugins maven-dependency-plugin + 2.1 ${project.artifactId}-fetch-deps diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/generator/DiagramGenerator.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/generator/DiagramGenerator.java index 511bced0e8..5d4a40fcfb 100755 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/generator/DiagramGenerator.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/generator/DiagramGenerator.java @@ -139,10 +139,11 @@ public class DiagramGenerator { private void addReferencePromotion() { - for (Iterator it = comp.getPromoteAReference().entrySet().iterator(); it.hasNext();) { - Entry entry = (Entry)it.next(); - String compositeRef = (String)entry.getKey(); - ArrayList componentRef = (ArrayList)entry.getValue(); + for (Iterator>> it = comp.getPromoteAReference().entrySet().iterator(); it + .hasNext();) { + Entry> entry = it.next(); + String compositeRef = entry.getKey(); + ArrayList componentRef = entry.getValue(); ReferenceArtifact r1 = getRef(compositeRef); @@ -160,10 +161,10 @@ public class DiagramGenerator { private void addServicePromotion() { - for (Iterator it = comp.getPromoteAService().entrySet().iterator(); it.hasNext();) { - Entry entry = (Entry)it.next(); - String compositeSer = (String)entry.getKey(); - String componentSer = (String)entry.getValue(); + for (Iterator> it = comp.getPromoteAService().entrySet().iterator(); it.hasNext();) { + Entry entry = it.next(); + String compositeSer = entry.getKey(); + String componentSer = entry.getValue(); ServiceArtifact s1 = getSer(compositeSer); ServiceArtifact s2 = getSer(componentSer); @@ -222,10 +223,11 @@ public class DiagramGenerator { for (Entity ent : comp.getComponentList()) { //if(ent instanceof ComponentEntity){ - for (Iterator it = ((ComponentEntity)ent).getReferenceToServiceMap().entrySet().iterator(); it.hasNext();) { - Entry entry = (Entry)it.next(); - String ref = (String)entry.getKey(); - String ser = (String)entry.getValue(); + for (Iterator> it = + ((ComponentEntity)ent).getReferenceToServiceMap().entrySet().iterator(); it.hasNext();) { + Entry entry = it.next(); + String ref = entry.getKey(); + String ser = entry.getValue(); ReferenceArtifact r = getRef(ref); ServiceArtifact s = getSer(ser); @@ -415,11 +417,11 @@ public class DiagramGenerator { sers.add(i, ""); } - for (Iterator it = e.getReferenceToServiceMap().entrySet().iterator(); it.hasNext();) { + for (Iterator> it = e.getReferenceToServiceMap().entrySet().iterator(); it.hasNext();) { - Entry entry = (Entry)it.next(); - String ref = (String)entry.getKey(); - String ser = (String)entry.getValue(); + Entry entry = it.next(); + String ref = entry.getKey(); + String ser = entry.getValue(); //System.out.println("---------"+ref); int idx = refs.indexOf(ref); //System.out.println("---------"+sers.get(idx)); diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/io/XMLReader.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/io/XMLReader.java deleted file mode 100755 index 087c6370f8..0000000000 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/io/XMLReader.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.diagram.io; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import org.w3c.dom.Document; - -public class XMLReader { - - //get the factory - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - private Document dom; - - public Document parseXMLFile(String file) throws Exception { - //Using factory get an instance of document builder - DocumentBuilder db = dbf.newDocumentBuilder(); - - //parse using builder to get DOM representation of the XML file - dom = db.parse(file); - - return dom; - } -} diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/io/XMLWriter.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/io/XMLWriter.java deleted file mode 100755 index b1d87e1381..0000000000 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/io/XMLWriter.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.diagram.io; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; - -public class XMLWriter { - - public void fileWriter(String content, File outFile) throws Exception { - - FileWriter fileWriter = new FileWriter(outFile); - BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); - bufferedWriter.write(content); - bufferedWriter.close(); - - // TransformerFactory transformerFactory = TransformerFactory.newInstance(); - // Transformer transformer = transformerFactory.newTransformer(); - // DOMSource source = new DOMSource(doc); - // StreamResult result = new StreamResult(bufferedWriter); - // transformer.transform(source, result); - - } -} 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 { diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/test/java/org/apache/tuscany/sca/diagram/layout/DiagramGeneratorTestCase.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/test/java/org/apache/tuscany/sca/diagram/layout/DiagramGeneratorTestCase.java new file mode 100644 index 0000000000..aee42dbfc0 --- /dev/null +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/test/java/org/apache/tuscany/sca/diagram/layout/DiagramGeneratorTestCase.java @@ -0,0 +1,33 @@ +/********************************************************************** + * NAME: + * DiagramGeneratorTestCase + * + * AUTHOR: + * rfeng + * + * DESCRIPTION: + * + * + * Copyright (c) Shutterfly, Inc. 2010. All Rights reserved. + **********************************************************************/ + +package org.apache.tuscany.sca.diagram.layout; + +import java.io.File; + +import org.apache.tuscany.sca.diagram.main.Main; +import org.junit.Test; + +public class DiagramGeneratorTestCase { + + @Test + public final void test() throws Exception { + for (File xml : new File("input").listFiles()) { + if (xml.getName().endsWith(".xml")) { + System.out.println(xml); + Main.generate(new File("target"), true, false, xml.toString()); + } + } + } + +} diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramShellPlugin/pom.xml b/collaboration/GSoC-2011-Nirmal/CompositeDiagramShellPlugin/pom.xml index 47ee1c9ddd..0c990f65a4 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramShellPlugin/pom.xml +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramShellPlugin/pom.xml @@ -1,21 +1,28 @@ - + - 4.0.0 - org.apache.tuscany.sca - tuscany-composite-diagram-shell-plugin - 0.0.1 - Apache Tuscany SCA Composite Diagram Generator Shell Plugin + 4.0.0 + org.apache.tuscany.sca + tuscany-composite-diagram-shell-plugin + 2.0-SNAPSHOT + Apache Tuscany SCA Composite Diagram Generator Shell Plugin @@ -33,71 +40,71 @@ - + - - org.apache.tuscany.sca - tuscany-composite-diagram - 0.0.1 - + + org.apache.tuscany.sca + tuscany-composite-diagram + 2.0-SNAPSHOT + - - org.apache.xmlgraphics - batik-swing - 1.7 - + + org.apache.xmlgraphics + batik-swing + 1.7 + - - org.apache.tuscany.sca - tuscany-base-runtime - 2.0-Beta3 - + + org.apache.tuscany.sca + tuscany-base-runtime + 2.0-SNAPSHOT + - - jline - jline - 0.9.95-huynhjl - + + jline + jline + 0.9.95-huynhjl + - - junit - junit - 4.5 - test - + + junit + junit + 4.5 + test + - + - - install - ${basedir}/target - ${artifactId}-${version} - + + install + ${basedir}/target + ${artifactId}-${version} + - - org.apache.maven.plugins - maven-compiler-plugin - - 1.5 - 1.5 - - + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + - - org.apache.tuscany.sca - tuscany-maven-plugin - 2.0-Beta3 - - - org.apache.tuscany.sca - tuscany-composite-diagram-shell-plugin - 0.0.1 - - - + + org.apache.tuscany.sca + tuscany-maven-plugin + 2.0-SNAPSHOT + + + org.apache.tuscany.sca + tuscany-composite-diagram-shell-plugin + 2.0-SNAPSHOT + + + - + - + diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramShellPlugin/src/main/java/org/apache/tuscany/sca/diagram/shell/DrawShellCommand.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramShellPlugin/src/main/java/org/apache/tuscany/sca/diagram/shell/DrawShellCommand.java index 2be3d1dedb..61b7fa0935 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramShellPlugin/src/main/java/org/apache/tuscany/sca/diagram/shell/DrawShellCommand.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramShellPlugin/src/main/java/org/apache/tuscany/sca/diagram/shell/DrawShellCommand.java @@ -23,9 +23,9 @@ import jline.Completor; import jline.NullCompletor; import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.impl.diagram.DiagramGenerator; -import org.apache.tuscany.sca.impl.layout.CompositeEntity; -import org.apache.tuscany.sca.impl.layout.TuscanyCompositeEntityBuilder; +import org.apache.tuscany.sca.diagram.generator.DiagramGenerator; +import org.apache.tuscany.sca.diagram.layout.CompositeEntity; +import org.apache.tuscany.sca.diagram.layout.TuscanyCompositeEntityBuilder; import org.apache.tuscany.sca.shell.Command; import org.apache.tuscany.sca.shell.Shell; import org.apache.tuscany.sca.shell.jline.CompositeURICompletor; @@ -69,7 +69,7 @@ public class DrawShellCommand implements Command { } TuscanyCompositeEntityBuilder eb = new TuscanyCompositeEntityBuilder(composite); CompositeEntity comp = eb.buildCompositeEntity(); - DiagramGenerator dg = new DiagramGenerator(comp); + DiagramGenerator dg = new DiagramGenerator(comp, false); Document svg = dg.buildSVGDocument(); new SVGViewer(svg); -- cgit v1.2.3