001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.deployment.cli;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.StringReader;
021:
022: import jline.ConsoleReader;
023: import org.apache.geronimo.deployment.plugin.ConfigIDExtractor;
024:
025: /**
026: * Various helpers for deployment.
027: *
028: * @version $Rev: 597481 $ $Date: 2007-11-22 11:25:03 -0800 (Thu, 22 Nov 2007) $
029: */
030: public class DeployUtils extends ConfigIDExtractor {
031: /**
032: * Split up an output line so it indents at beginning and end (to fit in a
033: * typical terminal) and doesn't break in the middle of a word.
034: *
035: * @param source The unformatted String
036: * @param indent The number of characters to indent on the left
037: * @param endCol The maximum width of the entire line in characters,
038: * including indent (indent 10 with endCol 70 results
039: * in 60 "usable" characters).
040: */
041: public static String reformat(String source, int indent, int endCol) {
042: if (endCol - indent < 10) {
043: throw new IllegalArgumentException("This is ridiculous!");
044: }
045: StringBuffer buf = new StringBuffer(
046: (int) (source.length() * 1.1));
047: String prefix = indent == 0 ? "" : buildIndent(indent);
048: try {
049: BufferedReader in = new BufferedReader(new StringReader(
050: source));
051: String line;
052: int pos;
053: while ((line = in.readLine()) != null) {
054: if (buf.length() > 0) {
055: buf.append('\n');
056: }
057: while (line.length() > 0) {
058: line = prefix + line;
059: if (line.length() > endCol) {
060: pos = line.lastIndexOf(' ', endCol);
061: if (pos < indent) {
062: pos = line.indexOf(' ', endCol);
063: if (pos < indent) {
064: pos = line.length();
065: }
066: }
067: buf.append(line.substring(0, pos)).append('\n');
068: if (pos < line.length() - 1) {
069: line = line.substring(pos + 1);
070: } else {
071: break;
072: }
073: } else {
074: buf.append(line).append("\n");
075: break;
076: }
077: }
078: }
079: } catch (IOException e) {
080: throw (AssertionError) new AssertionError(
081: "This should be impossible").initCause(e);
082: }
083: return buf.toString();
084: }
085:
086: public static void println(String line, int indent,
087: ConsoleReader consoleReader) throws IOException {
088: int endCol = consoleReader.getTermwidth();
089: int start = consoleReader.getCursorBuffer().cursor;
090: if (endCol - indent < 10) {
091: throw new IllegalArgumentException("This is ridiculous!");
092: }
093: // StringBuffer buf = new StringBuffer((int)(source.length()*1.1));
094: String prefix = indent == 0 ? "" : buildIndent(indent);
095: int pos;
096: while (line.length() > 0) {
097: if (start == 0) {
098: line = prefix + line;
099: }
100: if (line.length() > endCol - start) {
101: pos = line.lastIndexOf(' ', endCol - start);
102: if (pos < indent) {
103: pos = line.indexOf(' ', endCol - start);
104: if (pos < indent) {
105: pos = line.length();
106: }
107: }
108: consoleReader.printString(line.substring(0, pos));
109: consoleReader.printNewline();
110: if (pos < line.length() - 1) {
111: line = line.substring(pos + 1);
112: } else {
113: break;
114: }
115: start = 0;
116: } else {
117: consoleReader.printString(line);
118: consoleReader.printNewline();
119: break;
120: }
121: }
122: }
123:
124: public static void printTo(String string, int col,
125: ConsoleReader consoleReader) throws IOException {
126: consoleReader.printString(string);
127: for (int i = string.length(); i < col; i++) {
128: consoleReader.printString(" ");
129: }
130: }
131:
132: private static String buildIndent(int indent) {
133: StringBuffer buf = new StringBuffer(indent);
134: for (int i = 0; i < indent; i++) {
135: buf.append(' ');
136: }
137: return buf.toString();
138: }
139:
140: }
|