Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright the original author or authors.
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package uelbox;
 
 
 import  javax.el.ELContext;
 import  javax.el.ELException;
 import  javax.el.ELResolver;
 import  javax.el.ExpressionFactory;
 import  javax.el.PropertyNotFoundException;
 import  javax.el.PropertyNotWritableException;
 
UEL utility methods.
 
 public class UEL {
    
Default trigger character for delimited UEL expressions.
 
     public static final char DEFAULT_TRIGGER = '#';
 
     private static final char DOT = '.';
     private static final char LBRACK = '[';
     private static final Pattern DELIMITED_EXPR = Pattern.compile("^.\\{\\s*(.*?)\\s*\\}$");
 
     private static final ELResolver NOP_EL_RESOLVER = new ELResolver() {
 
         @Override
         public Class<?> getCommonPropertyType(ELContext arg0Object arg1) {
             return null;
         }
 
         @Override
         public Iterator<FeatureDescriptorgetFeatureDescriptors(ELContext arg0Object arg1) {
             return null;
         }
 
         @Override
         public Class<?> getType(ELContext arg0Object arg1Object arg2throws NullPointerException,
             PropertyNotFoundException, ELException {
             return null;
         }
 
         @Override
         public Object getValue(ELContext arg0Object arg1Object arg2throws NullPointerException,
             PropertyNotFoundException, ELException {
             return null;
         }
 
         @Override
         public boolean isReadOnly(ELContext arg0Object arg1Object arg2throws NullPointerException,
             PropertyNotFoundException, ELException {
             return false;
         }
 
         @Override
         public void setValue(ELContext arg0Object arg1Object arg2Object arg3throws NullPointerException,
             PropertyNotFoundException, PropertyNotWritableException, ELException {
         }
 
     };

    
Get an ExpressionFactory instance for the specified context, using ELContext.getContext(Class), and setting such a context value, if not found, to javax.el.ExpressionFactory.newInstance().

Parameters:
context
Returns:
ExpressionFactory
 
     public static ExpressionFactory getExpressionFactory(ELContext context) {
         ExpressionFactory result = getContext(context, ExpressionFactory.class);
         if (result == null) {
             result = ExpressionFactory.newInstance();
             context.putContext(ExpressionFactory.classresult);
         }
         return result;
     }

    
Casts context objects per documented convention.

Parameters:
<T>
context
key
Returns:
T
    public static <T> T getContext(ELContext contextClass<T> key) {
        return getContext(contextkeynull);
    }

    
Casts context objects per documented convention.

Parameters:
<T>
context
key
defaultValue
Returns:
T
    public static <T> T getContext(ELContext contextClass<T> key, T defaultValue) {
        @SuppressWarnings("unchecked")
        final T result = (T) context.getContext(key);
        return result == null ? defaultValue : result;
    }

    
Embed the specified expression, if necessary, using DEFAULT_TRIGGER as the triggering character.

Parameters:
expression
Returns:
String
    public static String embed(String expression) {
        return embed(expression);
    }

    
Embed the specified expression, if necessary, using the specified triggering character.

Parameters:
expression
trigger
Returns:
String
    public static String embed(final String expressionfinal char trigger) {
        return new StringBuilder().append(trigger).append('{').append(strip(expression)).append('}').toString();
    }

    
Learn whether the specified expression is delimited.

Parameters:
expression
Returns:
boolean
    public static boolean isDelimited(final String expression) {
        return .matcher(StringUtils.trimToEmpty(expression)).matches();
    }

    
Get the trigger character for the specified delimited expression.

Parameters:
delimitedExpression
Returns:
first non-whitespace character of delimitedExpression
Throws:
IllegalArgumentException if argument expression is not delimited
    public static char getTrigger(final String delimitedExpression) {
        final String expr = StringUtils.trimToEmpty(delimitedExpression);
        Validate.isTrue(isDelimited(expr));
        return expr.charAt(0);
    }

    
Strip any delimiter from the specified expression.

Parameters:
expression
Returns:
String
    public static String strip(final String expression) {
        final String expr = StringUtils.trimToEmpty(expression);
        final Matcher matcher = .matcher(expr);
        return matcher.matches() ? matcher.group(1) : expr;
    }

    
Join expressions using the trigger character, if any (DEFAULT_TRIGGER if absent), of the first.

Parameters:
expressions
Returns:
String
    public static String join(final String... expressions) {
        Validate.notEmpty(expressions);
        final char trigger = isDelimited(expressions[0]) ? getTrigger(expressions[0]) : ;
        return join(triggerexpressions);
    }

    
Join expressions using the specified trigger character.

Parameters:
trigger
expressions
Returns:
String
    public static String join(final char triggerfinal String... expressions) {
        Validate.notEmpty(expressions);
        final StringBuilder buf = new StringBuilder();
        for (String expression : expressions) {
            final String stripped = strip(expression);
            if (expression.isEmpty()) {
                continue;
            }
            final int len = buf.length();
            if (len > 0) {
                final int end = len - 1;
                final char last = buf.charAt(end);
                final char first = stripped.charAt(0);
                
                switch (first) {
                case :
                case :
                    if (last == ) {
                        buf.deleteCharAt(end);
                    }
                    break;
                default:
                    if (last != ) {
                        buf.append();
                    }
                    break;
                }
            }
            buf.append(stripped);
        }
        return embed(buf.toString(), trigger);
    }

    
Use EL specification coercion facilities to coerce an object to the specified type.

Parameters:
context
toType
object
Returns:
T
Throws:
ELException if the coercion fails.
    public static <T> T coerceToType(ELContext contextClass<T> toTypeObject object) {
        @SuppressWarnings("unchecked")
        T result = (T) getExpressionFactory(context).coerceToType(objecttoType);
        return result;
    }

    
Get an ELResolver that handles nothing.

Returns:
ELResolver
    public static ELResolver nopELResolver() {
        return ;
    }
New to GrepCode? Check out our FAQ X