aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/templates/UserTemplate.java
blob: bcdf3311c1f962287497e663e00f3153e183941d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.templates;

import com.yahoo.io.ByteWriter;
import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.prelude.fastsearch.XMLField;
import com.yahoo.search.Result;
import com.yahoo.text.Utf8String;
import com.yahoo.text.XMLWriter;

import java.io.IOException;
import java.io.Writer;
import java.util.logging.Logger;


/**
 * A wrapper for a template set, suitable for subclassing.
 *
 * <p>
 * A subclass of UserTemplate must implement header(), footer(), hit(),
 * hitFooter(), error() and noHits().
 *
 * @deprecated use a renderer instead
 * @author Steinar Knutsen
 */
@SuppressWarnings("deprecation")
@Deprecated // OK (But wait for deprecated handlers in vespaclient-container-plugin to be removed)
// TODO: Remove on Vespa 7
public abstract class UserTemplate<T extends Writer> extends GenericTemplateSet {

    // &amp;
    private static final byte[] ampersand = new byte[] { 38, 97, 109, 112, 59 };

    // &lt;
    private static final byte[] lessThan = new byte[] { 38, 108, 116, 59 };
    // &gt;
    private static final byte[] greaterThan = new byte[] { 38, 103, 116, 59 };

    // \\u00
    private static final byte[] quotePrefix = new byte[] { 92, 117, 48, 48 };

    private static final Logger log = Logger.getLogger(UserTemplate.class.getName());

    /**
     * The signature of this constructor is the one which is invoked
     * in a production setting.
     */
    public UserTemplate(String name, String mimeType,
                        String encoding) {
        super(name, mimeType, encoding);
    }

    public UserTemplate(String name) {
        this(name,
                DEFAULT_MIMETYPE,
                DEFAULT_ENCODING
        );
    }

    /**
     * This is called once before each result is rendered using this template.
     * The returned writer is used in all subsequent calls. Use this if another (wrapper)
     * writer of the raw incoming writer is desired in the implementation of this template.
     * The class of the returned type must be given as a type argument to the template class,
     * to be able to implement methods taking this wrapper writer as the argument type.
     * This default implementation returns an XMLWriter.
     */
    @SuppressWarnings("unchecked")
    public T wrapWriter(Writer writer) {
        //FIXME: Hack
        return (T) XMLWriter.from(writer, 10, -1);
    }

    /**
     * Creates a new context suitable for this template.
     * The context may be reused for several evaluations, but not multiple
     * concurrent evaluations
     */
    public Context createContext() {
        return new MapContext();
    }


    /**
     * For internal use only
     * TODO: get rid of this method *
     */
    public boolean isDefaultTemplateSet() {
        return getClass().equals(TemplateSet.getDefault().getClass());
    }

    /**
     * Render the result set header.
     *
     * <p>
     * The result set is available in the context object under the name
     * "result".
     *
     * @param context
     *                wrapper which will contain, among other thing, the result
     *                set instance
     * @param writer
     *                the destination for rendering the result
     * @throws IOException
     *                 may be propagated from the writer
     */
    public abstract void header(Context context, T writer)
            throws IOException;

    /**
     * Render the result set footer.
     *
     * <p>
     * The result set is available in the context object under the name
     * "result".
     *
     * @param context
     *                wrapper which will contain, among other thing, the result
     *                set instance
     * @param writer
     *                the destination for rendering the result
     * @throws IOException
     *                 may be propagated from the writer
     */
    public abstract void footer(Context context, T writer)
            throws IOException;

    /**
     * Render a single top level hit.
     *
     * <p>
     * The result set is available in the context object under the name
     * "result". The hit itself as "hit", the index of the hit as "hitno", and
     * all the fields under their normal names.
     *
     * @param context
     *                wrapper which will contain, among other thing, the hit
     *                instance
     * @param writer
     *                the destination for rendering the hit
     * @throws IOException
     *                 may be propagated from the writer
     */
    public abstract void hit(Context context, T writer) throws IOException;

    /**
     * Render a footer for a single top level hit. A typical implementation may
     * do nothing.
     *
     * <p>
     * The result set is available in the context object under the name
     * "result". The hit itself as "hit", the index of the hit as "hitno", and
     * all the fields under their normal names.
     *
     * @param context
     *                wrapper which will contain, among other thing, the hit
     *                instance
     * @param writer
     *                the destination for rendering the hit
     * @throws IOException
     *                 may be propagated from the writer
     */
    public abstract void hitFooter(Context context, T writer)
            throws IOException;

