1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.graphviz.internal.xdot;
14 import java.io.IOException;
15 import java.io.Reader;
16 import java.io.StringReader;
17 import java.util.ArrayList;
18 import java.util.Collection;
21 import org.simantics.graphviz.Edge;
22 import org.simantics.graphviz.Graph;
23 import org.simantics.graphviz.IGraph;
24 import org.simantics.graphviz.IGraphPart;
26 public class DrawCommandParser {
28 public static double readNumber(Reader s) throws IOException {
29 StringBuilder b = new StringBuilder();
32 if(c == ' ' || c <= 0)
36 return Double.parseDouble(b.toString());
39 public static int readInteger(Reader s) throws IOException {
40 StringBuilder b = new StringBuilder();
43 if(c == ' ' || c <= 0)
47 return Integer.parseInt(b.toString());
50 public static double[] readPoints(Reader s) throws IOException {
51 int n = readInteger(s);
52 double[] points = new double[n*2];
53 for(int i=0;i<n*2;++i)
54 points[i] = readNumber(s);
58 public static String readString(Reader s) throws IOException {
59 int n = readInteger(s);
61 char[] chars = new char[n];
62 for(int i=0;i<n;++i) {
63 char c = (char)s.read();
69 return new String(chars);
72 public static void parse(Collection<DrawCommand> commands, Reader s) throws IOException {
73 boolean hadSetStyle = false;
75 int commandChar = s.read();
77 if(commandChar <= 0) {
79 commands.add(new SetStyle("solid"));
84 double x = readNumber(s);
85 double y = readNumber(s);
86 double w = readNumber(s);
87 double h = readNumber(s);
88 commands.add(new FilledShape(Shapes.createEllipse(x, y, w, h)));
92 double x = readNumber(s);
93 double y = readNumber(s);
94 double w = readNumber(s);
95 double h = readNumber(s);
96 commands.add(new UnfilledShape(Shapes.createEllipse(x, y, w, h)));
100 commands.add(new FilledShape(Shapes.createPolygon(readPoints(s))));
104 commands.add(new UnfilledShape(Shapes.createPolygon(readPoints(s))));
107 commands.add(new UnfilledShape(Shapes.createPolyline(readPoints(s))));
110 commands.add(new UnfilledShape(Shapes.createCubicSegments(readPoints(s))));
113 commands.add(new FilledShape(Shapes.createCubicSegments(readPoints(s))));
116 double x = readNumber(s);
117 double y = readNumber(s);
118 int j = readInteger(s);
119 double w = readNumber(s);
120 String text = readString(s);
121 commands.add(new Text(x, y, j, w, text));
125 commands.add(new SetFillColor(readString(s)));
128 commands.add(new SetPenColor(readString(s)));
131 double points = readNumber(s);
132 String font = readString(s);
133 commands.add(new SetFont(points, font));
138 commands.add(new SetStyle(readString(s)));
141 double x = readNumber(s);
142 double y = readNumber(s);
143 double w = readNumber(s);
144 double h = readNumber(s);
145 String file = readString(s);
146 commands.add(new DrawImage(x, y, w, h, file));
153 public static void parse(Collection<DrawCommand> commands, String s) {
156 s = s.replace("\\\r\n", "");
157 parse(commands, new StringReader(s));
158 } catch(IOException e) {
163 private static void parse(Collection<DrawCommand> commands, IGraph graph) {
164 for(IGraphPart part : graph.getParts()) {
165 parse(commands, part.get("_draw_"));
166 parse(commands, part.get("_ldraw_"));
167 if(part instanceof Edge) {
168 parse(commands, part.get("_hdraw_"));
169 parse(commands, part.get("_tdraw_"));
170 parse(commands, part.get("_hldraw_"));
171 parse(commands, part.get("_tldraw_"));
173 else if(part instanceof IGraph) {
174 parse(commands, (IGraph)part);
179 private static void parse(Collection<DrawCommand> commands, Map<IGraphPart,Collection<DrawCommand>> partCommands, IGraph graph) {
180 for(IGraphPart part : graph.getParts()) {
181 Collection<DrawCommand> pCommands = new ArrayList<DrawCommand>();
182 parse(pCommands, part.get("_draw_"));
183 parse(pCommands, part.get("_ldraw_"));
184 if(part instanceof Edge) {
185 parse(pCommands, part.get("_hdraw_"));
186 parse(pCommands, part.get("_tdraw_"));
187 parse(pCommands, part.get("_hldraw_"));
188 parse(pCommands, part.get("_tldraw_"));
189 } else if(part instanceof IGraph) {
190 parse(commands, partCommands,(IGraph)part);
192 partCommands.put(part, pCommands);
193 commands.addAll(pCommands);
198 public static DrawCommand[] parse(Graph graph) {
199 Collection<DrawCommand> commands = new ArrayList<DrawCommand>();
200 parse(commands, graph);
201 return commands.toArray(new DrawCommand[commands.size()]);
204 public static void parse(Graph graph, Collection<DrawCommand> commands, Map<IGraphPart,Collection<DrawCommand>> partCommands) {
205 parse(commands,partCommands, graph);