|
What this is
Other links
The source code/* $Id: configuration.java,v 1.14 2001/04/10 17:14:51 agarcia3 Exp $ webEditor. The new way in content management Copyright (C) 2001 Alfredo Garcia 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. 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 webEditor.core; import java.io.*; import java.util.*; import org.w3c.dom.*; import org.apache.xerces.dom.*; import org.apache.xml.serialize.*; import webEditor.util.xml_parser; /** * Encapsulates the access to the has table configuration structures. * * @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia */ public class configuration { /** * Table with the Configuration parameters */ private static Hashtable configTable; /** * Access class to the configuration files */ private static xml_parser myParser; public configuration(String confFile) { // The class constructor just needs to read the config values myParser = new xml_parser (); configTable = myParser.parseConfFile (confFile); } public configuration(Hashtable givenTable) { // The class constructor just needs to read the config values configTable = givenTable; } public configuration() { // Empty configuration configTable = new Hashtable(); } /** * Returns the stored value for the given "category" and "key" * @param category Config category to read * @param key Config key to read * @return String Associated value or null if the key doesn't exists. */ public String readValue (String category, String key) { String value = null; Hashtable myTable = null; myTable = (Hashtable) this.configTable.get (category); if ( myTable == null ) { return (null); } value = (String) myTable.get (key); if ( value == null ) { return (null); } return (value); } /** * Stores into the configuration structure the given value * If the category doesn't exists, a new category with this name * will be created * Any other previous value with the same "category" and "key" * will be overwriten. * @param category category of the key * @param key key of the value * @param value value to write * @return void */ public void storeValue (String category, String key, String value) { Hashtable myTable = null; myTable = (Hashtable) this.configTable.get (category); if ( myTable == null ) { // We don't have this category, so we create one myTable = new Hashtable(); } myTable.put (key, (String) value); this.configTable.put (category, (Hashtable) myTable); return; } /** * Reads all the values for a given category * @param category Name of the category * @return Hashtable Table with the values of the category */ public Hashtable readCategory (String category) { Hashtable myTable = new Hashtable(); if ( category == null ) { return (myTable); } myTable = (Hashtable) this.configTable.get (category); return (myTable); } /** * Stores into the configuration structure the given category * If the category doesn't exists, a new category with this name * will be created * Any other previous values with the same "category" * will be overwriten. * @param category Name of the category * @param values Hashtable with the category values * @return void */ public void storeCategory (String category, Hashtable values) { this.configTable.put (category, (Hashtable) values); return; } /** * Modify the content of the configuration file. * @param dataStream Hash with the new values to write * @param configDoc DOM tree of the document * @return Document New DOM tree */ public Document writeDoc (Hashtable dataStream, Document configDoc) { String rootTag = null; String tagName = null; String tagValue = null; Element root = configDoc.getDocumentElement(); //rootTag = root.getTagName(); rootTag = "category"; NodeList list = configDoc.getElementsByTagName(rootTag).item(0).getChildNodes(); // Now, we look for document tags that appears in the hash, and // we try to change its content for (Enumeration e = dataStream.keys() ; e.hasMoreElements() ;) { tagName = (String) e.nextElement(); tagValue = (String) dataStream.get (tagName); for (int i=0; i < list.getLength(); i++) { if (list.item(i).getAttributes().getNamedItem("code").getNodeValue().equals(tagName)) { Node source = list.item(i); Node myDocNode = source.cloneNode (true); myDocNode.getLastChild().setNodeValue (tagValue); Node parent = source.getParentNode(); Node temp = parent.replaceChild (myDocNode, source); } } } return (configDoc); } /** * Saves the modifications of the configuration file * @param filePath Complete path of the configuration file * @param doc DOM tree of the document * @return void */ public void saveConfig (String filePath, Document doc) { if (filePath == null) { return; } try { OutputFormat format = new OutputFormat (doc, "ISO-8859-1" ,true); format.setIndent (2); format.setLineWidth (80); format.setPreserveSpace (true); StringWriter stringOut = new StringWriter (); XMLSerializer serial = new XMLSerializer (stringOut, format); serial.asDOMSerializer (); serial.serialize (doc.getDocumentElement()); // We need to delete the file before the write operation File fd = new File (filePath); if ( fd.exists() ) { fd.delete(); } RandomAccessFile myFile = new RandomAccessFile (filePath, "rw"); myFile.write (stringOut.toString().getBytes()); myFile.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Returns the internal hashtable * @return Hashtable */ public Hashtable returnData () { return (configTable); } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.