1
14 package demo.io;
15
16 import y.io.GIFIOHandler;
17 import y.io.GMLIOHandler;
18 import y.io.GraphMLIOHandler;
19 import y.io.IOHandler;
20 import y.io.ImageOutputHandler;
21 import y.io.JPGIOHandler;
22 import y.io.YGFIOHandler;
23 import y.io.ZipGraphMLIOHandler;
24 import y.util.D;
25 import y.view.Graph2D;
26 import y.view.Graph2DView;
27 import y.view.hierarchy.HierarchyManager;
28
29 import java.awt.Dimension;
30 import java.awt.Rectangle;
31 import java.io.IOException;
32 import java.util.Collection;
33 import java.util.Iterator;
34 import java.util.LinkedList;
35
36
37
51 public class GraphFormatConverter {
52 private Collection ioHandlers;
53 private int outputWidth = -1;
54 private int outputHeight = -1;
55 private String inFile;
56 private String outFile;
57
58
59 private static void usage(String msg) {
60 System.err.println(msg + "\n\n" +
61 "Usage: java demo.io.GraphFormatConverter -in <infile> -out <outfile> [options]\n" +
62 "Usage: where the format of infile is GraphML, ZIPGraphML, YGF or GML \n" +
63 "Usage: and the format of outfile in in GraphML, ZIPGraphML, YGF, GML, JPG or GIF.\n" +
64 "Usage: SVG/SVGZ output needs the ySVG extension package.\n" +
65 "Usage: EMF, PDF, EPS and SWF output needs the yExport extension package.\n" +
66 "Usage: File formats are determined by the file name extensions.\n" +
67 "Usage: Additional options which work for some output formats:\n" +
68 "Usage: -width <w> the width of the output format\n" +
69 "Usage: -height<h> the height of the output format\n" +
70 "Usage: If neither option is specified, a value of 1024\n" +
71 "Usage: is used for both dimensions\n");
72 System.exit(1);
73 }
74
75 private static void error(String msg) {
76 System.err.println(msg);
77 System.exit(1);
78 }
79
80
84 public GraphFormatConverter() {
85 ioHandlers = new LinkedList();
86 ioHandlers.add(new GraphMLIOHandler());
87 ioHandlers.add(new ZipGraphMLIOHandler());
88 ioHandlers.add(new YGFIOHandler());
89 ioHandlers.add(new GMLIOHandler());
90 ioHandlers.add(new GIFIOHandler());
91 ioHandlers.add(new JPGIOHandler());
92 try { ioHandlers.add((IOHandler) Class.forName("yext.svg.io.SVGIOHandler").newInstance());
94 ioHandlers.add((IOHandler) Class.forName("yext.svg.io.SVGZIOHandler").newInstance());
95 }
96 catch (ClassNotFoundException cnfex) {
97 }
99 catch (Exception ex) {
100 D.trace(ex);
101 }
102
103 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.PDFOutputHandler").newInstance());
105 } catch (ClassNotFoundException cnfex) {
106 } catch (Exception ex) {
108 D.trace(ex);
109 }
110
111 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.SWFOutputHandler").newInstance());
113 } catch (ClassNotFoundException cnfex) {
114 } catch (Exception ex) {
116 D.trace(ex);
117 }
118
119 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.EPSOutputHandler").newInstance());
121 } catch (ClassNotFoundException cnfex) {
122 } catch (Exception ex) {
124 D.trace(ex);
125 }
126
127 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.EMFOutputHandler").newInstance());
129 } catch (ClassNotFoundException cnfex) {
130 } catch (Exception ex) {
132 D.trace(ex);
133 }
134
135 }
136
137
141 public void convert(String[] args) {
142 parseArgs(args);
143
144 Graph2D graph = new Graph2D();
145
146 HierarchyManager hierarchy = new HierarchyManager(graph);
149
150 IOHandler inputHandler = getIOHandler(inFile);
152
153 if (inputHandler != null && inputHandler.canRead()) {
154 try {
155 inputHandler.read(graph, inFile);
156 }
157 catch (IOException iex) {
158 error("Error while decoding file " + inFile + "\n" + iex);
159 }
160 } else {
161 usage("Can't determine input format");
162 }
163
164 IOHandler outputHandler = getIOHandler(outFile);
166
167 if (outputHandler != null && outputHandler.canWrite()) {
168 Graph2DView view = null;
169 if (outputHandler instanceof ImageOutputHandler) {
170 view = ((ImageOutputHandler) outputHandler).createDefaultGraph2DView(graph);
172 } else {
173 view = new Graph2DView(graph);
174 }
175 configureView(view);
176 graph.setCurrentView(view);
178 try {
179 outputHandler.write(graph, outFile);
180 }
181 catch (IOException iex) {
182 error("Error while encoding file " + outFile + "\n" + iex);
183 }
184 graph.removeView(view);
186 } else {
187 usage("Can't determine output format");
188 }
189
190 }
191
192
196 private void configureView(Graph2DView view) {
197 Graph2D graph = view.getGraph2D();
198 Rectangle box = graph.getBoundingBox();
199 Dimension dim = getOutputSize(box);
200 view.setSize(dim);
201 view.zoomToArea(box.getX() - 10, box.getY() - 10, box.getWidth() + 20, box.getHeight() + 20);
202 view.setPaintDetailThreshold(0.0); }
204
205
208 public void parseArgs(String[] args) {
209 for (int i = 0; i < args.length; i++) {
210 if ("-in".equals(args[i]) && inFile == null) {
211 inFile = args[++i];
212 } else if ("-out".equals(args[i]) && outFile == null) {
213 outFile = args[++i];
214 } else if ("-width".equals(args[i])) {
215 outputWidth = Integer.parseInt(args[++i]);
216 } else if ("-height".equals(args[i])) {
217 outputHeight = Integer.parseInt(args[++i]);
218 }
219 }
220
221 if (inFile == null) {
222 usage("No input file specified");
223 }
224
225 if (outFile == null) {
226 usage("No output file specified");
227 }
228 }
229
230
235 private Dimension getOutputSize(Rectangle inBox) {
236 if (outputWidth > 0 && outputHeight > 0) {
237 return new Dimension((int) outputWidth, (int) outputHeight);
239 } else if (outputWidth > 0) {
240 return new Dimension(outputWidth,
242 (int) (outputWidth * (inBox.getHeight() / inBox.getWidth())));
243 } else if (outputHeight > 0) {
244 return new Dimension((int) (outputHeight * (inBox.getWidth() / inBox.getHeight())),
246 outputHeight);
247 } else {
249 double width = inBox.getWidth();
251 double height = inBox.getHeight();
252 if (width > 1024) {
254 height *= 1024 / width;
255 width = 1024;
256 }
257 if (height > 1024) {
258 width *= 1024 / height;
259 height = 1024;
260 }
261 return new Dimension((int) width, (int) height);
262 }
263 }
264
265
269 private IOHandler getIOHandler(String fileName) {
270 for (Iterator iter = ioHandlers.iterator(); iter.hasNext();) {
271 IOHandler ioh = (IOHandler) iter.next();
272 if (fileName.endsWith(ioh.getFileNameExtension())) {
273 return ioh;
274 }
275 }
276 return null;
277 }
278
279
283 public static void main(String[] args) {
284 GraphFormatConverter converter = new GraphFormatConverter();
285 converter.convert(args);
286 }
287
288 }
289