1 3 import org.faceless.graph2.*; 4 import org.xml.sax.*; 5 import java.io.*; 6 import java.awt.*; 7 import java.net.URL ; 8 9 21 public class XMLToGraph 22 { 23 private static String format="png"; 24 25 public static void main(String [] args) 26 throws Exception 27 { 28 if (args.length==0) { 29 System.out.println("Usage: java XMLToGraph [ --svg | --png | <filename> ] ...\n"); 30 System.exit(0); 31 } 32 for (int i=0;i<args.length;i++) { 33 if (args[i].equals("--svg")) { 34 format="svg"; 35 } else if (args[i].equals("--png")) { 36 format="png"; 37 } else { 38 URL url = getInput(args[i]); 39 InputSource input = new InputSource(url.openStream()); 40 input.setSystemId(url.toString()); 41 String outfile = getOutput(url); 42 43 System.out.println("--------------------------------\nParsing \""+url+"\"... "); 44 try { 45 XMLGraph xml = new XMLGraph(); 46 xml.parse(input); 47 Graph graph = xml.getGraph(); 48 49 OutputStream out = new FileOutputStream(outfile); 50 if (format=="svg") { 51 SVGOutput image = new SVGOutput(xml.getWidth(), xml.getHeight()); 52 graph.draw(image); 53 image.writeSVG(new OutputStreamWriter(out), true); 54 } else if (format=="png") { 55 ImageOutput image = new ImageOutput(xml.getWidth(), xml.getHeight()); 56 graph.draw(image); 57 image.writePNG(out, 0); 58 } 59 out.close(); 60 System.out.println("Created \""+outfile+"\""); 61 } catch (Exception e) { 62 e.printStackTrace(); 63 } 64 } 65 } 66 } 67 68 private static final URL getInput(String arg) 72 throws IOException 73 { 74 URL url = new URL ("file:"+System.getProperty("user.dir")+System.getProperty("file.separator")); 75 url = new URL (url, arg); 76 return url; 77 } 78 79 private static final String getOutput(URL url) 82 { 83 String file; 84 if (url.getProtocol().equals("file")) { 85 file = url.getFile(); 86 if (file.endsWith(".xml")) file = file.substring(0,file.length()-4); 87 file += "."+format; 88 } else { 89 file = "XMLGraph."+format; 90 } 91 return file; 92 } 93 } 94 | Popular Tags |