KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > DFSRChecker > utils > AnotatedProtocol


1 /*
2  * $Id: AnotatedProtocol.java,v 1.1 2005/07/13 09:07:43 kofron Exp $
3  *
4  * Copyright 2005
5  * Formal Methods In Software Engineering Group
6  * Institute of Computer Science
7  * Academy of Sciences of the Czech Republic
8  *
9  * This code was developed by Jan Kofron <kofron@nenya.ms.mff.cuni.cz>
10  */

11
12 package SOFA.SOFAnode.Util.DFSRChecker.utils;
13
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStream JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.TreeSet JavaDoc;
20
21 /**
22  * This class is used for pretty-printing of a protocol with anotated state.
23  */

24 public class AnotatedProtocol {
25     
26     /**
27      * Creates a new instance of AnotatedProtocol.
28      *
29      * @param protocol
30      * the protocol string
31      * @param indices
32      * the indices within the protocol string denotatting the state
33      */

34     public AnotatedProtocol(String JavaDoc protocol, ArrayList JavaDoc indices) {
35         this.protocol = protocol;
36         this.indices = indices;
37     }
38     
39     /**
40      * Pretty-prints the protocol and denotes the states.
41      * @param file false - print to the stdout, true - print to the file protocol.html
42      */

43     public void prettyPrint(boolean file) {
44         try {
45             if (!file)
46                 doPrettyPrint(System.out, "");
47             else {
48                 FileOutputStream JavaDoc f = new FileOutputStream JavaDoc("protocol.html");
49                 doPrettyPrint(f, "");
50                 f.close();
51             }
52         }
53         catch (IOException JavaDoc e) {
54             System.out.println("Error while writing protocol state");
55         }
56             
57     }
58
59     
60     /**
61      * Pretty-prints the protocol and denotes the states.
62      * Prints to the given stream.
63      */

64     public void doPrettyPrint(OutputStream JavaDoc stream, String JavaDoc indent) throws IOException JavaDoc {
65         TreeSet JavaDoc sorted = new TreeSet JavaDoc(indices);
66         Iterator JavaDoc it = sorted.iterator();
67         stream.write('\n');
68         stream.write(new String JavaDoc("<html><head /><body><pre>").getBytes());
69         
70         int index = it.hasNext() ? ((Integer JavaDoc)it.next()).intValue() : -1;
71         
72         for (int i = 0; i < protocol.length(); ++i) {
73             if (index == i) {
74                 
75                 stream.write(new String JavaDoc("<FONT COLOR=\"#00AA00\">").getBytes());
76                 stream.write(new String JavaDoc("&lt;HERE&gt;").getBytes());
77                 stream.write(new String JavaDoc("</FONT>").getBytes());
78                 
79                 index = it.hasNext() ? ((Integer JavaDoc)it.next()).intValue() : -1;
80             }
81             
82             char c = protocol.charAt(i);
83             
84             if (c == '%') {
85                 stream.write(new String JavaDoc("\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                 //stream.write('\n');
94
//stream.write(indent.getBytes());
95
}
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 JavaDoc("</pre></body></html>").getBytes());
136     }
137     
138
139     /**
140      * The protocol string
141      */

142     public String JavaDoc protocol;
143
144     /**
145      * The indices within the protocol string denotating the position in the
146      * state space
147      */

148     public ArrayList JavaDoc indices;
149     
150     /**
151      * How many spaces are in the indentation each step
152      */

153     private final static String JavaDoc INDENT = " ";
154 }
155
Popular Tags