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 InputIniFile extends Object {
047: private Hashtable hash;
048: private DataInputStream in;
049: private String name;
050:
051: public Hashtable getTable() {
052: return hash;
053: }
054:
055: public InputIniFile(String name) throws IOException {
056: this .name = name;
057: in = new DataInputStream(new FileInputStream(name));
058: hash = new Hashtable();
059: load();
060: }
061:
062: public InputIniFile(InputStream in) throws IOException {
063: this .name = in.toString();
064: this .in = new DataInputStream(in);
065: hash = new Hashtable();
066: load();
067: }
068:
069: public synchronized String getString(String key) throws IOException {
070: return (String) hash.get(key);
071: }
072:
073: public int getInt(String key) throws IOException {
074: try {
075: return Integer.parseInt(getString(key));
076: } catch (NumberFormatException e) {
077: return 0;
078: }
079: }
080:
081: public boolean getBoolean(String key) throws IOException {
082: String s = getString(key);
083: if ("True".equalsIgnoreCase(s))
084: return true;
085: if ("On".equalsIgnoreCase(s))
086: return true;
087: if ("1".equals(s))
088: return true;
089: return false;
090: }
091:
092: public synchronized void close() throws IOException {
093: if (in != null)
094: in.close();
095: in = null;
096: }
097:
098: private String postponed = null;
099:
100: private String readLine(DataInputStream in) throws IOException {
101: String x = postponed == null ? in.readLine() : postponed;
102: postponed = null;
103: if (x == null)
104: return x;
105: x = x.trim();
106:
107: while (x.endsWith("\\")) {
108: String v = in.readLine();
109: if (v == null)
110: break;
111: if (!v.startsWith(" ")) {
112: postponed = v;
113: break;
114: }
115: int i = v.indexOf('#');
116: if (i > 0) {
117: if (v.endsWith("\\"))
118: v = v.substring(0, i) + " \\";
119: else
120: v = v.substring(0, i);
121: }
122: x = x.substring(0, x.length() - 1) + v.trim();
123: }
124:
125: int j = x.indexOf('#');
126: if (j == 0)
127: return "";
128: if (j > 0)
129: x = x.substring(0, j);
130: return x;
131: }
132:
133: private void load() throws IOException {
134: String line = null;
135: while ((line = readLine(in)) != null) {
136: StringTokenizer st = new StringTokenizer(line, "=", true); // key = value
137: if (st.countTokens() < 3)
138: continue; // syntax error, ignored
139: String key = st.nextToken().trim();
140: st.nextToken(); // '='
141: String value = st.nextToken("").trim();
142: hash.put(key, value);
143: // System.err.println(key + "=["+value+"]");
144: }
145: }
146: }
|