1 | package eu.clarin.sru.client.fcs; |
---|
2 | |
---|
3 | /** |
---|
4 | * A Data View implementation that stores the content of a HITS Data View. |
---|
5 | */ |
---|
6 | public class DataViewHits extends DataView { |
---|
7 | /** |
---|
8 | * The MIME type for CLARIN-FCS KWIC data views. |
---|
9 | */ |
---|
10 | public static final String TYPE = "application/x-clarin-fcs-hits+xml"; |
---|
11 | private final String text; |
---|
12 | private final int[] offsets; |
---|
13 | private final int max_offset; |
---|
14 | |
---|
15 | |
---|
16 | /** |
---|
17 | * Constructor. |
---|
18 | * |
---|
19 | * @param pid |
---|
20 | * a persistent identifier or <code>null</code> |
---|
21 | * @param ref |
---|
22 | * a reference URI or <code>null</code> |
---|
23 | * @param text |
---|
24 | * the textual content of the hits |
---|
25 | * @param offsets |
---|
26 | * an array of (start, end) offset pairs that indicate the part |
---|
27 | * of the text, that is considered a hit |
---|
28 | * @param offsets_idx |
---|
29 | * the largest index (= hit_count * 2) within the offsets array |
---|
30 | * plus one |
---|
31 | * @throws NullPointerException |
---|
32 | * if any mandatory argument is <code>null</code> |
---|
33 | * @throws IllegalArgumentException |
---|
34 | * if any argument is illegal |
---|
35 | */ |
---|
36 | protected DataViewHits(String pid, String ref, String text, |
---|
37 | int[] offsets, int offsets_idx) { |
---|
38 | super(TYPE, pid, ref); |
---|
39 | if (text == null) { |
---|
40 | throw new NullPointerException("text == null"); |
---|
41 | } |
---|
42 | if (text.isEmpty()) { |
---|
43 | throw new IllegalArgumentException("text is empty"); |
---|
44 | } |
---|
45 | this.text = text; |
---|
46 | if (offsets == null) { |
---|
47 | throw new NullPointerException("offsets == null"); |
---|
48 | } |
---|
49 | this.offsets = offsets; |
---|
50 | if (offsets_idx < 0) { |
---|
51 | throw new IllegalArgumentException("offset_idx < 0"); |
---|
52 | } |
---|
53 | if (offsets_idx > offsets.length) { |
---|
54 | throw new IllegalArgumentException("offset_idx > offsets.length"); |
---|
55 | } |
---|
56 | this.max_offset = (offsets_idx / 2); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * Get the total number of hits in the result. |
---|
62 | * @return the number of hits |
---|
63 | */ |
---|
64 | public int getHitCount() { |
---|
65 | return max_offset; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | * Get the text content of the hit. Usually this is complete sentence. |
---|
71 | * |
---|
72 | * @return the text content of the hit |
---|
73 | */ |
---|
74 | public String getText() { |
---|
75 | return text; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | /** |
---|
80 | * Get the offsets pointing to range in the text content that yield the hit. |
---|
81 | * |
---|
82 | * @param idx |
---|
83 | * the hit to retrieve. Must be larger than <code>0</code> and |
---|
84 | * smaller than the result of {@link #getHitCount()}. |
---|
85 | * @return An array of two elements. The first array element is the start |
---|
86 | * offset, the second array element is the end offset of the hit |
---|
87 | * range. |
---|
88 | * @throws ArrayIndexOutOfBoundsException |
---|
89 | * of the <code>idx</code> argument is out of bounds. |
---|
90 | */ |
---|
91 | public int[] getHitOffsets(int idx) { |
---|
92 | if (idx < 0) { |
---|
93 | throw new ArrayIndexOutOfBoundsException("idx < 0"); |
---|
94 | } |
---|
95 | if (idx < max_offset) { |
---|
96 | int[] result = new int[2]; |
---|
97 | result[0] = offsets[(2 *idx)]; |
---|
98 | result[1] = offsets[(2 * idx) + 1]; |
---|
99 | return result; |
---|
100 | } else { |
---|
101 | throw new ArrayIndexOutOfBoundsException("idx > " + |
---|
102 | (max_offset - 1)); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | } // class DataViewHits |
---|