View Javadoc

1   package pl.caltha.forms.internal.model;
2   
3   import java.util.List;
4   
5   import org.dom4j.DocumentHelper;
6   import org.dom4j.InvalidXPathException;
7   import org.dom4j.XPath;
8   
9   import pl.caltha.forms.ConstructionException;
10  
11  /***
12   *
13   * @author  damian
14   */
15  public class InstanceReference
16  {
17      String name;
18      boolean absolute;
19      String expression;
20      XPath xpath;
21  
22      /*** Creates a default instance of InstanceReference. */
23      public InstanceReference()
24      {
25          this.name = "default";
26          this.expression = "/";
27          this.absolute = true;
28  
29          try
30          {
31              this.xpath = createXPath(name, expression);
32          }
33          catch(ConstructionException e)
34          {
35              throw new RuntimeException("Whoops! Cannot construct a default InstanceReference:"+e);
36          }
37      }
38  
39      /*** Creates a new instance of InstanceReference */
40      public InstanceReference(String name, String xPathExpression)
41      throws ConstructionException
42      {
43          this.name = name;
44          this.expression = xPathExpression;
45          this.xpath = createXPath(name, expression);
46  
47          // check for absolute XPath
48          this.absolute = (this.xpath.getText().charAt(0) == '/');
49          // calculate absolute XPath for common functions
50          // which are frequently used for bind expressions
51          if(this.xpath.getText().equals("true()") ||
52             this.xpath.getText().equals("false()"))
53          {
54              this.absolute = true;
55          }
56      }
57  
58      protected XPath createXPath(String name, String xPathExpression)
59      throws ConstructionException
60      {
61          try
62          {
63              return DocumentHelper.createXPath(xPathExpression);
64          }
65          catch(InvalidXPathException e)
66          {
67              throw new ConstructionException("Invalid XPath in attribute '"+name+"'", e);
68          }
69      }
70  
71      public Object getValue(InstanceImpl instance)
72      {
73         return this.xpath.evaluate(instance.getDocument().getRootElement());
74      }
75  
76      public Object getValue(Object context)
77      {
78         return this.xpath.evaluate(context);
79      }
80  
81      public String getStringValue(InstanceImpl instance)
82      {
83         return this.xpath.valueOf(instance.getDocument().getRootElement());
84      }
85  
86      public String getStringValue(Object context)
87      {
88         return this.xpath.valueOf(context);
89      }
90  
91      public org.dom4j.Node getNode(InstanceImpl instance)
92      {
93         return this.xpath.selectSingleNode(instance.getDocument().getRootElement());
94      }
95  
96      public org.dom4j.Node getNode(Object context)
97      {
98         return this.xpath.selectSingleNode(context);
99      }
100 
101     public List getNodes(InstanceImpl instance)
102     {
103        return this.xpath.selectNodes(instance.getDocument().getRootElement());
104     }
105 
106     public List getNodes(Object context)
107     {
108        return this.xpath.selectNodes(context);
109     }
110 
111     public boolean isAbsolute()
112     {
113         return absolute;
114     }
115 }