001    package railo.runtime.exp;
002    
003    import railo.runtime.PageContext;
004    import railo.runtime.config.Config;
005    import railo.runtime.err.ErrorPage;
006    import railo.runtime.type.Struct;
007    import railo.runtime.type.util.KeyConstants;
008    
009    
010    /**
011     * Exception throwed by CFML Code
012     */
013    public final class CustomTypeException extends PageExceptionImpl {
014            
015            /**
016             * constructor of the Exception 
017             * @param message Exception Message
018             * @param detail Detailed Exception Message
019             * @param errorCode Error Code
020             * @param customType Type of the Exception
021             */
022            public CustomTypeException(String message, String detail, String errorCode, String customType,String extendedinfo) {
023                    super(message,"custom_type",customType);
024                    setDetail(detail);
025                    setErrorCode(errorCode);
026                    if(extendedinfo!=null)setExtendedInfo(extendedinfo);
027            }
028    
029            @Override
030            public CatchBlock getCatchBlock(Config config) {
031                    CatchBlock cb=super.getCatchBlock(config);
032                    cb.setEL(KeyConstants._code,cb.get("errorcode",null));
033                    cb.setEL(KeyConstants._type,getCustomTypeAsString());
034                    String ei=getExtendedInfo();
035                    if(ei!=null)cb.setEL("extended_info",ei);
036                    //cb.setEL("ErrorCode","");
037                    return cb;
038            }
039    
040            @Override
041            public Struct getErrorBlock(PageContext pc, ErrorPage ep) {
042                    Struct eb = super.getErrorBlock(pc, ep);
043                    eb.setEL(KeyConstants._type,getCustomTypeAsString());
044                    return eb;
045            }
046    
047        @Override
048        public boolean typeEqual(String type) {
049            if(type==null) return true;
050            type=type.toLowerCase().trim();
051            if(type.equals("any")) return true;
052            
053            // Custom Type
054            if(getTypeAsString().equals("custom_type") || getTypeAsString().equals("customtype")) {
055                return compareCustomType(type,getCustomTypeAsString().toLowerCase().trim());
056            }
057            return super.typeEqual(type);
058        }
059        
060        /**
061         * @param leftType
062         * @param rightType
063         * @return is same custom type
064         */
065        private boolean compareCustomType(String leftType, String rightType) {
066            int left=leftType.length();
067            int right=rightType.length();
068            if(left>right) return false;
069            if(left==right) return leftType.equals(rightType);
070            
071            for(int i=0;i<left;i++) {
072                if(leftType.charAt(i)!=rightType.charAt(i)) return false;
073            }
074            return rightType.charAt(left)=='.';
075        }
076    }