Console.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /***************************************************************************
  2. Title: GraphBrowser/Console.java
  3. ID: $Id$
  4. Author: Gerwin Klein, TU Muenchen
  5. License: GPL (GNU GENERAL PUBLIC LICENSE)
  6. This is the graph browser's main class when run as a console application.
  7. It duplicates some logic from GraphBrowser and GraphView.
  8. It does so to remove dependency on AWT.
  9. ***************************************************************************/
  10. package GraphBrowser;
  11. import java.io.*;
  12. import java.util.*;
  13. import java.net.*;
  14. import awtUtilities.*;
  15. public class Console {
  16. Graph g;
  17. String gfname;
  18. public Console(String name) {
  19. gfname = name;
  20. }
  21. public void PS(String fname, boolean printable) throws IOException {
  22. g.layout(null);
  23. g.PS(fname,printable);
  24. }
  25. public void collapseNodes(Vector collapsedDir) {
  26. Enumeration e1=collapsedDir.elements();
  27. Graph gra=(Graph)(g.clone());
  28. while (e1.hasMoreElements()) {
  29. Directory d=(Directory)(e1.nextElement());
  30. Vector v=gra.decode(d.getCollapsed());
  31. if (!v.isEmpty())
  32. gra.collapse(v,"["+d.getName()+"]",d.getCollapsed());
  33. }
  34. g = gra;
  35. }
  36. public void initBrowser(InputStream is) {
  37. try {
  38. TreeNode tn = new TreeNode("Root", "", -1, true);
  39. g = new Graph(is, tn);
  40. Vector v = new Vector(10,10);
  41. tn.collapsedDirectories(v);
  42. collapseNodes(v);
  43. } catch (IOException exn) {
  44. System.err.println("\nI/O error while reading graph file.");
  45. } catch (ParseError exn) {
  46. System.err.println("\nParse error in graph file:");
  47. System.err.println(exn.getMessage());
  48. System.err.println("\nSyntax:\n<vertexname> <vertexID> <dirname> [ + ] <path> "+
  49. "[ < | > ] [ <vertexID> [ ... [ <vertexID> ] ... ] ] ;");
  50. }
  51. }
  52. public static void main(String[] args) {
  53. try {
  54. if (args.length <= 1) {
  55. System.err.println("Graph and output file expected.");
  56. return;
  57. }
  58. Console console=new Console(args[0]);
  59. InputStream is=new FileInputStream(args[0]);
  60. console.initBrowser(is);
  61. is.close();
  62. try {
  63. if (args[1].endsWith(".ps"))
  64. console.PS(args[1], true);
  65. else if (args[1].endsWith(".eps"))
  66. console.PS(args[1], false);
  67. else
  68. System.err.println("Unknown file type: " + args[1]);
  69. } catch (IOException exn) {
  70. System.err.println("Unable to write file " + args[1]);
  71. }
  72. } catch (IOException exn) {
  73. System.err.println("Can't open graph file "+args[0]);
  74. }
  75. }
  76. }