aboutsummaryrefslogtreecommitdiffstats
path: root/config-lib/src/main/java/com/yahoo/config/StringNode.java
blob: 91b078bea46cee18938cd3ae678846fa907c2d23 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config;

import com.yahoo.config.text.StringUtilities;

/**
 * A StringNode class represents a string in a {@link ConfigInstance}.
 *
 * @author larschr
 */
public class StringNode extends LeafNode<String> {

    /**
     * Creates a new un-initialized StringNode.
     */
    public StringNode() {
    }

    /**
     * Creates a new StringNode, initialized to <code>value</code>.
     *
     * @param value the value of this StringNode.
     */
    public StringNode(String value) {
        super(true);
        this.value = value;
    }

    /**
     * Returns the value of this string. Same as {@link #getValue()}
     * since the value of this node is a String (but implementations
     * in other {@link LeafNode} subclasses differ).
     *
     * @return the string representation of this StringNode, or null if
     *         the value is explicitly set to null
     */
    public String value() {
        return value;
    }

    @Override
    public String getValue() {
        return value();
    }

    @Override
    public String toString() {
        return (value == null) ? "(null)" : '"' + StringUtilities.escape(getValue()) + '"';
    }

    /**
     * Remove character escape codes.
     *
     * @param string escaped string
     * @return unescaped string
     */
    public static String unescapeQuotedString(String string) {
        StringBuilder sb = new StringBuilder(string);
        for (int i = 0; i < sb.length(); i++) {
            if (sb.charAt(i) == '\\') {
                sb.deleteCharAt(i);
                if (i == sb.length()) {
                    throw new IllegalArgumentException("Parse error" + string);
                }
                switch (sb.charAt(i)) {
                    case 'n' -> sb.setCharAt(i, '\n');
                    case 'r' -> sb.setCharAt(i, '\r');
                    case 't' -> sb.setCharAt(i, '\t');
                    case 'f' -> sb.setCharAt(i, '\f');
                    case 'x' -> {
                        if (i + 2 >= sb.length()) {
                            throw new IllegalArgumentException("Could not parse hex value " + string);
                        }
                        sb.setCharAt(i, (char) Integer.parseInt(sb.substring(i + 1, i + 3), 16));
                        sb.delete(i + 1, i + 3);
                    }
                    case '\\' -> sb.setCharAt(i, '\\');
                }
            }
        }

        if (sb.length() > 0 && (sb.charAt(0) == '"') && sb.charAt(sb.length() - 1) == '"') {
            sb.deleteCharAt(sb.length() - 1);//remove last quote
            if (sb.length() > 0) {
                sb.deleteCharAt(0);  //remove first quote
            }
        }
        return sb.toString();
    }

    /**
     * Sets the value of this string from the string representation
     * of this value in the (escaped) input configuration. The value
     * supplied to this method needs un-escaping and will be
     * un-escaped.
     *
     * @param value the new value of this node.
     */
    @Override
    protected boolean doSetValue(String value) {
        if (value.startsWith("\"") && value.endsWith("\""))
            this.value = unescapeQuotedString(value);
        else {
            //TODO: unquoted strings can be probably be prohibited now.(?) -gv
            this.value = value;
        }
        return true;
    }

}