1 | /** |
---|
2 | /** |
---|
3 | * This software is copyright (c) 2011-2013 by |
---|
4 | * - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
5 | * This is free software. You can redistribute it |
---|
6 | * and/or modify it under the terms described in |
---|
7 | * the GNU General Public License v3 of which you |
---|
8 | * should have received a copy. Otherwise you can download |
---|
9 | * it from |
---|
10 | * |
---|
11 | * http://www.gnu.org/licenses/gpl-3.0.txt |
---|
12 | * |
---|
13 | * @copyright Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
14 | * |
---|
15 | * @license http://www.gnu.org/licenses/gpl-3.0.txt |
---|
16 | * GNU General Public License v3 |
---|
17 | */ |
---|
18 | package eu.clarin.sru.client; |
---|
19 | |
---|
20 | import java.util.List; |
---|
21 | |
---|
22 | import org.slf4j.Logger; |
---|
23 | import org.slf4j.LoggerFactory; |
---|
24 | import org.w3c.dom.Node; |
---|
25 | |
---|
26 | import eu.clarin.sru.client.SRUExplainRecordData.ConfigInfo; |
---|
27 | import eu.clarin.sru.client.SRUExplainRecordData.Schema; |
---|
28 | import eu.clarin.sru.client.fcs.ClarinFCSRecordData; |
---|
29 | import eu.clarin.sru.client.fcs.DataView; |
---|
30 | import eu.clarin.sru.client.fcs.DataViewGenericDOM; |
---|
31 | import eu.clarin.sru.client.fcs.DataViewGenericString; |
---|
32 | import eu.clarin.sru.client.fcs.DataViewKWIC; |
---|
33 | import eu.clarin.sru.client.fcs.Resource; |
---|
34 | |
---|
35 | class TestUtils { |
---|
36 | private static final Logger logger = |
---|
37 | LoggerFactory.getLogger(TestUtils.class); |
---|
38 | |
---|
39 | public static SRUExplainRequest makeExplainRequest(String baseURI) { |
---|
40 | SRUExplainRequest request = new SRUExplainRequest(baseURI); |
---|
41 | request.setExtraRequestData("x-indent-response", "4"); |
---|
42 | request.setParseRecordDataEnabled(true); |
---|
43 | return request; |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | public static SRUScanRequest makeScanRequest(String baseURI) { |
---|
48 | SRUScanRequest request = new SRUScanRequest(baseURI); |
---|
49 | request.setScanClause("fcs.resource = root"); |
---|
50 | request.setExtraRequestData("x-clarin-resource-info", "true"); |
---|
51 | request.setExtraRequestData("x-indent-response", "4"); |
---|
52 | return request; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | public static SRUSearchRetrieveRequest makeSearchRequest(String baseURI, String query) { |
---|
57 | if ((query == null) || query.isEmpty()) { |
---|
58 | query = "Faustus"; |
---|
59 | } |
---|
60 | SRUSearchRetrieveRequest request = new SRUSearchRetrieveRequest(baseURI); |
---|
61 | request.setQuery(query); |
---|
62 | request.setRecordSchema(ClarinFCSRecordData.RECORD_SCHEMA); |
---|
63 | request.setMaximumRecords(5); |
---|
64 | request.setRecordPacking(SRURecordPacking.XML); |
---|
65 | request.setExtraRequestData("x-indent-response", "4"); |
---|
66 | return request; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | public static void printExplainResponse(SRUExplainResponse response) { |
---|
71 | logger.info("displaying results of 'explain' request ..."); |
---|
72 | if (response.hasDiagnostics()) { |
---|
73 | for (SRUDiagnostic diagnostic : response.getDiagnostics()) { |
---|
74 | logger.info("uri={}, message={}, detail={}", |
---|
75 | diagnostic.getURI(), |
---|
76 | diagnostic.getMessage(), |
---|
77 | diagnostic.getDetails()); |
---|
78 | } |
---|
79 | } |
---|
80 | if (response.hasRecord()) { |
---|
81 | SRURecord record = response.getRecord(); |
---|
82 | logger.info("schema = {}", record.getRecordSchema()); |
---|
83 | if (record.isRecordSchema(SRUExplainRecordData.RECORD_SCHEMA)) { |
---|
84 | dumpExplainRecordData(record.getRecordData()); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | public static void printScanResponse(SRUScanResponse response) { |
---|
91 | logger.info("displaying results of 'scan' request ..."); |
---|
92 | if (response.hasDiagnostics()) { |
---|
93 | for (SRUDiagnostic diagnostic : response.getDiagnostics()) { |
---|
94 | logger.info("uri={}, message={}, detail={}", |
---|
95 | diagnostic.getURI(), |
---|
96 | diagnostic.getMessage(), |
---|
97 | diagnostic.getDetails()); |
---|
98 | } |
---|
99 | } |
---|
100 | if (response.hasTerms()) { |
---|
101 | for (SRUTerm term : response.getTerms()) { |
---|
102 | logger.info("value={}, numberOfRecords={}, displayTerm={}", |
---|
103 | term.getValue(), |
---|
104 | term.getNumberOfRecords(), |
---|
105 | term.getDisplayTerm()); |
---|
106 | } |
---|
107 | } else { |
---|
108 | logger.info("no terms"); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | public static void printSearchResponse(SRUSearchRetrieveResponse response) { |
---|
114 | logger.info("displaying results of 'searchRetrieve' request ..."); |
---|
115 | logger.info("numberOfRecords = {}, nextResultPosition = {}", |
---|
116 | response.getNumberOfRecords(), |
---|
117 | response.getNextRecordPosition()); |
---|
118 | if (response.hasDiagnostics()) { |
---|
119 | for (SRUDiagnostic diagnostic : response.getDiagnostics()) { |
---|
120 | logger.info("uri={}, message={}, detail={}", |
---|
121 | diagnostic.getURI(), |
---|
122 | diagnostic.getMessage(), |
---|
123 | diagnostic.getDetails()); |
---|
124 | } |
---|
125 | } |
---|
126 | if (response.hasRecords()) { |
---|
127 | for (SRURecord record : response.getRecords()) { |
---|
128 | logger.info("schema = {}, identifier = {}, position = {}", |
---|
129 | record.getRecordSchema(), |
---|
130 | record.getRecordIdentifier(), |
---|
131 | record.getRecordPosition()); |
---|
132 | if (record.isRecordSchema(ClarinFCSRecordData.RECORD_SCHEMA)) { |
---|
133 | ClarinFCSRecordData rd = |
---|
134 | (ClarinFCSRecordData) record.getRecordData(); |
---|
135 | dumpResource(rd.getResource()); |
---|
136 | } else if (record.isRecordSchema(SRUSurrogateRecordData.RECORD_SCHEMA)) { |
---|
137 | SRUSurrogateRecordData r = |
---|
138 | (SRUSurrogateRecordData) record.getRecordData(); |
---|
139 | logger.info("SURROGATE DIAGNOSTIC: uri={}, message={}, detail={}", |
---|
140 | r.getURI(), r.getMessage(), r.getDetails()); |
---|
141 | } else { |
---|
142 | logger.info("UNSUPPORTED SCHEMA: {}", |
---|
143 | record.getRecordSchema()); |
---|
144 | } |
---|
145 | } |
---|
146 | } else { |
---|
147 | logger.info("no results"); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | public static void dumpExplainRecordData(SRURecordData recordData) { |
---|
153 | if (SRUExplainRecordData.RECORD_SCHEMA.equals(recordData.getRecordSchema())) { |
---|
154 | SRUExplainRecordData data = (SRUExplainRecordData) recordData; |
---|
155 | logger.info("host={}, port={}, database={}", |
---|
156 | data.getServerInfo().getHost(), |
---|
157 | data.getServerInfo().getPort(), |
---|
158 | data.getServerInfo().getDatabase()); |
---|
159 | List<Schema> schemaInfo = data.getSchemaInfo(); |
---|
160 | if (schemaInfo != null) { |
---|
161 | for (Schema schema : schemaInfo) { |
---|
162 | logger.debug("schema: identifier={}, name={}, " + |
---|
163 | "location={}, sort={}, retrieve={}", |
---|
164 | schema.getIdentifier(), |
---|
165 | schema.getName(), |
---|
166 | schema.getLocation(), |
---|
167 | schema.getSort(), |
---|
168 | schema.getRetrieve()); |
---|
169 | } |
---|
170 | } |
---|
171 | ConfigInfo configInfo = data.getConfigInfo(); |
---|
172 | if (configInfo != null) { |
---|
173 | if (configInfo.getDefaults() != null) { |
---|
174 | logger.debug("configInfo/default = {}", |
---|
175 | configInfo.getDefaults()); |
---|
176 | } |
---|
177 | if (configInfo.getSettings() != null) { |
---|
178 | logger.debug("configInfo/setting = {}", |
---|
179 | configInfo.getSettings()); |
---|
180 | } |
---|
181 | if (configInfo.getSupports() != null) { |
---|
182 | logger.debug("configInfo/supports = {}", |
---|
183 | configInfo.getSupports()); |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | public static void dumpResource(Resource resource) { |
---|
191 | logger.info("CLARIN-FCS: pid={}, ref={}", |
---|
192 | resource.getPid(), resource.getRef()); |
---|
193 | if (resource.hasDataViews()) { |
---|
194 | dumpDataView("CLARIN-FCS: ", resource.getDataViews()); |
---|
195 | } |
---|
196 | if (resource.hasResourceFragments()) { |
---|
197 | for (Resource.ResourceFragment fragment : resource.getResourceFragments()) { |
---|
198 | logger.debug("CLARIN-FCS: ResourceFragment: pid={}, ref={}", |
---|
199 | fragment.getPid(), fragment.getRef()); |
---|
200 | if (fragment.hasDataViews()) { |
---|
201 | dumpDataView("CLARIN-FCS: ResourceFragment/", fragment.getDataViews()); |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | private static void dumpDataView(String s, List<DataView> dataviews) { |
---|
209 | for (DataView dataview : dataviews) { |
---|
210 | logger.info("{}DataView: type={}, pid={}, ref={}", |
---|
211 | s, dataview.getMimeType(), dataview.getPid(), |
---|
212 | dataview.getRef()); |
---|
213 | if (dataview instanceof DataViewGenericDOM) { |
---|
214 | final DataViewGenericDOM view = (DataViewGenericDOM) dataview; |
---|
215 | final Node root = view.getDocument().getFirstChild(); |
---|
216 | logger.info("{}DataView: root element <{}> / {}", |
---|
217 | s, root.getNodeName(), |
---|
218 | root.getOwnerDocument().hashCode()); |
---|
219 | } else if (dataview instanceof DataViewGenericString) { |
---|
220 | final DataViewGenericString view = (DataViewGenericString) dataview; |
---|
221 | logger.info("{}DataView: data = {}", s, view.getContent()); |
---|
222 | } else if (dataview.isMimeType(DataViewKWIC.TYPE)) { |
---|
223 | final DataViewKWIC kw = (DataViewKWIC) dataview; |
---|
224 | logger.info("{}DataView: {} / {} / {}", |
---|
225 | s, kw.getLeft(), kw.getKeyword(), kw.getRight()); |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | } // class TestUtils |
---|