Source code: org/jbpm/workflow/delegation/impl/AttributeExpressionResolver.java
1 package org.jbpm.workflow.delegation.impl;
2
3 import org.apache.log4j.Logger;
4 import org.jbpm.workflow.delegation.*;
5
6 public class AttributeExpressionResolver {
7
8 private static final String LEFT_MARKER = "${";
9 private static final String RIGHT_MARKER = "}";
10
11 // the singleton pattern constructor
12 private AttributeExpressionResolver() {
13 }
14
15 // the singleton pattern getInstance
16 public static AttributeExpressionResolver getInstance() {
17 return instance;
18 }
19
20 public String resolveAttributeExpression( String expression, HandlerContext handlerContext ) {
21
22 String text = expression;
23
24 int leftMarkerIndex = text.indexOf( LEFT_MARKER );
25 int rightMarkerIndex = text.indexOf( RIGHT_MARKER, leftMarkerIndex + LEFT_MARKER.length() );
26
27 while ( ( leftMarkerIndex != -1 )
28 && ( rightMarkerIndex != -1 ) ) {
29
30
31 String attributeName = text.substring( leftMarkerIndex + LEFT_MARKER.length(), rightMarkerIndex ).trim();
32
33
34 try {
35 Object attribute = handlerContext.getAttribute( attributeName );
36 if ( attribute != null ) {
37 String attributeString = attribute.toString();
38 text = text.substring( 0, leftMarkerIndex ) +
39 attributeString +
40 text.substring( rightMarkerIndex + RIGHT_MARKER.length() );
41 rightMarkerIndex = rightMarkerIndex + attributeString.length() - attributeName.length() - LEFT_MARKER.length() - RIGHT_MARKER.length();
42 }
43 } catch ( Exception e ) {
44 log.debug("attribute '" + attributeName + "' could not be resolved in attribute expression '" + expression + "'");
45 }
46
47 leftMarkerIndex = text.indexOf( LEFT_MARKER, rightMarkerIndex + RIGHT_MARKER.length() );
48 rightMarkerIndex = text.indexOf( RIGHT_MARKER, leftMarkerIndex + LEFT_MARKER.length() );
49 }
50
51 return text;
52 }
53
54 private static final AttributeExpressionResolver instance = new AttributeExpressionResolver();
55 private static final Logger log = Logger.getLogger(AttributeExpressionResolver.class);
56 }