View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
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  			// TODO Auto-generated catch block
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 			// TODO Auto-generated catch block
120 			e.printStackTrace();
121 			return Collections.EMPTY_LIST;
122 		}
123 	}
124 
125 }