PageRenderTime 43ms CodeModel.GetById 12ms app.highlight 24ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/src/test/java/org/sigmah/endtoend/xpath/XPath.java

http://sigma-h.googlecode.com/
Java | 133 lines | 92 code | 31 blank | 10 comment | 2 complexity | 5c99390c8e0418dd64c866577a30e0cb MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.endtoend.xpath;
  7
  8public class XPath {
  9
 10
 11    public static Step descendant(NodeTest nodeTest, Predicate... predicate) {
 12        return new Step(Axis.Descendant, predicate);
 13    }
 14
 15    public static Step descendant(NodeTest nodeTest) {
 16        return new Step(Axis.Descendant, nodeTest);
 17    }
 18
 19    public static Step descendant(Predicate... predicate) {
 20        return new Step(Axis.Descendant, predicate);
 21    }
 22
 23    public static Step following(NodeTest nodeTest) {
 24        return new Step(Axis.Following, nodeTest);
 25    }
 26
 27    public static Step following(Predicate... predicate) {
 28        return new Step(Axis.Following, predicate);
 29    }
 30
 31    public static Step child(Predicate... predicates) {
 32        return new Step(Axis.Child, predicates);
 33    }
 34
 35    public static String relative(Step... step) {
 36        return compile("", step);
 37    }
 38    
 39
 40    public static String absolute(Step... step) {
 41        return compile("/", step);
 42    }
 43
 44    private static String compile(String prefix, Step[] steps) {
 45        StringBuilder xpath = new StringBuilder(prefix);
 46        for(int depth=0; depth!=steps.length;++depth) {
 47            if(depth > 0) {
 48                xpath.append("/");
 49            }
 50            xpath.append(steps[depth].toString());
 51        }
 52        return xpath.toString();
 53    }
 54
 55    public static Predicate having(Step... step) {
 56        return new PredicateLiteral(compile("", step));
 57    }
 58
 59    public static Predicate havingTextEqualTo(String text) {
 60        return new PredicateLiteral(XPath.format("normalize-space(.) = '%s'", text));
 61    }
 62
 63    public static Predicate containingText(String text) {
 64        return new PredicateLiteral(XPath.format("contains(., '%s')", text));
 65    }
 66
 67    public static RelationalExprBuilder<Integer> position() {
 68        return new RelationalExprBuilder<Integer>("position()");
 69    }
 70
 71    public static Predicate position(int index) {
 72        return new PredicateLiteral("position() = " + index);
 73    }
 74
 75    public static Predicate not(final Predicate predicate) {
 76        return new Predicate() {
 77            @Override
 78            public String toString() {
 79                return String.format("not(%s)", predicate.toString());
 80            }
 81        };
 82    }
 83
 84    /**
 85     * Wrapper for a call to String.format with XPath. This is simply
 86     * used to mark certain calls as XPath for IDEs that support language injection
 87     * 
 88     * @see String#format(String, Object...)
 89     */
 90    public static String format(String xpath, Object... args) {
 91        return String.format(xpath, args);
 92    }
 93
 94    public static class ElementNodeTest implements NodeTest {
 95        private String name;
 96        public ElementNodeTest(String name) {
 97            this.name = name;
 98        }
 99
100        @Override
101        public String toString() {
102            return this.name;
103        }
104    }
105
106    public static class Attribute implements NodeTest, Predicate {
107        private String name;
108
109        public Attribute(String name) {
110            this.name = name;
111        }
112
113        @Override
114        public String toString() {
115            return "@" + name;
116        }
117
118        public Predicate equalTo(String text) {
119            return new PredicateLiteral(String.format("@%s = '%s'", name, text));
120        }
121
122    }
123
124    public static NodeTest element(String elementName) {
125        return new ElementNodeTest(elementName);
126    }
127
128    public static Attribute attribute(String attributeName) {
129        return new Attribute(attributeName);
130    }
131
132
133}