    /**
     * Render the error message for a result set.
     *
     * <p>
     * The result set is available in the context object under the name
     * "result".
     *
     * @param context
     *                wrapper which will contain, among other thing, main error
     *                and result set instances.
     * @param writer
     *                the destination for rendering the hit
     * @throws IOException
     *                 may be propagated from the writer
     */
    public abstract void error(Context context, T writer)
            throws IOException;

    /**
     * Invoked when the result set has no hits.
     *
     * <p>
     * The result set is available in the context object under the name
     * "result".
     *
     * @param context
     *                wrapper which will contain, among other thing, the result
     *                set instance
     * @param writer
     *                the destination for rendering the hit
     * @throws IOException
     *                 may be propagated from the writer
     */
    public abstract void noHits(Context context, T writer)
            throws IOException;

    /**
     * Override this to add custom rendering for the query context of the result.
     * Only called when the query context is present.
     *
     * <p>
     * The result set is available in the context object under the name
     * "result". The query context is retrieved from the result by calling
     * result.getQuery.getContext(false)
     *
     * @param context
     *                wrapper which will contain, among other things, the result
     *                set instance
     * @param writer
     *                the destination for rendering the hit
     * @throws IOException
     *                 may be propagated from the writer
     */
    public void queryContext(Context context, T writer) throws IOException {
        Result result = (Result) context.get("result");
        result.getContext(false).render(writer);
    }

    /**
     * Dump UTF-8 byte array to writer, but escape low ASCII codes except
     * TAB, NL and CR, and escape ampersand, less than and greater than.
     *
     * <p>
     * It is presumed the writer is buffered (which is the case in normal
     * result rendering), as the method may perform a large number of write
     * operations.
     *
     * <p>
     * public only for testing.
     */
    public static void dumpAndXMLQuoteUTF8(ByteWriter writer, byte[] utf) throws java.io.IOException {
        int startDump = 0;

        for (int i = 0; i < utf.length; ++i) {
            byte b = utf[i];
            if (b < 0) {
                // Not ASCII, above character 127
                // Don't try to do something smart with UNICODE characters,
                // just pass them through.
            } else if (b < 32) {
                switch (b) {
                case 9:
                case 10:
                case 13:
                    break;
                default:
                    writer.append(utf, startDump, i - startDump);
                    startDump = i + 1;
                    quoteByte(writer, b);
                    break;
                }
            } else {
                // printable ASCII
                // quote special characters, otherwise do nothing
                switch (b) {
                // case 34: // double quote
                //     writer.append(utf, startDump, i - startDump);
                //     startDump = i + 1;
                //     writer.append(doubleQuote);
                //     break;
                case 38: // ampersand
                    writer.append(utf, startDump, i - startDump);
                    startDump = i + 1;
                    writer.append(ampersand);
                    break;
                case 60: // less than
                    writer.append(utf, startDump, i - startDump);
                    startDump = i + 1;
                    writer.append(lessThan);
                    break;
                case 62: // greater than
                    writer.append(utf, startDump, i - startDump);
                    startDump = i + 1;
                    writer.append(greaterThan);
                    break;
                }
            }
        }
        if (startDump < utf.length) {
            writer.append(utf, startDump, utf.length - startDump);
        }
    }

    /**
     * If the field is available as a UTF-8 byte array,
     * dump it to the writer.
     */
    public static boolean dumpBytes(ByteWriter writer,
                                    FastHit hit,
                                    String fieldName) throws java.io.IOException {
        return false;
    }

    private static void quoteByte(ByteWriter writer, byte b) throws java.io.IOException {
        byte[] quoted = new byte[2];
        writer.append(quotePrefix);
        quoted[0] = (byte) ((b >>> 4) + 0x30);
        if (quoted[0] > 0x39) {
            quoted[0] = (byte) (quoted[0] + 7);
        }
        quoted[1] = (byte) ((b & 0x0f) + 0x30);
        if (quoted[1] > 0x39) {
            quoted[1] = (byte) (quoted[1] + 7);
        }
        writer.append(quoted);
    }
}