1 11 12 package SOFA.SOFAnode.Util.DFSRChecker.utils; 13 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.OutputStream ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.TreeSet ; 20 21 24 public class AnotatedProtocol { 25 26 34 public AnotatedProtocol(String protocol, ArrayList indices) { 35 this.protocol = protocol; 36 this.indices = indices; 37 } 38 39 43 public void prettyPrint(boolean file) { 44 try { 45 if (!file) 46 doPrettyPrint(System.out, ""); 47 else { 48 FileOutputStream f = new FileOutputStream ("protocol.html"); 49 doPrettyPrint(f, ""); 50 f.close(); 51 } 52 } 53 catch (IOException e) { 54 System.out.println("Error while writing protocol state"); 55 } 56 57 } 58 59 60 64 public void doPrettyPrint(OutputStream stream, String indent) throws IOException { 65 TreeSet sorted = new TreeSet (indices); 66 Iterator it = sorted.iterator(); 67 stream.write('\n'); 68 stream.write(new String ("<html><head /><body><pre>").getBytes()); 69 70 int index = it.hasNext() ? ((Integer )it.next()).intValue() : -1; 71 72 for (int i = 0; i < protocol.length(); ++i) { 73 if (index == i) { 74 75 stream.write(new String ("<FONT COLOR=\"#00AA00\">").getBytes()); 76 stream.write(new String ("<HERE>").getBytes()); 77 stream.write(new String ("</FONT>").getBytes()); 78 79 index = it.hasNext() ? ((Integer )it.next()).intValue() : -1; 80 } 81 82 char c = protocol.charAt(i); 83 84 if (c == '%') { 85 stream.write(new String ("\n<BR><hr>\n").getBytes()); 86 indent = ""; 87 } 88 else if (c == '(') { 89 stream.write('\n'); 90 stream.write(indent.getBytes()); 91 stream.write('('); 92 indent += INDENT; 93 } 96 else if (c == ')') { 97 char bc = protocol.charAt(i - 1); 98 if (!((bc == '$') || (bc == '^') || (bc==']'))) { 99 stream.write('\n'); 100 indent = indent.substring(0, indent.length() - INDENT.length()); 101 stream.write(indent.getBytes()); 102 } 103 else 104 indent = indent.substring(0, indent.length() - INDENT.length()); 105 106 stream.write(')'); 107 if ((protocol.length() > i + 1) && (protocol.charAt(i + 1) != '*') && (protocol.charAt(i + 1) != ';') && (protocol.charAt(i + 1) != ')')) { 108 stream.write('\n'); 109 stream.write(indent.getBytes()); 110 } 111 } 112 else if (c == '{') { 113 stream.write('\n'); 114 stream.write(indent.getBytes()); 115 stream.write('{'); 116 indent += INDENT; 117 } 118 else if (c == '}') { 119 stream.write('\n'); 120 indent = indent.substring(0, indent.length() - INDENT.length()); 121 stream.write(indent.getBytes()); 122 stream.write('}'); 123 stream.write('\n'); 124 stream.write(indent.getBytes()); 125 } 126 else if (c == '+') { 127 stream.write('+'); 128 stream.write(indent.getBytes()); 129 } 130 131 else 132 stream.write(protocol.charAt(i)); 133 } 134 stream.write('\n'); 135 stream.write(new String ("</pre></body></html>").getBytes()); 136 } 137 138 139 142 public String protocol; 143 144 148 public ArrayList indices; 149 150 153 private final static String INDENT = " "; 154 } 155 | Popular Tags |