01: /*
02: * Copyright 2005 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */
16: package org.tp23.antinstaller.renderer.text;
17:
18: import java.io.BufferedReader;
19: import java.io.IOException;
20: import java.io.PrintStream;
21:
22: import org.tp23.antinstaller.InstallerContext;
23: import org.tp23.antinstaller.input.CommentOutput;
24: import org.tp23.antinstaller.input.OutputField;
25:
26: public class CommentOutputRenderer implements TextOutputFieldRenderer {
27: protected InstallerContext ctx;
28:
29: public CommentOutputRenderer() {
30: }
31:
32: public void setContext(InstallerContext ctx) {
33: this .ctx = ctx;
34: }
35:
36: public void renderOutput(OutputField field, BufferedReader reader,
37: PrintStream out) throws IOException {
38: CommentOutput comField = (CommentOutput) field;
39: String text = field.getDisplayText();
40: if (text == null) {
41: return;
42: }
43:
44: if (OutputField.isTrue(comField.getBold())) {
45: text = stripHtml(text).toUpperCase();
46: } else if (OutputField.isTrue(comField.getTitle())) {
47: text = stripHtml(text).toUpperCase();
48: }
49:
50: out.println(stripHtml(text));
51: }
52:
53: public boolean isAbort() {
54: return false;
55: }
56:
57: public static String stripHtml(String input) throws IOException {
58: if (input == null) {
59: return "";
60: }
61: String trimmed = input.trim();
62: if (trimmed.startsWith("<html>")) {
63: boolean inTag = false;
64: char[] characters = trimmed.toCharArray();
65: StringBuffer output = new StringBuffer();
66: for (int i = 0; i < characters.length; i++) {
67: char c = characters[i];
68: if (inTag) {
69: if (c == '>') {
70: inTag = false;
71: continue;
72: } else if (c == '<') {
73: throw new IOException("Invlaid HTML: " + input);
74: } else {
75: continue;
76: }
77: } else if (c == '<') {
78: inTag = true;
79: continue;
80: } else {
81: output.append(c);
82: }
83: }
84: return output.toString();
85: } else {
86: return input;
87: }
88: }
89:
90: /**
91: * renderError
92: *
93: * @param field InputField
94: * @param out PrintStream
95: */
96: public void renderError(OutputField field, BufferedReader reader,
97: PrintStream out) {
98: }
99: }
|