001: /*
002: * Copyright 2002-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.compiler;
017:
018: import java.util.Collection;
019: import java.util.HashSet;
020: import java.util.Iterator;
021:
022: import org.apache.commons.jxpath.Pointer;
023: import org.apache.commons.jxpath.ri.EvalContext;
024: import org.apache.commons.jxpath.ri.InfoSetUtil;
025: import org.apache.commons.jxpath.ri.axes.InitialContext;
026: import org.apache.commons.jxpath.ri.axes.SelfContext;
027:
028: /**
029: * Common superclass for the implementations of Expression for the operations
030: * "=" and "!=".
031: *
032: * @author Dmitri Plotnikov
033: * @version $Revision: 1.3 $ $Date: 2004/02/29 14:17:38 $
034: */
035: public abstract class CoreOperationCompare extends CoreOperation {
036:
037: public CoreOperationCompare(Expression arg1, Expression arg2) {
038: super (new Expression[] { arg1, arg2 });
039: }
040:
041: /**
042: * Compares two values
043: */
044: protected boolean equal(EvalContext context, Expression left,
045: Expression right) {
046: Object l = left.compute(context);
047: Object r = right.compute(context);
048:
049: // System.err.println("COMPARING: " +
050: // (l == null ? "null" : l.getClass().getName()) + " " +
051: // (r == null ? "null" : r.getClass().getName()));
052:
053: if (l instanceof InitialContext || l instanceof SelfContext) {
054: l = ((EvalContext) l).getSingleNodePointer();
055: }
056:
057: if (r instanceof InitialContext || r instanceof SelfContext) {
058: r = ((EvalContext) r).getSingleNodePointer();
059: }
060:
061: if (l instanceof Collection) {
062: l = ((Collection) l).iterator();
063: }
064:
065: if (r instanceof Collection) {
066: r = ((Collection) r).iterator();
067: }
068:
069: if ((l instanceof Iterator) && !(r instanceof Iterator)) {
070: return contains((Iterator) l, r);
071: } else if (!(l instanceof Iterator) && (r instanceof Iterator)) {
072: return contains((Iterator) r, l);
073: } else if (l instanceof Iterator && r instanceof Iterator) {
074: return findMatch((Iterator) l, (Iterator) r);
075: }
076:
077: return equal(l, r);
078: }
079:
080: protected boolean contains(Iterator it, Object value) {
081: while (it.hasNext()) {
082: Object element = it.next();
083: if (equal(element, value)) {
084: return true;
085: }
086: }
087: return false;
088: }
089:
090: protected boolean findMatch(Iterator lit, Iterator rit) {
091: HashSet left = new HashSet();
092: while (lit.hasNext()) {
093: left.add(lit.next());
094: }
095: while (rit.hasNext()) {
096: if (contains(left.iterator(), rit.next())) {
097: return true;
098: }
099: }
100: return false;
101: }
102:
103: protected boolean equal(Object l, Object r) {
104: if (l instanceof Pointer && r instanceof Pointer) {
105: if (l.equals(r)) {
106: return true;
107: }
108: }
109:
110: if (l instanceof Pointer) {
111: l = ((Pointer) l).getValue();
112: }
113:
114: if (r instanceof Pointer) {
115: r = ((Pointer) r).getValue();
116: }
117:
118: if (l == r) {
119: return true;
120: }
121:
122: // System.err.println("COMPARING VALUES: " + l + " " + r);
123: if (l instanceof Boolean || r instanceof Boolean) {
124: return (InfoSetUtil.booleanValue(l) == InfoSetUtil
125: .booleanValue(r));
126: } else if (l instanceof Number || r instanceof Number) {
127: return (InfoSetUtil.doubleValue(l) == InfoSetUtil
128: .doubleValue(r));
129: } else if (l instanceof String || r instanceof String) {
130: return (InfoSetUtil.stringValue(l).equals(InfoSetUtil
131: .stringValue(r)));
132: } else if (l == null) {
133: return r == null;
134: }
135: return l.equals(r);
136: }
137:
138: }
|