package us.codecraft.xsoup;

import org.jsoup.nodes.Element;

/**
 * @author code4crafter@gmail.com
 */
public abstract class ElementOperator {

    public ElementOperator(String expr) {
        this.expr = expr;
    }

    protected String expr;

    public abstract String operate(Element element);

    @Override
    public String toString() {
        return expr;
    }

    public static class AttributeGetter extends ElementOperator {

        private String attribute;

        public AttributeGetter(String expr) {
            super(expr);
            if (expr.startsWith("@")) {
                attribute = expr.substring(1);
            }
        }

        @Override
        public String operate(Element element) {
            return element.attr(attribute);
        }
    }

    public static class Function extends ElementOperator {

        public Function(String expr) {
            super(expr);
        }

        @Override
        public String operate(Element element) {
            if (expr.equals("text()")) {
                return element.text();
            } else {
                throw new IllegalArgumentException("Unsupported function " + expr);
            }
        }
    }
}

@@ -55,16 +55,49 @@
 ement;%0A%0A
+import java.util.regex.Pattern;%0A%0A
 /**%0A * @
@@ -1096,16 +1096,103 @@
 text();%0A
+            %7D else if (expr.equals(%22html()%22)) %7B%0A                return element.html();%0A
         
@@ -1317,11 +1317,399 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * usage:%0A     * %3Cp/%3E%0A     * regex('.*')%0A     * regex(@attr,'.*')%0A     * regex(@attr,'.*',group)%0A     */%0A    public static class Regex extends Function %7B%0A%0A        private Pattern pattern;%0A%0A        private String attribute;%0A%0A        public Regex(String expr) %7B%0A            super(expr);%0A        %7D%0A%0A        protected void parse(String expr) %7B%0A            //todo%0A        %7D%0A    %7D
 %0A%7D%0A

add html