]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
index f75ed1d157012761e1042846a278aa133ff1324f..4f1c2f8e18b3f063a02f293da8ccd9615f6daf0f 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.graphviz.internal.xdot;\r
-\r
-import java.io.IOException;\r
-import java.io.Reader;\r
-import java.io.StringReader;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Map;\r
-\r
-import org.simantics.graphviz.Edge;\r
-import org.simantics.graphviz.Graph;\r
-import org.simantics.graphviz.IGraph;\r
-import org.simantics.graphviz.IGraphPart;\r
-\r
-public class DrawCommandParser {\r
-\r
-       public static double readNumber(Reader s) throws IOException {\r
-               StringBuilder b = new StringBuilder();\r
-               while( true ) {\r
-                       int c = s.read();\r
-                       if(c == ' ' || c <= 0)\r
-                               break;\r
-                       b.append((char)c);\r
-               }\r
-               return Double.parseDouble(b.toString());\r
-       }\r
-       \r
-       public static int readInteger(Reader s) throws IOException {\r
-               StringBuilder b = new StringBuilder();\r
-               while( true ) {\r
-                       int c = s.read();\r
-                       if(c == ' ' || c <= 0)\r
-                               break;\r
-                       b.append((char)c);\r
-               }\r
-               return Integer.parseInt(b.toString());\r
-       }\r
-       \r
-       public static double[] readPoints(Reader s) throws IOException {\r
-               int n = readInteger(s);\r
-               double[] points = new double[n*2];\r
-               for(int i=0;i<n*2;++i)\r
-                       points[i] = readNumber(s);\r
-               return points;\r
-       }\r
-       \r
-       public static String readString(Reader s) throws IOException {\r
-               int n = readInteger(s);\r
-               s.read();\r
-               char[] chars = new char[n];\r
-               for(int i=0;i<n;++i) {\r
-                       char c = (char)s.read();\r
-                       if(c == '\\')\r
-                               c = (char)s.read();\r
-                       chars[i] = c;\r
-               }\r
-               s.read();\r
-               return new String(chars);\r
-       }\r
-       \r
-       public static void parse(Collection<DrawCommand> commands, Reader s) throws IOException {\r
-           boolean hadSetStyle = false;\r
-               while(true) {\r
-                       int commandChar = s.read();\r
-                       s.read();\r
-                       if(commandChar <= 0) {\r
-                           if(hadSetStyle)\r
-                               commands.add(new SetStyle("solid"));\r
-                               return;\r
-                       }\r
-                       switch(commandChar) {\r
-                       case 'E': {\r
-                               double x = readNumber(s);\r
-                               double y = readNumber(s);\r
-                               double w = readNumber(s);\r
-                               double h = readNumber(s);\r
-                               commands.add(new FilledShape(Shapes.createEllipse(x, y, w, h)));\r
-                               break;\r
-                       }\r
-                       case 'e': {\r
-                               double x = readNumber(s);\r
-                               double y = readNumber(s);\r
-                               double w = readNumber(s);\r
-                               double h = readNumber(s);\r
-                               commands.add(new UnfilledShape(Shapes.createEllipse(x, y, w, h)));\r
-                               break;\r
-                       }\r
-                       case 'P': {\r
-                               commands.add(new FilledShape(Shapes.createPolygon(readPoints(s))));\r
-                               break;\r
-                       }\r
-                       case 'p':\r
-                               commands.add(new UnfilledShape(Shapes.createPolygon(readPoints(s))));\r
-                               break;\r
-                       case 'L':\r
-                               commands.add(new UnfilledShape(Shapes.createPolyline(readPoints(s))));\r
-                               break;\r
-                       case 'B':                               \r
-                               commands.add(new UnfilledShape(Shapes.createCubicSegments(readPoints(s))));\r
-                               break;\r
-                       case 'b':\r
-                               commands.add(new FilledShape(Shapes.createCubicSegments(readPoints(s))));\r
-                               break;\r
-                       case 'T': {\r
-                               double x = readNumber(s);\r
-                               double y = readNumber(s);\r
-                               int j = readInteger(s);\r
-                               double w = readNumber(s);\r
-                               String text = readString(s);\r
-                               commands.add(new Text(x, y, j, w, text));\r
-                               break;\r
-                       }\r
-                       case 'C':\r
-                               commands.add(new SetFillColor(readString(s)));\r
-                               break;\r
-                       case 'c':\r
-                               commands.add(new SetPenColor(readString(s)));\r
-                               break;\r
-                       case 'F': {\r
-                               double points = readNumber(s);\r
-                               String font = readString(s);\r
-                               commands.add(new SetFont(points, font));\r
-                               break;\r
-                       }\r
-                       case 'S':\r
-                           hadSetStyle = true;\r
-                               commands.add(new SetStyle(readString(s)));\r
-                               break;\r
-                       case 'I': {\r
-                               double x = readNumber(s);\r
-                               double y = readNumber(s);\r
-                               double w = readNumber(s);\r
-                               double h = readNumber(s);\r
-                               String file = readString(s);\r
-                               commands.add(new DrawImage(x, y, w, h, file));\r
-                               break;\r
-                       }\r
-                       }\r
-               }\r
-       }\r
-       \r
-       public static void parse(Collection<DrawCommand> commands, String s) {\r
-               if(s != null)\r
-                       try {\r
-                               s = s.replace("\\\r\n", "");\r
-                               parse(commands, new StringReader(s));\r
-                       } catch(IOException e) {\r
-                               e.printStackTrace();\r
-                       }\r
-       }\r
-\r
-       private static void parse(Collection<DrawCommand> commands, IGraph graph) {\r
-           for(IGraphPart part : graph.getParts()) {\r
-            parse(commands, part.get("_draw_"));\r
-            parse(commands, part.get("_ldraw_"));\r
-            if(part instanceof Edge) {\r
-                parse(commands, part.get("_hdraw_"));\r
-                parse(commands, part.get("_tdraw_"));\r
-                parse(commands, part.get("_hldraw_"));\r
-                parse(commands, part.get("_tldraw_"));\r
-            }\r
-            else if(part instanceof IGraph) {\r
-                parse(commands, (IGraph)part);\r
-            }\r
-        }\r
-       }\r
-       \r
-       private static void parse(Collection<DrawCommand> commands, Map<IGraphPart,Collection<DrawCommand>> partCommands, IGraph graph) {\r
-           for(IGraphPart part : graph.getParts()) {\r
-               Collection<DrawCommand> pCommands = new ArrayList<DrawCommand>();\r
-               parse(pCommands, part.get("_draw_"));\r
-            parse(pCommands, part.get("_ldraw_"));\r
-            if(part instanceof Edge) {\r
-                parse(pCommands, part.get("_hdraw_"));\r
-                parse(pCommands, part.get("_tdraw_"));\r
-                parse(pCommands, part.get("_hldraw_"));\r
-                parse(pCommands, part.get("_tldraw_"));\r
-            } else if(part instanceof IGraph) {\r
-                parse(commands, partCommands,(IGraph)part);\r
-            }\r
-            partCommands.put(part, pCommands);\r
-            commands.addAll(pCommands);\r
-            \r
-        }\r
-       }\r
-       \r
-       public static DrawCommand[] parse(Graph graph) {\r
-               Collection<DrawCommand> commands = new ArrayList<DrawCommand>();\r
-               parse(commands, graph);\r
-               return commands.toArray(new DrawCommand[commands.size()]);\r
-       }\r
-       \r
-       public static void parse(Graph graph, Collection<DrawCommand> commands, Map<IGraphPart,Collection<DrawCommand>> partCommands) {\r
-               parse(commands,partCommands, graph);\r
-               \r
-       }\r
-       \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.graphviz.internal.xdot;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+
+import org.simantics.graphviz.Edge;
+import org.simantics.graphviz.Graph;
+import org.simantics.graphviz.IGraph;
+import org.simantics.graphviz.IGraphPart;
+
+public class DrawCommandParser {
+
+       public static double readNumber(Reader s) throws IOException {
+               StringBuilder b = new StringBuilder();
+               while( true ) {
+                       int c = s.read();
+                       if(c == ' ' || c <= 0)
+                               break;
+                       b.append((char)c);
+               }
+               return Double.parseDouble(b.toString());
+       }
+       
+       public static int readInteger(Reader s) throws IOException {
+               StringBuilder b = new StringBuilder();
+               while( true ) {
+                       int c = s.read();
+                       if(c == ' ' || c <= 0)
+                               break;
+                       b.append((char)c);
+               }
+               return Integer.parseInt(b.toString());
+       }
+       
+       public static double[] readPoints(Reader s) throws IOException {
+               int n = readInteger(s);
+               double[] points = new double[n*2];
+               for(int i=0;i<n*2;++i)
+                       points[i] = readNumber(s);
+               return points;
+       }
+       
+       public static String readString(Reader s) throws IOException {
+               int n = readInteger(s);
+               s.read();
+               char[] chars = new char[n];
+               for(int i=0;i<n;++i) {
+                       char c = (char)s.read();
+                       if(c == '\\')
+                               c = (char)s.read();
+                       chars[i] = c;
+               }
+               s.read();
+               return new String(chars);
+       }
+       
+       public static void parse(Collection<DrawCommand> commands, Reader s) throws IOException {
+           boolean hadSetStyle = false;
+               while(true) {
+                       int commandChar = s.read();
+                       s.read();
+                       if(commandChar <= 0) {
+                           if(hadSetStyle)
+                               commands.add(new SetStyle("solid"));
+                               return;
+                       }
+                       switch(commandChar) {
+                       case 'E': {
+                               double x = readNumber(s);
+                               double y = readNumber(s);
+                               double w = readNumber(s);
+                               double h = readNumber(s);
+                               commands.add(new FilledShape(Shapes.createEllipse(x, y, w, h)));
+                               break;
+                       }
+                       case 'e': {
+                               double x = readNumber(s);
+                               double y = readNumber(s);
+                               double w = readNumber(s);
+                               double h = readNumber(s);
+                               commands.add(new UnfilledShape(Shapes.createEllipse(x, y, w, h)));
+                               break;
+                       }
+                       case 'P': {
+                               commands.add(new FilledShape(Shapes.createPolygon(readPoints(s))));
+                               break;
+                       }
+                       case 'p':
+                               commands.add(new UnfilledShape(Shapes.createPolygon(readPoints(s))));
+                               break;
+                       case 'L':
+                               commands.add(new UnfilledShape(Shapes.createPolyline(readPoints(s))));
+                               break;
+                       case 'B':                               
+                               commands.add(new UnfilledShape(Shapes.createCubicSegments(readPoints(s))));
+                               break;
+                       case 'b':
+                               commands.add(new FilledShape(Shapes.createCubicSegments(readPoints(s))));
+                               break;
+                       case 'T': {
+                               double x = readNumber(s);
+                               double y = readNumber(s);
+                               int j = readInteger(s);
+                               double w = readNumber(s);
+                               String text = readString(s);
+                               commands.add(new Text(x, y, j, w, text));
+                               break;
+                       }
+                       case 'C':
+                               commands.add(new SetFillColor(readString(s)));
+                               break;
+                       case 'c':
+                               commands.add(new SetPenColor(readString(s)));
+                               break;
+                       case 'F': {
+                               double points = readNumber(s);
+                               String font = readString(s);
+                               commands.add(new SetFont(points, font));
+                               break;
+                       }
+                       case 'S':
+                           hadSetStyle = true;
+                               commands.add(new SetStyle(readString(s)));
+                               break;
+                       case 'I': {
+                               double x = readNumber(s);
+                               double y = readNumber(s);
+                               double w = readNumber(s);
+                               double h = readNumber(s);
+                               String file = readString(s);
+                               commands.add(new DrawImage(x, y, w, h, file));
+                               break;
+                       }
+                       }
+               }
+       }
+       
+       public static void parse(Collection<DrawCommand> commands, String s) {
+               if(s != null)
+                       try {
+                               s = s.replace("\\\r\n", "");
+                               parse(commands, new StringReader(s));
+                       } catch(IOException e) {
+                               e.printStackTrace();
+                       }
+       }
+
+       private static void parse(Collection<DrawCommand> commands, IGraph graph) {
+           for(IGraphPart part : graph.getParts()) {
+            parse(commands, part.get("_draw_"));
+            parse(commands, part.get("_ldraw_"));
+            if(part instanceof Edge) {
+                parse(commands, part.get("_hdraw_"));
+                parse(commands, part.get("_tdraw_"));
+                parse(commands, part.get("_hldraw_"));
+                parse(commands, part.get("_tldraw_"));
+            }
+            else if(part instanceof IGraph) {
+                parse(commands, (IGraph)part);
+            }
+        }
+       }
+       
+       private static void parse(Collection<DrawCommand> commands, Map<IGraphPart,Collection<DrawCommand>> partCommands, IGraph graph) {
+           for(IGraphPart part : graph.getParts()) {
+               Collection<DrawCommand> pCommands = new ArrayList<DrawCommand>();
+               parse(pCommands, part.get("_draw_"));
+            parse(pCommands, part.get("_ldraw_"));
+            if(part instanceof Edge) {
+                parse(pCommands, part.get("_hdraw_"));
+                parse(pCommands, part.get("_tdraw_"));
+                parse(pCommands, part.get("_hldraw_"));
+                parse(pCommands, part.get("_tldraw_"));
+            } else if(part instanceof IGraph) {
+                parse(commands, partCommands,(IGraph)part);
+            }
+            partCommands.put(part, pCommands);
+            commands.addAll(pCommands);
+            
+        }
+       }
+       
+       public static DrawCommand[] parse(Graph graph) {
+               Collection<DrawCommand> commands = new ArrayList<DrawCommand>();
+               parse(commands, graph);
+               return commands.toArray(new DrawCommand[commands.size()]);
+       }
+       
+       public static void parse(Graph graph, Collection<DrawCommand> commands, Map<IGraphPart,Collection<DrawCommand>> partCommands) {
+               parse(commands,partCommands, graph);
+               
+       }
+       
+}