-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathUnknownPropertiesParser.java
More file actions
44 lines (38 loc) · 1.17 KB
/
UnknownPropertiesParser.java
File metadata and controls
44 lines (38 loc) · 1.17 KB
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
/**
* @license
* Copyright 2017 The FOAM Authors. All Rights Reserved.
* http://www.apache.org/licenses/LICENSE-2.0
*/
package foam.lib.json;
import foam.lib.parse.*;
import foam.util.SafetyUtil;
public class UnknownPropertiesParser
extends ProxyParser
{
public UnknownPropertiesParser() {
super(new Parser() {
Parser delegate = new Repeat(
new Seq1(1,
new Optional(CommentParser.create()),
new Alt(new UnknownKeyValueParser0(), Whitespace.instance())
), ",");
public PStream parse(PStream ps, ParserContext x) {
ps = ps.apply(delegate, x);
if ( ps == null ) return null;
Object[] objs = (Object[]) ps.value();
StringBuilder res = new StringBuilder();
for ( int i = 0 ; i < objs.length ; i++ ) {
if ( objs[i] instanceof String str ) {
// Do not include empty string and invalid key-value pair
if ( SafetyUtil.isEmpty(str) || str.indexOf(":") == -1 ) continue;
res.append(str);
if ( i < objs.length - 1) {
res.append(',');
}
}
}
return ps.setValue(res.toString());
}
});
}
}