OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 20 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 20 Rev 73
Line 26... Line 26...
26
import org.jdom.Element;
26
import org.jdom.Element;
27
import org.jdom.filter.Filter;
27
import org.jdom.filter.Filter;
28
import org.jdom.xpath.XPath;
28
import org.jdom.xpath.XPath;
29
 
29
 
30
/**
30
/**
31
 * Like an {@link XPath} with less features but a greater speed.
31
 * Like an {@link XPath} with less features but a greater speed. Thread-safe if its {@link Step
-
 
32
 * steps} are.
32
 * 
33
 * 
33
 * @author Sylvain CUAZ
34
 * @author Sylvain CUAZ
34
 * 
35
 * 
35
 * @param <T> type of result.
36
 * @param <T> type of result.
36
 */
37
 */
37
public final class SimpleXMLPath<T> {
38
public final class SimpleXMLPath<T> {
38
 
39
 
39
    public static <T> SimpleXMLPath<T> create(final List<Step<?>> steps, final Step<T> lastStep) {
40
    public static <T> SimpleXMLPath<T> create(final List<Step<?>> steps, final Step<T> lastStep) {
40
        final SimpleXMLPath<T> res = new SimpleXMLPath<T>(lastStep);
41
        return new SimpleXMLPath<T>(Collections.unmodifiableList(new ArrayList<Step<?>>(steps)), lastStep);
41
        for (final Step<?> s : steps)
-
 
42
            res.add(s);
-
 
43
        return res;
-
 
44
    }
42
    }
45
 
43
 
46
    public static <T> SimpleXMLPath<T> create(final Step<T> lastStep) {
44
    public static <T> SimpleXMLPath<T> create(final Step<T> lastStep) {
47
        return new SimpleXMLPath<T>(lastStep);
45
        return new SimpleXMLPath<T>(Collections.<Step<?>> emptyList(), lastStep);
48
    }
46
    }
49
 
47
 
50
    public static <T> SimpleXMLPath<T> create(final Step<?> first, final Step<T> lastStep) {
48
    public static <T> SimpleXMLPath<T> create(final Step<?> first, final Step<T> lastStep) {
51
        final SimpleXMLPath<T> res = new SimpleXMLPath<T>(lastStep);
49
        return new SimpleXMLPath<T>(Collections.<Step<?>> singletonList(first), lastStep);
52
        res.add(first);
-
 
53
        return res;
-
 
54
    }
50
    }
55
 
51
 
56
    public static <T> SimpleXMLPath<T> create(final Step<?> first, final Step<?> second, final Step<T> lastStep) {
52
    public static <T> SimpleXMLPath<T> create(final Step<?> first, final Step<?> second, final Step<T> lastStep) {
57
        return create(Arrays.<Step<?>> asList(first, second), lastStep);
53
        return new SimpleXMLPath<T>(Arrays.<Step<?>> asList(first, second), lastStep);
58
    }
54
    }
59
 
55
 
60
    /**
56
    /**
61
     * Create a path searching for all descendant attributes with the passed name and namespace.
57
     * Create a path searching for all descendant attributes with the passed name and namespace.
62
     * I.e. in XPath this would be ".//@ns:name".
58
     * I.e. in XPath this would be ".//@ns:name".
Line 82... Line 78...
82
    }
78
    }
83
 
79
 
84
    private final List<Step<?>> items;
80
    private final List<Step<?>> items;
85
    private final Step<T> lastItem;
81
    private final Step<T> lastItem;
86
 
82
 
-
 
83
    // private since we don't copy steps
87
    private SimpleXMLPath(Step<T> lastStep) {
84
    private SimpleXMLPath(final List<Step<?>> steps, Step<T> lastStep) {
88
        this.lastItem = lastStep;
85
        this.lastItem = lastStep;
89
        this.items = new ArrayList<Step<?>>();
-
 
90
    }
-
 
91
 
-
 
92
    public final SimpleXMLPath<T> add(final String name) {
-
 
93
        return this.add(Step.createElementStep(name, null, null));
-
 
94
    }
-
 
95
 
-
 
96
    public final SimpleXMLPath<T> add(Step<?> step) {
-
 
97
        this.items.add(step);
86
        this.items = steps;
98
        return this;
-
 
99
    }
87
    }
100
 
88
 
101
    // return Element or Attribute
89
    // return Element or Attribute
102
    public final T selectSingleNode(final Object n) {
90
    public final T selectSingleNode(final Object n) {
103
        return selectSingleNode(n, this.items);
91
        return selectSingleNode(n, this.items);
Line 122... Line 110...
122
            return CollectionUtils.getFirst(this.lastItem.nextNodes(Node.get(currentNode), currentNode));
110
            return CollectionUtils.getFirst(this.lastItem.nextNodes(Node.get(currentNode), currentNode));
123
        }
111
        }
124
    }
112
    }
125
 
113
 
126
    public final List<T> selectNodes(final Object n) {
114
    public final List<T> selectNodes(final Object n) {
127
        List<?> currentNodes = Collections.singletonList(n);
115
        return this.selectNodes(Collections.singletonList(n));
-
 
116
    }
-
 
117
 
-
 
118
    public final List<T> selectNodes(final List<?> nodes) {
-
 
119
        List<?> currentNodes = nodes;
128
        final int stop = this.items.size();
120
        final int stop = this.items.size();
129
        for (int i = 0; i < stop; i++) {
121
        for (int i = 0; i < stop; i++) {
130
            final Step<?> currentStep = this.items.get(i);
122
            final Step<?> currentStep = this.items.get(i);
131
 
123
 
132
            final List<?> nextNodes = currentStep.nextNodes(currentNodes);
124
            final List<?> nextNodes = currentStep.nextNodes(currentNodes);