Project

General

Profile

1
package eu.dnetlib.springutils.condbean;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.util.Properties;
6

    
7
import org.apache.commons.lang3.StringUtils;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10

    
11
/**
12
 * This is the original implementation of Robert Maldon's property expression parser, factored out in a
13
 * ConditionExpressionParser so that we can easly replace it with a more powerful parser.
14
 * 
15
 * @see JParsecConditionExpression
16
 * 
17
 * @author marko
18
 * 
19
 */
20
public class TrivialConditionExpressionParser implements ConditionExpressionParser {
21
	private static final Log log = LogFactory.getLog(TrivialConditionExpressionParser.class); // NOPMD by marko on 11/24/08 5:02 PM
22

    
23
	/** Default placeholder prefix: "${". */
24
	public static final String PH_PREFIX = "${";
25

    
26
	/** Default placeholder suffix: "}". */
27
	public static final String PH_SUFFIX = "}";
28

    
29
	private PropertyFinder finder;
30

    
31
	@Override
32
	public boolean expressionValue(final String expression) {
33
		log.debug("EXPRESSION: " + expression);
34
		log.debug("EXPRESSION VALUE: " + getProperty(expression));
35
		return !StringUtils.isEmpty(getProperty(expression));
36
	}
37

    
38
	protected String getProperty(final String strVal) {
39
		if (StringUtils.isEmpty(strVal))
40
			return null;
41

    
42
		if (strVal.startsWith(PH_PREFIX) && strVal.endsWith(PH_SUFFIX))
43
			return basicGetProperty(strVal.substring(PH_PREFIX.length(), strVal.length() - PH_SUFFIX.length()));
44

    
45
		return basicGetProperty(strVal);
46
	}
47

    
48
	protected String basicGetProperty(final String name) {
49
		return finder.getProperty(name);
50
	}
51

    
52
	protected String xxbasicGetProperty(final String name) {
53
		// TODO: merge properties from many files (wildcards) and provide a configuration
54
		final Properties props = new Properties();
55
		try {
56
			final InputStream input = TrivialConditionExpressionParser.class.getResourceAsStream("/eu/dnetlib/cnr-default.properties");
57
			if (input == null)
58
				log.warn("cannot open properties (null)");
59
			else
60
				props.load(input);
61

    
62
		} catch (IOException e) {
63
			log.warn("cannot open properties", e);
64
		}
65
		final String res = props.getProperty(name);
66
		if (res != null && !res.isEmpty())
67
			return res;
68

    
69
		return System.getProperty(name);
70
	}
71

    
72
	public PropertyFinder getFinder() {
73
		return finder;
74
	}
75

    
76
	public void setFinder(PropertyFinder finder) {
77
		this.finder = finder;
78
	}
79

    
80
}
(10-10/10)