aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/pagetemplates/config/PageTemplateConfigurer.java
blob: 6715546878024ee93254b10a1940f7b14ea51719 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.pagetemplates.config;

import com.yahoo.config.subscription.ConfigSubscriber;
import com.yahoo.search.pagetemplates.PageTemplatesConfig;
import com.yahoo.io.reader.NamedReader;
import com.yahoo.search.pagetemplates.PageTemplateRegistry;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Provides a static method to convert a page template config into a PageTemplateRegistry.
 * In addition, instances of this can be created to subscribe to config and keep an up to date registry reference.
 *
 * @author bratseth
 */
public class PageTemplateConfigurer {

    /**
     * Creates a new page template registry from the content of a config and returns it.
     * The returned registry will <b>not</b> be frozen. This should be done, by calling freeze(), before it is used.
     */
    public static PageTemplateRegistry toRegistry(PageTemplatesConfig config) {
        List<NamedReader> pageReaders=new ArrayList<>();
        int pageNumber=0;
        for (String pageString : config.page())
            pageReaders.add(new NamedReader("page[" + pageNumber++ + "]",new StringReader(pageString)));
        return new PageTemplateXMLReader().read(pageReaders,false);
    }

}