Java Code Examples for javax.el.ExpressionFactory#newInstance()

The following examples show how to use javax.el.ExpressionFactory#newInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestELParser.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testJavaKeyWordIdentifier() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    TesterBeanA beanA = new TesterBeanA();
    beanA.setInt("five");
    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("this", var);

    // Should fail
    Exception e = null;
    try {
        factory.createValueExpression(context, "${this}", String.class);
    } catch (ELException ele) {
        e = ele;
    }
    Assert.assertNotNull(e);
}
 
Example 2
Source File: TestValueExpressionImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Test using list directly as variable.
 */
@Test
public void testBug51544Direct() throws Exception {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    List<?> list = Collections.emptyList();

    ValueExpression var =
        factory.createValueExpression(list, List.class);
    context.getVariableMapper().setVariable("list", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${list.size()}", Integer.class);

    Integer result = (Integer) ve.getValue(context);
    assertEquals(Integer.valueOf(0), result);
}
 
Example 3
Source File: TestELParser.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private void doTestBug56179(int parenthesesCount, String innerExpr) {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    ValueExpression var =
        factory.createValueExpression(Boolean.TRUE, Boolean.class);
    context.getVariableMapper().setVariable("test", var);

    StringBuilder expr = new StringBuilder();
    expr.append("${");
    for (int i = 0; i < parenthesesCount; i++) {
        expr.append("(");
    }
    expr.append(innerExpr);
    for (int i = 0; i < parenthesesCount; i++) {
        expr.append(")");
    }
    expr.append("}");
    ValueExpression ve = factory.createValueExpression(
            context, expr.toString(), String.class);

    String result = (String) ve.getValue(context);
    assertEquals("true", result);
}
 
Example 4
Source File: ExpressionEvaluatorImpl.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
public Expression parseExpression(String expression,
                                  Class expectedType,
                                  FunctionMapper fMapper )
        throws ELException {

    ExpressionFactory fac = ExpressionFactory.newInstance();
    javax.el.ValueExpression expr;
    ELContextImpl elContext = new ELContextImpl(null);
    javax.el.FunctionMapper fm = new FunctionMapperWrapper(fMapper);
    elContext.setFunctionMapper(fm);
    try {
        expr = fac.createValueExpression(
                       elContext,
                       expression, expectedType);
    } catch (javax.el.ELException ex) {
        throw new ELException(ex);
    }
    return new ExpressionImpl(expr, pageContext);
}
 
Example 5
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Test using list directly as variable.
 */
@Test
public void testBug51544Direct() throws Exception {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    List<?> list = Collections.emptyList();

    ValueExpression var =
        factory.createValueExpression(list, List.class);
    context.getVariableMapper().setVariable("list", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${list.size()}", Integer.class);

    Integer result = (Integer) ve.getValue(context);
    assertEquals(Integer.valueOf(0), result);
}
 
Example 6
Source File: TestELParser.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaKeyWordIdentifier() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    beanA.setInt("five");
    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("this", var);

    // Should fail
    Exception e = null;
    try {
        factory.createValueExpression(context, "${this}", String.class);
    } catch (ELException ele) {
        e = ele;
    }
    assertNotNull(e);
}
 
Example 7
Source File: TestValueExpressionImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug50105() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    TesterEnum testEnum = TesterEnum.APPLE;

    ValueExpression var =
        factory.createValueExpression(testEnum, TesterEnum.class);
    context.getVariableMapper().setVariable("testEnum", var);

    // When coercing an Enum to a String, name() should always be used.
    ValueExpression ve1 = factory.createValueExpression(
            context, "${testEnum}", String.class);
    String result1 = (String) ve1.getValue(context);
    Assert.assertEquals("APPLE", result1);

    ValueExpression ve2 = factory.createValueExpression(
            context, "foo${testEnum}bar", String.class);
    String result2 = (String) ve2.getValue(context);
    Assert.assertEquals("fooAPPLEbar", result2);
}
 
