/* * Copyright (C) 1999 Norman Walsh, * This program is free software; you can redistribute it and/or modify * it to your hearts content. If you change this file, please remove * my name from it. * * WARRANTY * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE. */ import java.io.*; import java.net.URL; import java.net.MalformedURLException; import org.xml.sax.Parser; import org.xml.sax.SAXException; import org.xml.sax.InputSource; import org.xml.sax.helpers.ParserFactory; import com.ibm.xml.parsers.DOMParser; import org.w3c.dom.*; public class cfgFile { static String defaultParserClass = "com.ibm.xml.parsers.DOMParser"; static Document doc = null; public cfgFile(String cfgDoc) throws Exception { // Construct a new cfgFile using the default parser class loadDoc(cfgDoc, defaultParserClass); } public cfgFile(String cfgDoc, String parserClass) throws Exception { // Construct a new cfgFile using the specified parser class loadDoc(cfgDoc, parserClass); } public String getProfileString(String sectname, String varname, String defval) { // Find varname in sectname and return its 'value', or return defval if (doc == null) { // This should never happen... return defval; } // Find the section Element section = getSection(sectname); if (section == null) { return defval; } // Find the entry Element entry = getEntry(section, varname); if (entry == null) { return defval; } else { return entry.getAttribute("value"); } } public void setProfileString(String sectname, String varname, String value) { // Set varname to value in sectname if (doc == null) { // This should never happen... return; } Element root = doc.getDocumentElement(); Element section = getSection(sectname); if (section == null) { // Create one section = doc.createElement("section"); section.setAttribute("name", sectname); root.appendChild(section); } Element entry = getEntry(section, varname); if (entry == null) { // Create one entry = doc.createElement("entry"); entry.setAttribute("name", varname); section.appendChild(entry); } entry.setAttribute("value", value); } public void write(PrintStream out) { // Write out the configuration file Element root = doc.getDocumentElement(); out.println(""); writeElement(out, root, ""); } private void loadDoc(String cfgDoc, String parserClass) throws Exception { // Parse the cfgDoc, or construct an empty document if cfgDoc is null. // Create the underlying parser Parser parser = ParserFactory.makeParser(parserClass); if (cfgDoc == null) { // There's no document, so manufacture one with a simple // ByteInputStream... String cfgText = ""; ByteArrayInputStream cfgStream = new ByteArrayInputStream(cfgText.getBytes()); InputSource cfgSource = new InputSource(cfgStream); // Parse the "document" parser.parse(cfgSource); } else { // Create a URL for the cfgDoc URL cfgURL = createURL(cfgDoc); // Parse the document parser.parse(cfgURL.toString()); } // Initialize doc to the DOM document built by the parser doc = ((DOMParser) parser).getDocument(); } static void writeElement(PrintStream out, Element e, String indent) { // Write the XML for an element to the specified stream. // This function calls itself recursively to process chilren // of the starting element. Only element children are written; // comments, PIs, and other nodes are ignored. Node node = e.getFirstChild(); NamedNodeMap attrs = e.getAttributes(); Node attr; int count; out.print(indent + "<" + e.getTagName()); for (count = 0; count < attrs.getLength(); count++) { attr = attrs.item(count); out.print(" "); out.print(attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } if (node == null) { out.println("/>"); } else { out.println(">"); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { writeElement(out, (Element) node, indent+" "); } node = node.getNextSibling(); } out.println(indent + ""); } } private Element getSection(String name) { // Find the section named 'name' and return it return findChild(doc.getDocumentElement(), "section", name); } private Element getEntry(Element section, String name) { // Find the entry named 'name' in 'section' and return it, return findChild(section, "entry", name); } private Element findChild(Element parent, String elemname, String name) { // Find the 'elemname' child of parent named 'name' and return it; // return null if no such child exists. Node node = parent.getFirstChild(); Element child; while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { child = (Element) node; if (child.getTagName().equals(elemname) && child.getAttribute("name").equals(name)) { return child; } } node = node.getNextSibling(); } return null; } private URL createURL(String name) throws Exception { // Turn a URL-string or filename into a URL object URL url = null; File file = null; try { url = new URL(name); // if this doesn't cause an exception, return the URL return url; } catch (MalformedURLException ex) { // ignore the exception } // If it wasn't a URL, it must be a file... file = new File(name); url = new URL("file:" + file.getAbsolutePath()); return url; }; }