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     */
017    
018    package org.apache.geronimo.deployment.cli;
019    
020    import java.io.BufferedReader;
021    import java.io.IOException;
022    import java.io.StringReader;
023    
024    import org.apache.geronimo.deployment.plugin.ConfigIDExtractor;
025    
026    /**
027     * Various helpers for deployment.
028     *
029     * @version $Rev: 549455 $ $Date: 2007-06-21 08:12:27 -0400 (Thu, 21 Jun 2007) $
030     */
031    public class DeployUtils extends ConfigIDExtractor {
032        /**
033         * Split up an output line so it indents at beginning and end (to fit in a
034         * typical terminal) and doesn't break in the middle of a word.
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((int)(source.length()*1.1));
046            String prefix = indent == 0 ? "" : buildIndent(indent);
047            try {
048                BufferedReader in = new BufferedReader(new StringReader(source));
049                String line;
050                int pos;
051                while((line = in.readLine()) != null) {
052                    if(buf.length() > 0) {
053                        buf.append('\n');
054                    }
055                    while(line.length() > 0) {
056                        line = prefix + line;
057                        if(line.length() > endCol) {
058                            pos = line.lastIndexOf(' ', endCol);
059                            if(pos < indent) {
060                                pos = line.indexOf(' ', endCol);
061                                if(pos < indent) {
062                                    pos = line.length();
063                                }
064                            }
065                            buf.append(line.substring(0, pos)).append('\n');
066                            if(pos < line.length()-1) {
067                                line = line.substring(pos+1);
068                            } else {
069                                break;
070                            }
071                        } else {
072                            buf.append(line).append("\n");
073                            break;
074                        }
075                    }
076                }
077            } catch (IOException e) {
078                throw (AssertionError)new AssertionError("This should be impossible").initCause(e);
079            }
080            return buf.toString();
081        }
082    
083        private static String buildIndent(int indent) {
084            StringBuffer buf = new StringBuffer(indent);
085            for(int i=0; i<indent; i++) {
086                buf.append(' ');
087            }
088            return buf.toString();
089        }
090    
091    
092    }