001: package groovy.xml.streamingmarkupsupport;
002:
003: /*
004:
005: Copyright 2004 (C) John Wilson. All Rights Reserved.
006:
007: Redistribution and use of this software and associated documentation
008: ("Software"), with or without modification, are permitted provided
009: that the following conditions are met:
010:
011: 1. Redistributions of source code must retain copyright
012: statements and notices. Redistributions must also contain a
013: copy of this document.
014:
015: 2. Redistributions in binary form must reproduce the
016: above copyright notice, this list of conditions and the
017: following disclaimer in the documentation and/or other
018: materials provided with the distribution.
019:
020: 3. The name "groovy" must not be used to endorse or promote
021: products derived from this Software without prior written
022: permission of The Codehaus. For written permission,
023: please contact info@codehaus.org.
024:
025: 4. Products derived from this Software may not be called "groovy"
026: nor may "groovy" appear in their names without prior written
027: permission of The Codehaus. "groovy" is a registered
028: trademark of The Codehaus.
029:
030: 5. Due credit should be given to The Codehaus -
031: http://groovy.codehaus.org/
032:
033: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
034: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
035: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
037: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
039: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
040: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
041: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
042: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
043: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
044: OF THE POSSIBILITY OF SUCH DAMAGE.
045:
046: */
047:
048: import groovy.lang.Closure;
049: import groovy.lang.GroovyObjectSupport;
050:
051: import java.util.HashMap;
052: import java.util.Iterator;
053: import java.util.List;
054: import java.util.Map;
055:
056: public abstract class Builder extends GroovyObjectSupport {
057: protected final Map namespaceMethodMap = new HashMap();
058:
059: public Builder(final Map namespaceMethodMap) {
060: final Iterator keyIterator = namespaceMethodMap.keySet()
061: .iterator();
062:
063: while (keyIterator.hasNext()) {
064: final Object key = keyIterator.next();
065: final List value = (List) namespaceMethodMap.get(key);
066: final Closure dg = ((Closure) value.get(1)).asWritable();
067:
068: this .namespaceMethodMap.put(key, new Object[] {
069: value.get(0), dg,
070: fettleMethodMap(dg, (Map) value.get(2)) });
071: }
072: }
073:
074: private static Map fettleMethodMap(final Closure defaultGenerator,
075: final Map methodMap) {
076: final Map newMethodMap = new HashMap();
077: final Iterator keyIterator = methodMap.keySet().iterator();
078:
079: while (keyIterator.hasNext()) {
080: final Object key = keyIterator.next();
081: final Object value = methodMap.get(key);
082:
083: if ((value instanceof Closure)) {
084: newMethodMap.put(key, value);
085: } else {
086: newMethodMap.put(key, defaultGenerator
087: .curry((Object[]) value));
088: }
089: }
090:
091: return newMethodMap;
092: }
093:
094: abstract public Object bind(Closure root);
095:
096: protected static abstract class Built extends GroovyObjectSupport {
097: protected final Closure root;
098: protected final Map namespaceSpecificTags = new HashMap();
099:
100: public Built(final Closure root, final Map namespaceTagMap) {
101: this .namespaceSpecificTags.putAll(namespaceTagMap);
102:
103: this .root = (Closure) root.clone();
104:
105: this.root.setDelegate(this);
106: }
107: }
108: }
|