1 /* 
2  * Copyright 2005 Paul Hinds
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 */
16package org.tp23.antinstaller.renderer.text;
17
18import java.io.BufferedReader;
19import java.io.IOException;
20import java.io.PrintStream;
21
22import org.tp23.antinstaller.InstallException;
23import org.tp23.antinstaller.InstallerContext;
24import org.tp23.antinstaller.ValidationException;
25import org.tp23.antinstaller.input.OutputField;
26import org.tp23.antinstaller.page.Page;
27import org.tp23.antinstaller.page.SimpleInputPage;
28import org.tp23.antinstaller.renderer.RendererFactory;
29
30public class SimpleInputPageRenderer
31    extends AbstractTextPageRenderer {
32    
33    public SimpleInputPageRenderer() {
34    }
35
36    public boolean renderPage(Page page) throws InstallException{
37        if (page instanceof SimpleInputPage) {
38            try {
39                return renderSimpleInputPage( (SimpleInputPage) page);
40            }
41            catch (ClassNotFoundException ex) {
42                // this would be a code error
43                throw new InstallException("Cant find acceptable TextField renderer in SimpleInputPageRenderer.renderPage:" + ex.getMessage(),
44                                           ex);
45            }
46        }
47        else {
48            //this would be a code error
49            throw new InstallException("Wrong Renderer in SimpleInputPageRenderer.renderPage");
50        }
51    }
52
53    private boolean renderSimpleInputPage(SimpleInputPage page)
54            throws InstallException, ClassNotFoundException, ValidationException
55    {
56
57        try {
58            printHeader(page);
59            OutputField[] fields = page.getOutputField();
60            return renderFields( getContext(), fields, reader, out );
61        }
62        catch (IOException ex) {
63            // If you cant write to the console there is not much you can do
64            throw new InstallException("IOException",ex);
65        }
66    }
67
68    public static boolean renderFields( InstallerContext context, OutputField[] fields, BufferedReader reader, PrintStream out )
69            throws ClassNotFoundException, IOException, ValidationException, InstallException
70    {
71
72        for (int f = 0; f < fields.length; f++) {
73            String text = fields[f].getExplanatoryText();
74            if(text != null){
75                out.println(text);
76                out.println();
77            }
78
79            TextOutputFieldRenderer frenderer = RendererFactory.getTextRenderer(fields[f]);
80            frenderer.setContext( context );
81            frenderer.renderOutput(fields[f], reader, out);
82            if (frenderer.isAbort()) {
83                return false;
84            }
85            while(!fields[f].validate( context ) ){
86                frenderer.renderError(fields[f], reader, out);
87            };
88        }
89        return true;
90    }
91}
92