summaryrefslogtreecommitdiffstats
path: root/dcaedt_validator/kwalify/src/main/java/kwalify/YamlParser.java
blob: b5789d3235ba0b6fac070def4b22ebb628bcd30a (plain)
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
/*
 * copyright(c) 2005 kuwata-lab all rights reserved.
 */
package kwalify;

import java.util.Map;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Iterator;

/**
 *  yaml parser which can keep line number of path.
 */
public class YamlParser extends PlainYamlParser {
    private Map linenumsTable = new IdentityHashMap(); // object => sequence or mapping
    private int firstLinenum = -1;
    private Object document = null;

    YamlParser(String yamlStr) {
        super(yamlStr);
    }

    public Object parse() throws SyntaxException {
        document = super.parse();
        return document;
    }

    protected String getLine() {
        String line = super.getLine();
        if (firstLinenum < 0) {
            firstLinenum = currentLineNumber();
        }
        return line;
    }


    private int getPathLineNumber(String ypath) throws InvalidPathException {
        if (document == null) {
            return -1;
        }
        if (ypath.length() == 0 || "/".equals(ypath)) {
            return 1;
        }
        String[] elems = ypath.split("/");
        String lastElem = elems.length > 0 ? elems[elems.length - 1] : null;
        int i = ypath.charAt(0) == '/' ? 1 : 0;
        int len = elems.length - 1;
        Object documentCollection = this.document;   // collection
        for ( ; i < len ; i++) {
            if (documentCollection == null) {
                throw new InvalidPathException(ypath);
            } else if (documentCollection instanceof Map) {
                documentCollection = ((Map)documentCollection).get(elems[i]);
            } else if (documentCollection instanceof List) {
                int index = Integer.parseInt(elems[i]);
                if (index < 0 || ((List)documentCollection).size() < index) {
                    throw new InvalidPathException(ypath);
                }
                documentCollection = ((List)documentCollection).get(index);
            } else {
                throw new InvalidPathException(ypath);
            }
        }

        if (documentCollection == null) {
            throw new InvalidPathException(ypath);
        }
        Object linenums = linenumsTable.get(documentCollection); // Map or List
        int linenum;
        if (documentCollection instanceof Map) {
            assert linenums instanceof Map;
            Object d = ((Map)linenums).get(lastElem);
            linenum = (Integer) d;
        } else if (documentCollection instanceof List) {
            assert linenums instanceof List;
            int index = Integer.parseInt(lastElem);
            if (index < 0 || ((List)linenums).size() <= index) {
                throw new InvalidPathException(ypath);
            }
            Object d = ((List)linenums).get(index);
            linenum = (Integer) d;
        } else {
            throw new InvalidPathException(ypath);
        }
        return linenum;
    }

    public void setErrorsLineNumber(List errors) throws InvalidPathException {
        for (Iterator it = errors.iterator(); it.hasNext(); ) {
            ValidationException ex = (ValidationException)it.next();
            ex.setLineNumber(getPathLineNumber(ex.getPath()));
        }
    }

    protected Map createMapping() {
        Map map = super.createMapping();
        linenumsTable.put(map, new HashMap());
        return map;
    }
}