aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search/src/main/java/com/yahoo/search/predicate/utils/VespaFeedWriter.java
blob: 7c8c1513a50dc2cc375b156346c1473cad9d1f02 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.utils;

import com.google.common.html.HtmlEscapers;
import com.yahoo.document.predicate.Predicate;

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

/**
 * @author Magnar Nedland
 */
public class VespaFeedWriter extends BufferedWriter {

    private String namespace;
    private String documentType;

    VespaFeedWriter(Writer writer, String namespace, String documentType) throws IOException {
        super(writer);
        this.namespace = namespace;
        this.documentType = documentType;

        this.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
        this.append("<vespafeed>\n");
    }

    @Override
    public void close() throws IOException {
        this.append("</vespafeed>\n");
        super.close();
    }

    public void writePredicateDocument(int id, String fieldName, Predicate predicate) {
        try {
            this.append(String.format("<document documenttype=\"%2$s\" documentid=\"id:%1$s:%2$s::%3$d\">\n",
                    namespace, documentType, id));
            this.append("<" + fieldName + ">" + HtmlEscapers.htmlEscaper().escape(predicate.toString()) + "</" + fieldName + ">\n");
            this.append("</document>\n");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}