1 package org.jahia.data.fields; 2 3 import org.jahia.params.ParamBean; 4 import org.jahia.params.DummyServletRequestWrapper; 5 6 import java.util.Collections ; 7 import org.jahia.utils.JahiaTools; 8 import java.util.Vector ; 9 import org.jahia.exceptions.JahiaException; 10 import java.util.Comparator ; 11 import org.apache.commons.jexl.Expression; 12 import org.apache.commons.jexl.ExpressionFactory; 13 import org.apache.commons.jexl.JexlContext; 14 import org.apache.commons.jexl.JexlHelper; 15 16 25 26 public class ExpressionMarker implements Comparator { 27 28 private static org.apache.log4j.Logger logger = 29 org.apache.log4j.Logger.getLogger(ExpressionMarker.class); 30 31 private String expr; 32 private boolean storeMarker; 33 private ExpressionContext expressionContext; 34 private String value = null; 35 private ParamBean paramBean = null; 36 37 static private final String EXPR_ATTR_PREFIX = " expr=\""; 38 static private final String STOREMARKER_ATTR_PREFIX = " storeMarker=\""; 39 static private final int EXPR_ATTR_PREFIX_LEN = EXPR_ATTR_PREFIX.length(); 40 static private final int STOREMARKER_ATTR_PREFIX_LEN = 41 STOREMARKER_ATTR_PREFIX.length(); 42 43 public ExpressionMarker (String expr, boolean storeMarker, 44 ParamBean paramBean) { 45 this.expr = expr; 46 this.storeMarker = storeMarker; 47 this.paramBean = paramBean; 48 this.expressionContext = new ExpressionContext(paramBean); 49 } 50 51 public String getExpr () { 52 return expr; 53 } 54 55 public void setExpr (String expr) { 56 this.expr = expr; 57 } 58 59 public boolean isStoreMarker () { 60 return storeMarker; 61 } 62 63 public void setStoreMarker (boolean storeMarker) { 64 this.storeMarker = storeMarker; 65 } 66 67 public String getValue () { 68 if (value == null && expr != null && !expr.equals("")) { 69 try { 70 Expression e = ExpressionFactory.createExpression(expr); 71 72 JexlContext jc = JexlHelper.createContext(); 73 jc.getVars().put("currentContext", expressionContext); 74 jc.getVars().put("currentPage", expressionContext.getPageBean()); 75 jc.getVars().put("currentSite", expressionContext.getSiteBean()); 76 jc.getVars().put("currentJahia", expressionContext.getJahiaBean()); 77 jc.getVars().put("currentUser", expressionContext.getUser()); 78 jc.getVars().put("currentRequest", 79 expressionContext.getRequestBean()); 80 81 89 90 Object o = e.evaluate(jc); 91 92 if (o == null) { 93 logger.warn("Didn't find object for expression " + expr); 94 } else { 95 if (o instanceof String ) { 96 value = (String ) o; 97 } else { 98 value = o.toString(); 99 } 100 } 101 } catch (Exception e) { 102 logger.error("Error while evaluating JEXL expression [" + expr + 103 "]", e); 104 } 105 106 return value; 107 } else { 108 return value; 109 } 110 } 111 112 125 public static String getValue(String value, ParamBean paramBean) 126 throws JahiaException { 127 128 if ( paramBean == null || paramBean.getRealRequest() == null 130 || (paramBean.getRealRequest() instanceof DummyServletRequestWrapper) ){ 131 return value; 132 } 133 134 ExpressionMarker marker = 135 ExpressionMarker.parseMarkerValue(value, paramBean); 136 if ( marker == null ){ 137 return value; 138 } 139 140 String result = marker.getValue(); 141 142 return result; 143 } 144 145 public void setValue (String value) { 146 this.value = value; 147 } 148 149 public ExpressionContext getExpressionContext () { 150 return expressionContext; 151 } 152 153 public void setExpressionContext (ExpressionContext expressionContext) { 154 this.expressionContext = expressionContext; 155 } 156 157 164 public static ExpressionMarker parseMarkerValue (String markerStr, 165 ParamBean paramBean) { 166 167 if (markerStr == null) { 168 return null; 169 } 170 171 ExpressionMarker marker = null; 172 String val = markerStr.trim(); 173 String expr = null; 174 boolean storeMarker = false; 175 176 if (val.startsWith("<jahia-expression") && val.endsWith("/>")) { 177 178 try { 179 180 int pos = val.indexOf(EXPR_ATTR_PREFIX); 181 if (pos != -1) { 182 expr = 183 val.substring(pos + EXPR_ATTR_PREFIX_LEN, 184 val.indexOf("\" ", 185 pos + EXPR_ATTR_PREFIX_LEN)); 186 expr = JahiaTools.replacePattern(expr, """, "\""); 187 expr = JahiaTools.replacePattern(expr, "&", "&"); 188 } 189 190 pos = val.indexOf(STOREMARKER_ATTR_PREFIX); 191 if (pos != -1) { 192 storeMarker = new Boolean ( 193 val.substring(pos + STOREMARKER_ATTR_PREFIX_LEN, 194 val.indexOf("\"", 195 pos + STOREMARKER_ATTR_PREFIX_LEN)). 196 trim()).booleanValue(); 197 } 198 199 marker = new ExpressionMarker(expr, storeMarker, paramBean); 200 } catch (Throwable t) { 201 logger.error("Error while parsing expression marker", t); 202 } 203 } else { 204 marker = new ExpressionMarker("", false, paramBean); 205 marker.setValue(markerStr); 206 } 207 208 return marker; 209 } 210 211 221 public static Vector buildExpressionMarkers (String enumValues, 222 ParamBean paramBean) 223 throws JahiaException { 224 225 String [] tokens = JahiaTools.getTokens(enumValues, ":"); 226 Vector markers = new Vector (); 227 for (int i = 0; i < tokens.length; i++) { 228 logger.debug("token=" + tokens[i]); 229 ExpressionMarker marker = 230 ExpressionMarker.parseMarkerValue(tokens[i], paramBean); 231 markers.add(marker); 241 } 242 ExpressionMarker compMarker = new ExpressionMarker("", false, paramBean); 244 compMarker.setValue(""); 245 Collections.sort(markers, compMarker); 246 return markers; 247 } 248 249 259 public static String drawMarker (String expr, boolean storeMarker) { 260 StringBuffer buff = new StringBuffer ("<jahia-expression"); 261 buff.append(EXPR_ATTR_PREFIX); 262 String escapedExpr = expr; 263 escapedExpr = JahiaTools.replacePattern(escapedExpr, "&", "&"); 264 escapedExpr = JahiaTools.replacePattern(escapedExpr, "\"", """); 265 buff.append(escapedExpr); 266 buff.append("\""); 267 buff.append(STOREMARKER_ATTR_PREFIX); 268 buff.append(storeMarker); 269 buff.append("\"/>"); 270 return buff.toString(); 271 } 272 273 280 public String drawMarker () { 281 if (expr != null && !expr.equals("")) { 282 return drawMarker(this.getExpr(), 283 this.isStoreMarker()); 284 } else { 285 return value; 286 } 287 } 288 289 296 public int compare (Object c1, Object c2) 297 throws ClassCastException { 298 299 ExpressionMarker e1 = (ExpressionMarker) c1; 300 ExpressionMarker e2 = (ExpressionMarker) c2; 301 302 if ( (e1.getValue() == null) && (e2.getValue() == null)) { 303 return 0; 304 } 305 306 if (e1.getValue() == null) { 307 return -1; 308 } 309 310 if (e2.getValue() == null) { 311 return 1; 312 } 313 314 return ( (ExpressionMarker) c1) 315 .getValue() 316 .compareToIgnoreCase( ( (ExpressionMarker) c2).getValue(). 317 toLowerCase()); 318 319 } 320 321 } 322 | Popular Tags |