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.InputStream;
21import java.io.InputStreamReader;
22import java.util.ResourceBundle;
23
24import org.tp23.antinstaller.InstallException;
25import org.tp23.antinstaller.runtime.ConfigurationException;
26import org.tp23.antinstaller.page.LicensePage;
27import org.tp23.antinstaller.page.Page;
28
29
30public class LicensePageRenderer
31    extends AbstractTextPageRenderer {
32
33    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.text.Res");
34    private static final String nextChar = res.getString("nextChar");
35    
36    private boolean usePaging = false;
37
38    public LicensePageRenderer() {
39    }
40
41    public boolean renderPage(Page page) throws InstallException {
42        if (page instanceof LicensePage) {
43            LicensePage lPage = (LicensePage) page;
44            String strUsePaging = lPage.getUsePaging();
45            usePaging = strUsePaging!=null && isTrue(strUsePaging);
46            return renderLicensePage(lPage);
47        }
48        else {
49            throw new InstallException("Wrong Renderer in LicensePageRenderer.renderPage");
50        }
51    }
52
53    private boolean renderLicensePage(LicensePage page) throws InstallException {
54        try {
55            BufferedReader commandReader = reader;
56            out.println();
57            out.println(res.getString("clickViewLicense"));
58            commandReader.readLine();
59
60            String resource = page.getResource();
61            InputStream licensein = this.getClass().getResourceAsStream(resource);
62            if (licensein == null) {
63                throw new ConfigurationException("License resource '" + resource + "' is missing from installer");
64            }
65            BufferedReader reader = new BufferedReader(new InputStreamReader(licensein));
66            printHeader(page);
67            String lineread = null;
68            StringBuffer sb = new StringBuffer();
69
70            while ( (lineread = reader.readLine()) != null) {
71                sb.append(lineread);
72                sb.append('\n');
73            }
74            // as per FindBugs
75            reader.close();
76
77            String command = null;
78            Pager pager = new Pager(sb.toString());
79            if (usePaging) {
80                do {
81                    if (!pager.next(out)) {
82                        break;
83                    }
84                    out.println();
85                    out.println(getNextInstructions());
86                    command = commandReader.readLine();
87                }
88                while (command.toUpperCase().startsWith(nextChar));
89                pager.rest(out);
90            }
91            else {
92                out.println(pager.getText());
93            }
94
95            for (int i = 0; i < PAGE_DECO_WIDTH; i++) {
96                out.print('~');
97            }
98            out.println();
99            out.println(res.getString("licenseAccept"));
00            command = commandReader.readLine();
01            command = command.trim();
02            if (isTrue(command)) {
03                return true;
04            }
05            else {
06                page.setAbort(true);
07                return false;
08            }
09        }
10        catch (IOException ex) {
11            throw new InstallException("Not able to read license file", ex);
12        }
13    }
14
15    private String getNextInstructions() {
16        return res.getString("license_next");
17    }
18}
19
20