aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/templates/PageTemplateSet.java
blob: a24fd623e4dcd6964594123f691748d3b9a87982 (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
// 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.search.Result;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.HitGroup;
import com.yahoo.text.XMLWriter;

import java.io.IOException;
import java.io.Writer;

/**
 * A template implementing the 'page' format.
 * This is a variant of the tiled template set - see that class for details.
 *
 * @author bratseth
 */
public class PageTemplateSet extends TiledTemplateSet {

    public PageTemplateSet() {
        super("page");
    }

    @Override
    /** Uses an XML writer in this */
    public XMLWriter wrapWriter(Writer writer) { return new XMLWriter(super.wrapWriter(writer)); }

    @Override
    public void header(Context context,XMLWriter writer) throws IOException {
        Result result=(Result)context.get("result");
        writer.xmlHeader(getRequestedEncoding(result.getQuery()));
        writer.openTag("page").attribute("version","1.0").attribute("layout",result.hits().getField("layout"));
        renderCoverageAttributes(result.getCoverage(false), writer);
        writer.closeStartTag();
        renderSectionContent(result.hits(),writer);
    }

    @Override
    public void footer(Context context,XMLWriter writer) throws IOException {
        if (writer.isIn("content"))
            writer.closeTag();
        super.footer(context,writer);
    }

    @Override
    protected void renderSection(HitGroup hit, XMLWriter writer) throws IOException {
        writer.openTag("section");
        writer.attribute("id",hit.getDisplayId());
        writer.attribute("layout",hit.getField("layout"));
        writer.attribute("region",hit.getField("region"));
        writer.closeStartTag();
        renderSectionContent(hit,writer);
    }

    @Override
    public void hit(Context context, XMLWriter writer) throws IOException {
        Hit hit = (Hit) context.get("hit");
        if (!hit.isMeta() && !writer.isIn("content"))
            writer.openTag("content");
        super.hit(context,writer);
    }

    @Override
    public void hitFooter(Context context, XMLWriter writer) throws IOException {
        if (writer.isIn("content"))
            writer.closeTag();
        super.hitFooter(context, writer);
    }

    public String toString() { return "page template"; }

}