/src/solidstack/template/groovy/GroovyConvertingWriter.java

http://solidstack.googlecode.com/ · Java · 115 lines · 75 code · 12 blank · 28 comment · 22 complexity · 1c33e6cd1ff3d7b0e8c67a4de762cd24 MD5 · raw file

  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. package solidstack.template.groovy;
  17. import groovy.lang.Closure;
  18. import groovy.lang.GString;
  19. import java.io.IOException;
  20. import org.codehaus.groovy.runtime.InvokerHelper;
  21. import solidstack.template.ConvertingWriter;
  22. import solidstack.template.EncodingWriter;
  23. import solidstack.template.TemplateException;
  24. /**
  25. * A ConvertingWriter that converts Groovy specific data types to Java data types.
  26. *
  27. * @author René de Bloois
  28. */
  29. public class GroovyConvertingWriter implements ConvertingWriter
  30. {
  31. /**
  32. * The EncodingWriter to write to.
  33. */
  34. protected EncodingWriter writer;
  35. /**
  36. * Constructor.
  37. *
  38. * @param writer The EncodingWriter to write to.
  39. */
  40. public GroovyConvertingWriter( EncodingWriter writer )
  41. {
  42. this.writer = writer;
  43. }
  44. public void write( Object o ) throws IOException
  45. {
  46. if( o == null )
  47. this.writer.write( null );
  48. else if( o instanceof String )
  49. this.writer.write( (String)o );
  50. else if( o instanceof GString )
  51. {
  52. GString gString = (GString)o;
  53. String[] strings = gString.getStrings();
  54. Object[] values = gString.getValues();
  55. if( !( strings.length == values.length + 1 ) )
  56. throw new IllegalStateException();
  57. for( int i = 0; i < values.length; i++ )
  58. {
  59. this.writer.write( strings[ i ] );
  60. writeEncoded( values[ i ] );
  61. }
  62. this.writer.write( strings[ values.length ] );
  63. }
  64. else if( o instanceof Closure )
  65. {
  66. Closure c = (Closure)o;
  67. int pars = c.getMaximumNumberOfParameters();
  68. if( pars > 0 )
  69. throw new TemplateException( "Closures with parameters are not supported in expressions." );
  70. write( c.call() ); // May be recursive
  71. }
  72. else
  73. this.writer.write( (String)InvokerHelper.invokeMethod( o, "asType", String.class ) );
  74. }
  75. public void writeEncoded( Object o ) throws IOException
  76. {
  77. if( o == null )
  78. this.writer.writeEncoded( null );
  79. else if( o instanceof String )
  80. this.writer.writeEncoded( o );
  81. else if( o instanceof GString )
  82. this.writer.writeEncoded( o.toString() );
  83. else if( o instanceof Closure )
  84. {
  85. Closure c = (Closure)o;
  86. int pars = c.getMaximumNumberOfParameters();
  87. if( pars > 0 )
  88. throw new TemplateException( "Closures with parameters are not supported in expressions." );
  89. writeEncoded( c.call() ); // May be recursive
  90. }
  91. else
  92. {
  93. if( this.writer.stringsOnly() )
  94. this.writer.writeEncoded( InvokerHelper.invokeMethod( o, "asType", String.class ) );
  95. else
  96. this.writer.writeEncoded( o );
  97. }
  98. }
  99. public void flush() throws IOException
  100. {
  101. this.writer.flush();
  102. }
  103. }