Example 8
Source File: TestValueExpressionImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug51177ObjectList() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    Object o1 = "String value";
    Object o2 = Integer.valueOf(32);

    List<Object> list = new ArrayList<Object>();
    list.add(0, o1);
    list.add(1, o2);

    ValueExpression var =
        factory.createValueExpression(list, List.class);
    context.getVariableMapper().setVariable("list", var);

    ValueExpression ve1 = factory.createValueExpression(
            context, "${list[0]}", Object.class);
    ve1.setValue(context, o2);
    assertEquals(o2, ve1.getValue(context));

    ValueExpression ve2 = factory.createValueExpression(
            context, "${list[1]}", Object.class);
    ve2.setValue(context, o1);
    assertEquals(o1, ve2.getValue(context));
}
 
Example 9
Source File: MetricNameFactory.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
@Produces
// TODO: should be declared @ApplicationScoped when WELD-2083 is fixed
private MetricName metricName(BeanManager manager) {
    try {
        // Cannot be inlined as OWB throws a NPE when manager.getELResolver() gets called
        ExpressionFactory factory = ExpressionFactory.newInstance();
        return new ElMetricName(manager.getELResolver(), manager.wrapExpressionFactory(factory), manager.getExtension(MetricsExtension.class));
    } catch (ELException cause) {
        // Falls back to SE
        return new SeMetricName(manager.getExtension(MetricsExtension.class));
    }
}
 
Example 10
Source File: Myfaces1.java    From ysoserial with MIT License 5 votes vote down vote up
public static Object makeExpressionPayload ( String expr ) throws IllegalArgumentException, IllegalAccessException, Exception  {
    FacesContextImpl fc = new FacesContextImpl((ServletContext) null, (ServletRequest) null, (ServletResponse) null);
    ELContext elContext = new FacesELContext(new CompositeELResolver(), fc);
    Reflections.getField(FacesContextImplBase.class, "_elContext").set(fc, elContext);
    ExpressionFactory expressionFactory = ExpressionFactory.newInstance();

    ValueExpression ve1 = expressionFactory.createValueExpression(elContext, expr, Object.class);
    ValueExpressionMethodExpression e = new ValueExpressionMethodExpression(ve1);
    ValueExpression ve2 = expressionFactory.createValueExpression(elContext, "${true}", Object.class);
    ValueExpressionMethodExpression e2 = new ValueExpressionMethodExpression(ve2);

    return Gadgets.makeMap(e2, e);
}
 
Example 11
Source File: TestELParser.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void doTestParser(String input, String expectedResult, String expectedBuilderOutput) throws JasperException {

        ELException elException = null;
        String elResult = null;

        // Don't try and evaluate expressions that depend on variables or functions
        if (expectedResult != null) {
            try {
                ExpressionFactory factory = ExpressionFactory.newInstance();
                ELContext context = new ELContextImpl();
                ValueExpression ve = factory.createValueExpression(context, input, String.class);
                elResult = ve.getValue(context).toString();
                Assert.assertEquals(expectedResult, elResult);
            } catch (ELException ele) {
                elException = ele;
            }
        }

        Nodes nodes = null;
        try {
            nodes = ELParser.parse(input, false);
            Assert.assertNull(elException);
        } catch (IllegalArgumentException iae) {
            Assert.assertNotNull(elResult, elException);
            // Not strictly true but enables us to report both
            iae.initCause(elException);
            throw iae;
        }

        TextBuilder textBuilder = new TextBuilder(false);

        nodes.visit(textBuilder);

        Assert.assertEquals(expectedBuilderOutput, textBuilder.getText());
    }
 
Example 12
Source File: TestELParser.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void testExpression(String expression, String expected) {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    ValueExpression ve = factory.createValueExpression(
            context, expression, String.class);

    String result = (String) ve.getValue(context);
    assertEquals(expected, result);
}
 
Example 13
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug49345() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    beanA.setBean(beanB);

    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${beanA.bean.name}", String.class);

    // First check the basics work
    String result = (String) ve.getValue(context);
    assertEquals("Tomcat", result);

    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    assertNotNull(vr);

    assertEquals(beanB, vr.getBase());
    assertEquals("name", vr.getProperty());
}
 
Example 14
Source File: ExpressionFactoryTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatelessSessionBean() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    Thread.currentThread().setContextClassLoader(classLoader);
    ExpressionFactory factory = ExpressionFactory.newInstance();
    Assert.assertNotNull("Factory not null", factory);
}
 
