001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 1.3
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.io;
042:
043: import java.util.*;
044: import java.io.*;
045:
046: //========================================================================
047:
048: public class WinIniFile {
049: public static final int MAX_LEN_LINE = 256;
050: public static final int CREATE = 1;
051: public static final int OPEN_EXISTS = 2;
052: public static final String NO_VALUE = "NOVALUE";
053:
054: private Hashtable sections = null;
055: private String name = null;
056: private InputStream isdata = null;
057: private char buf[] = new char[WinIniFile.MAX_LEN_LINE];
058: private String begSecSymb = "[";
059: private String endSecSymb = "]";
060: private int openFlag = OPEN_EXISTS;
061:
062: //========================================================================
063:
064: public WinIniFile(String name, int flag) throws IOException {
065: this .name = name;
066: openFlag = flag;
067: switch (flag) {
068: case CREATE: {
069: // if this file allready exist => clear it
070: FileOutputStream file = new FileOutputStream(name);
071: file.close();
072: sections = new Hashtable();
073: }
074: break;
075: case OPEN_EXISTS:
076: init((InputStream) new FileInputStream(name));
077: break;
078: }
079: }
080:
081: //========================================================================
082:
083: public WinIniFile(String name) throws IOException {
084: this .name = name;
085: init((InputStream) new FileInputStream(name));
086: }
087:
088: //========================================================================
089:
090: public WinIniFile(InputStream is) throws IOException {
091: init(is);
092: }
093:
094: //========================================================================
095:
096: public void init(InputStream is) throws IOException {
097: String data = new String();
098: Hashtable sect = null;
099: sections = new Hashtable();
100: isdata = is;
101: DataInputStream dis = new DataInputStream(isdata);
102: for (;;) {
103: data = readLine(is);
104: if (data == null)
105: break;
106: if ((isEmpty(data)) || (data.indexOf(";") == 0))
107: continue;
108: if (data.equals("EOF"))
109: break;
110: int beg = data.indexOf(begSecSymb);
111: if (beg == 0) {
112: int end = data.indexOf(endSecSymb);
113: if (end != (data.length() - 1))
114: continue;
115: sect = new Hashtable();
116: sections.put(
117: data.substring(beg + 1, end).toLowerCase(),
118: sect);
119: continue;
120: }
121: if (sect == null)
122: continue;
123: StringTokenizer st = new StringTokenizer(data, "=");
124: if (!st.hasMoreTokens())
125: continue;
126: String vname = st.nextToken().toLowerCase();
127:
128: String value = null;
129: if (!st.hasMoreTokens())
130: value = NO_VALUE;
131: value = st.nextToken();
132:
133: if (isEmpty(vname) || isEmpty(value))
134: continue;
135: sect.put(vname, value);
136: }
137: dis.close();
138: dis = null;
139: }
140:
141: //========================================================================
142:
143: private String readLine(InputStream is) throws IOException {
144: int res = -1, i = 0;
145: while ((res = is.read()) > 0 && res != '\n' && i < buf.length)
146: buf[i++] = (char) res;
147:
148: if (i == 0)
149: return null;
150: return new String(buf, 0, i).trim();
151: }
152:
153: //========================================================================
154:
155: private void writeLine(RandomAccessFile raf, String data)
156: throws IOException {
157: byte buf[] = new byte[data.length()];
158: data.getBytes(0, data.length(), buf, 0);
159: raf.write(buf);
160: raf.write(0x0D);
161: raf.write(0x0A);
162: }
163:
164: //========================================================================
165:
166: public String getValue(String section, String name) {
167: Hashtable tab = (Hashtable) sections.get(section.toLowerCase());
168: if (tab == null)
169: return null;
170: return (String) tab.get(name.toLowerCase());
171: }
172:
173: //========================================================================
174:
175: public String getValue(String name) {
176: Hashtable tab = foundSection(name.toLowerCase());
177: if (tab == null)
178: return null;
179: return (String) tab.get(name.toLowerCase());
180: }
181:
182: //========================================================================
183:
184: public Hashtable getSection(String name) {
185: return (Hashtable) sections.get(name.toLowerCase());
186: }
187:
188: public Hashtable foundSection(String namevalue) {
189: Enumeration el = sections.elements();
190: while (el.hasMoreElements()) {
191: Hashtable tab = (Hashtable) el.nextElement();
192: String res = (String) tab.get(namevalue.toLowerCase());
193: if (res != null)
194: return tab;
195: }
196: return null;
197: }
198:
199: public int size(String section) {
200: Hashtable tab = getSection(section);
201: if (tab == null)
202: return -1;
203: return tab.size();
204: }
205:
206: public int size() {
207: Enumeration el = sections.elements();
208: int count = 0;
209: if ((el == null) || (!el.hasMoreElements()))
210: return -1;
211: while (el.hasMoreElements()) {
212: Hashtable tab = (Hashtable) el.nextElement();
213: count += tab.size();
214: }
215: return count;
216: }
217:
218: public int sizeNumber(String section) {
219: int count = 0;
220: Hashtable tab = (Hashtable) sections.get(section.toLowerCase());
221: if (tab == null)
222: return -1;
223: String data = "";
224: for (count = 0; data != null; count++)
225: data = (String) tab.get("" + count);
226: return count - 1;
227: }
228:
229: public int putValue(String section, String name, String value) {
230: Hashtable tab = getSection(section.toLowerCase());
231: if (tab == null)
232: return -1;
233: tab.put(name.toLowerCase(), value);
234: return 0;
235: }
236:
237: //========================================================================
238:
239: public int putValue(String name, String value) {
240: Hashtable tab = foundSection(name.toLowerCase());
241: if (tab == null)
242: return -1;
243: tab.put(name.toLowerCase(), value);
244: return 0;
245: }
246:
247: //========================================================================
248:
249: public int delSection(String name) {
250: if (sections == null)
251: return -1;
252: sections.remove(name.toLowerCase());
253: return 0;
254: }
255:
256: //========================================================================
257:
258: public int delValue(String section, String name) {
259: Hashtable tab = getSection(section.toLowerCase());
260: if (tab == null)
261: return -1;
262: tab.remove(name.toLowerCase());
263: return 0;
264: }
265:
266: //========================================================================
267:
268: public int putSection(String name) {
269: if ((sections == null)
270: || (sections.get(name.toLowerCase()) != null))
271: return -1;
272: sections.put(name.toLowerCase(), new Hashtable());
273: return 0;
274: }
275:
276: //========================================================================
277:
278: public int flush(String name) throws IOException {
279: if (sections == null)
280: return -1;
281: RandomAccessFile file = new RandomAccessFile(name, "rw");
282: Enumeration el = sections.elements();
283: Enumeration sk = sections.keys();
284: while (el.hasMoreElements()) {
285: Hashtable tab = (Hashtable) el.nextElement();
286: if (tab == null)
287: break;
288: Enumeration keys = tab.keys();
289: Enumeration values = tab.elements();
290: writeLine(file, begSecSymb + (String) sk.nextElement()
291: + endSecSymb);
292: while (keys.hasMoreElements()) {
293: String key = (String) keys.nextElement();
294: String value = (String) values.nextElement();
295: if (value.equals(NO_VALUE))
296: writeLine(file, key);
297: else
298: writeLine(file, key + "=" + value);
299: }
300: }
301: file.close();
302: return 0;
303: }
304:
305: //========================================================================
306:
307: public int flush() throws IOException {
308: if (this .name == null)
309: return -1;
310: return flush(name);
311: }
312:
313: //========================================================================
314:
315: public String getName() {
316: return name;
317: }
318:
319: //========================================================================
320:
321: public void setSectionSymbols(String beg, String end) {
322: begSecSymb = beg;
323: endSecSymb = end;
324: }
325:
326: private boolean isEmpty(String s) {
327: return s == null || s.length() == 0 || s.trim().length() == 0;
328: }
329: }
|