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.io.*;
044: import java.util.*;
045:
046: public class IniFile {
047: private Vector keys = new Vector();
048: private Vector vals = new Vector();
049: private boolean dirty = false;
050:
051: private File file = null;
052:
053: public IniFile(String name) throws IOException {
054: file = new File(name);
055: if (file.canRead())
056: loadFile();
057: }
058:
059: private void saveFile() throws IOException {
060: if (!dirty)
061: return;
062: dirty = false;
063: PrintStream pr = new PrintStream(new FileOutputStream(file));
064: for (int j = 0; j < keys.size(); ++j) {
065: pr.print(keys.elementAt(j));
066: pr.print("=");
067: pr.println(vals.elementAt(j));
068: }
069: pr.close();
070: }
071:
072: public void removeKey(String s) throws IOException {
073: int i = keys.indexOf(s);
074: if (i < 0)
075: return;
076: keys.removeElementAt(i);
077: vals.removeElementAt(i);
078: dirty = true;
079: saveFile();
080: }
081:
082: private void loadFile() throws IOException {
083: char ch = ' ';
084: String line = null;
085: DataInputStream in = new DataInputStream(new FileInputStream(
086: file));
087:
088: // [ \t]* ({symbol}+ '=' [ \t]* {symbol}*
089:
090: while ((line = in.readLine()) != null) {
091: int j = 0, k = line.length(), i;
092: if (k <= 0)
093: continue;
094: for (; j < k; ++j) {
095: ch = line.charAt(j);
096: if (ch != '\t' && ch != ' ')
097: break;
098: }
099: if (ch == '#' || ch == '\n' || ch == '\r')
100: continue;
101: for (i = j; j < k; ++j) {
102: ch = line.charAt(j);
103: if (ch == '\t' || ch == ' ' || ch == '\n' || ch == '\r'
104: || ch == '=' || ch == '#')
105: break;
106: }
107: if (j != i)
108: keys.addElement(line.substring(i, j));
109: for (; j < k; ++j) {
110: ch = line.charAt(j);
111: if (ch == '=' || ch == '\n' || ch == '\r' || ch == '#')
112: break;
113: }
114: if (ch == '\n' || ch == '\r' || ch == '#') {
115: vals.addElement("");
116: continue;
117: }
118: for (++j; j < k; ++j) {
119: ch = line.charAt(j);
120: if (ch != ' ' && ch != '\t')
121: break;
122: }
123: if (ch == '\n' || ch == '\r' || ch == '#') {
124: vals.addElement("");
125: continue;
126: }
127: for (i = j; j < k; ++j) {
128: ch = line.charAt(j);
129: if (ch == '\n' || ch == '\r' || ch == '#')
130: break;
131: }
132: vals.addElement(j != i ? line.substring(i, j).trim() : "");
133: }
134: in.close();
135: in = null;
136: }
137:
138: public synchronized String getString(String key) throws IOException {
139: int j = keys.indexOf(key);
140: if (j < 0)
141: return "UNDEFINED";
142: return (String) vals.elementAt(j);
143: }
144:
145: public int getInt(String key) throws IOException {
146: return Integer.parseInt(getString(key));
147: }
148:
149: public boolean getBoolean(String key) throws IOException {
150: return getString(key).compareTo("True") == 0;
151: }
152:
153: public synchronized void putString(String key, String value)
154: throws IOException {
155: int j = keys.indexOf(key);
156: if (j < 0) {
157: keys.addElement(key);
158: vals.addElement(value);
159: } else
160: vals.setElementAt(value, j);
161: dirty = true;
162: saveFile();
163: }
164:
165: public void putInt(String key, int value) throws IOException {
166: putString(key, Integer.toString(value));
167: }
168:
169: public void putBoolean(String key, boolean value)
170: throws IOException {
171: putString(key, value ? "True" : "False");
172: }
173:
174: public synchronized void close() throws IOException {
175: saveFile();
176: }
177:
178: public String toString() {
179: StringBuffer sb = new StringBuffer();
180: sb.append("[IniFile = " + file.toString() + "]={");
181: for (int j = 0; j < keys.size(); ++j) {
182: sb.append("\n\t" + keys.elementAt(j) + "="
183: + vals.elementAt(j));
184: }
185: sb.append("}");
186: return sb.toString();
187: }
188:
189: public static String getValue(String iniName, String name) {
190: IniFile ini = null;
191: name = name.trim();
192: try {
193: ini = new IniFile(iniName);
194:
195: String value = ini.getString(name);
196: if (value == null || value.length() == 0
197: || value.equals("UNDEFINED"))
198: return null;
199: else
200: return value;
201: } catch (IOException e) {
202: System.out.println("Loader getValue():" + e);
203: //e.printStackTrace();
204: return null;
205: } finally {
206: try {
207: ini.close();
208: } catch (IOException ee) {
209: System.out.println("Loader start():" + ee);
210: }
211: }
212: }
213:
214: public static int setValue(String iniName, String name, String value) {
215: IniFile ini = null;
216: name = name.trim();
217: value = value.trim();
218: try {
219: ini = new IniFile(iniName);
220: ini.putString(name, value);
221: } catch (IOException e) {
222: System.out.println("Loader setValue():" + e);
223: //e.printStackTrace();
224: return -1;
225: } finally {
226: try {
227: ini.close();
228: } catch (IOException ee) {
229: System.out.println("Loader start():" + ee);
230: }
231: }
232: return 0;
233: }
234: }
|