1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.nuiton.decorator;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.util.Comparator;
28
29
30
31
32
33
34
35
36
37 public class MultiJXPathDecorator<O> extends JXPathDecorator<O> {
38
39 private static final long serialVersionUID = 1L;
40
41
42 private static final Log log =
43 LogFactory.getLog(MultiJXPathDecorator.class);
44
45
46 protected Context<O>[] contexts;
47
48
49 protected String separator;
50
51
52 protected String separatorReplacement;
53
54 protected MultiJXPathDecorator(
55 Class<O> internalClass,
56 String expression,
57 String separator,
58 String separatorReplacement,
59 Context<O>[] contexts) throws IllegalArgumentException,
60 NullPointerException {
61 super(internalClass, expression, null);
62 this.separator = separator;
63 this.separatorReplacement = separatorReplacement;
64 this.contexts = contexts;
65
66 setContextIndex(0);
67
68 if (log.isDebugEnabled()) {
69 log.debug(expression + " --> " + context);
70 }
71 }
72
73 protected MultiJXPathDecorator(
74 Class<O> internalClass,
75 String expression,
76 String separator,
77 String separatorReplacement) throws IllegalArgumentException,
78 NullPointerException {
79 this(internalClass,
80 expression,
81 separator,
82 separatorReplacement,
83 DecoratorUtil.<O>createMultiJXPathContext(expression,
84 separator,
85 separatorReplacement)
86 );
87 }
88
89 public void setContextIndex(int index) {
90 ensureContextIndex(this, index);
91 setContext(contexts[index]);
92 }
93
94 public int getNbContext() {
95 return contexts.length;
96 }
97
98 public String getSeparator() {
99 return separator;
100 }
101
102 public String getSeparatorReplacement() {
103 return separatorReplacement;
104 }
105
106 @Override
107 protected Comparator<O> getComparator(int pos) {
108 ensureContextIndex(this, pos);
109 Context<O> context1 = contexts[pos];
110 return context1.getComparator(0);
111 }
112
113 protected void ensureContextIndex(MultiJXPathDecorator<?> decorator,
114 int pos) {
115 if (pos < -1 || pos > decorator.contexts.length) {
116 throw new ArrayIndexOutOfBoundsException(
117 "context index " + pos +
118 " is out of bound, can be inside [" + 0 + "," +
119 decorator.contexts.length + "]");
120 }
121 }
122 }