77import java .util .ArrayList ;
88import java .util .List ;
99import java .util .function .Function ;
10+ import java .util .function .Predicate ;
1011import java .util .regex .Matcher ;
1112import java .util .regex .Pattern ;
1213
1617 * (Internal) Parses .env file
1718 */
1819public 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+
1924 private final DotenvReader reader ;
2025 private final boolean throwIfMissing ;
2126 private final boolean throwIfMalformed ;
2227
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 );
2732
2833 /**
2934 * Creates a dotenv parser
@@ -46,11 +51,11 @@ public List<DotenvEntry> parse() throws DotenvException {
4651 List <DotenvEntry > entries = new ArrayList <>();
4752 for (String line : lines ()) {
4853 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 ;
5055
5156 DotenvEntry entry = parseLine .apply (l );
5257 if (entry == null ) {
53- if (throwIfMalformed ) throw new DotenvException ("Malformed entry " + l );
58+ if (throwIfMalformed ) throw new DotenvException ("Malformed entry " + l );
5459 continue ;
5560 }
5661 String key = entry .getKey ();
@@ -73,26 +78,24 @@ private List<String> lines() throws DotenvException {
7378
7479 private String normalizeValue (String value ) {
7580 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 )
7883 : tr ;
7984 }
8085
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 );
8488 return matcher .matches ();
8589 }
8690
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 );
9093 boolean result = matcher .matches ();
9194 if (!result || matcher .groupCount () < 3 ) return null ;
9295 return new DotenvEntry (matcher .group (1 ), matcher .group (3 ));
9396 }
9497
95- private boolean isBlank (String s ) {
98+ private static boolean isBlank (String s ) {
9699 return s == null || s .trim ().isEmpty ();
97100 }
98101}
0 commit comments