1
28 package demo.io;
29
30 import y.io.GIFIOHandler;
31 import y.io.GMLIOHandler;
32 import y.io.GraphMLIOHandler;
33 import y.io.IOHandler;
34 import y.io.ImageOutputHandler;
35 import y.io.JPGIOHandler;
36 import y.io.YGFIOHandler;
37 import y.io.ZipGraphMLIOHandler;
38 import y.util.D;
39 import y.view.Graph2D;
40 import y.view.Graph2DView;
41 import y.view.hierarchy.HierarchyManager;
42
43 import java.awt.Dimension;
44 import java.awt.Rectangle;
45 import java.io.IOException;
46 import java.util.Collection;
47 import java.util.Iterator;
48 import java.util.LinkedList;
49
50
51
65 public class GraphFormatConverter {
66 private Collection ioHandlers;
67 private int outputWidth = -1;
68 private int outputHeight = -1;
69 private String inFile;
70 private String outFile;
71
72
73 private static void usage(String msg) {
74 System.err.println(msg + "\n\n" +
75 "Usage: java demo.io.GraphFormatConverter -in <infile> -out <outfile> [options]\n" +
76 "Usage: where the format of infile is GraphML, ZIPGraphML, YGF or GML \n" +
77 "Usage: and the format of outfile in in GraphML, ZIPGraphML, YGF, GML, JPG or GIF.\n" +
78 "Usage: SVG/SVGZ output needs the ySVG extension package.\n" +
79 "Usage: EMF, PDF, EPS and SWF output needs the yExport extension package.\n" +
80 "Usage: File formats are determined by the file name extensions.\n" +
81 "Usage: Additional options which work for some output formats:\n" +
82 "Usage: -width <w> the width of the output format\n" +
83 "Usage: -height<h> the height of the output format\n" +
84 "Usage: If neither option is specified, a value of 1024\n" +
85 "Usage: is used for both dimensions\n");
86 System.exit(1);
87 }
88
89 private static void error(String msg) {
90 System.err.println(msg);
91 System.exit(1);
92 }
93
94
98 public GraphFormatConverter() {
99 ioHandlers = new LinkedList();
100 ioHandlers.add(new GraphMLIOHandler());
101 ioHandlers.add(new ZipGraphMLIOHandler());
102 ioHandlers.add(new YGFIOHandler());
103 ioHandlers.add(new GMLIOHandler());
104 ioHandlers.add(new GIFIOHandler());
105 ioHandlers.add(new JPGIOHandler());
106 try { ioHandlers.add((IOHandler) Class.forName("yext.svg.io.SVGIOHandler").newInstance());
108 ioHandlers.add((IOHandler) Class.forName("yext.svg.io.SVGZIOHandler").newInstance());
109 }
110 catch (ClassNotFoundException cnfex) {
111 }
113 catch (Exception ex) {
114 D.trace(ex);
115 }
116
117 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.PDFOutputHandler").newInstance());
119 } catch (ClassNotFoundException cnfex) {
120 } catch (Exception ex) {
122 D.trace(ex);
123 }
124
125 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.SWFOutputHandler").newInstance());
127 } catch (ClassNotFoundException cnfex) {
128 } catch (Exception ex) {
130 D.trace(ex);
131 }
132
133 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.EPSOutputHandler").newInstance());
135 } catch (ClassNotFoundException cnfex) {
136 } catch (Exception ex) {
138 D.trace(ex);
139 }
140
141 try { ioHandlers.add((IOHandler) Class.forName("yext.export.io.EMFOutputHandler").newInstance());
143 } catch (ClassNotFoundException cnfex) {
144 } catch (Exception ex) {
146 D.trace(ex);
147 }
148
149 }
150
151
155 public void convert(String[] args) {
156 parseArgs(args);
157
158 Graph2D graph = new Graph2D();
159
160 HierarchyManager hierarchy = new HierarchyManager(graph);
163
164 IOHandler inputHandler = getIOHandler(inFile);
166
167 if (inputHandler != null && inputHandler.canRead()) {
168 try {
169 inputHandler.read(graph, inFile);
170 }
171 catch (IOException iex) {
172 error("Error while decoding file " + inFile + "\n" + iex);
173 }
174 } else {
175 usage("Can't determine input format");
176 }
177
178 IOHandler outputHandler = getIOHandler(outFile);
180
181 if (outputHandler != null && outputHandler.canWrite()) {
182 Graph2DView view = null;
183 if (outputHandler instanceof ImageOutputHandler) {
184 view = ((ImageOutputHandler) outputHandler).createDefaultGraph2DView(graph);
186 } else {
187 view = new Graph2DView(graph);
188 }
189 configureView(view);
190 graph.setCurrentView(view);
192 try {
193 outputHandler.write(graph, outFile);
194 }
195 catch (IOException iex) {
196 error("Error while encoding file " + outFile + "\n" + iex);
197 }
198 graph.removeView(view);
200 } else {
201 usage("Can't determine output format");
202 }
203
204 }
205
206
210 private void configureView(Graph2DView view) {
211 Graph2D graph = view.getGraph2D();
212 Rectangle box = graph.getBoundingBox();
213 Dimension dim = getOutputSize(box);
214 view.setSize(dim);
215 view.zoomToArea(box.getX() - 10, box.getY() - 10, box.getWidth() + 20, box.getHeight() + 20);
216 view.setPaintDetailThreshold(0.0); }
218
219
222 public void parseArgs(String[] args) {
223 for (int i = 0; i < args.length; i++) {
224 if ("-in".equals(args[i]) && inFile == null) {
225 inFile = args[++i];
226 } else if ("-out".equals(args[i]) && outFile == null) {
227 outFile = args[++i];
228 } else if ("-width".equals(args[i])) {
229 outputWidth = Integer.parseInt(args[++i]);
230 } else if ("-height".equals(args[i])) {
231 outputHeight = Integer.parseInt(args[++i]);
232 }
233 }
234
235 if (inFile == null) {
236 usage("No input file specified");
237 }
238
239 if (outFile == null) {
240 usage("No output file specified");
241 }
242 }
243
244
249 private Dimension getOutputSize(Rectangle inBox) {
250 if (outputWidth > 0 && outputHeight > 0) {
251 return new Dimension((int) outputWidth, (int) outputHeight);
253 } else if (outputWidth > 0) {
254 return new Dimension(outputWidth,
256 (int) (outputWidth * (inBox.getHeight() / inBox.getWidth())));
257 } else if (outputHeight > 0) {
258 return new Dimension((int) (outputHeight * (inBox.getWidth() / inBox.getHeight())),
260 outputHeight);
261 } else {
263 double width = inBox.getWidth();
265 double height = inBox.getHeight();
266 if (width > 1024) {
268 height *= 1024 / width;
269 width = 1024;
270 }
271 if (height > 1024) {
272 width *= 1024 / height;
273 height = 1024;
274 }
275 return new Dimension((int) width, (int) height);
276 }
277 }
278
279
283 private IOHandler getIOHandler(String fileName) {
284 for (Iterator iter = ioHandlers.iterator(); iter.hasNext();) {
285 IOHandler ioh = (IOHandler) iter.next();
286 if (fileName.endsWith(ioh.getFileNameExtension())) {
287 return ioh;
288 }
289 }
290 return null;
291 }
292
293
297 public static void main(String[] args) {
298 GraphFormatConverter converter = new GraphFormatConverter();
299 converter.convert(args);
300 }
301
302 }
303