]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/xdot/DrawCommandParser.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / internal / xdot / DrawCommandParser.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.graphviz.internal.xdot;
13
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;
19 import java.util.Map;
20
21 import org.simantics.graphviz.Edge;
22 import org.simantics.graphviz.Graph;
23 import org.simantics.graphviz.IGraph;
24 import org.simantics.graphviz.IGraphPart;
25
26 public class DrawCommandParser {
27
28         public static double readNumber(Reader s) throws IOException {
29                 StringBuilder b = new StringBuilder();
30                 while( true ) {
31                         int c = s.read();
32                         if(c == ' ' || c <= 0)
33                                 break;
34                         b.append((char)c);
35                 }
36                 return Double.parseDouble(b.toString());
37         }
38         
39         public static int readInteger(Reader s) throws IOException {
40                 StringBuilder b = new StringBuilder();
41                 while( true ) {
42                         int c = s.read();
43                         if(c == ' ' || c <= 0)
44                                 break;
45                         b.append((char)c);
46                 }
47                 return Integer.parseInt(b.toString());
48         }
49         
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);
55                 return points;
56         }
57         
58         public static String readString(Reader s) throws IOException {
59                 int n = readInteger(s);
60                 s.read();
61                 char[] chars = new char[n];
62                 for(int i=0;i<n;++i) {
63                         char c = (char)s.read();
64                         if(c == '\\')
65                                 c = (char)s.read();
66                         chars[i] = c;
67                 }
68                 s.read();
69                 return new String(chars);
70         }
71         
72         public static void parse(Collection<DrawCommand> commands, Reader s) throws IOException {
73             boolean hadSetStyle = false;
74                 while(true) {
75                         int commandChar = s.read();
76                         s.read();
77                         if(commandChar <= 0) {
78                             if(hadSetStyle)
79                                 commands.add(new SetStyle("solid"));
80                                 return;
81                         }
82                         switch(commandChar) {
83                         case 'E': {
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)));
89                                 break;
90                         }
91                         case 'e': {
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)));
97                                 break;
98                         }
99                         case 'P': {
100                                 commands.add(new FilledShape(Shapes.createPolygon(readPoints(s))));
101                                 break;
102                         }
103                         case 'p':
104                                 commands.add(new UnfilledShape(Shapes.createPolygon(readPoints(s))));
105                                 break;
106                         case 'L':
107                                 commands.add(new UnfilledShape(Shapes.createPolyline(readPoints(s))));
108                                 break;
109                         case 'B':                               
110                                 commands.add(new UnfilledShape(Shapes.createCubicSegments(readPoints(s))));
111                                 break;
112                         case 'b':
113                                 commands.add(new FilledShape(Shapes.createCubicSegments(readPoints(s))));
114                                 break;
115                         case 'T': {
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));
122                                 break;
123                         }
124                         case 'C':
125                                 commands.add(new SetFillColor(readString(s)));
126                                 break;
127                         case 'c':
128                                 commands.add(new SetPenColor(readString(s)));
129                                 break;
130                         case 'F': {
131                                 double points = readNumber(s);
132                                 String font = readString(s);
133                                 commands.add(new SetFont(points, font));
134                                 break;
135                         }
136                         case 'S':
137                             hadSetStyle = true;
138                                 commands.add(new SetStyle(readString(s)));
139                                 break;
140                         case 'I': {
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));
147                                 break;
148                         }
149                         }
150                 }
151         }
152         
153         public static void parse(Collection<DrawCommand> commands, String s) {
154                 if(s != null)
155                         try {
156                                 s = s.replace("\\\r\n", "");
157                                 parse(commands, new StringReader(s));
158                         } catch(IOException e) {
159                                 e.printStackTrace();
160                         }
161         }
162
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_"));
172             }
173             else if(part instanceof IGraph) {
174                 parse(commands, (IGraph)part);
175             }
176         }
177         }
178         
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);
191             }
192             partCommands.put(part, pCommands);
193             commands.addAll(pCommands);
194             
195         }
196         }
197         
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()]);
202         }
203         
204         public static void parse(Graph graph, Collection<DrawCommand> commands, Map<IGraphPart,Collection<DrawCommand>> partCommands) {
205                 parse(commands,partCommands, graph);
206                 
207         }
208         
209 }