/****************************************************************************** * swconfig.cpp - implementation of Class SWConfig used for saving and * retrieval of configuration information * * $Id: SWConfig.java,v 1.5 2002/11/25 20:11:24 scribe Exp $ * * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org) * CrossWire Bible Society * P. O. Box 2528 * Tempe, AZ 85280-2528 * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation version 2. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * */ package org.crosswire.sword.mgr; import java.io.FileInputStream; import java.io.File; import com.objectspace.jgl.HashMap; import com.objectspace.jgl.Map; import com.objectspace.jgl.HashMapIterator; import java.io.InputStream; import java.io.StringWriter; import java.lang.StringBuffer; import java.io.ByteArrayInputStream; import java.util.HashSet; import java.util.Set; public class SWConfig { private boolean foldCase = false; public String fileName; public Map sections = new HashMap(); public boolean allowDups = false; public Set binarySections = new HashSet(); public SWConfig(String fileName) { this(fileName, null); } public SWConfig(String fileName, Set binarySections) { this(fileName, binarySections, false); } public SWConfig(String fileName, Set binarySections, boolean foldCase) { this.foldCase = foldCase; this.fileName = fileName; this.binarySections = (binarySections != null) ? binarySections : new HashSet(); if (this.binarySections != null && foldCase) { Set bsFolded = new HashSet(); for (String s : this.binarySections) { bsFolded.add(s.toUpperCase()); } this.binarySections = bsFolded; } load(); } public SWConfig(InputStream is) { this(is, null); } public SWConfig(InputStream is, Set binarySections) { this(is, binarySections, false); } public SWConfig(InputStream is, Set binarySections, boolean foldCase) { this.foldCase = foldCase; this.binarySections = (binarySections != null) ? binarySections : new HashSet(); if (this.binarySections != null && foldCase) { Set bsFolded = new HashSet(); for (String s : this.binarySections) { bsFolded.add(s.toUpperCase()); } this.binarySections = bsFolded; } load(is); } char getLine(InputStream fp, StringBuffer line) { char retval = 0; int ch = 0; StringBuffer bytes = new StringBuffer(); line.setLength(0); try { boolean junk = true; while (junk) { ch = fp.read(); if ((ch != '\n') && (ch != '\r')) junk = false; } while (ch != -1) { bytes.append((char)ch); ch = fp.read(); if ((ch == '\n') || (ch == '\r')) { if (bytes.charAt(bytes.length()-1) != '\\') break; else bytes.setLength(bytes.length()-1); } } } catch (Exception e) { e.printStackTrace(); } try { if (bytes.length() > 0) line.append(new String(bytes.toString().getBytes("iso8859-1"), "UTF-8")); } catch (Exception e) {} return (char)((ch != -1) ? 1:0); } public String getProperty(String section, String key) { String retVal = null; try { retVal = (String)((Map)sections.get(foldCase ? section.toUpperCase() : section)).get(foldCase ? key.toUpperCase() : key); } catch (Exception e) {} return retVal; } public void setProperty(String section, String key, String value) { String retVal = null; try { Map sect = (Map)sections.get(foldCase ? section.toUpperCase() : section); if (sect == null) { sect = new HashMap(allowDups); // allow duplicates sections.put(foldCase ? section.toUpperCase() : section, sect); } sect.put(foldCase ? key.toUpperCase() : key, value); } catch (Exception e) {} } /* public Iterator getProperty(String section, String key) { List results = new List(); try { retVal = (String)((Map)sections.get(section)).get(key); } catch (Exception e) {} return retVal; } */ public boolean contains(String section) { boolean retVal = false; Map sectionTest = null; try { sectionTest = (Map)sections.get(foldCase ? section.toUpperCase() : section); retVal = (sectionTest != null); } catch (Exception e) {} return retVal; } /** * @deprecated Use load() instead */ public void Load() { load(); } public void load() { FileInputStream cfile; try { cfile = new FileInputStream(new File(fileName)); load(cfile); cfile.close(); } catch (Exception e) {} } public void load(InputStream is) { String buf, key, data; StringBuffer line = new StringBuffer(""); HashMap cursect = null; String sectname = ""; boolean first = true; String newLine = "\n"; StringBuffer binarySection = null; try { sections.clear(); char end = 0; do { end = getLine(is, line); buf = line.toString(); if (buf.length() > 0) { if (buf.charAt(0) == '[') { if (!first) sections.put(sectname, (binarySection == null ? cursect : binarySection)); else first = false; cursect = new HashMap(allowDups); // allow duplicates sectname = buf.substring(1, buf.indexOf(']')); if (foldCase) sectname = sectname.toUpperCase(); binarySection = binarySections.contains(sectname) ? new StringBuffer() : null; } else { if (binarySection != null) { binarySection.append(buf).append(newLine); } else if (cursect != null) { int keylen = buf.indexOf('='); if (keylen > 0) { key = buf.substring(0, keylen); if (foldCase) key = key.toUpperCase(); data = buf.substring(keylen + 1); if (data.length() > 0) { cursect.put(key, data); //System.out.println("Putting key/value: " + key + "=" + data); } else cursect.put(key, ""); } } } } } while (end != 0); if (!first) { sections.put(sectname, binarySection == null ? cursect : binarySection); } } catch (Exception e) { e.printStackTrace(); } } public String toString() { HashMapIterator sectionIT; HashMapIterator entryIT; StringBuffer out = new StringBuffer(); // String newLine = "\n"; String newLine = "\r\n"; for (sectionIT = (HashMapIterator) sections.start(); !sectionIT.atEnd(); sectionIT.advance()) { out.append("[").append(sectionIT.key().toString()).append("]").append(newLine); if (sectionIT.value() instanceof HashMap) { for (entryIT = (HashMapIterator) ((Map) sectionIT.value()).start(); !entryIT.atEnd(); entryIT.advance()) { out.append(entryIT.key().toString()).append("=").append(entryIT.value()).append(newLine); } } else { out.append(sectionIT.value().toString()).append(newLine); } } return out.toString(); } /* void SWConfig::Save() { FILE *cfile; string buf; SectionMap::iterator sit; ConfigEntMap::iterator entry; string sectname; if ((cfile = fopen(fileName.c_str(), "w"))) { for (sit = Sections.begin(); sit != Sections.end(); sit++) { buf = "\n["; buf += (*sit).first.c_str(); buf += "]\n"; fputs(buf.c_str(), cfile); for (entry = (*sit).second.begin(); entry != (*sit).second.end(); entry++) { buf = (*entry).first.c_str(); buf += "="; buf += (*entry).second.c_str(); buf += "\n"; fputs(buf.c_str(), cfile); } } fputs("\n", cfile); // so getLine will find last line fclose(cfile); } } */ public String getBinarySection(String sectionName) { String retVal = null; try { Object section = sections.get(foldCase ? sectionName.toUpperCase() : sectionName); if (section == null) return null; if (section instanceof StringBuffer) { retVal = section.toString(); } else { HashMapIterator entryIT; StringBuffer out = new StringBuffer(); String newLine = "\r\n"; for (entryIT = (HashMapIterator) ((Map) section).start(); !entryIT.atEnd(); entryIT.advance()) { out.append(entryIT.key().toString()).append("=").append(entryIT.value()).append(newLine); } retVal = out.toString(); } } catch (Exception e) { e.printStackTrace(); } return retVal; } public void setBinarySection(String sectionName, String value) { try { StringBuffer sect = new StringBuffer(value); // allow duplicates sections.put(foldCase ? sectionName.toUpperCase() : sectionName, sect); } catch (Exception e) { e.printStackTrace(); } } public SWConfig augment(SWConfig addFrom) { HashMapIterator sectionIT; HashMapIterator entryIT; // iterator for (sectionIT = (HashMapIterator)addFrom.sections.start(); !sectionIT.atEnd(); sectionIT.advance()) { Object fromSect = sectionIT.value(); if (fromSect instanceof StringBuffer) { this.setBinarySection((String)sectionIT.key(), fromSect.toString()); } else { for (entryIT = (HashMapIterator)((Map)sectionIT.value()).start(); !entryIT.atEnd(); entryIT.advance()) { Object sect = sections.get(foldCase ? ((String)sectionIT.key()).toUpperCase() : sectionIT.key()); if (sect == null) { sect = new HashMap(allowDups); // allow duplicates sections.put(foldCase ? ((String)sectionIT.key()).toUpperCase() : sectionIT.key(), sect); } if (!(sect instanceof StringBuffer)) { ((Map)sect).put(foldCase ? ((String)entryIT.key()).toUpperCase() : entryIT.key(), entryIT.value()); } } } } return this; } public static void main(String args[]) { String params="yoyo=ma\ngreek=χαρις"; SWConfig test = new SWConfig(new ByteArrayInputStream(("[yo]\n"+params).getBytes())); System.out.println(test.toString()); System.out.println("greek: " + test.getProperty("yo", "greek")); } }