1 /*
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15package org.tp23.antinstaller.renderer.text;
16
17import java.io.BufferedReader;
18import java.io.IOException;
19import java.io.PrintStream;
20
21import org.tp23.antinstaller.InstallException;
22import org.tp23.antinstaller.InstallerContext;
23import org.tp23.antinstaller.ResourceBundleHelper;
24import org.tp23.antinstaller.ValidationException;
25import org.tp23.antinstaller.input.ConditionalField;
26import org.tp23.antinstaller.input.OutputField;
27import org.tp23.antinstaller.runtime.ConfigurationException;
28
29/**
30 * Conditionally render a collection of fields
31 *
32 * @author mwilson
33 * @version $Id
34 * @since 0.7.4.patch 7
35 */
36public class ConditionalFieldRenderer implements TextOutputFieldRenderer
37{
38    private static final ResourceBundleHelper res = new ResourceBundleHelper("org.tp23.antinstaller.renderer.text.Res");
39
40    private InstallerContext context;
41
42    public void setContext( InstallerContext context )
43    {
44        this.context = context;
45    }
46
47    public void renderOutput( OutputField field, BufferedReader reader, PrintStream out )
48            throws ValidationException, InstallException, IOException
49    {
50        ConditionalField conditional = (ConditionalField) field;
51
52        try
53        {
54
55            OutputField[] fields = null;
56
57            if( conditional.getExpression().evaluate() )
58            {
59                fields = conditional.getFields();
60
61                SimpleInputPageRenderer.renderFields( context, fields, reader, out);
62            }
63        }
64        catch( ConfigurationException configExc )
65        {
66            throw new InstallException( res.getMessage( "invalid.conditional.expression", conditional.getIfProperty()),
67                                                        configExc );
68        }
69        catch( ClassNotFoundException clsNotFoundExc )
70        {
71            throw new InstallException( res.getMessage( "text.render.not.found" ),
72                                                        clsNotFoundExc );
73        }
74    }
75
76    public void renderError( OutputField field, BufferedReader reader, PrintStream out ) throws IOException
77    {
78        //renderOutput will have already rendered any errors
79    }
80
81    public boolean isAbort()
82    {
83        return false;
84    }
85}
86