Example 15
Source File: TestMethodExpressionImpl.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    factory = ExpressionFactory.newInstance();
    context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    beanA.setName("A");
    context.getVariableMapper().setVariable("beanA",
            factory.createValueExpression(beanA, TesterBeanA.class));

    TesterBeanAA beanAA = new TesterBeanAA();
    beanAA.setName("AA");
    context.getVariableMapper().setVariable("beanAA",
            factory.createValueExpression(beanAA, TesterBeanAA.class));

    TesterBeanAAA beanAAA = new TesterBeanAAA();
    beanAAA.setName("AAA");
    context.getVariableMapper().setVariable("beanAAA",
            factory.createValueExpression(beanAAA, TesterBeanAAA.class));

    beanB = new TesterBeanB();
    beanB.setName("B");
    context.getVariableMapper().setVariable("beanB",
            factory.createValueExpression(beanB, TesterBeanB.class));

    TesterBeanBB beanBB = new TesterBeanBB();
    beanBB.setName("BB");
    context.getVariableMapper().setVariable("beanBB",
            factory.createValueExpression(beanBB, TesterBeanBB.class));

    TesterBeanBBB beanBBB = new TesterBeanBBB();
    beanBBB.setName("BBB");
    context.getVariableMapper().setVariable("beanBBB",
            factory.createValueExpression(beanBBB, TesterBeanBBB.class));

    TesterBeanC beanC = new TesterBeanC();
    context.getVariableMapper().setVariable("beanC",
            factory.createValueExpression(beanC, TesterBeanC.class));

    TesterBeanEnum beanEnum = new TesterBeanEnum();
    context.getVariableMapper().setVariable("beanEnum",
            factory.createValueExpression(beanEnum, TesterBeanEnum.class));
}
 
Example 16
Source File: PathPartitionHelper.java    From spork with Apache License 2.0 4 votes vote down vote up
/**
    * This method is called by the FileInputFormat to find the input paths for
    * which splits should be calculated.<br/>
    * If applyDateRanges == true: Then the HiveRCDateSplitter is used to apply
    * filtering on the input files.<br/>
    * Else the default FileInputFormat listStatus method is used.
    * 
    * @param ctx
    *            JobContext
    * @param loaderClass
    *            this is chosen to be a subclass of LoadFunc to maintain some
    *            consistency.
    */
   public List<FileStatus> listStatus(JobContext ctx,
    Class<? extends LoadFunc> loaderClass, String signature)
    throws IOException {

Properties properties = UDFContext.getUDFContext().getUDFProperties(
	loaderClass, new String[] { signature });

String partitionExpression = properties
	.getProperty(PARITITION_FILTER_EXPRESSION);

ExpressionFactory expressionFactory = null;

if (partitionExpression != null) {
    expressionFactory = ExpressionFactory.newInstance();
}

String partitionColumnStr = properties
	.getProperty(PathPartitionHelper.PARTITION_COLUMNS);
String[] partitionKeys = (partitionColumnStr == null) ? null
	: partitionColumnStr.split(",");

Path[] inputPaths = FileInputFormat.getInputPaths(ctx);

List<FileStatus> splitPaths = null;

if (partitionKeys != null) {

    splitPaths = new ArrayList<FileStatus>();

    for (Path inputPath : inputPaths) {
	// for each input path work recursively through each partition
	// level to find the rc files

	FileSystem fs = inputPath.getFileSystem(ctx.getConfiguration());

	if (fs.getFileStatus(inputPath).isDir()) {
	    // assure that we are at the root of the partition tree.
	    FileStatus fileStatusArr[] = fs.listStatus(inputPath);

	    if (fileStatusArr != null) {
		for (FileStatus childFileStatus : fileStatusArr) {
		    getPartitionedFiles(expressionFactory,
			    partitionExpression, fs, childFileStatus,
			    0, partitionKeys, splitPaths);
		}
	    }

	} else {
	    splitPaths.add(fs.getFileStatus(inputPath));
	}

    }

    if (splitPaths.size() < 1) {
	LOG.error("Not split paths where found, please check that the filter logic for the partition keys does not filter out everything ");
    }

}

return splitPaths;
   }
 
