summaryrefslogtreecommitdiffstats
path: root/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/main/SVGDiagramGenerator.java
blob: bce24de749e6e6e8ffc551aab2f713dcea128719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
un * 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/reference.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, 0, 0, 130, 200);	
		Element text1 = Text.addTextElement(doc, svgNS, 100, 70, "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, 15, 15, 30);
		
		Element polygon2 = ref1.addElement(doc, svgNS, 45, 15, 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", "200");
//		svgRoot.setAttributeNS(null, "height", "130");
		
		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);
        
	}
}