DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_validator / kwalify / src / main / java / kwalify / YamlParser.java
1 /*
2  * copyright(c) 2005 kuwata-lab all rights reserved.
3  */
4 package kwalify;
5
6 import java.util.Map;
7 import java.util.HashMap;
8 import java.util.IdentityHashMap;
9 import java.util.List;
10 import java.util.Iterator;
11
12 /**
13  *  yaml parser which can keep line number of path.
14  */
15 public class YamlParser extends PlainYamlParser {
16     private Map linenumsTable = new IdentityHashMap(); // object => sequence or mapping
17     private int firstLinenum = -1;
18     private Object document = null;
19
20     YamlParser(String yamlStr) {
21         super(yamlStr);
22     }
23
24     public Object parse() throws SyntaxException {
25         document = super.parse();
26         return document;
27     }
28
29     protected String getLine() {
30         String line = super.getLine();
31         if (firstLinenum < 0) {
32             firstLinenum = currentLineNumber();
33         }
34         return line;
35     }
36
37
38     private int getPathLineNumber(String ypath) throws InvalidPathException {
39         if (document == null) {
40             return -1;
41         }
42         if (ypath.length() == 0 || "/".equals(ypath)) {
43             return 1;
44         }
45         String[] elems = ypath.split("/");
46         String lastElem = elems.length > 0 ? elems[elems.length - 1] : null;
47         int i = ypath.charAt(0) == '/' ? 1 : 0;
48         int len = elems.length - 1;
49         Object documentCollection = this.document;   // collection
50         for ( ; i < len ; i++) {
51             if (documentCollection == null) {
52                 throw new InvalidPathException(ypath);
53             } else if (documentCollection instanceof Map) {
54                 documentCollection = ((Map)documentCollection).get(elems[i]);
55             } else if (documentCollection instanceof List) {
56                 int index = Integer.parseInt(elems[i]);
57                 if (index < 0 || ((List)documentCollection).size() < index) {
58                     throw new InvalidPathException(ypath);
59                 }
60                 documentCollection = ((List)documentCollection).get(index);
61             } else {
62                 throw new InvalidPathException(ypath);
63             }
64         }
65
66         if (documentCollection == null) {
67             throw new InvalidPathException(ypath);
68         }
69         Object linenums = linenumsTable.get(documentCollection); // Map or List
70         int linenum;
71         if (documentCollection instanceof Map) {
72             assert linenums instanceof Map;
73             Object d = ((Map)linenums).get(lastElem);
74             linenum = (Integer) d;
75         } else if (documentCollection instanceof List) {
76             assert linenums instanceof List;
77             int index = Integer.parseInt(lastElem);
78             if (index < 0 || ((List)linenums).size() <= index) {
79                 throw new InvalidPathException(ypath);
80             }
81             Object d = ((List)linenums).get(index);
82             linenum = (Integer) d;
83         } else {
84             throw new InvalidPathException(ypath);
85         }
86         return linenum;
87     }
88
89     public void setErrorsLineNumber(List errors) throws InvalidPathException {
90         for (Iterator it = errors.iterator(); it.hasNext(); ) {
91             ValidationException ex = (ValidationException)it.next();
92             ex.setLineNumber(getPathLineNumber(ex.getPath()));
93         }
94     }
95
96     protected Map createMapping() {
97         Map map = super.createMapping();
98         linenumsTable.put(map, new HashMap());
99         return map;
100     }
101 }