View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $ 3 * $Revision: 1.7 $ 4 * $Date: 2002/05/17 15:18:12 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $ 61 */ 62 package org.apache.commons.jelly.tags.betwixt; 63 64 import org.apache.commons.beanutils.Converter; 65 import org.apache.commons.beanutils.ConvertUtils; 66 import org.apache.commons.beanutils.ConversionException; 67 68 import org.apache.commons.betwixt.XMLIntrospector; 69 import org.apache.commons.betwixt.strategy.CapitalizeNameMapper; 70 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper; 71 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper; 72 import org.apache.commons.betwixt.strategy.NameMapper; 73 74 import org.apache.commons.jelly.JellyException; 75 import org.apache.commons.jelly.MissingAttributeException; 76 import org.apache.commons.jelly.TagSupport; 77 import org.apache.commons.jelly.XMLOutput; 78 79 import org.apache.commons.logging.Log; 80 import org.apache.commons.logging.LogFactory; 81 82 /*** 83 * Creates a Betwixt XMLIntrospector instance that can be used by the other Betwixt tags.</p> 84 * 85 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 86 * @version $Revision: 1.7 $ 87 */ 88 public class IntrospectorTag extends TagSupport { 89 90 /*** The Log to which logging calls will be made. */ 91 private static final Log log = LogFactory.getLog(IntrospectorTag.class); 92 93 private XMLIntrospector introspector; 94 private String var; 95 96 static { 97 98 // register converters to standard Strategies 99 ConvertUtils.register( 100 new Converter() { 101 public Object convert(Class type, Object value) { 102 if ( value instanceof String ) { 103 return createNameMapper((String) value); 104 } 105 else if ( value == null ) { 106 return null; 107 } 108 else { 109 throw new ConversionException( 110 "Don't know how to convert: " + value 111 + " of type: " + value.getClass().getName() 112 + " into a NameMapper" 113 ); 114 } 115 } 116 }, 117 NameMapper.class 118 ); 119 } 120 121 122 123 public IntrospectorTag() { 124 } 125 126 // Tag interface 127 //------------------------------------------------------------------------- 128 public void doTag(final XMLOutput output) throws Exception { 129 130 if ( var == null ) { 131 throw new MissingAttributeException( "var" ); 132 } 133 invokeBody(output); 134 135 XMLIntrospector introspector = getIntrospector(); 136 137 context.setVariable( var, introspector ); 138 139 // now lets clear this introspector so that its recreated again next time 140 this.introspector = null; 141 } 142 143 // Properties 144 //------------------------------------------------------------------------- 145 146 /*** 147 * @return the current XMLIntrospector, lazily creating one if required 148 */ 149 public XMLIntrospector getIntrospector() { 150 if ( introspector == null ) { 151 introspector = createIntrospector(); 152 } 153 return introspector; 154 } 155 156 /*** 157 * Sets whether attributes or elements should be used for primitive types. 158 * The default is false. 159 */ 160 public void setAttributesForPrimitives(boolean attributesForPrimitives) { 161 getIntrospector().setAttributesForPrimitives(attributesForPrimitives); 162 } 163 164 /*** 165 * Sets the name mapper used for element names. 166 * You can also use the Strings 'lowercase', 'uppercase' or 'hyphenated' 167 * as aliases to the common name mapping strategies or specify a class name String. 168 */ 169 public void setElementNameMapper(NameMapper nameMapper) { 170 getIntrospector().setElementNameMapper(nameMapper); 171 } 172 173 /*** 174 * Sets the name mapper used for attribute names. 175 * You can also use the Strings 'lowercase', 'uppercase' or 'hyphenated' 176 * as aliases to the common name mapping strategies or specify a class name String. 177 */ 178 public void setAttributeNameMapper(NameMapper nameMapper) { 179 getIntrospector().setAttributeNameMapper(nameMapper); 180 } 181 182 183 /*** 184 * Sets the variable name to output the new XMLIntrospector to. 185 * If this attribute is not specified then this tag must be nested 186 * inside an <parse> or <output> tag 187 */ 188 public void setVar(String var) { 189 this.var = var; 190 } 191 192 // Implementation methods 193 //------------------------------------------------------------------------- 194 195 /*** 196 * Static helper method which will convert the given string into 197 * standard named strategies such as 'lowercase', 'uppercase' or 'hyphenated' 198 * or use the name as a class name and create a new instance. 199 */ 200 protected static NameMapper createNameMapper(String name) { 201 if ( name.equalsIgnoreCase( "lowercase" ) ) { 202 return new DecapitalizeNameMapper(); 203 } 204 else if ( name.equalsIgnoreCase( "uppercase" ) ) { 205 return new CapitalizeNameMapper(); 206 } 207 else if ( name.equalsIgnoreCase( "hyphenated" ) ) { 208 return new HyphenatedNameMapper(); 209 } 210 else { 211 // lets try load the class of this name 212 Class theClass = null; 213 try { 214 theClass = Thread.currentThread().getContextClassLoader().loadClass( name ); 215 } 216 catch (Exception e) { 217 throw new ConversionException( "Could not load class called: " + name, e ); 218 } 219 220 Object object = null; 221 try { 222 object = theClass.newInstance(); 223 } 224 catch (Exception e) { 225 throw new ConversionException( "Could not instantiate an instance of: " + name, e ); 226 } 227 if ( object instanceof NameMapper ) { 228 return (NameMapper) object; 229 } 230 if ( object == null ) { 231 throw new ConversionException( "No NameMapper created for type: " + name ); 232 } 233 else { 234 throw new ConversionException( 235 "Created object: " + object 236 + " is not a NameMapper! Its type is: " + object.getClass().getName() 237 ); 238 } 239 } 240 } 241 242 /*** 243 * Factory method to create a new XMLIntrospector 244 */ 245 protected XMLIntrospector createIntrospector() { 246 return new XMLIntrospector(); 247 } 248 }

This page was automatically generated by Maven