Example 17
Source File: JspUtil.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
private static ExpressionFactory getExpressionFactory() {
    if (expFactory == null) {
        expFactory = ExpressionFactory.newInstance();
    }
    return expFactory;
}
 
Example 18
Source File: TestMethodExpressionImpl.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Before
public void setUp() {
    factory = ExpressionFactory.newInstance();
    context = new ELContextImpl(factory);

    TesterBeanA beanA = new TesterBeanA();
    beanA.setName("A");
    context.getVariableMapper().setVariable("beanA",
            factory.createValueExpression(beanA, TesterBeanA.class));

    TesterBeanAA beanAA = new TesterBeanAA();
    beanAA.setName("AA");
    context.getVariableMapper().setVariable("beanAA",
            factory.createValueExpression(beanAA, TesterBeanAA.class));

    TesterBeanAAA beanAAA = new TesterBeanAAA();
    beanAAA.setName("AAA");
    context.getVariableMapper().setVariable("beanAAA",
            factory.createValueExpression(beanAAA, TesterBeanAAA.class));

    beanB = new TesterBeanB();
    beanB.setName("B");
    context.getVariableMapper().setVariable("beanB",
            factory.createValueExpression(beanB, TesterBeanB.class));

    TesterBeanBB beanBB = new TesterBeanBB();
    beanBB.setName("BB");
    context.getVariableMapper().setVariable("beanBB",
            factory.createValueExpression(beanBB, TesterBeanBB.class));

    TesterBeanBBB beanBBB = new TesterBeanBBB();
    beanBBB.setName("BBB");
    context.getVariableMapper().setVariable("beanBBB",
            factory.createValueExpression(beanBBB, TesterBeanBBB.class));

    TesterBeanC beanC = new TesterBeanC();
    context.getVariableMapper().setVariable("beanC",
            factory.createValueExpression(beanC, TesterBeanC.class));

    TesterBeanEnum beanEnum = new TesterBeanEnum();
    context.getVariableMapper().setVariable("beanEnum",
            factory.createValueExpression(beanEnum, TesterBeanEnum.class));
}
 
Example 19
Source File: JspApplicationContextImpl.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
public ExpressionFactory getExpressionFactory() {
    if (expressionFactory == null) {
        expressionFactory = ExpressionFactory.newInstance();
    }
    return expressionFactory;
}
 
Example 20
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    factory = ExpressionFactory.newInstance();
    context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    beanA.setName("A");
    context.getVariableMapper().setVariable("beanA",
            factory.createValueExpression(beanA, TesterBeanA.class));

    TesterBeanAA beanAA = new TesterBeanAA();
    beanAA.setName("AA");
    context.getVariableMapper().setVariable("beanAA",
            factory.createValueExpression(beanAA, TesterBeanAA.class));

    TesterBeanAAA beanAAA = new TesterBeanAAA();
    beanAAA.setName("AAA");
    context.getVariableMapper().setVariable("beanAAA",
            factory.createValueExpression(beanAAA, TesterBeanAAA.class));

    beanB = new TesterBeanB();
    beanB.setName("B");
    context.getVariableMapper().setVariable("beanB",
            factory.createValueExpression(beanB, TesterBeanB.class));

    TesterBeanBB beanBB = new TesterBeanBB();
    beanBB.setName("BB");
    context.getVariableMapper().setVariable("beanBB",
            factory.createValueExpression(beanBB, TesterBeanBB.class));

    TesterBeanBBB beanBBB = new TesterBeanBBB();
    beanBBB.setName("BBB");
    context.getVariableMapper().setVariable("beanBBB",
            factory.createValueExpression(beanBBB, TesterBeanBBB.class));

    TesterBeanC beanC = new TesterBeanC();
    context.getVariableMapper().setVariable("beanC",
            factory.createValueExpression(beanC, TesterBeanC.class));

    TesterBeanEnum beanEnum = new TesterBeanEnum();
    context.getVariableMapper().setVariable("beanEnum",
            factory.createValueExpression(beanEnum, TesterBeanEnum.class));
}