From: Marcus <the...@us...> - 2004-03-26 19:12:52
|
Update of /cvsroot/junk/junk/WEB-INF/classes/junk/controller In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1494 Added Files: ConfigurationAction.java Log Message: initial import --- NEW FILE: ConfigurationAction.java --- /* * juNK - a file search system for smb shares * * Copyright 2004 by * Marcus Proest (theevilflow at users dot sf dot net) * Uwe van Heesch (tyron_e at users dot sf dot net) * * This file is part of junk (java useful net kollektor). * * junk 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; either version 2 of the License, or * (at your option) any later version. * * junk 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. * * You should have received a copy of the GNU General Public License * along with junk; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /* * ConfigurationAction.java * * Created on 24. März 2004, 19:41 */ package junk.controller; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import junk.util.ConfigurationForm; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import java.util.TreeSet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.crimson.tree.XmlDocument; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * Action to handle the configuration dialog */ public class ConfigurationAction extends Action { /** * savekey for current plugin */ public static final String KEY_CHOSEN_PLUGIN = "config-chosen-plugin"; /** * savekey for complete configuration */ public static final String KEY_PLUGIN_LIST = "config-plugin-list"; /** * struts-config.xml root element */ private static final String ROOT = "struts-config"; /** * struts-config.xml plug-in element */ private static final String PLUGIN = "plug-in"; /** * struts-config.xml className element */ private static final String NAME = "className"; /** * struts-config.xml set-property element */ private static final String SET_PROP = "set-property"; /** * struts-config.xml property element */ private static final String PROP = "property"; /** * struts-config.xml value element */ private static final String VAL = "value"; /** * struts-config.xml description property id */ private static final String DESC = "description"; /** log writer */ private Log log = LogFactory.getLog(ConfigurationAction.class); /** execute method of the action * @param am actionMapping * @param af actionForm * @param hsrq httpServletRequest * @param hsrp httpServletRespone * @return actionforward * @throws Exception Exception */ public ActionForward execute(ActionMapping am, ActionForm af, HttpServletRequest hsrq, HttpServletResponse hsrp) throws Exception { //- String plugin = hsrq.getParameter("plugin"); String reset = hsrq.getParameter("reset"); String path = this.getServlet().getServletContext().getRealPath("/WEB-INF") + "/junk-config.xml"; if (plugin == null) { //prevent nullpointers plugin = ""; } if (reset == null) { //prevent nullpointers reset = ""; } ConfigurationForm cf = (ConfigurationForm) af; //cast the form TreeSet plugins = this.loadConfig(path, plugin); //load the saved data if (!plugin.equals("")) { //if a plugin is selected... if (reset.equals("true")) { //if we clicked a link, reset the form //values by ref with loaded data resetForm(plugins, plugin, cf); } else { //if we klicked the button //apply changed config from cf to treeset plugins = applyConfig(plugins, plugin, cf); //save the applied stuff this.saveConfig(path, plugins); } } //this might become superfluos... check from time to time hsrq.setAttribute(KEY_CHOSEN_PLUGIN, plugin); hsrq.setAttribute(KEY_PLUGIN_LIST, plugins.iterator()); return am.findForward("config"); } /** * apply changed config values from the form to the treeset * @param plugins plugin configuration TreeSet * @param plugin chosen plugin * @param cf ConfigurationForm * @return the plugin configuration TreeSet, with modified values from the ConfigurationForm */ private TreeSet applyConfig(TreeSet plugins, String plugin, ConfigurationForm cf) { TreeSet retval = new TreeSet(); Iterator i = plugins.iterator(); while (i.hasNext()) { //cycle through plugins PluginWrapper plw = (PluginWrapper) i.next(); if (plw.getName().equals(plugin)) { //change values for chosen plugin Iterator j = cf.keySet().iterator(); TreeSet props = new TreeSet(); String desc = plw.getDescription(); while (j.hasNext()) { String key = (String) j.next(); String value = (String) cf.getValue(key); props.add(new PropertyWrapper(key, value)); if (key.equals(DESC)) { desc = value; //apply description for header display } } //create new wrapper plw = new PluginWrapper(plw.getName(), desc, props, true); //add it to return value retval.add(plw); } else { //save values for unchanged plugin retval.add(plw); } } return retval; } /** * load the config from the xml file * @param path path to junk-config.xml * @param chosenPlugin chosen plugin * @return the plugin configuration as defined in junk-config.xml * @throws ParserConfigurationException ParserConfigurationException * @throws SAXException SAXException * @throws IOException IOException */ private TreeSet loadConfig(String path, String chosenPlugin) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, IOException { TreeSet plugins = new TreeSet(); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new File(path)); // normalize text representation doc.getDocumentElement().normalize(); NodeList listOfPlugins = doc.getElementsByTagName(PLUGIN); //cycle through plugins for (int x = 0; x < listOfPlugins.getLength(); x++) { Node plugin = listOfPlugins.item(x); String name = plugin.getAttributes().getNamedItem(NAME) .getNodeValue(); //className //apply name to description in case there is no real description String desc = name; boolean show = name.equals(chosenPlugin); //disply THIS plugin NodeList listOfProps = plugin.getChildNodes(); //list properties TreeSet props = new TreeSet(); //cycle through properties for (int y = 0; y < listOfProps.getLength(); y++) { Node property = listOfProps.item(y); //the dom parser treats spaces, linewraps and tabs as #text node. //we only want to edit #element nodes if (property.getNodeType() == property.ELEMENT_NODE) { //get the key/ value pair and wrap it NamedNodeMap setProp = property.getAttributes(); String prop = setProp.getNamedItem(PROP).getNodeValue(); String val = setProp.getNamedItem(VAL).getNodeValue(); props.add(new PropertyWrapper(prop, val)); if (prop.equals(DESC)) { //apply description desc = val; } } } //wrap the plugin PluginWrapper plw = new PluginWrapper(name, desc, props, show); plugins.add(plw); } return plugins; } /** * reset the form and populate it with values from the xml file. * every operation on cf needs to be byref, * so it is never allowd to assign cf * @param plugins plugin configuration TreeSet * @param plugin chosen plugin * @param cf ConfigurationForm */ private void resetForm(TreeSet plugins, String plugin, ConfigurationForm cf) { //clear the data, because it might still contain data from another plugin cf.reset(); Iterator i = plugins.iterator(); while (i.hasNext()) { PluginWrapper plw = (PluginWrapper) i.next(); //find the correct plugin if (plw.getName().equals(plugin)) { Iterator j = ((TreeSet) plw.getProperties()).iterator(); //copy the values while (j.hasNext()) { PropertyWrapper prw = (PropertyWrapper) j.next(); cf.setValue(prw.getKey(), prw.getValue()); } break; } } } /** * save the config to the xml file * @param path path to junk-config.xml * @param plugins plugin configuration TreeSet * @throws FactoryConfigurationError FactoryConfigurationError * @throws ParserConfigurationException ParserConfigurationException * @throws IOException IOException */ private void saveConfig(String path, TreeSet plugins) throws FactoryConfigurationError, ParserConfigurationException, IOException { String PUBLIC_ID = "-//Apache Software Foundation//" + "DTD Struts Configuration 1.1//EN"; String SYSTEM_ID = "http://jakarta.apache.org/struts/" + "dtds/struts-config_1_1.dtd"; String I_SUBSET = ""; //- DocumentBuilderFactory domFactory = null; DocumentBuilder domBuilder = null; domFactory = DocumentBuilderFactory.newInstance(); domBuilder = domFactory.newDocumentBuilder(); Document doc = domBuilder.newDocument(); //create and append root element Element root = doc.createElement(ROOT); doc.appendChild(root); Iterator i = plugins.iterator(); //cycle over plugins while (i.hasNext()) { //create plugin tags with className attribute PluginWrapper plw = (PluginWrapper) i.next(); Element plugin = doc.createElement(PLUGIN); plugin.setAttribute(NAME, plw.getName()); root.appendChild(plugin); Iterator j = plw.getProperties().iterator(); while (j.hasNext()) { //create set-property with attributes PropertyWrapper prw = (PropertyWrapper) j.next(); Element prop = doc.createElement(SET_PROP); prop.setAttribute(PROP, prw.getKey()); prop.setAttribute(VAL, prw.getValue()); plugin.appendChild(prop); } } //save that doc! BufferedWriter bufferWriter = new BufferedWriter(new FileWriter(path)); XmlDocument xDoc = (XmlDocument) doc; xDoc.setDoctype(PUBLIC_ID, SYSTEM_ID, I_SUBSET); xDoc.write(bufferWriter); bufferWriter.close(); } /** * wrapper for plugins */ public class PluginWrapper implements Comparable { /** * description of the pluggins, should always be the same as the value * of the "description" property */ private String desc; /** * className attribute value */ private String name; /** * set-property values */ private TreeSet props; /** * whether to show it or not... this needs some investigation, * i dont know if this is still used */ private boolean show; /** * constructor * @param name className * @param desc description * @param props properties * @param show display? */ public PluginWrapper(String name, String desc, TreeSet props, boolean show) { this.name = name; this.desc = desc; this.props = props; this.show = show; } /** * return description * @return description */ public String getDescription() { return this.desc; } /** * return classname * @return name */ public String getName() { return this.name; } /** * return properties * @return properties */ public TreeSet getProperties() { return this.props; } /** * return show (it must go on!) * @return display? */ public boolean getShow() { return this.show; } /** * implemented to achieve automatic sorting within treesets * @return compare value * @param obj object to compare to */ public int compareTo(Object obj) { return -((PluginWrapper) obj).name.compareTo(this.name); } } /** * wrapper for pluginproperties */ public class PropertyWrapper implements Comparable { /** * key */ private String key; /** * vvalue */ private String value; /** * constructor * @param key key * @param value value */ public PropertyWrapper(String key, String value) { this.key = key; this.value = value; } /** * return key * @return key */ public String getKey() { return this.key; } /** * return value * @return value */ public String getValue() { return this.value; } /** * implemented to achieve automatic sorting within treesets * @return compare value * @param obj object to compare to */ public int compareTo(Object obj) { return -((PropertyWrapper) obj).key.compareTo(this.key); } } } |