001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.util;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.logging.Logger;
026: import org.onemind.commons.java.datastructure.InheritableValueMap;
027: import org.onemind.commons.java.lang.reflect.ReflectUtils;
028: import org.onemind.commons.java.util.StringUtils;
029: import org.onemind.commons.java.xml.digest.DefaultDigester;
030: import org.onemind.commons.java.xml.digest.SaxDigesterHandler;
031: import org.xml.sax.Attributes;
032: import org.xml.sax.SAXException;
033:
034: public class ComponentOptions extends DefaultDigester {
035:
036: /** the logger **/
037: private static final Logger _logger = Logger
038: .getLogger(ComponentOptions.class.getName());
039:
040: private class ClassConfigDigester extends DefaultDigester {
041:
042: private String[] _options;
043:
044: public ClassConfigDigester(String[] aspects) {
045: super ("class");
046: _options = aspects;
047: }
048:
049: public void startDigest(SaxDigesterHandler handler,
050: Attributes attrs) throws SAXException {
051: handler.addSubDigester(this );
052: String className = attrs.getValue("name");
053: try {
054: _currentClass = ReflectUtils.getClass(className);
055: for (int i = 0; i < _options.length; i++) {
056: handler.addSubDigester("options",
057: new OptionConfigDigester(_options[i]));
058: }
059: ;
060: } catch (ClassNotFoundException e) {
061: _logger
062: .warning("Cannot configure options for not-found class "
063: + className);
064: }
065: }
066: }
067:
068: private class OptionConfigDigester extends DefaultDigester {
069:
070: StringBuffer buffer = new StringBuffer();
071:
072: public OptionConfigDigester(String name) {
073: super (name);
074: // TODO Auto-generated constructor stub
075: }
076:
077: public void startDigest(SaxDigesterHandler handler,
078: Attributes attrs) throws SAXException {
079: buffer.delete(0, buffer.length());
080: }
081:
082: public void characters(SaxDigesterHandler handler,
083: char[] chars, int offset, int length)
084: throws SAXException {
085: String str = new String(chars).substring(offset,
086: offset + length).trim();
087: buffer.append(str);
088: super .characters(handler, chars, offset, length);
089: }
090:
091: public void endDigest(SaxDigesterHandler handler)
092: throws SAXException {
093: InheritableValueMap config = (InheritableValueMap) _configs
094: .get(getElementName());
095: config.put(_currentClass, buffer.toString());
096: super .endDigest(handler);
097: }
098: }
099:
100: private Class _currentClass;
101:
102: private Map _configs = new HashMap();
103:
104: public ComponentOptions() {
105: super ("componentOptions");
106: }
107:
108: public void startDigest(SaxDigesterHandler handler, Attributes attrs)
109: throws SAXException {
110: String optionNames = attrs.getValue("optionNames");
111: if (StringUtils.isNullOrEmpty(optionNames)) {
112: throw new SAXException(
113: "\"optionNames\" attributes must be defined");
114: }
115: String[] options = optionNames.split(",");
116: for (int i = 0; i < options.length; i++) {
117: _configs.put(options[i], new InheritableValueMap());
118: }
119: ClassConfigDigester dig = new ClassConfigDigester(options);
120: handler.addSubDigester(dig);
121: }
122:
123: public void endDigest(SaxDigesterHandler handler)
124: throws SAXException {
125: // TODO Auto-generated method stub
126: super .endDigest(handler);
127: }
128:
129: public InheritableValueMap getOptions(String option) {
130: InheritableValueMap dim = (InheritableValueMap) _configs
131: .get(option);
132: if (dim == null) {
133: throw new IllegalArgumentException("No option " + option
134: + " defined");
135: } else {
136: return dim;
137: }
138: }
139: }
|