1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package no.sintef.clix.functions;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import no.sintef.clix.Link;
30 import no.sintef.clix.jaxb.Exists;
31
32 import org.apache.log4j.Logger;
33 import org.jaxen.JaxenException;
34 import org.jaxen.NamespaceContext;
35 import org.jaxen.VariableContext;
36 import org.jaxen.XPath;
37 import org.w3c.dom.Node;
38
39 public class FunExists extends FunQuantifier {
40
41 private static final Logger LOGGER = Logger.getLogger(FunExists.class);
42
43 public FunExists(Exists f) {
44 super(f);
45 }
46
47 public boolean evaluate(Object context, final VariableContext variableContext, NamespaceContext namespaceContext) {
48 if (LOGGER.isDebugEnabled()) {
49 LOGGER.debug("FunExists.evaluate(" + context + ", " + variableContext + ")");
50 }
51 try {
52 Fun formula = getSubformula();
53 if (formula == null) {
54 return false;
55 }
56
57 final String varname = getVar();
58 XPath xpath = getIn();
59 Collection nodes = Selector.selectNodes(context, xpath, variableContext, namespaceContext);
60 for (Iterator i = nodes.iterator(); i.hasNext();) {
61 Object o = i.next();
62 boolean result = formula.evaluate(context, Helper.bind(variableContext, varname, o), namespaceContext);
63 if (result == true) {
64 return true;
65 }
66 }
67 return false;
68 } catch (JaxenException e) {
69
70 e.printStackTrace();
71 return false;
72 }
73 }
74
75 public List genLinks(Object context, VariableContext variableContext, NamespaceContext namespaceContext) {
76 return genLinks(context, variableContext, null, false);
77 }
78
79 public List genRuleLinks(Object context, VariableContext variableResolver, NamespaceContext namespaceContext) {
80 return genLinks(context, variableResolver, namespaceContext, true);
81 }
82
83 private List genLinks(Object context, VariableContext variableResolver, NamespaceContext namespaceContext, boolean all) {
84 try {
85 Fun formula = getSubformula();
86 if (formula == null) {
87 return Collections.EMPTY_LIST;
88 }
89
90 List links = new ArrayList();
91
92 final String varname = getVar();
93 XPath xpath = getIn();
94 Collection nodes = Selector.selectNodes(context, xpath, variableResolver, namespaceContext);
95 for (Iterator i = nodes.iterator(); i.hasNext(); ) {
96 Object o = i.next();
97 VariableContext vc = Helper.bind(variableResolver, varname, o);
98 boolean result = formula.evaluate(context, vc, namespaceContext);
99 if (result == true || all == true) {
100 Link link;
101 if (o instanceof Node) {
102 link = new Link(result, (Node)o);
103 } else if (o instanceof String) {
104 link = new Link(result, (String)o);
105 } else {
106 System.err.println("Unexpected variable type: " + o);
107 continue;
108 }
109 link.setVariableBinding(varname, o);
110 List newLinks = link.cross(formula.genLinks(context, vc, namespaceContext));
111 links.addAll(newLinks);
112 }
113 }
114 if (LOGGER.isDebugEnabled()) {
115 LOGGER.debug("FunExists.genLinks = " + links);
116 }
117 return links;
118 } catch (JaxenException e) {
119
120 e.printStackTrace();
121 return Collections.EMPTY_LIST;
122 }
123 }
124
125 }