aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/ModelElement.java
blob: 2ffc513c23caa8a03d8f00e383883e89879d5d80 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.builder.xml.dom;

import com.yahoo.text.XML;
import com.yahoo.vespa.model.utils.Duration;

import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * A w3c Element wrapper with a better API.
 *
 * Author unknown.
 */
public class ModelElement {

    private final Element xml;

    public ModelElement(Element xml) {
        if (xml == null) throw new NullPointerException("Can not create ModelElement with null element");
        if (xml.getNodeName() == null) throw new NullPointerException("Can not create ModelElement with unnamed element");
        this.xml = xml;
    }

    public Element getXml() { return xml; }

    /** Returns the child with the given name, or null if none. */
    public ModelElement child(String name) {
        Element e = XML.getChild(xml, name);
        if (e == null) return null;
        return new ModelElement(e);
    }

    /** If not found, return empty list. */
    public List<ModelElement> children(String name) {
        List<Element> e = XML.getChildren(xml, name);

        List<ModelElement> list = new ArrayList<>();
        e.forEach(element -> list.add(new ModelElement(element)));
        return list;
    }

    public ModelElement childByPath(String path) {
        StringTokenizer tokenizer = new StringTokenizer(path, ".");
        ModelElement curElem = this;
        while (tokenizer.hasMoreTokens() && curElem != null) {
            String pathElem = tokenizer.nextToken();
            ModelElement child = curElem.child(pathElem);
            if (!tokenizer.hasMoreTokens()) {
                if (child != null)
                    return child;
            }
            curElem = child;
        }
        return null;
    }

    public String childAsString(String path) {
        StringTokenizer tokenizer = new StringTokenizer(path, ".");
        ModelElement curElem = this;
        while (tokenizer.hasMoreTokens() && curElem != null) {
            String pathElem = tokenizer.nextToken();
            ModelElement child = curElem.child(pathElem);
            if (!tokenizer.hasMoreTokens()) {
                String attr = curElem.stringAttribute(pathElem);
                if (attr != null) {
                    return attr;
                } else if (child != null) {
                    return child.asString();
                }
            }
            curElem = child;
        }
        return null;
    }

    public String asString() {
        return xml.getFirstChild().getTextContent();
    }

    public double asDouble() {
        return Double.parseDouble(asString());
    }

    public long asLong() {
        return (long) BinaryUnit.valueOf(asString());
    }

    public Duration asDuration() {
        return new Duration(asString());
    }

    public Long childAsLong(String path) {
        String child = childAsString(path);
        if (child == null) return null;
        return Long.parseLong(child.trim());
    }

    public Integer childAsInteger(String path) {
        String child = childAsString(path);
        if (child == null) return null;
        return Integer.parseInt(child.trim());
    }

    public Double childAsDouble(String path) {
        String child = childAsString(path);
        if (child == null) return null;
        return Double.parseDouble(child.trim());
    }

    public Boolean childAsBoolean(String path) {
        String child = childAsString(path);
        if (child == null) return null;
        return Boolean.parseBoolean(child.trim());
    }

    public Duration childAsDuration(String path) {
        String child = childAsString(path);
        if (child == null) return null;
        return new Duration(child);
    }

    /** Returns the given attribute or throws IllegalArgumentException if not present */
    public int requiredIntegerAttribute(String name) {
        if (stringAttribute(name) == null)
            throw new IllegalArgumentException("Required attribute '" + name + "' is missing");
        return integerAttribute(name, null);
    }

    /** Returns the value of this attribute or null if not present */
    public Integer integerAttribute(String name) {
        return integerAttribute(name, null);
    }

    public Integer integerAttribute(String name, Integer defaultValue) {
        String value = stringAttribute(name);
        if (value == null) return defaultValue;
        return (int) BinaryUnit.valueOf(value);
    }

    public boolean booleanAttribute(String name) {
        return booleanAttribute(name, false);
    }

    public boolean booleanAttribute(String name, boolean defaultValue) {
        String value = stringAttribute(name);
        if (value == null) return defaultValue;
        return Boolean.parseBoolean(value);
    }

    public Long longAttribute(String name) {
        String value = stringAttribute(name);
        if (value == null) return null;
        return (long) BinaryUnit.valueOf(value);
    }

    public Double doubleAttribute(String name) {
        String value = stringAttribute(name);
        if (value == null) return null;
        return Double.parseDouble(value);
    }

    /** Returns the given attribute or throws IllegalArgumentException if not present */
    public double requiredDoubleAttribute(String name) {
        if (stringAttribute(name) == null)
            throw new IllegalArgumentException("Required attribute '" + name + "' is missing");
        return doubleAttribute(name);
    }

    /** Returns the content of the attribute with the given name, or null if none */
    public String stringAttribute(String name) {
        return stringAttribute(name, null);
    }

    /** Returns the content of the attribute with the given name, or the default value if none */
    public String stringAttribute(String name, String defaultValue) {
        if ( ! xml.hasAttribute(name)) return defaultValue;
        return xml.getAttribute(name);
    }

    /** Returns the content of the attribute with the given name or throws IllegalArgumentException if not present */
    public String requiredStringAttribute(String name) {
        if (stringAttribute(name) == null)
            throw new IllegalArgumentException("Required attribute '" + name + "' is missing");
        return stringAttribute(name);
    }


    public List<ModelElement> subElements(String name) {
        List<Element> elements = XML.getChildren(xml, name);

        List<ModelElement> helpers = new ArrayList<>();
        for (Element e : elements)
            helpers.add(new ModelElement(e));
        return helpers;
    }
    
    @Override
    public String toString() {
        return xml.getNodeName();
    }

}