aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/templates/HitContext.java
blob: 5ef28d6f6c6721d72d9f3d5180ffcd4c7de4b62c (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
// 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.prelude.hitfield.HitField;
import com.yahoo.prelude.hitfield.JSONString;
import com.yahoo.prelude.hitfield.XMLString;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.StructuredData;
import com.yahoo.text.XML;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * A context providing all the fields of a hit, and falls back to MapContext behavior for all other keys.
 *
 * @author Tony Vaagenes
 * @deprecated use a Renderer instead
 */
@SuppressWarnings("deprecation")
// TODO: Remove on Vespa 7
@Deprecated // OK (But wait for deprecated handlers in vespaclient-container-plugin to be removed)
public class HitContext extends Context {

    private final Hit hit;
    private final Context fallbackContext;

    public HitContext(Hit hit, Context fallbackContext) {
        this.hit = hit;
        this.fallbackContext = fallbackContext;
    }

    @Override
    public Object put(String key, Object value) {
        return fallbackContext.put(key, value);
    }

    @Override
    public Object get(String key) {
        Object value = normalizedHitProperty(key);
        return value != null ?
                value :
                fallbackContext.get(key);
    }

    @Override
    public Object remove(Object key) {
        return fallbackContext.remove(key);
    }

    @Override
    public Collection<? extends Object> getKeys() {
        Set<Object> keys = new HashSet<>(fallbackContext.getKeys());
        keys.addAll(hit.fieldKeys());
        return keys;
    }

    @Override
    public void setBoldOpenTag(String boldOpenTag) {
        fallbackContext.setBoldOpenTag(boldOpenTag);
    }

    @Override
    public void setBoldCloseTag(String boldCloseTag) {
        fallbackContext.setBoldCloseTag(boldCloseTag);
    }

    @Override
    public void setSeparatorTag(String separatorTag) {
        fallbackContext.setSeparatorTag(separatorTag);
    }

    @Override
    public String getBoldOpenTag() {
        return fallbackContext.getBoldOpenTag();
    }

    @Override
    public String getBoldCloseTag() {
        return fallbackContext.getBoldCloseTag();
    }

    @Override
    public String getSeparatorTag() {
        return fallbackContext.getSeparatorTag();
    }

    @Override
    //TVT: TODO: Make this package private again.
    public boolean isUtf8Output() {
        return fallbackContext.isUtf8Output();
    }

    @Override
    //TODO: TVT: make this package private again
    public void setUtf8Output(boolean utf8Output) {
        fallbackContext.setUtf8Output(utf8Output);
    }

    @Override
    public void setXmlEscape(boolean xmlEscape) {
        fallbackContext.setXmlEscape(xmlEscape);
    }

    @Override
    public boolean getXmlEscape() {
        return fallbackContext.getXmlEscape();
    }

    @Override
    protected Object normalizeValue(Object value) {
        return fallbackContext.normalizeValue(value);
    }

    private Object normalizedHitProperty(String key) {
        Object value = hit.getField(key);
        return value == null ?
                null :
                normalizeHitFieldValue(value);
    }

    private Object normalizeHitFieldValue(Object value) {
        if (value instanceof HitField) {
            HitField hf = (HitField) value;
            if (getXmlEscape()) {
                return hf.quotedContent(getBoldOpenTag(),
                        getBoldCloseTag(),
                        getSeparatorTag(),
                        true);
            } else {
                return hf.getContent(getBoldOpenTag(),
                        getBoldCloseTag(),
                        getSeparatorTag());
            }
        } else if (value instanceof StructuredData) {
            return value.toString();
        } else if (value instanceof XMLString || value instanceof JSONString) {
            return value.toString();
        } else if (getXmlEscape()) {
            return XML.xmlEscape(value.toString(), true, null);
        } else {
            return value.toString();
        }
    }

}