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;
21import java.util.ResourceBundle;
22
23import org.tp23.antinstaller.InstallException;
24import org.tp23.antinstaller.InstallerContext;
25import org.tp23.antinstaller.page.Page;
26import org.tp23.antinstaller.renderer.PageRenderer;
27/**
28 * renamed from TextPageRenderer in version 0.8 to enable
29 * the use of the page type TextPage, TextPageRenderer is now 
30 * the renderer for these page types
31 * @author teknopaul
32 *
33 */
34public abstract class AbstractTextPageRenderer
35    implements PageRenderer {
36
37    public static final int PAGE_BLANK_LINES = 20;
38    public static final int PAGE_DECO_WIDTH = 80;
39
40    protected BufferedReader reader;
41    protected PrintStream out;
42    private InstallerContext ctx;
43
44
45    public AbstractTextPageRenderer() {
46    }
47
48    public void setContext(InstallerContext ctx){
49        this.ctx = ctx;
50    }
51    public InstallerContext getContext(){
52        return ctx;
53    }
54
55    public void init( BufferedReader reader, PrintStream out){
56        this.out = out;
57        this.reader = reader;
58    }
59    /**
60     *
61     * @param page Page
62     * @throws InstallException
63     * @return boolean false implys user aborted
64     */
65    public abstract boolean renderPage(Page page) throws InstallException;
66
67    protected void printHeader(Page page) throws IOException{
68        for (int i = 0; i < PAGE_BLANK_LINES; i++) {
69            out.println();
70        }
71
72
73        for (int i = 0; i < PAGE_DECO_WIDTH; i++) {
74            out.print('~');
75        }
76        out.println();
77        out.println("  " + page.getDisplayText());
78        for (int i = 0; i < PAGE_DECO_WIDTH; i++) {
79            out.print('~');
80        }
81        out.println();
82        out.println();
83        out.println();
84    }
85
86    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.text.Res");
87    private static final char[] affimativeChars = parseChars(res.getString("affirmativeChars"));
88    
89    private static char[] parseChars(String commaSeparated){
90        char[] input = commaSeparated.toCharArray();
91        char[] theChars = new char[input.length];
92        int j = 0;
93        for (int i = 0; i < input.length; i++) {
94            if(Character.isWhitespace(input[i]))continue;
95            if(',' == input[i]) {
96                continue;
97            }
98            else theChars[j++] = input[i];
99        }
00        char[] toReturn = new char[j];
01        System.arraycopy(theChars,0,toReturn,0,j);
02        return toReturn;
03    }
04
05    /**
06     * does the string represent true default = true
07     * @param entered String
08     * @return boolean
09     */
10    protected boolean isTrue(String entered){
11        if(entered.length() == 0) {
12            return true;
13        }
14        char first = entered.charAt(0);
15        boolean isTrue= false;
16        for (int i = 0; i < affimativeChars.length; i++) {
17            isTrue |= Character.toUpperCase(first) == affimativeChars[i];
18        }
19        return isTrue;
20    }
21}
22