| 1 | package com.hammurapi.extract.jxpath; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | import java.util.Collections; |
| 5 | import java.util.Map; |
| 6 | import java.util.Set; |
| 7 | import java.util.TreeSet; |
| 8 | |
| 9 | import org.apache.commons.jxpath.JXPathContext; |
| 10 | |
| 11 | import com.hammurapi.extract.Extractor; |
| 12 | import com.hammurapi.extract.ExtractorBase; |
| 13 | |
| 14 | class JXPathExtractor<T, V, C> extends ExtractorBase<T, V, C> implements Extractor<T, V, C> { |
| 15 | |
| 16 | private Set<Integer> parameterIndices; |
| 17 | private String code; |
| 18 | private String[] parameterNames; |
| 19 | private Class<V> valueType; |
| 20 | |
| 21 | public JXPathExtractor(String code, String[] parameterNames, Class<V> valueType) { |
| 22 | super(0, null); |
| 23 | this.code = code; |
| 24 | this.parameterNames = parameterNames; |
| 25 | this.valueType = valueType; |
| 26 | Set<Integer> pi = new TreeSet<Integer>(); |
| 27 | for (int i=0; i<parameterNames.length; ++i) { |
| 28 | if (parameterNames[i]!=null) { |
| 29 | pi.add(i); |
| 30 | } |
| 31 | } |
| 32 | parameterIndices = Collections.unmodifiableSet(pi); |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public Set<Integer> parameterIndices() { |
| 37 | return parameterIndices; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public boolean isContextDependent() { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | @SuppressWarnings("unchecked") |
| 46 | @Override |
| 47 | protected V extractInternal( |
| 48 | C context, |
| 49 | Map<C, Map<Extractor<T, ? super V, C>, ? super V>> cache, |
| 50 | T... obj) { |
| 51 | |
| 52 | JXPathContext ctx = JXPathContext.newContext(context==null ? obj : context); |
| 53 | for (int i=0; i<parameterNames.length; ++i) { |
| 54 | if (parameterNames[i]!=null) { |
| 55 | ctx.getVariables().declareVariable(parameterNames[i], obj[i]); |
| 56 | } |
| 57 | } |
| 58 | return (V) ctx.getValue(code, valueType); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public int hashCode() { |
| 63 | final int prime = 31; |
| 64 | int result = 1; |
| 65 | result = prime * result + ((code == null) ? 0 : code.hashCode()); |
| 66 | result = prime |
| 67 | * result |
| 68 | + ((parameterIndices == null) ? 0 : parameterIndices.hashCode()); |
| 69 | result = prime * result + Arrays.hashCode(parameterNames); |
| 70 | result = prime * result |
| 71 | + ((valueType == null) ? 0 : valueType.hashCode()); |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean equals(Object obj) { |
| 77 | if (this == obj) |
| 78 | return true; |
| 79 | if (!super.equals(obj)) |
| 80 | return false; |
| 81 | if (getClass() != obj.getClass()) |
| 82 | return false; |
| 83 | JXPathExtractor other = (JXPathExtractor) obj; |
| 84 | if (code == null) { |
| 85 | if (other.code != null) |
| 86 | return false; |
| 87 | } else if (!code.equals(other.code)) |
| 88 | return false; |
| 89 | if (parameterIndices == null) { |
| 90 | if (other.parameterIndices != null) |
| 91 | return false; |
| 92 | } else if (!parameterIndices.equals(other.parameterIndices)) |
| 93 | return false; |
| 94 | if (!Arrays.equals(parameterNames, other.parameterNames)) |
| 95 | return false; |
| 96 | if (valueType == null) { |
| 97 | if (other.valueType != null) |
| 98 | return false; |
| 99 | } else if (!valueType.equals(other.valueType)) |
| 100 | return false; |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | } |