aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/chains/InheritanceBuilder.java
blob: e617ea934058cc4c531cb6001df773c9e205cf3d (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.builder.xml.dom.chains;

import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.chain.model.ChainSpecification;
import com.yahoo.config.model.builder.xml.XmlHelper;
import com.yahoo.text.XML;
import org.w3c.dom.Element;

import java.util.*;

/**
 * Build an Inheritance object from an inheritance section.
 * @author tonytv
 */
public class InheritanceBuilder {
    final ChainSpecification.Inheritance inheritance;

    public InheritanceBuilder(Element spec) {
        inheritance = new ChainSpecification.Inheritance(
                // XXX: for this to work, the tagname in the spec must match the tagname inside the 'inherits' elem, e.g. 'searchchain->inherits->searchchain'
                read(spec, "inherits", spec.getTagName()),
                read(spec, "excludes", "exclude"));
    }

    public ChainSpecification.Inheritance build() {
        return inheritance;
    }

    private Set<ComponentSpecification> read(Element spec, String attributeName, String elementName) {
        Set<ComponentSpecification> componentSpecifications = new LinkedHashSet<>();

        componentSpecifications.addAll(spaceSeparatedComponentSpecificationsFromAttribute(spec, attributeName));

        // TODO: the 'inherits' element is undocumented, and can be removed in an upcoming version of Vespa
        componentSpecifications.addAll(idRefFromElements(XML.getChild(spec, "inherits"), elementName));


        return componentSpecifications;
    }

    private Collection<ComponentSpecification> idRefFromElements(Element spec, String elementName) {
        Collection<ComponentSpecification> result = new ArrayList<>();
        if (spec == null)
            return result;

        for (Element element : XML.getChildren(spec, elementName)) {
            result.add(XmlHelper.getIdRef(element));
        }
        return result;
    }

    private Collection<ComponentSpecification> spaceSeparatedComponentSpecificationsFromAttribute(Element spec, String attributeName) {
        return toComponentSpecifications(XmlHelper.spaceSeparatedSymbolsFromAttribute(spec, attributeName));
    }

    private Set<ComponentSpecification> toComponentSpecifications(Collection<String> symbols) {
        Set<ComponentSpecification> specifications = new LinkedHashSet<>();
        for (String symbol : symbols) {
            specifications.add(new ComponentSpecification(symbol));
        }
        return specifications;
    }
}