/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Email: thlee@onemindsoft.org
*/
package org.onemind.swingweb.util;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.onemind.commons.java.datastructure.InheritableValueMap;
import org.onemind.commons.java.lang.reflect.ReflectUtils;
import org.onemind.commons.java.util.StringUtils;
import org.onemind.commons.java.xml.digest.DefaultDigester;
import org.onemind.commons.java.xml.digest.SaxDigesterHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
public class ComponentOptions extends DefaultDigester
{
/** the logger **/
private static final Logger _logger = Logger.getLogger(ComponentOptions.class.getName());
private class ClassConfigDigester extends DefaultDigester
{
private String[] _options;
public ClassConfigDigester(String[] aspects)
{
super("class");
_options = aspects;
}
public void startDigest(SaxDigesterHandler handler, Attributes attrs) throws SAXException
{
handler.addSubDigester(this);
String className = attrs.getValue("name");
try
{
_currentClass = ReflectUtils.getClass(className);
for (int i = 0; i < _options.length; i++)
{
handler.addSubDigester("options", new OptionConfigDigester(_options[i]));
};
} catch (ClassNotFoundException e)
{
_logger.warning("Cannot configure options for not-found class " + className);
}
}
}
private class OptionConfigDigester extends DefaultDigester
{
StringBuffer buffer = new StringBuffer();
public OptionConfigDigester(String name)
{
super(name);
// TODO Auto-generated constructor stub
}
public void startDigest(SaxDigesterHandler handler, Attributes attrs) throws SAXException
{
buffer.delete(0, buffer.length());
}
public void characters(SaxDigesterHandler handler, char[] chars, int offset, int length) throws SAXException
{
String str = new String(chars).substring(offset, offset + length).trim();
buffer.append(str);
super.characters(handler, chars, offset, length);
}
public void endDigest(SaxDigesterHandler handler) throws SAXException
{
InheritableValueMap config = (InheritableValueMap) _configs.get(getElementName());
config.put(_currentClass, buffer.toString());
super.endDigest(handler);
}
}
private Class _currentClass;
private Map _configs = new HashMap();
public ComponentOptions()
{
super("componentOptions");
}
public void startDigest(SaxDigesterHandler handler, Attributes attrs) throws SAXException
{
String optionNames = attrs.getValue("optionNames");
if (StringUtils.isNullOrEmpty(optionNames))
{
throw new SAXException("\"optionNames\" attributes must be defined");
}
String[] options = optionNames.split(",");
for (int i = 0; i < options.length; i++)
{
_configs.put(options[i], new InheritableValueMap());
}
ClassConfigDigester dig = new ClassConfigDigester(options);
handler.addSubDigester(dig);
}
public void endDigest(SaxDigesterHandler handler) throws SAXException
{
// TODO Auto-generated method stub
super.endDigest(handler);
}
public InheritableValueMap getOptions(String option)
{
InheritableValueMap dim = (InheritableValueMap) _configs.get(option);
if (dim == null)
{
throw new IllegalArgumentException("No option " + option + " defined");
} else
{
return dim;
}
}
}