1
14 package demo.io;
15
16 import java.util.Collection;
17 import java.util.Iterator;
18 import java.util.Vector;
19 import y.util.D;
20 import y.io.GMLIOHandler;
21 import y.io.YGFIOHandler;
22 import y.io.IOHandler;
23 import y.io.JPGIOHandler;
24 import y.io.GIFIOHandler;
25 import y.io.ImageOutputHandler;
26 import y.view.Graph2DView;
27 import y.view.Graph2D;
28 import java.awt.Rectangle;
29 import java.awt.Dimension;
30 import java.io.IOException;
31 import y.view.hierarchy.HierarchyManager;
32
33
34
45 public class GraphFormatConverter
46 {
47 private Collection ioHandlers;
48 private int outputWidth = -1, outputHeight = -1;
49 private String inFile = null, outFile = null;
50
51
52 private static void usage(String msg)
53 {
54 System.err.println(msg + "\n\n" +
55 "Usage: java demo.io.GraphFormatConverter -in <infile> -out <outfile> [options]\n" +
56 "Usage: where the format of infile is YGF or GML \n" +
57 "Usage: and the format of outfile in in YGF, GML, JPG or GIF.\n" +
58 "Usage: SVG/SVGZ output needs the ySVG extension package.\n" +
59 "Usage: EMF, PDF, EPS and SWF output needs the yExport extension package.\n" +
60 "Usage: File formats are determined by the file name extensions.\n" +
61 "Usage: Additional options which work for some output formats:\n" +
62 "Usage: -width <w> the width of the output format\n" +
63 "Usage: -height<h> the height of the output format\n"+
64 "Usage: If neither option is specified, a value of 1024\n"+
65 "Usage: is used for both dimensions\n");
66 System.exit(1);
67 }
68
69 private static void error(String msg)
70 {
71 System.err.println(msg);
72 System.exit(1);
73 }
74
75
79 public GraphFormatConverter()
80 {
81 ioHandlers = new Vector();
82 ioHandlers.add(new YGFIOHandler());
83 ioHandlers.add(new GMLIOHandler());
84 ioHandlers.add(new GIFIOHandler());
85 ioHandlers.add(new JPGIOHandler());
86 try { ioHandlers.add((IOHandler)Class.forName("yext.svg.io.SVGIOHandler").newInstance());
88 ioHandlers.add((IOHandler)Class.forName("yext.svg.io.SVGZIOHandler").newInstance());
89 }
90 catch(ClassNotFoundException cnfex)
91 {
92 }
94 catch(Exception ex)
95 {
96 D.trace(ex);
97 }
98
99 try { ioHandlers.add((IOHandler)Class.forName("yext.graphml.graph2D.GraphMLIOHandler").newInstance());
101 ioHandlers.add((IOHandler)Class.forName("yext.graphml.graph2D.ZipGraphMLIOHandler").newInstance());
102 }
103 catch(ClassNotFoundException cnfex)
104 {
105 }
107 catch(Exception ex)
108 {
109 D.trace(ex);
110 }
111
112 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.PDFOutputHandler").newInstance());
114 } catch (ClassNotFoundException cnfex) {
115 } catch (Exception ex) {
117 D.trace(ex);
118 }
119
120 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.SWFOutputHandler").newInstance());
122 } catch (ClassNotFoundException cnfex) {
123 } catch (Exception ex) {
125 D.trace(ex);
126 }
127
128 try { ioHandlers.add((IOHandler)Class.forName("yext.export.io.EPSOutputHandler").newInstance());
130 } catch(ClassNotFoundException cnfex) {
131 } catch(Exception ex) {
133 D.trace(ex);
134 }
135
136 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.EMFOutputHandler").newInstance());
138 } catch (ClassNotFoundException cnfex) {
139 } catch (Exception ex) {
141 D.trace(ex);
142 }
143
144 }
145
146
150 public void convert(String[] args)
151 {
152 parseArgs(args);
153
154 Graph2D graph = new Graph2D();
155
156 HierarchyManager hierarchy = new HierarchyManager(graph);
159
160 IOHandler inputHandler = getIOHandler(inFile);
162
163 if(inputHandler != null && inputHandler.canRead())
164 {
165 try
166 {
167 inputHandler.read(graph, inFile);
168 }
169 catch(IOException iex)
170 {
171 error("Error while decoding file " + inFile + "\n" + iex);
172 }
173 }
174 else
175 {
176 usage("Can't determine input format");
177 }
178
179 IOHandler outputHandler = getIOHandler(outFile);
181
182 if(outputHandler != null && outputHandler.canWrite())
183 {
184 Graph2DView view = null;
185 if(outputHandler instanceof ImageOutputHandler)
186 {
187 view = ((ImageOutputHandler)outputHandler).createDefaultGraph2DView(graph);
189 }
190 else
191 {
192 view = new Graph2DView(graph);
193 }
194 configureView(view);
195 graph.setCurrentView(view);
197 try
198 {
199 outputHandler.write(graph, outFile);
200 }
201 catch(IOException iex)
202 {
203 error("Error while encoding file " + outFile + "\n" + iex);
204 }
205 graph.removeView(view);
207 }
208 else
209 {
210 usage("Can't determine output format");
211 }
212
213 }
214
215
219 private void configureView(Graph2DView view)
220 {
221 Graph2D graph = view.getGraph2D();
222 Rectangle box = graph.getBoundingBox();
223 Dimension dim = getOutputSize(box);
224 view.setSize(dim);
225 view.zoomToArea(box.getX()-10,box.getY()-10,box.getWidth()+20,box.getHeight()+20);
226 view.setPaintDetailThreshold(0.0); }
228
229
232 public void parseArgs(String[] args)
233 {
234 for(int i = 0; i < args.length; i++)
235 {
236 if(args[i].equals("-in") && inFile == null)
237 {
238 inFile = args[++i];
239 }
240 else if(args[i].equals("-out") && outFile == null)
241 {
242 outFile = args[++i];
243 }
244 else if(args[i].equals("-width"))
245 {
246 outputWidth = Integer.parseInt(args[++i]);
247 }
248 else if(args[i].equals("-height"))
249 {
250 outputHeight = Integer.parseInt(args[++i]);
251 }
252 }
253
254 if(inFile == null)
255 {
256 usage("No input file specified");
257 }
258
259 if(outFile == null)
260 {
261 usage("No output file specified");
262 }
263 }
264
265
270 private Dimension getOutputSize(Rectangle inBox)
271 {
272 if(outputWidth > 0 && outputHeight > 0)
273 {
274 return new Dimension((int)outputWidth,(int)outputHeight);
276 }
277 else if(outputWidth > 0)
278 {
279 return new Dimension(outputWidth,
281 (int)(outputWidth*(inBox.getHeight()/inBox.getWidth())));
282 }
283 else if(outputHeight > 0)
284 {
285 return new Dimension((int)(outputHeight*(inBox.getWidth()/inBox.getHeight())),
287 outputHeight);
288 }
289 else {
291 double width = inBox.getWidth();
293 double height = inBox.getHeight();
294 if(width > 1024) {
296 height *= 1024/width;
297 width = 1024;
298 }
299 if(height > 1024) {
300 width *= 1024/height;
301 height = 1024;
302 }
303 return new Dimension((int)width,(int)height);
304 }
305 }
306
307
311 private IOHandler getIOHandler(String fileName)
312 {
313 for(Iterator iter = ioHandlers.iterator(); iter.hasNext();)
314 {
315 IOHandler ioh = (IOHandler)iter.next();
316 if(fileName.endsWith(ioh.getFileNameExtension()))
317 return ioh;
318 }
319 return null;
320 }
321
322
326 public static void main(String[] args)
327 {
328 GraphFormatConverter converter = new GraphFormatConverter();
329 converter.convert(args);
330 }
331
332 }
333