001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.text;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022:
023: import org.tp23.antinstaller.InstallException;
024: import org.tp23.antinstaller.page.LicensePage;
025: import org.tp23.antinstaller.page.Page;
026: import org.tp23.antinstaller.renderer.AIResourceBundle;
027: import org.tp23.antinstaller.runtime.ConfigurationException;
028:
029: public class LicensePageRenderer extends AbstractTextPageRenderer {
030:
031: private static final AIResourceBundle res = new AIResourceBundle();
032: private static final String nextChar = res.getString("next.char");
033:
034: private boolean usePaging = false;
035:
036: public LicensePageRenderer() {
037: }
038:
039: public boolean renderPage(Page page) throws InstallException {
040: if (page instanceof LicensePage) {
041: LicensePage lPage = (LicensePage) page;
042: String strUsePaging = lPage.getUsePaging();
043: usePaging = strUsePaging != null && isTrue(strUsePaging);
044: return renderLicensePage(lPage);
045: } else {
046: throw new InstallException(
047: "Wrong Renderer in LicensePageRenderer.renderPage");
048: }
049: }
050:
051: private boolean renderLicensePage(LicensePage page)
052: throws InstallException {
053: try {
054: BufferedReader commandReader = reader;
055: out.println();
056: out.println(res.getString("click.view.license"));
057: commandReader.readLine();
058:
059: String resource = page.getResource();
060: InputStream licensein = this .getClass()
061: .getResourceAsStream(resource);
062: if (licensein == null) {
063: throw new ConfigurationException("License resource '"
064: + resource + "' is missing from installer");
065: }
066: BufferedReader reader = new BufferedReader(
067: new InputStreamReader(licensein));
068: printHeader(page);
069: String lineread = null;
070: StringBuffer sb = new StringBuffer();
071:
072: while ((lineread = reader.readLine()) != null) {
073: sb.append(lineread);
074: sb.append('\n');
075: }
076: // as per FindBugs
077: reader.close();
078:
079: String command = null;
080: Pager pager = new Pager(sb.toString());
081: if (usePaging) {
082: do {
083: if (!pager.next(out)) {
084: break;
085: }
086: out.println();
087: out.println(getNextInstructions());
088: command = commandReader.readLine();
089: } while (command.toUpperCase().startsWith(nextChar));
090: pager.rest(out);
091: } else {
092: out.println(pager.getText());
093: }
094:
095: for (int i = 0; i < PAGE_DECO_WIDTH; i++) {
096: out.print('~');
097: }
098: out.println();
099: out.println(res.getString("license.accept"));
100: command = commandReader.readLine();
101: command = command.trim();
102: if (isTrue(command)) {
103: return true;
104: } else {
105: page.setAbort(true);
106: return false;
107: }
108: } catch (IOException ex) {
109: throw new InstallException("Not able to read license file",
110: ex);
111: }
112: }
113:
114: private String getNextInstructions() {
115: return res.getString("license.next");
116: }
117: }
|