/* * 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.main; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.apache.batik.dom.svg.SVGDOMImplementation; import org.apache.tuscany.sca.impl.artifacts.Component; import org.apache.tuscany.sca.impl.artifacts.Composite; import org.apache.tuscany.sca.impl.artifacts.Property; import org.apache.tuscany.sca.impl.artifacts.Reference; import org.apache.tuscany.sca.impl.artifacts.Service; import org.apache.tuscany.sca.impl.artifacts.Text; import org.apache.tuscany.sca.impl.artifacts.Wire; public class SVGDiagramGenerator { private static final File outFile = new File(System.getProperty("user.dir") + "/output/test.svg"); /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { fileWriter(buildSVGDocument()); } public static Document buildSVGDocument(){ DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; Document doc = impl.createDocument(svgNS, "svg", null); // Get the root element (the 'svg' element). Element svgRoot = doc.getDocumentElement(); Composite composite = new Composite(); Component comp1 = new Component(); Component comp2 = new Component(); Component comp3 = new Component(); Property prop1 = new Property(); Property prop2 = new Property(); Service ser1 = new Service(); Service ser2 = new Service(); Service ser3 = new Service(); Reference ref1 = new Reference(); Reference ref2 = new Reference(); Wire edge1 = new Wire(); Wire edge2 = new Wire(); Element rectangle1 = comp1.addElement(doc, svgNS, 50, 20, 130, 200); Element text1 = Text.addTextElement(doc, svgNS, 150, 90, "SCA Component"); Element property1 = prop1.addElement(doc, svgNS, 120, 10, 20); Element property2 = prop2.addElement(doc, svgNS, 160, 10, 20); Element polygon1 = ser1.addElement(doc, svgNS, 50, 85, 30); Element polygon2 = ref1.addElement(doc, svgNS, 270, 60, 30); Element polygon3 = ref2.addElement(doc, svgNS, 270, 110, 30); //Element wire1 = edge1.addElement(doc, svgNS, 270, 60, 390, 85); Element rectangle2 = comp2.addElement(doc, svgNS, 400, 20, 130, 200); Element text2 = Text.addTextElement(doc, svgNS, 500, 90, "SCA Component"); Element polygon4 = ser2.addElement(doc, svgNS, 400, 90, 30); //Element polygon3 = Reference.addReferenceElement(doc, svgNS, 600, 105); Element rectangle3 = comp3.addElement(doc, svgNS, 400, 200, 130, 200); Element text3 = Text.addTextElement(doc, svgNS, 500, 270, "SCA Component"); Element polygon5 = ser3.addElement(doc, svgNS, 400, 270, 30); Element wire1 = edge1.addElement(doc, svgNS, ref1, ser2); Element wire2 = edge2.addElement(doc, svgNS, ref2, ser3); //Element wire2 = Wire.addWireElement(doc, svgNS, 270, 110, 390, 265); // Set the width and height attributes on the root 'svg' element. svgRoot.setAttributeNS(null, "width", "650"); svgRoot.setAttributeNS(null, "height", "450"); Element composi = composite.addElement(doc, svgNS, 0, 0, 350, 650); Element text4 = Text.addTextElement(doc, svgNS, 75, 320, "Composite"); svgRoot.appendChild(composi); svgRoot.appendChild(text4); // Attach the rectangle to the root 'svg' element. svgRoot.appendChild(rectangle1); svgRoot.appendChild(text1); svgRoot.appendChild(property1); svgRoot.appendChild(property2); svgRoot.appendChild(polygon1); svgRoot.appendChild(polygon2); svgRoot.appendChild(polygon3); svgRoot.appendChild(rectangle2); svgRoot.appendChild(text2); svgRoot.appendChild(polygon4); //svgRoot.appendChild(polygon3); svgRoot.appendChild(rectangle3); svgRoot.appendChild(text3); svgRoot.appendChild(polygon5); svgRoot.appendChild(wire1); svgRoot.appendChild(wire2); //svgRoot.setAttributeNS(null, "fill", "#CAE1FF"); //System.out.println(doc.getDocumentElement().getAttribute("width")); return doc; } public static void fileWriter(Document doc) throws Exception{ FileWriter fileWriter = new FileWriter(outFile); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(bufferedWriter); transformer.transform(source, result); } }