Changeset 3316

Show
Ignore:
Timestamp:
26/10/07 11:19:06 (4 years ago)
Author:
michael
Message:

Fix NPE when setting variable to null. Fixes #15, spent 0.5

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/java/org/sarugo/xtc/restlet/TemplateRepresentation.java

    r2968 r3316  
    3636public class TemplateRepresentation extends OutputRepresentation { 
    3737 
    38        Template template; 
     38    Template template; 
    3939 
    40        TemplateContext context; 
     40    TemplateContext context; 
    4141 
    42        public TemplateRepresentation(Template template, MediaType mediaType) { 
    43                super(mediaType); 
    44                this.template = template; 
    45                context = template.createContext(); 
    46                // We always expire because the content is dynamic 
    47                setModificationDate(new Date()); 
    48                setExpirationDate(getModificationDate()); 
    49        
     42    public TemplateRepresentation(Template template, MediaType mediaType) { 
     43        super(mediaType); 
     44        this.template = template; 
     45        context = template.createContext(); 
     46        // We always expire because the content is dynamic 
     47        setModificationDate(new Date()); 
     48        setExpirationDate(getModificationDate()); 
     49   
    5050 
    51        @Override 
    52        public void write(OutputStream arg0) throws IOException { 
    53                context.setOutputWriter(new OutputStreamWriter(arg0)); 
    54                template.render(context); 
    55                context.getXMLWriter().flush(); 
    56        
     51    @Override 
     52    public void write(OutputStream arg0) throws IOException { 
     53        context.setOutputWriter(new OutputStreamWriter(arg0)); 
     54        template.render(context); 
     55        context.getXMLWriter().flush(); 
     56   
    5757 
    58         public void setVariable(String name, Object value) { 
    59                 ValueExpression tag = context.getExpressionFactory() 
    60                                 .createValueExpression(value, value.getClass()); 
    61                 context.getVariableMapper().setVariable(name, tag); 
    62         } 
    63          
    64         public TemplateContext getContext() { 
    65                 return context; 
    66         } 
     58    public void setVariable(String name, Object value) { 
     59        ValueExpression tag = null; 
     60        if (value != null) { 
     61            tag = context.getExpressionFactory().createValueExpression(value, 
     62                    value.getClass()); 
     63        } 
     64        context.getVariableMapper().setVariable(name, tag); 
     65    } 
     66 
     67    public TemplateContext getContext() { 
     68        return context; 
     69    } 
    6770 
    6871}