7
7
import java .util .ArrayList ;
8
8
import java .util .List ;
9
9
import java .util .function .Function ;
10
+ import java .util .function .Predicate ;
10
11
import java .util .regex .Matcher ;
11
12
import java .util .regex .Pattern ;
12
13
16
17
* (Internal) Parses .env file
17
18
*/
18
19
public class DotenvParser {
20
+
21
+ private static final Pattern WHITE_SPACE_REGEX = Pattern .compile ("^\\ s*$" ); // ^\s*${'$'}
22
+ private static final Pattern DOTENV_ENTRY_REGEX = Pattern .compile ("^\\ s*([\\ w.\\ -]+)\\ s*(=)\\ s*(.*)?\\ s*$" ); // ^\s*([\w.\-]+)\s*(=)\s*(.*)?\s*$
23
+
19
24
private final DotenvReader reader ;
20
25
private final boolean throwIfMissing ;
21
26
private final boolean throwIfMalformed ;
22
27
23
- private final Function <String , Boolean > isWhiteSpace = s -> matches ("^ \\ s*$" , s ); // ^\s*${'$'}
24
- private final Function <String , Boolean > isComment = s -> s .startsWith ("#" ) || s .startsWith ("////" );
25
- private final Function <String , Boolean > isQuoted = s -> s .startsWith ("\" " ) && s .endsWith ("\" " );
26
- private final Function <String , DotenvEntry > parseLine = s -> matchEntry ("^ \\ s*([ \\ w. \\ -]+) \\ s*(=) \\ s*(.*)? \\ s*$" , s ); // ^\s*([\w.\-]+)\s*(=)\s*(.*)?\s*$
28
+ private final Predicate <String > isWhiteSpace = s -> matches (WHITE_SPACE_REGEX , s );
29
+ private final Predicate <String > isComment = s -> s .startsWith ("#" ) || s .startsWith ("////" );
30
+ private final Predicate <String > isQuoted = s -> s .startsWith ("\" " ) && s .endsWith ("\" " );
31
+ private final Function <String , DotenvEntry > parseLine = s -> matchEntry (DOTENV_ENTRY_REGEX , s );
27
32
28
33
/**
29
34
* Creates a dotenv parser
@@ -46,11 +51,11 @@ public List<DotenvEntry> parse() throws DotenvException {
46
51
List <DotenvEntry > entries = new ArrayList <>();
47
52
for (String line : lines ()) {
48
53
String l = line .trim ();
49
- if (isWhiteSpace .apply (l ) || isComment .apply (l ) || isBlank (l )) continue ;
54
+ if (isWhiteSpace .test (l ) || isComment .test (l ) || isBlank (l )) continue ;
50
55
51
56
DotenvEntry entry = parseLine .apply (l );
52
57
if (entry == null ) {
53
- if (throwIfMalformed ) throw new DotenvException ("Malformed entry " + l );
58
+ if (throwIfMalformed ) throw new DotenvException ("Malformed entry " + l );
54
59
continue ;
55
60
}
56
61
String key = entry .getKey ();
@@ -73,26 +78,24 @@ private List<String> lines() throws DotenvException {
73
78
74
79
private String normalizeValue (String value ) {
75
80
String tr = value .trim ();
76
- return isQuoted .apply (tr )
77
- ? tr .substring (1 , value .length () -1 )
81
+ return isQuoted .test (tr )
82
+ ? tr .substring (1 , value .length () - 1 )
78
83
: tr ;
79
84
}
80
85
81
- private static boolean matches (String regex , String text ) {
82
- Pattern pattern = Pattern .compile (regex );
83
- Matcher matcher = pattern .matcher (text );
86
+ private static boolean matches (Pattern regex , String text ) {
87
+ Matcher matcher = regex .matcher (text );
84
88
return matcher .matches ();
85
89
}
86
90
87
- private static DotenvEntry matchEntry (String regex , String text ) {
88
- Pattern pattern = Pattern .compile (regex );
89
- Matcher matcher = pattern .matcher (text );
91
+ private static DotenvEntry matchEntry (Pattern regex , String text ) {
92
+ Matcher matcher = regex .matcher (text );
90
93
boolean result = matcher .matches ();
91
94
if (!result || matcher .groupCount () < 3 ) return null ;
92
95
return new DotenvEntry (matcher .group (1 ), matcher .group (3 ));
93
96
}
94
97
95
- private boolean isBlank (String s ) {
98
+ private static boolean isBlank (String s ) {
96
99
return s == null || s .trim ().isEmpty ();
97
100
}
98
101
}
0 commit comments