View Javadoc
1   /*
2    * #%L
3    * Nuiton Decorator
4    * %%
5    * Copyright (C) 2011 CodeLutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
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   * {@link JXPathDecorator} implementation with multiple contexts.
31   *
32   * @param <O> type of data to decorate
33   * @author tchemit <chemit@codelutin.com>
34   * @see Decorator
35   * @since 2.3
36   */
37  public class MultiJXPathDecorator<O> extends JXPathDecorator<O> {
38  
39      private static final long serialVersionUID = 1L;
40  
41      /** Logger */
42      private static final Log log =
43              LogFactory.getLog(MultiJXPathDecorator.class);
44  
45      /** Contexts of the decorator */
46      protected Context<O>[] contexts;
47  
48      /** context separator */
49      protected String separator;
50  
51      /** context separator replacement */
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 }