1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.application.state.impl;
21
22 import com.cosylab.application.state.*;
23
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.OutputStream;
29 import java.io.PrintWriter;
30
31 import java.util.Iterator;
32
33
34
35
36
37
38
39
40
41
42
43 public class TextStateStorage extends DefaultStateStorage {
44 protected final static String stateStart = "<State>";
45 protected final static String stateEnd = "<EndState>";
46 protected final static String childDelimiter = "<Child>";
47
48
49
50
51 public TextStateStorage() {
52 super();
53 }
54
55
56
57
58
59
60 public TextStateStorage(StateStorage ss) {
61 super(ss);
62 }
63
64 private void readState(BufferedReader br, State state)
65 throws IOException {
66 String line;
67
68 while (true) {
69 line = br.readLine();
70
71 if (line == null) {
72 break;
73 }
74
75 line = line.trim();
76
77 if (line.startsWith(stateEnd)) {
78 break;
79 }
80
81 if (line.startsWith(childDelimiter)) {
82 String childKey = line.substring(childDelimiter.length());
83 State s = state.createState(childKey);
84 br.readLine();
85 readState(br, s);
86
87 continue;
88 }
89
90 int index = line.indexOf('=');
91 String key = line.substring(0, index);
92 String value = line.substring(index + 1);
93 state.putString(key, value);
94 }
95 }
96
97
98
99
100 public void load(String filePath, String applicationName)
101 throws IOException {
102 if (delegate != null) {
103 delegate.load(filePath, applicationName);
104 addAll(delegate.getStates());
105
106 return;
107 }
108
109
110 InputStream is = getInputStream(filePath, applicationName + ".txt");
111 load(is);
112 }
113
114
115
116
117 public void load(InputStream is) throws IOException {
118 if (is == null) {
119 return;
120 }
121
122 BufferedReader br = new BufferedReader(new InputStreamReader(is));
123 String line;
124
125 while (true) {
126 line = br.readLine();
127
128 if (line == null) {
129 break;
130 }
131
132 line = line.trim();
133
134 if (line.startsWith(stateStart)) {
135 State state = StateFactory.createState();
136 readState(br, state);
137 states.add(state);
138 }
139 }
140 }
141
142 private void printIndent(PrintWriter pw, int indent) {
143 for (int i = 0; i < indent; i++) {
144 pw.print(" ");
145 }
146 }
147
148 private void print(PrintWriter pw, State s, int indent) {
149 int i;
150 Iterator iter = s.keySet().iterator();
151 printIndent(pw, indent);
152 pw.println(stateStart);
153
154 while (iter.hasNext()) {
155 String key = (String) iter.next();
156 Object value = s.getObject(key);
157
158 if (value instanceof State) {
159 printIndent(pw, indent + 1);
160 pw.println(childDelimiter + key);
161 print(pw, (State) value, indent + 1);
162
163 continue;
164 }
165
166 printIndent(pw, indent);
167 pw.println(key + "=" + value.toString());
168 }
169
170 printIndent(pw, indent);
171 pw.println(stateEnd);
172 }
173
174
175
176
177 public void store(String filePath, String applicationName)
178 throws IOException {
179 OutputStream os = getOutputStream(filePath, applicationName + ".txt");
180 store(os);
181 }
182
183
184
185
186 public void store(OutputStream os) throws IOException {
187 if (os == null) {
188 return;
189 }
190
191 PrintWriter pw = new PrintWriter(os);
192
193 for (int i = 0; i < states.size(); i++) {
194 State s = (State) states.get(i);
195 print(pw, s, 0);
196 }
197
198 pw.flush();
199 pw.close();
200 }
201 }
202
203
204