001    package railo.commons.lang;
002    
003    import java.io.IOException;
004    import java.io.PrintWriter;
005    import java.io.StringWriter;
006    import java.lang.reflect.InvocationTargetException;
007    import java.util.Arrays;
008    import java.util.Iterator;
009    
010    import railo.runtime.exp.NativeException;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.exp.PageExceptionImpl;
013    import railo.runtime.type.Collection;
014    import railo.runtime.type.util.CollectionUtil;
015    import railo.runtime.type.util.KeyConstants;
016    import railo.runtime.type.util.ListUtil;
017    
018    public final class ExceptionUtil {
019            
020            public static String getStacktrace(Throwable t, boolean addMessage) {
021                    StringWriter sw = new StringWriter();
022                    PrintWriter pw = new PrintWriter(sw);
023                    t.printStackTrace(pw);
024                    pw.close();
025                    String st = sw.toString();
026                    String msg=t.getMessage();
027                    if(addMessage && !StringUtil.isEmpty(msg) && !st.startsWith(msg.trim()))
028                            st=msg+"\n"+st;
029                    return st;
030                    
031            }
032            
033    
034            public static String getMessage(Throwable t) {
035                    String msg=t.getMessage();
036                    if(StringUtil.isEmpty(msg,true)) msg=t.getClass().getName();
037                    
038                    StringBuilder sb=new StringBuilder(msg);
039                    
040                    if(t instanceof PageException){
041                            PageException pe=(PageException)t;
042                            String detail = pe.getDetail();
043                            if(!StringUtil.isEmpty(detail,true)) {
044                                    sb.append('\n');
045                                    sb.append(detail);
046                            }
047                    }
048                    return sb.toString();
049            }
050    
051            public static PageException addHint(PageExceptionImpl pe,String hint) {
052                    pe.setAdditional(KeyConstants._Hint, hint);
053                    return pe;
054            }
055    
056            /**
057             * creates a message for key not found with soundex check for similar key
058             * @param keys
059             * @param keyLabel
060             * @return
061             */
062            public static String similarKeyMessage(Collection.Key[] _keys,String keySearched, String keyLabel, String keyLabels) {
063                    
064                    Arrays.sort(_keys);
065                    String list=ListUtil.arrayToList(_keys, ",");
066                    String keySearchedSoundex=StringUtil.soundex(keySearched);
067                    
068                    for(int i=0;i<_keys.length;i++){
069                            if(StringUtil.soundex(_keys[i].getString()).equals(keySearchedSoundex))
070                                    return keyLabel+" ["+keySearched+"] does not exist, but there is a similar "+keyLabel+" ["+_keys[i].getString()+"] available, complete list of all available "+keyLabels+" ["+list+"]";
071                    }
072                    return keyLabel+" ["+keySearched+"] does not exist, only the followings are available "+keyLabels+" ["+list+"]";
073            }
074            
075    
076            public static String similarKeyMessage(Collection coll,String keySearched, String keyLabel, String keyLabels) {
077                    return similarKeyMessage(CollectionUtil.keys(coll), keySearched, keyLabel, keyLabels);
078            }
079    
080            public static IOException toIOException(Throwable t) {
081                    if(t instanceof IOException) return (IOException) t;
082                    if(t instanceof InvocationTargetException) return toIOException(((InvocationTargetException) t).getCause());
083                    if(t instanceof NativeException) return toIOException(((NativeException)t).getCause());
084                    
085                    IOException ioe = new IOException(t.getClass().getName()+":"+t.getMessage());
086                    ioe.setStackTrace(t.getStackTrace());
087                    return ioe;
088            }
089    
090    
091            public static String createSoundexDetail(String name, Iterator<String> it, String keyName) {
092                    StringBuilder sb=new StringBuilder();
093                    String k ,sname=StringUtil.soundex(name);
094                    while(it.hasNext()){
095                            k = it.next();
096                            if(StringUtil.soundex(k).equals(sname))
097                                    return "did you mean ["+k+"]";
098                            if(sb.length()!=0)sb.append(',');
099                            sb.append(k);
100                    }
101                    return "existing "+keyName+" are ["+sb+"]";
102            }
103    }