001    package railo.commons.cli;
002    
003    import java.io.IOException;
004    import java.io.InputStream;
005    import java.util.ArrayList;
006    
007    import railo.commons.io.IOUtil;
008    import railo.commons.io.SystemUtil;
009    import railo.commons.lang.StringUtil;
010    
011    public class Command {
012        
013        public static Process createProcess(String cmdline,boolean translate) throws IOException {
014            if(!translate)return Runtime.getRuntime().exec(cmdline);
015            return Runtime.getRuntime().exec(toArray(cmdline));
016        }
017            
018        /**
019         * @param cmdline command line
020         * @param translate translate the command line or not
021         * @return
022         * @throws IOException
023         * @throws InterruptedException
024         */
025        public static String execute(String cmdline,boolean translate) throws IOException, InterruptedException {
026            if(!translate)return execute(Runtime.getRuntime().exec(cmdline));
027            return execute(Runtime.getRuntime().exec(toArray(cmdline)));
028        }
029        
030            public static String execute(String[] cmdline) throws IOException, InterruptedException {
031                    return execute(Runtime.getRuntime().exec(cmdline));
032        }
033        public static String execute(String cmd,String[] args) throws IOException, InterruptedException {
034            return execute(StringUtil.merge(cmd,args));
035        }
036    
037        public static String execute(Process p) throws IOException, InterruptedException {
038            InputStream is=null;
039            InputStream es=null;
040            IOException ioe;
041                try {
042                    StreamGobbler in=new StreamGobbler(is=p.getInputStream());
043                    StreamGobbler err=new StreamGobbler(es=p.getErrorStream());
044                    in.start();
045                    err.start();
046                    if(p.waitFor()!=0){
047                            err.join();
048                            if((ioe=err.getException())!=null) throw ioe;
049                            String str=err.getString();
050                            if(!StringUtil.isEmpty(str))
051                                    throw new CommandException(str);
052                    }
053                    in.join();
054                    if((ioe=in.getException())!=null) throw ioe;
055                            return in.getString();
056            }
057            finally {
058                    IOUtil.closeEL(is);
059                    IOUtil.closeEL(es);
060            }
061        }
062        
063        
064    
065    
066        private static String[] toArray(String str) {
067            if(StringUtil.isEmpty(str)) return new String[]{""};
068            str=str.trim();
069            StringBuilder sb=new StringBuilder();
070            ArrayList<String> list=new ArrayList<String>();
071                    char[] carr = str.toCharArray();
072                    char c;//,last=0;
073                    char inside=0;
074                    for(int i=0;i<carr.length;i++){
075                            c=carr[i];
076                            //if(i>0)last=carr[i-1];
077                            switch(c){
078                    // DELIMITER
079                            /*case '\\':    
080                                    if(i+1<carr.length){
081                                            sb.append(carr[++i]);
082                                    }
083                                    else sb.append(c);
084                                    break;*/
085                    // QUOTE
086                            case '\'':
087                            case '"':
088                                    if(inside==0){
089                                            if(str.lastIndexOf(c)>i)
090                                                    inside=c;
091                                            else
092                                                    sb.append(c);
093                                    }
094                                    else if(inside==c) {
095                                            inside=0;
096                                    }
097                                    else sb.append(c);
098                            break;
099                    // WHITE SPACE
100                            case ' ':
101                            case '\b':
102                            case '\t':
103                            case '\n':
104                            case '\r':
105                            case '\f':
106                                    //if(last=='\\')sb.setCharAt(sb.length()-1,c);
107                                    if(inside==0) {
108                                            populateList(sb,list);
109                                    }
110                                    else sb.append(c);
111                                    break;
112                    // OTHERS
113                            default:
114                                            sb.append(c);
115                            }
116                    }
117                    populateList(sb, list);
118                    
119                    
120            return  list.toArray(new String[list.size()]);
121            }
122        
123        private static void populateList(StringBuilder sb, ArrayList<String> list) {
124    
125                    String tmp = sb.toString();
126                    tmp=tmp.trim();
127                    if(tmp.length()>0)list.add(tmp);
128                    sb.delete(0, sb.length());
129            }
130    }
131    
132    class StreamGobbler extends Thread {
133      
134    
135            InputStream is;
136            private String str;
137            private IOException ioe;
138        
139        StreamGobbler(InputStream is)       {
140            this.is = is;
141        }
142        
143        @Override
144        public void run() {
145            try {
146                            str=IOUtil.toString(is,SystemUtil.getCharset());
147                    } catch (IOException ioe) {
148                            this.ioe=ioe;
149                    }  
150        }
151        
152        /**
153             * @return the str
154             */
155            public String getString() {
156                    return str;
157            }
158    
159            /**
160             * @return the ioe
161             */
162            public IOException getException() {
163                    return ioe;
164            }
165            
166    }