/* * 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.impl.diagram; import java.util.ArrayList; import java.util.Iterator; import java.util.Map.Entry; 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; import org.apache.tuscany.sca.impl.layout.Entity; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.Element; public class DiagramGenerator { Entity[] entities; int height, width; Document doc; String compositeName; String svgNS ; Element svgRoot; ArrayList refs= new ArrayList(); ArrayList sers= new ArrayList(); public DiagramGenerator(Entity[] entities, int height, int width, String compositeName) { this.entities = entities; this.height = height; this.width = width; this.compositeName = compositeName; } public Document buildSVGDocument(){ DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; doc = impl.createDocument(svgNS, "svg", null); // Get the root element (the 'svg' element). svgRoot = doc.getDocumentElement(); addComposite(); for(Entity ent: entities){ addComponent(ent); addService(ent); addReference(ent); addProperties(ent); } for(Entity ent: entities){ for(Iterator it= ent.getReferenceToServiceMap().entrySet().iterator();it.hasNext();){ Entry entry = (Entry) it.next(); String ref = (String)entry.getKey(); String ser = (String)entry.getValue(); Reference r = getRef(ref); Service s = getSer(ser); if(r != null && s != null){ addWire(r,s); } } } return doc; } private void addWire(Reference r, Service s) { Wire edge = new Wire(); Element wire = edge.addElement(doc, svgNS, r, s); svgRoot.appendChild(wire); } private Service getSer(String ser) { for(Service s: sers){ if(s.getContainerName().equals(ser) || s.getName().equals(ser)){ return s; } } return null; } private Reference getRef(String ref) { for(Reference r: refs){ if(r.getContainerName().equals(ref) || r.getName().equals(ref)){ return r; } } return null; } private void addProperties(Entity ent) { int propLen = ent.getPropLength(); int x=ent.getX()+Property.SPACING; int y= ent.getY()-propLen/2; for(String prop: ent.getProperties()){ Property pro = new Property(); Element property = pro.addElement(doc, svgNS, x, y, propLen); Element text = Text.addTextElement(doc, svgNS, x, y, prop); svgRoot.appendChild(property); svgRoot.appendChild(text); x += (propLen + Property.SPACING); pro.setName(prop); pro.setContainerName(ent.getComponentName()); } } private void addReference(Entity ent) { int refHeight = ent.getRefHeight(); int x=(ent.getX()+ent.getWidth())-(refHeight*2/3); int y=ent.getY()+Reference.SPACING; for(String ref: setRefOrder(ent)){ Reference refer= new Reference(); Element polygon = refer.addElement(doc, svgNS, x, y, refHeight); Element text = Text.addTextElement(doc, svgNS, x, y+refHeight/2, ref); svgRoot.appendChild(polygon); svgRoot.appendChild(text); y += (refHeight + Reference.SPACING); refer.setName(ref); refer.setContainerName(ent.getComponentName()); refs.add(refer); } } private String[] setRefOrder(Entity e){ ArrayList refs = e.getReferences(); ArrayList sers = new ArrayList(); String[] orderedRefs = new String[refs.size()]; for(int i=0;i