Commit 7ccd8815 authored by Administrator's avatar Administrator

udpate

parent 1cd932b4
......@@ -5,9 +5,17 @@ import android.view.ViewGroup;
import com.virjar.superappium.lazy.ValueGetter;
import com.virjar.superappium.lazy.ValueGetters;
import com.virjar.superappium.traversor.Collector;
import com.virjar.superappium.traversor.Evaluator;
import com.virjar.superappium.util.Constants;
import com.virjar.superappium.util.Lists;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class ViewImage {
......@@ -15,14 +23,23 @@ public class ViewImage {
private Map<String, ValueGetter> attributes;
private ViewImage parent = null;
private ValueGetter<String> type;
private ValueGetter<String> text;
private int indexOfParent = -1;
public ViewImage(View originView) {
this.originView = originView;
attributes = ValueGetters.valueGetters(this);
type = attrName(Constants.className);
text = attrName(Constants.text);
}
public String getType() {
return type.get(this);
}
public String getText() {
return text.get(this);
}
@SuppressWarnings("unchecked")
private <T> ValueGetter<T> attrName(String attrName) {
......@@ -86,4 +103,84 @@ public class ViewImage {
}
public List<ViewImage> parents() {
List<ViewImage> ret = Lists.newArrayList();
ViewImage parent = this.parent;
while (parent != null) {
ret.add(parent);
parent = parent.parent;
}
return ret;
}
public List<ViewImage> children() {
if (childCount() < 0) {
return Lists.newArrayList();
}
List<ViewImage> ret = new ArrayList<>(childCount());
for (int i = 0; i < theChildCount; i++) {
ret.add(childAt(i));
}
return ret;
}
public ViewImages getAllElements() {
return Collector.collect(new Evaluator.AllElements(), this);
}
public ViewImage parentNode() {
return parent;
}
public ViewImage nextSibling() {
if (parent == null) {
//root
return null;
}
int nextSiblingIndex = indexOfParent + 1;
if (parent.childCount() > nextSiblingIndex) {
return parent.childAt(nextSiblingIndex);
}
return null;
}
public ViewImage previousSibling() {
if (parent == null) {
//root
return null;
}
int nextSiblingIndex = indexOfParent - 1;
if (nextSiblingIndex < 0) {
return null;
}
return parent.childAt(nextSiblingIndex);
}
public ViewImages siblings() {
if (parent == null) {
return new ViewImages();
}
int parentChildren = parent.childCount();
ViewImages viewImages = new ViewImages(parentChildren - 1);
for (int i = 0; i < parentChildren; i++) {
ViewImage viewImage = parent.childAt(i);
if (viewImage == this) {
continue;
}
viewImages.add(viewImage);
}
return viewImages;
}
public String attributes() {
JSONObject jsonObject = new JSONObject();
for(String key:attributeKeys()){
try {
jsonObject.put(key,attribute(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject.toString();
}
}
package com.virjar.superappium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ViewImages extends ArrayList<ViewImage> {
public ViewImages(ViewImage... parentNodes) {
addAll(Arrays.asList(parentNodes));
}
public ViewImages(List<ViewImage> tempList) {
super(tempList);
}
public ViewImages(int initialCapacity) {
super(initialCapacity);
}
}
package com.virjar.superappium.traversor;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import com.virjar.superappium.util.Lists;
import java.util.List;
public class Collector {
private Collector() {
}
/**
* Build a list of elements, by visiting root and every descendant of root, and testing it against the evaluator.
*
* @param eval Evaluator to test elements against
* @param root root of tree to descend
* @return list of matches; empty if none
*/
public static ViewImages collect(Evaluator eval, ViewImage root) {
ViewImages elements = new ViewImages();
new NodeTraversor(new Accumulator(root, elements, eval)).traverse(root);
return elements;
}
private static class Accumulator implements NodeVisitor {
private final ViewImage root;
private final List<ViewImage> elements;
private final Evaluator eval;
Accumulator(ViewImage root, List<ViewImage> elements, Evaluator eval) {
this.root = root;
this.elements = elements;
this.eval = eval;
}
public void head(ViewImage node, int depth) {
if (eval.matches(root, node))
elements.add(node);
}
public void tail(ViewImage node, int depth) {
// void
}
}
}
package com.virjar.superappium.traversor;
import com.virjar.superappium.ViewImage;
public abstract class Evaluator {
protected Evaluator() {
}
/**
* Test if the element meets the evaluator's requirements.
*
* @param root Root of the matching subtree
* @param element tested element
* @return Returns <tt>true</tt> if the requirements are met or
* <tt>false</tt> otherwise
*/
public abstract boolean matches(ViewImage root, ViewImage element);
public static class AllElements extends Evaluator {
@Override
public boolean matches(ViewImage root, ViewImage element) {
return true;
}
}
public static class ByTag extends Evaluator {
private String tag;
public ByTag(String tag) {
this.tag = tag;
}
@Override
public boolean matches(ViewImage root, ViewImage element) {
return element.getType().equals(tag);
}
}
}
package com.virjar.superappium.traversor;
import com.virjar.superappium.ViewImage;
public class NodeTraversor {
private NodeVisitor visitor;
/**
* Create a new traversor.
* @param visitor a class implementing the {@link NodeVisitor} interface, to be called when visiting each node.
*/
public NodeTraversor(NodeVisitor visitor) {
this.visitor = visitor;
}
/**
* Start a depth-first traverse of the root and all of its descendants.
* @param root the root node point to traverse.
*/
public void traverse(ViewImage root) {
ViewImage node = root;
int depth = 0;
while (node != null) {
visitor.head(node, depth);
if (node.childCount() > 0) {
node = node.childAt(0);
depth++;
} else {
while (node.nextSibling() == null && depth > 0) {
visitor.tail(node, depth);
node = node.parentNode();
depth--;
}
visitor.tail(node, depth);
if (node == root)
break;
node = node.nextSibling();
}
}
}
}
\ No newline at end of file
package com.virjar.superappium.traversor;
import com.virjar.superappium.ViewImage;
public interface NodeVisitor {
/**
* Callback for when a node is first visited.
*
* @param node the node being visited.
* @param depth the depth of the node, relative to the root node. E.g., the root node has depth 0, and a child node
* of that will have depth 1.
*/
void head(ViewImage node, int depth);
/**
* Callback for when a node is last visited, after all of its descendants have been visited.
*
* @param node the node being visited.
* @param depth the depth of the node, relative to the root node. E.g., the root node has depth 0, and a child node
* of that will have depth 1.
*/
void tail(ViewImage node, int depth);
}
package com.virjar.superappium;
package com.virjar.superappium.traversor;
import android.util.Log;
import android.util.Xml;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.util.Constants;
import org.json.JSONArray;
......
......@@ -132,4 +132,8 @@ public class StringUtils {
}
return true;
}
public static boolean isNotBlank(String cs) {
return !isBlank(cs);
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import java.util.List;
public class AncestorFunction implements AxisFunction {
@Override
public List<ViewImage> call(ViewImage e, List<String> args) {
return e.parents();
}
@Override
public String getName() {
return "ancestor";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import java.util.List;
public class AncestorOrSelfFunction implements AxisFunction {
@Override
public List<ViewImage> call(ViewImage e, List<String> args) {
List<ViewImage> rs = e.parents();
rs.add(e);
return rs;
}
@Override
public String getName() {
return "ancestorOrSelf";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import java.util.List;
public class ChildFunction implements AxisFunction {
@Override
public List<ViewImage> call(ViewImage e, List<String> args) {
return e.children();
}
@Override
public String getName() {
return "child";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import java.util.List;
/**
* 全部子代节点 儿子,孙子,孙子的儿子...
*/
public class DescendantFunction implements AxisFunction {
@Override
public List<ViewImage> call(ViewImage e, List<String> args) {
return e.getAllElements();
}
@Override
public String getName() {
return "descendant";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import java.util.List;
public class DescendantOrSelfFunction implements AxisFunction {
@Override
public List<ViewImage> call(ViewImage e, List<String> args) {
List<ViewImage> rs = e.getAllElements();
rs.add(e);
return rs;
}
@Override
public String getName() {
return "descendantOrSelf";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import java.util.List;
public class FollowingSiblingFunction implements AxisFunction {
@Override
public List<ViewImage> call(ViewImage e, List<String> args) {
ViewImages rs = new ViewImages();
ViewImage tmp = e.nextSibling();
while (tmp != null) {
rs.add(tmp);
tmp = tmp.nextSibling();
}
return rs;
}
@Override
public String getName() {
return "following-sibling";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import java.util.List;
public class FollowingSiblingOneFunction implements AxisFunction {
@Override
public ViewImages call(ViewImage e, List<String> args) {
ViewImages rs = new ViewImages();
if (e.nextSibling() != null) {
rs.add(e.nextSibling());
}
return rs;
}
@Override
public String getName() {
return "following-sibling-one";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import java.util.List;
public class ParentFunction implements AxisFunction {
@Override
public ViewImages call(ViewImage e, List<String> args) {
return new ViewImages(e.parentNode());
}
@Override
public String getName() {
return "parent";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import com.virjar.superappium.util.Lists;
import java.util.LinkedList;
import java.util.List;
public class PrecedingSiblingFunction implements AxisFunction {
@Override
public ViewImages call(ViewImage e, List<String> args) {
ViewImage tmp = e.previousSibling();
LinkedList<ViewImage> tempList = Lists.newLinkedList();
while (tmp != null) {
tempList.addFirst(tmp);
tmp = tmp.previousSibling();
}
return new ViewImages(tempList);
}
@Override
public String getName() {
return "preceding-sibling";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import java.util.List;
public class SelfFunction implements AxisFunction {
@Override
public ViewImages call(ViewImage e, List<String> args) {
return new ViewImages(e);
}
@Override
public String getName() {
return "self";
}
}
package com.virjar.superappium.xpath.function.axis;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import java.util.List;
public class SiblingFunction implements AxisFunction {
@Override
public ViewImages call(ViewImage e, List<String> args) {
return e.siblings();
}
@Override
public String getName() {
return "sibling";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.exception.EvaluateException;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.math.BigDecimal;
import java.util.List;
public class AbsFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
//Preconditions.checkArgument(params.size() > 0, getName() + " must have one parameter");
Object calc = params.get(0).calc(element);
if (!(calc instanceof Number)) {
throw new EvaluateException(getName() + " must have one number parameter, now it is: " + calc);
}
Number number = (Number) calc;
if (calc instanceof Integer) {
return Math.abs(number.intValue());
}
if (calc instanceof Double) {
return Math.abs(number.doubleValue());
}
if (calc instanceof Long) {
return Math.abs(number.longValue());
}
if (calc instanceof Float) {
return Math.abs(number.floatValue());
}
if (calc instanceof BigDecimal) {
return ((BigDecimal) calc).abs();
}
// default transform to a double
return Math.abs(number.doubleValue());
}
@Override
public String getName() {
return "abs";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.exception.EvaluateException;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public abstract class AbstractStringFunction implements FilterFunction {
protected String firstParamToString(ViewImage element, List<SyntaxNode> params) {
//Preconditions.checkArgument(params.size() > 0, getName() + " must have parameter at lest 1");
Object string = params.get(0).calc(element);
if (!(string instanceof String)) {
throw new EvaluateException(getName() + " first parameter is not a string :" + string);
}
return string.toString();
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.util.BooleanUtils;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class BooleanFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
if (params.size() == 0) {
return false;
}
Object calc = params.get(0).calc(element);
if (calc == null) {
return false;
}
if (calc instanceof Boolean) {
return calc;
}
if (calc instanceof String) {
return BooleanUtils.toBoolean(calc.toString());
}
if (calc instanceof Integer) {
return ((Integer) calc) != 0;
}
if (calc instanceof Number) {
return ((Number) calc).doubleValue() > 0D;
}
return false;
}
@Override
public String getName() {
return "boolean";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.Iterator;
import java.util.List;
public class ConcatFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
if (params.size() == 0) {
return "";
}
StringBuilder stringBuilder = new StringBuilder();
Iterator<SyntaxNode> iterator = params.iterator();
stringBuilder.append(iterator.next().calc(element));
while (iterator.hasNext()) {
stringBuilder.append(" ").append(iterator.next().calc(element));
}
return stringBuilder.toString();
}
@Override
public String getName() {
return "concat";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class ContainsFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
// Preconditions.checkArgument(params.size() >= 2, "contains need 2 params");
return params.get(0).calc(element).toString().contains(params.get(1).calc(element).toString());
}
@Override
public String getName() {
return "contains";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class FalseFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
return false;
}
@Override
public String getName() {
return "false";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class FirstFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
return element.index() == 0;
}
@Override
public String getName() {
return "first";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class LastFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
// return XpathUtil.getElIndexInSameTags(element) == XpathUtil.sameTagElNums(element);
return element.index() == element.parentNode().childCount() - 1;
}
@Override
public String getName() {
return "last";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class LowerCaseFunction extends AbstractStringFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
return firstParamToString(element, params).toLowerCase();
}
@Override
public String getName() {
return "lower-case";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.exception.EvaluateException;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class MatchesFunction extends AbstractStringFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
//Preconditions.checkArgument(params.size() >= 2, getName() + " must has 2 parameters");
Object calc = params.get(1).calc(element);
if (!(calc instanceof String)) {
throw new EvaluateException(getName() + " second parameter is not a string:" + calc);
}
return firstParamToString(element, params).matches(calc.toString());
}
@Override
public String getName() {
return "matches";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class NameFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
return element.getType();
}
@Override
public String getName() {
return "name";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class NotFunction extends BooleanFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
return !((Boolean) super.call(element, params));
}
@Override
public String getName() {
return "not";
}
}
\ No newline at end of file
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class NullToDefaultFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
// Preconditions.checkArgument(params.size() >= 2, getName() + " must have 2 parameter");
Object calc = params.get(0).calc(element);
if (calc != null) {
return calc;
}
return params.get(1).calc(element);
}
@Override
public String getName() {
return "nullToDefault";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import com.virjar.superappium.xpath.util.XpathUtil;
import java.util.List;
public class ParentFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
int index = 1;
Integer integer = XpathUtil.firstParamToInt(params, element, getName());
if (integer != null) {
index = integer;
}
for (int i = 0; i < index; i++) {
element = element.parentNode();
}
return element;
}
@Override
public String getName() {
return "parent";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class PositionFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
if (params.size() > 0) {
Object calc = params.get(0).calc(element);
if (calc instanceof ViewImage) {
return ((ViewImage) calc).index();
} else {
throw new IllegalStateException("result of function :<position> not a view element");
}
}
return element.index();
}
@Override
public String getName() {
return "position";
}
}
package com.virjar.superappium.xpath.function.filter;
import android.util.Log;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.util.BooleanUtils;
import com.virjar.superappium.util.Constants;
import com.virjar.superappium.util.StringUtils;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import com.virjar.superappium.xpath.util.XpathUtil;
import java.util.List;
public class PredicateFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
if (element == null) {
return false;
}
Object ret = params.get(0).calc(element);
if (ret == null) {
return false;
}
if (ret instanceof Number) {
int i = ((Number) ret).intValue();
return element.index() == i;
}
if (ret instanceof Boolean) {
return ret;
}
if (ret instanceof CharSequence) {
String s = ret.toString();
Boolean booleanValue = BooleanUtils.toBooleanObject(s);
if (booleanValue != null) {
return booleanValue;
}
return StringUtils.isNotBlank(s);
}
//log.warn("can not recognize predicate expression calc result:" + ret);
Log.w(Constants.TAG, "can not recognize predicate expression calc result:" + ret);
return false;
}
@Override
public String getName() {
return "inner_predicate";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import com.virjar.superappium.xpath.util.XpathUtil;
import java.util.List;
public class RootFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
if (params.size() > 0) {
Object calc = params.get(0).calc(element);
if (calc instanceof ViewImage) {
return XpathUtil.root((ViewImage) calc);
} else {
throw new IllegalStateException("result of function :<" + getName() + "> not a view element");
}
}
return XpathUtil.root(element);
}
@Override
public String getName() {
return "root";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class StringFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
//Preconditions.checkArgument(params.size() > 0, getName() + " must has one parameter");
Object calc = params.get(0).calc(element);
if (calc == null) {
return "null";
}
return calc.toString();
}
@Override
public String getName() {
return "string";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class StringLengthFunction extends AbstractStringFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
return firstParamToString(element, params).length();
}
@Override
public String getName() {
return "string-length";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.exception.EvaluateException;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class SubstringFunction extends AbstractStringFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
// Preconditions.checkArgument(params.size() >= 2, getName() + " must have parameter at lest 2");
String string = firstParamToString(element, params);
Object index = params.get(1).calc(element);
if (!(index instanceof Integer)) {
throw new EvaluateException(getName() + " index must be a integer :" + index);
}
int end = -1;
if (params.size() > 2) {
Object endObj = params.get(2).calc(element);
if (!(endObj instanceof Integer)) {
throw new EvaluateException(getName() + "end index must be a integer :" + index);
}
end = (int) endObj;
}
return end < 0 ? string.substring((int) index) : string.substring((int) index, end);
}
@Override
public String getName() {
return "substring";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class TextFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
//return element.ownText();
return element.getText();
}
@Override
public String getName() {
return "text";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.util.NumberUtils;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class ToDoubleFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
// Preconditions.checkArgument(params.size() > 0, getName() + " at last has one parameter");
Object calc = params.get(0).calc(element);
if (calc instanceof Double) {
return calc;
}
if (calc == null) {
return null;
}
if (params.size() > 1) {
Object defaultValue = params.get(1).calc(element);
// Preconditions.checkArgument(defaultValue != null && defaultValue instanceof Double,
// getName() + " parameter 2 must to be a Double now is:" + defaultValue);
return NumberUtils.toDouble(calc.toString(), (Double) defaultValue);
}
return NumberUtils.toDouble(calc.toString());
}
@Override
public String getName() {
return "toDouble";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.util.NumberUtils;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class ToIntFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
// Preconditions.checkArgument(params.size() > 0, getName() + " at last has one parameter");
Object calc = params.get(0).calc(element);
if (calc instanceof Integer) {
return calc;
}
if (calc == null) {
return null;
}
if (params.size() > 1) {
Object defaultValue = params.get(1).calc(element);
// Preconditions.checkArgument(defaultValue != null && defaultValue instanceof Integer,
// getName() + " parameter 2 must to be a integer now is:" + defaultValue);
return NumberUtils.toInt(calc.toString(), (Integer) defaultValue);
}
return NumberUtils.toInt(calc.toString());
}
@Override
public String getName() {
return "toInt";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class TrueFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
return true;
}
@Override
public String getName() {
return "true";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class TryExeptionFunction implements FilterFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
if (params.size() == 0) {
return true;
}
try {
return params.get(0).calc(element);
} catch (Exception e) {
// log.debug("exception occur", e);
if (params.size() > 1) {
return params.get(1).calc(element);
} else {
return false;
}
}
}
@Override
public String getName() {
return "try";
}
}
package com.virjar.superappium.xpath.function.filter;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.util.List;
public class UpperCaseFunction extends AbstractStringFunction {
@Override
public Object call(ViewImage element, List<SyntaxNode> params) {
return firstParamToString(element, params).toUpperCase();
}
@Override
public String getName() {
return "upper-case";
}
}
package com.virjar.superappium.xpath.function.select;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import com.virjar.superappium.util.Lists;
import com.virjar.superappium.xpath.model.XNode;
import com.virjar.superappium.xpath.model.XNodes;
import com.virjar.superappium.xpath.model.XpathNode;
import java.util.List;
public abstract class AttrBaseFunction implements SelectFunction {
@Override
public XNodes call(XpathNode.ScopeEm scopeEm, ViewImages elements, List<String> args) {
XNodes ret = new XNodes();
boolean allAttr = "*".equals(args.get(0));
String attrName = args.get(0);
for (ViewImage element : elements) {
handle(allAttr, attrName, element, ret);
if (scopeEm == XpathNode.ScopeEm.RECURSIVE || scopeEm == XpathNode.ScopeEm.CURREC) {
ViewImages allElements = element.getAllElements();
for (ViewImage subElement : allElements) {
handle(allAttr, attrName, subElement, ret);
}
}
}
return ret;
}
abstract void handle(boolean allAttr, String attrKey, ViewImage element, List<XNode> ret);
}
package com.virjar.superappium.xpath.function.select;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.util.StringUtils;
import com.virjar.superappium.xpath.model.XNode;
import java.util.List;
public class AttrFunction extends AttrBaseFunction {
@Override
public void handle(boolean allAttr, String attrKey, ViewImage element, List<XNode> ret) {
if (allAttr) {
ret.add(XNode.t(element.attributes()));
} else {
Object value = element.attribute(attrKey);
if (value == null) {
return;
}
String str = value.toString();
if (StringUtils.isNotBlank(str)) {
ret.add(XNode.t(str));
}
}
}
@Override
public String getName() {
return "@";
}
}
package com.virjar.superappium.xpath.function.select;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import com.virjar.superappium.xpath.function.NameAware;
import com.virjar.superappium.xpath.model.XNode;
import com.virjar.superappium.xpath.model.XNodes;
import com.virjar.superappium.xpath.model.XpathNode;
import java.util.List;
public interface SelectFunction extends NameAware {
List<XNode> call(XpathNode.ScopeEm scopeEm, List<ViewImage> elements, List<String> args);
XNodes call(XpathNode.ScopeEm scopeEm, ViewImages elements, List<String> args);
}
package com.virjar.superappium.xpath.function.select;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import com.virjar.superappium.xpath.model.XNode;
import com.virjar.superappium.xpath.model.XNodes;
import com.virjar.superappium.xpath.model.XpathNode;
import java.util.List;
public class SelfFunction implements SelectFunction {
@Override
public XNodes call(XpathNode.ScopeEm scopeEm, ViewImages elements, List<String> args) {
XNodes xNodes = new XNodes();
for (ViewImage viewImage : elements) {
xNodes.add(XNode.e(viewImage));
}
return xNodes;
}
@Override
public String getName() {
return "self";
}
}
package com.virjar.superappium.xpath.function.select;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import com.virjar.superappium.traversor.Collector;
import com.virjar.superappium.traversor.Evaluator;
import com.virjar.superappium.util.Lists;
import com.virjar.superappium.xpath.model.XNodes;
import com.virjar.superappium.xpath.model.XpathNode;
import com.virjar.superappium.xpath.util.XpathUtil;
import java.util.List;
/**
* Created by virjar on 17/6/11.
*/
public class TagSelectFunction implements SelectFunction {
@Override
public XNodes call(XpathNode.ScopeEm scopeEm, ViewImages elements, List<String> args) {
String tagName = args.get(0);
List<ViewImage> temp = Lists.newLinkedList();
if (scopeEm == XpathNode.ScopeEm.RECURSIVE || scopeEm == XpathNode.ScopeEm.CURREC) {// 递归模式
Evaluator evaluator;
if ("*".equals(tagName)) {
evaluator = new Evaluator.AllElements();
} else {
evaluator = new Evaluator.ByTag(tagName);
}
for (ViewImage element : elements) {
temp.addAll(Collector.collect(evaluator, element));
}
if (scopeEm == XpathNode.ScopeEm.RECURSIVE) {
//向下递归,不应该包含自身
temp.removeAll(elements);
}
return XpathUtil.transform(temp);
}
// 直接子代查找
if ("*".equals(tagName)) {
for (ViewImage element : elements) {
temp.addAll(element.children());
}
} else {
for (ViewImage element : elements) {
for (ViewImage child : element.children()) {
if (child.getType().equals(tagName)) {
temp.add(child);
}
}
}
}
return XpathUtil.transform(temp);
}
@Override
public String getName() {
return "tag";
}
}
package com.virjar.superappium.xpath.function.select;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import com.virjar.superappium.xpath.model.XNode;
import com.virjar.superappium.xpath.model.XNodes;
import com.virjar.superappium.xpath.model.XpathNode;
import java.util.List;
public class TextFunction implements SelectFunction {
/**
* 只获取节点自身的子文本
*/
@Override
public XNodes call(XpathNode.ScopeEm scopeEm, ViewImages elements, List<String> args) {
XNodes res = new XNodes();
if (elements != null && elements.size() > 0) {
for (ViewImage e : elements) {
res.add(XNode.t(e.getText()));
}
}
return res;
}
@Override
public String getName() {
return "text";
}
}
......@@ -12,4 +12,7 @@ public class XNodes extends LinkedList<XNode> {
public XNodes(XNode e) {
add(e);
}
public XNodes() {
}
}
package com.virjar.superappium.xpath.util;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.ViewImages;
import com.virjar.superappium.xpath.exception.EvaluateException;
import com.virjar.superappium.xpath.model.XNode;
import com.virjar.superappium.xpath.model.XNodes;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
......@@ -56,4 +59,20 @@ public class XpathUtil {
}
return null;
}
public static ViewImage root(ViewImage element) {
while (element.parentNode() != null) {
element = element.parentNode();
}
return element;
}
public static XNodes transform(Collection<ViewImage> viewImages) {
XNodes xNodes = new XNodes();
for (ViewImage viewImage : viewImages) {
xNodes.add(XNode.e(viewImage));
}
return xNodes;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment