View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
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   * This class is an implementation of the <code>StateStorage</code> that uses
36   * simple text file to store the array of the <code>State</code> objects.
37   * Stored file is the plain <code>java.util.Properties</code> file format with
38   * the few 'weird' properties added which are used for fast hierarchy
39   * resolving.
40   *
41   * @author dvitas
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       * Creates a new TextStateStorage object.
57       *
58       * @param ss DOCUMENT ME!
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(); // eat state header
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      /* (non-Javadoc)
98       * @see com.cosylab.application.state.StateStorage#load(java.io.InputStream)
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         // get the InputStream if file exists
110         InputStream is = getInputStream(filePath, applicationName + ".txt");
111         load(is);
112     }
113 
114     /* (non-Javadoc)
115 	 * @see com.cosylab.application.state.StateStorage#load(java.io.InputStream)
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     /* (non-Javadoc)
175      * @see com.cosylab.application.state.StateStorage#store(java.io.OutputStream)
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     /* (non-Javadoc)
184 	 * @see com.cosylab.application.state.StateStorage#store(java.io.OutputStream, java.lang.String)
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 /* __oOo__ */