/src/solidstack/template/groovy/GroovyConvertingWriter.java
Java | 115 lines | 75 code | 12 blank | 28 comment | 22 complexity | 1c33e6cd1ff3d7b0e8c67a4de762cd24 MD5 | raw file
Possible License(s): Apache-2.0
1/*-- 2 * Copyright 2012 René M. de Bloois 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package solidstack.template.groovy; 18 19import groovy.lang.Closure; 20import groovy.lang.GString; 21 22import java.io.IOException; 23 24import org.codehaus.groovy.runtime.InvokerHelper; 25 26import solidstack.template.ConvertingWriter; 27import solidstack.template.EncodingWriter; 28import solidstack.template.TemplateException; 29 30 31/** 32 * A ConvertingWriter that converts Groovy specific data types to Java data types. 33 * 34 * @author René de Bloois 35 */ 36public class GroovyConvertingWriter implements ConvertingWriter 37{ 38 /** 39 * The EncodingWriter to write to. 40 */ 41 protected EncodingWriter writer; 42 43 /** 44 * Constructor. 45 * 46 * @param writer The EncodingWriter to write to. 47 */ 48 public GroovyConvertingWriter( EncodingWriter writer ) 49 { 50 this.writer = writer; 51 } 52 53 public void write( Object o ) throws IOException 54 { 55 if( o == null ) 56 this.writer.write( null ); 57 else if( o instanceof String ) 58 this.writer.write( (String)o ); 59 else if( o instanceof GString ) 60 { 61 GString gString = (GString)o; 62 String[] strings = gString.getStrings(); 63 Object[] values = gString.getValues(); 64 if( !( strings.length == values.length + 1 ) ) 65 throw new IllegalStateException(); 66 67 for( int i = 0; i < values.length; i++ ) 68 { 69 this.writer.write( strings[ i ] ); 70 writeEncoded( values[ i ] ); 71 } 72 this.writer.write( strings[ values.length ] ); 73 } 74 else if( o instanceof Closure ) 75 { 76 Closure c = (Closure)o; 77 int pars = c.getMaximumNumberOfParameters(); 78 if( pars > 0 ) 79 throw new TemplateException( "Closures with parameters are not supported in expressions." ); 80 write( c.call() ); // May be recursive 81 } 82 else 83 this.writer.write( (String)InvokerHelper.invokeMethod( o, "asType", String.class ) ); 84 } 85 86 public void writeEncoded( Object o ) throws IOException 87 { 88 if( o == null ) 89 this.writer.writeEncoded( null ); 90 else if( o instanceof String ) 91 this.writer.writeEncoded( o ); 92 else if( o instanceof GString ) 93 this.writer.writeEncoded( o.toString() ); 94 else if( o instanceof Closure ) 95 { 96 Closure c = (Closure)o; 97 int pars = c.getMaximumNumberOfParameters(); 98 if( pars > 0 ) 99 throw new TemplateException( "Closures with parameters are not supported in expressions." ); 100 writeEncoded( c.call() ); // May be recursive 101 } 102 else 103 { 104 if( this.writer.stringsOnly() ) 105 this.writer.writeEncoded( InvokerHelper.invokeMethod( o, "asType", String.class ) ); 106 else 107 this.writer.writeEncoded( o ); 108 } 109 } 110 111 public void flush() throws IOException 112 { 113 this.writer.flush(); 114 } 115}