GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — codeFactory/java_src/Ognl.java at master · hacxer/codeFactory Expand file tree/ Copy pathOgnl.javaMore file actions 167 lines (151 loc) · 4.27 KB / Copy pathOgnl.javaFile metadata and controls167 lines (151 loc) · 4.27 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 import java.lang.reflect.Array; import java.util.Collection; import java.util.List; import java.util.Map; import org.springframework.dao.DataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; import cn.org.rapid_framework.page.SortInfo; /** * Ognl工具类,主要是为了在ognl表达式访问静态方法时可以减少长长的类名称编写 * Ognl访问静态方法的表达式为: @class@method(args) * * 示例使用: * <pre> * <if test="@Ognl@isNotEmpty(userId)"> * and user_id = #{userId} * </if> * </pre> * @author badqiu * */ public class Ognl { /** * 可以用于判断String,Map,Collection,Array是否为空 * @param o * @return */ public static boolean isEmpty(Object o) throws IllegalArgumentException { if(o == null) return true; if(o instanceof String) { if(((String)o).length() == 0){ return true; } } else if(o instanceof Collection) { if(((Collection)o).isEmpty()){ return true; } } else if(o.getClass().isArray()) { if(Array.getLength(o) == 0){ return true; } } else if(o instanceof Map) { if(((Map)o).isEmpty()){ return true; } }else { return false; // throw new IllegalArgumentException("Illegal argument type,must be : Map,Collection,Array,String. but was:"+o.getClass()); } return false; } /** * 可以用于判断 Map,Collection,String,Array是否不为空 * @param c * @return */ public static boolean isNotEmpty(Object o) { return !isEmpty(o); } public static boolean isNotBlank(Object o) { return !isBlank(o); } public static boolean isNumber(Object o) { if(o == null) return false; if(o instanceof Number) { return true; } if(o instanceof String) { String str = (String)o; if(str.length() == 0) return false; if(str.trim().length() == 0) return false; try { Double.parseDouble(str); return true; }catch(NumberFormatException e) { return false; } } return false; } public static boolean isBlank(Object o) { if(o == null) return true; if(o instanceof String) { String str = (String)o; return isBlank(str); } return false; } public static boolean isBlank(String str) { if(str == null || str.length() == 0) { return true; } for (int i = 0; i < str.length(); i++) { if (!Character.isWhitespace(str.charAt(i))) { return false; } } return true; } /** * 用于验证那些列可以排序 * * ibatis示列使用 * <if test="@Ognl@checkOrderBy(orderBy,'username,password')"> * ORDER BY ${orderBy} * </if> * * <pre> * 返回示例: * 返回false相关验证: * checkOrderBy(null,"user,pwd") * checkOrderBy(" ","user,pwd") * checkOrderBy("user asc,pwd desc","user") pwd不能排序 * * 返回true相关验证: * checkOrderBy("user asc,pwd desc",null) * checkOrderBy("user asc,pwd desc","") * checkOrderBy("user asc,pwd desc","user,pwd") * </pre> * @param orderBy 需要验证的order by字符串 * @param validSortColumns 可以排序的列 * @throws DataAccessException */ public static boolean checkOrderBy(String orderby,String validSortColumns) throws DataAccessException{ if(isBlank(orderby)) return false; if(orderby.indexOf("'") >= 0 || orderby.indexOf("\\") >= 0) { throw new IllegalArgumentException("orderBy:"+orderby+" has SQL Injection risk"); } if(orderby != null && orderby.length() > 50) { throw new IllegalArgumentException("orderby.length() <= 50 must be true"); } if(validSortColumns == null) return true; List<SortInfo> infos = SortInfo.parseSortColumns(orderby); String[] passColumns = validSortColumns.split(","); for(SortInfo info : infos) { String columnName = info.getColumnName(); if(!isPass(passColumns, info, columnName)) { throw new InvalidDataAccessApiUsageException("orderby:["+orderby+"] is invalid, only can orderby:"+validSortColumns); } } return true; } private static boolean isPass(String[] passColumns, SortInfo info, String columnName) { for(String column : passColumns) { if(column.equalsIgnoreCase(info.getColumnName())) { return true; } } return false; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |