001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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    import java.util.Collection;
024    import java.util.LinkedList;
025    import java.util.List;
026    import javax.enterprise.deploy.spi.TargetModuleID;
027    import org.apache.geronimo.common.DeploymentException;
028    import org.apache.geronimo.deployment.plugin.ConfigIDExtractor;
029    import org.apache.geronimo.kernel.repository.Artifact;
030    
031    /**
032     * Various helpers for deployment.
033     *
034     * @version $Rev: 396315 $ $Date: 2006-04-23 13:48:50 -0700 (Sun, 23 Apr 2006) $
035     */
036    public class DeployUtils extends ConfigIDExtractor {
037        /**
038         * Split up an output line so it indents at beginning and end (to fit in a
039         * typical terminal) and doesn't break in the middle of a word.
040         * @param source The unformatted String
041         * @param indent The number of characters to indent on the left
042         * @param endCol The maximum width of the entire line in characters,
043         *               including indent (indent 10 with endCol 70 results
044         *               in 60 "usable" characters).
045         */
046        public static String reformat(String source, int indent, int endCol) {
047            if(endCol-indent < 10) {
048                throw new IllegalArgumentException("This is ridiculous!");
049            }
050            StringBuffer buf = new StringBuffer((int)(source.length()*1.1));
051            String prefix = indent == 0 ? "" : buildIndent(indent);
052            try {
053                BufferedReader in = new BufferedReader(new StringReader(source));
054                String line;
055                int pos;
056                while((line = in.readLine()) != null) {
057                    if(buf.length() > 0) {
058                        buf.append('\n');
059                    }
060                    while(line.length() > 0) {
061                        line = prefix + line;
062                        if(line.length() > endCol) {
063                            pos = line.lastIndexOf(' ', endCol);
064                            if(pos < indent) {
065                                pos = line.indexOf(' ', endCol);
066                                if(pos < indent) {
067                                    pos = line.length();
068                                }
069                            }
070                            buf.append(line.substring(0, pos)).append('\n');
071                            if(pos < line.length()-1) {
072                                line = line.substring(pos+1);
073                            } else {
074                                break;
075                            }
076                        } else {
077                            buf.append(line).append("\n");
078                            break;
079                        }
080                    }
081                }
082            } catch (IOException e) {
083                throw new AssertionError("This should be impossible");
084            }
085            return buf.toString();
086        }
087    
088        private static String buildIndent(int indent) {
089            StringBuffer buf = new StringBuffer(indent);
090            for(int i=0; i<indent; i++) {
091                buf.append(' ');
092            }
093            return buf.toString();
094        }
095    
096    
097    }