001 package groovy.xml.streamingmarkupsupport; 002 /* 003 004 Copyright 2004 (C) John Wilson. All Rights Reserved. 005 006 Redistribution and use of this software and associated documentation 007 ("Software"), with or without modification, are permitted provided 008 that the following conditions are met: 009 010 1. Redistributions of source code must retain copyright 011 statements and notices. Redistributions must also contain a 012 copy of this document. 013 014 2. Redistributions in binary form must reproduce the 015 above copyright notice, this list of conditions and the 016 following disclaimer in the documentation and/or other 017 materials provided with the distribution. 018 019 3. The name "groovy" must not be used to endorse or promote 020 products derived from this Software without prior written 021 permission of The Codehaus. For written permission, 022 please contact info@codehaus.org. 023 024 4. Products derived from this Software may not be called "groovy" 025 nor may "groovy" appear in their names without prior written 026 permission of The Codehaus. "groovy" is a registered 027 trademark of The Codehaus. 028 029 5. Due credit should be given to The Codehaus - 030 http://groovy.codehaus.org/ 031 032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS 033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 043 OF THE POSSIBILITY OF SUCH DAMAGE. 044 045 */ 046 047 import groovy.lang.Closure; 048 import groovy.lang.GroovyInterceptable; 049 import groovy.lang.GroovyObjectSupport; 050 051 import java.util.Collections; 052 import java.util.HashMap; 053 import java.util.Map; 054 055 public class BaseMarkupBuilder extends Builder { 056 public BaseMarkupBuilder(final Map namespaceMethodMap) { 057 super(namespaceMethodMap); 058 } 059 060 public Object bind(final Closure root) { 061 return new Document(root, this.namespaceMethodMap); 062 } 063 064 private static class Document extends Built implements GroovyInterceptable { 065 private Object out; 066 private final Map pendingNamespaces = new HashMap(); 067 private final Map namespaces = new HashMap(); 068 private final Map specialProperties = new HashMap(); 069 private String prefix = ""; 070 071 { 072 073 namespaces.put("xml", "http://www.w3.org/XML/1998/namespace"); // built in namespace 074 namespaces.put("mkp", "http://www.codehaus.org/Groovy/markup/keywords"); // pseudo namespace for markup keywords 075 076 specialProperties.put("out", new OutputSink("out") { 077 public Object leftShift(final Object value) { 078 return leftShift("yield", value); 079 } 080 }); 081 specialProperties.put("unescaped", new OutputSink("unescaped") { 082 public Object leftShift(final Object value) { 083 return leftShift("yieldUnescaped", value); 084 } 085 }); 086 specialProperties.put("namespaces", new OutputSink("namespaces") { 087 public Object leftShift(final Object value) { 088 return leftShift("declareNamespace", value); 089 } 090 }); 091 specialProperties.put("pi", new OutputSink("pi") { 092 public Object leftShift(final Object value) { 093 return leftShift("pi", value); 094 } 095 }); 096 specialProperties.put("comment", new OutputSink("comment") { 097 public Object leftShift(final Object value) { 098 return leftShift("comment", value); 099 } 100 }); 101 } 102 103 private abstract class OutputSink extends GroovyObjectSupport { 104 private final String name; 105 106 public OutputSink(final String name) { 107 this.name = name; 108 } 109 110 public Object invokeMethod(final String name, final Object args) { 111 Document.this.prefix = this.name; 112 return Document.this.invokeMethod(name, args); 113 } 114 115 public abstract Object leftShift(Object item); 116 117 protected Object leftShift(final String command, final Object value) { 118 Document.this.getProperty("mkp"); 119 Document.this.invokeMethod(command, new Object[]{value}); 120 return this; 121 } 122 } 123 124 public Document(final Closure root, final Map namespaceMethodMap) { 125 super(root, namespaceMethodMap); 126 } 127 128 /* (non-Javadoc) 129 * @see groovy.lang.GroovyObject#invokeMethod(java.lang.String, java.lang.Object) 130 */ 131 public Object invokeMethod(final String name, final Object args) { 132 final Object[] arguments = (Object[]) args; 133 Map attrs = Collections.EMPTY_MAP; 134 Object body = null; 135 136 137 // 138 // Sort the parameters out 139 // 140 for (int i = 0; i != arguments.length; i++) { 141 final Object arg = arguments[i]; 142 143 if (arg instanceof Map) { 144 attrs = (Map)arg; 145 } else if (arg instanceof Closure) { 146 final Closure c = ((Closure) arg); 147 148 c.setDelegate(this); 149 body = c.asWritable(); 150 } else { 151 body = arg; 152 } 153 } 154 155 // 156 // call the closure corresponding to the tag 157 // 158 final Object uri; 159 160 if (this.pendingNamespaces.containsKey(this.prefix)) { 161 uri = this.pendingNamespaces.get(this.prefix); 162 } else if (this.namespaces.containsKey(this.prefix)) { 163 uri = this.namespaces.get(this.prefix); 164 } else { 165 uri = ":"; 166 } 167 168 final Object[] info = (Object[])this.namespaceSpecificTags.get(uri); 169 final Map tagMap = (Map)info[2]; 170 final Closure defaultTagClosure = (Closure)info[0]; 171 172 final String prefix = this.prefix; 173 this.prefix = ""; 174 175 if (tagMap.containsKey(name)) { 176 return ((Closure)tagMap.get(name)).call(new Object[]{this, this.pendingNamespaces, this.namespaces, this.namespaceSpecificTags, prefix, attrs, body, this.out}); 177 } else { 178 return defaultTagClosure.call(new Object[]{name, this, this.pendingNamespaces, this.namespaces, this.namespaceSpecificTags, prefix, attrs, body, this.out}); 179 } 180 } 181 182 /* (non-Javadoc) 183 * @see groovy.lang.GroovyObject#getProperty(java.lang.String) 184 */ 185 public Object getProperty(final String property) { 186 final Object special = this.specialProperties.get(property); 187 188 if (special == null) { 189 this.prefix = property; 190 return this; 191 } else { 192 return special; 193 } 194 } 195 196 /* (non-Javadoc) 197 * @see groovy.lang.GroovyObject#setProperty(java.lang.String, java.lang.Object) 198 */ 199 public void setProperty(String property, Object newValue) { 200 if ("trigger".equals(property)) { 201 this.out = newValue; 202 this.root.call(this); 203 } else { 204 super.setProperty(property, newValue); 205 } 206 } 207 } 208 }