/* * Copyright (C) 2009 JavaRosa * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package org.openrosa.client.jr.xpath.expr; import java.io.IOException; import java.util.Vector; import org.openrosa.client.java.io.DataInputStream; import org.openrosa.client.java.io.DataOutputStream; import org.openrosa.client.jr.core.model.condition.EvaluationContext; import org.openrosa.client.jr.core.model.instance.FormInstance; import org.openrosa.client.jr.core.util.externalizable.DeserializationException; import org.openrosa.client.jr.core.util.externalizable.ExtUtil; import org.openrosa.client.jr.core.util.externalizable.ExtWrapListPoly; import org.openrosa.client.jr.core.util.externalizable.ExtWrapTagged; import org.openrosa.client.jr.core.util.externalizable.PrototypeFactory; import org.openrosa.client.jr.xpath.XPathUnsupportedException; public class XPathFilterExpr extends XPathExpression { public XPathExpression x; public XPathExpression[] predicates; public XPathFilterExpr () { } //for deserialization public XPathFilterExpr (XPathExpression x, XPathExpression[] predicates) { this.x = x; this.predicates = predicates; } public Object eval(FormInstance model, EvaluationContext evalContext) { throw new XPathUnsupportedException("filter expression"); } public String toString () { StringBuffer sb = new StringBuffer(); sb.append("{filt-expr:"); sb.append(x.toString()); sb.append(",{"); for (int i = 0; i < predicates.length; i++) { sb.append(predicates[i].toString()); if (i < predicates.length - 1) sb.append(","); } sb.append("}}"); return sb.toString(); } public boolean equals (Object o) { if (o instanceof XPathFilterExpr) { XPathFilterExpr fe = (XPathFilterExpr)o; Vector a = new Vector(); for (int i = 0; i < predicates.length; i++) a.addElement(predicates[i]); Vector b = new Vector(); for (int i = 0; i < fe.predicates.length; i++) b.addElement(fe.predicates[i]); return x.equals(fe.x) && ExtUtil.vectorEquals(a, b); } else { return false; } } public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException { x = (XPathExpression)ExtUtil.read(in, new ExtWrapTagged(), pf); Vector v = (Vector)ExtUtil.read(in, new ExtWrapListPoly(), pf); predicates = new XPathExpression[v.size()]; for (int i = 0; i < predicates.length; i++) predicates[i] = (XPathExpression)v.elementAt(i); } public void writeExternal(DataOutputStream out) throws IOException { Vector v = new Vector(); for (int i = 0; i < predicates.length; i++) v.addElement(predicates[i]); ExtUtil.write(out, new ExtWrapTagged(x)); ExtUtil.write(out, new ExtWrapListPoly(v)); } }