@@ -36,48 +36,87 @@ public final class JdbcUrlAntFormatter implements Provider<String> {
36
36
private final List <KeyResolver > resolvers = new ArrayList <KeyResolver >();
37
37
38
38
/**
39
- * Instantiates a new jdbc url ant formatter .
39
+ * Constructs a new JdbcUrlAntFormatter based on the provided pattern .
40
40
*
41
41
* @param pattern
42
- * the pattern
42
+ * the pattern for URL formatting
43
43
*/
44
44
public JdbcUrlAntFormatter (final String pattern ) {
45
- int prev = 0 ;
46
- int pos ;
47
- while ((pos = pattern .indexOf (VAR_BEGIN , prev )) >= 0 ) {
48
- if (pos > 0 ) {
49
- appenders .add (Providers .of (pattern .substring (prev , pos )));
50
- }
51
- if (pos == pattern .length () - 1 ) {
52
- appenders .add (Providers .of (VAR_BEGIN ));
53
- prev = pos + 1 ;
54
- } else if (pattern .charAt (pos + 1 ) != '{' ) {
55
- if (pattern .charAt (pos + 1 ) == '$' ) {
56
- appenders .add (Providers .of (VAR_BEGIN ));
57
- prev = pos + 2 ;
58
- } else {
59
- appenders .add (Providers .of (pattern .substring (pos , pos + 2 )));
60
- prev = pos + 2 ;
61
- }
62
- } else {
63
- int endName = pattern .indexOf ('}' , pos );
64
- if (endName < 0 ) {
65
- throw new IllegalArgumentException ("Syntax error in property: " + pattern );
66
- }
67
- StringTokenizer keyTokenizer = new StringTokenizer (pattern .substring (pos + 2 , endName ), PIPE_SEPARATOR );
68
- String key = keyTokenizer .nextToken ();
69
- String defaultValue = null ;
70
- if (keyTokenizer .hasMoreTokens ()) {
71
- defaultValue = keyTokenizer .nextToken ();
72
- }
73
- KeyResolver resolver = new KeyResolver (key , defaultValue );
74
- appenders .add (resolver );
75
- resolvers .add (resolver );
76
- prev = endName + 1 ;
77
- }
45
+ initializeAppender (pattern );
46
+ }
47
+
48
+ /**
49
+ * Initializes the appenders based on the given pattern.
50
+ *
51
+ * @param pattern
52
+ * the pattern for URL formatting
53
+ */
54
+ private void initializeAppender (String pattern ) {
55
+ int previousIndex = 0 ;
56
+ int currentIndex ;
57
+ while ((currentIndex = pattern .indexOf (VAR_BEGIN , previousIndex )) >= 0 ) {
58
+ processPatternSubstring (pattern , previousIndex , currentIndex );
59
+ previousIndex = updatePrevPosition (pattern , currentIndex );
60
+ }
61
+ appendRemainingPatternSubstring (pattern , previousIndex );
62
+ }
63
+
64
+ // Processes the substring of the pattern based on the currentIndex and previousIndex
65
+ private void processPatternSubstring (String pattern , int previousIndex , int currentIndex ) {
66
+ if (currentIndex > 0 ) {
67
+ appenders .add (Providers .of (pattern .substring (previousIndex , currentIndex )));
68
+ }
69
+
70
+ if (currentIndex == pattern .length () - 1 ) {
71
+ appenders .add (Providers .of (VAR_BEGIN ));
72
+ } else if (pattern .charAt (currentIndex + 1 ) != '{' ) {
73
+ handleNonCurlyBracePattern (pattern , currentIndex );
74
+ } else {
75
+ handleCurlyBracePattern (pattern , currentIndex );
76
+ }
77
+ }
78
+
79
+ // Handles patterns without curly braces
80
+ private void handleNonCurlyBracePattern (String pattern , int currentIndex ) {
81
+ if (pattern .charAt (currentIndex + 1 ) == '$' ) {
82
+ appenders .add (Providers .of (VAR_BEGIN ));
83
+ } else {
84
+ appenders .add (Providers .of (pattern .substring (currentIndex , currentIndex + 2 )));
85
+ }
86
+ }
87
+
88
+ // Handles patterns with curly braces
89
+ private void handleCurlyBracePattern (String pattern , int currentIndex ) {
90
+ int endName = pattern .indexOf ('}' , currentIndex );
91
+ if (endName < 0 ) {
92
+ throw new IllegalArgumentException ("Syntax error in property: " + pattern );
93
+ }
94
+ processKeyResolver (pattern , currentIndex , endName );
95
+ }
96
+
97
+ // Method to append KeyResolver based on the variable
98
+ private void processKeyResolver (String pattern , int startPos , int endPos ) {
99
+ StringTokenizer keyTokenizer = new StringTokenizer (pattern .substring (startPos + 2 , endPos ), PIPE_SEPARATOR );
100
+ String key = keyTokenizer .nextToken ();
101
+ String defaultValue = keyTokenizer .hasMoreTokens () ? keyTokenizer .nextToken () : null ;
102
+ KeyResolver resolver = new KeyResolver (key , defaultValue );
103
+ appenders .add (resolver );
104
+ resolvers .add (resolver );
105
+ }
106
+
107
+ // Updates the previous position based on the current index and pattern
108
+ private int updatePrevPosition (String pattern , int currentIndex ) {
109
+ if (pattern .charAt (currentIndex + 1 ) == '{' ) {
110
+ return pattern .indexOf ('}' , currentIndex ) + 1 ;
111
+ } else {
112
+ return currentIndex + (pattern .charAt (currentIndex + 1 ) == '$' ? 2 : 1 );
78
113
}
79
- if (prev < pattern .length ()) {
80
- appenders .add (Providers .of (pattern .substring (prev )));
114
+ }
115
+
116
+ // Appends the remaining substring of the pattern
117
+ private void appendRemainingPatternSubstring (String pattern , int previousIndex ) {
118
+ if (previousIndex < pattern .length ()) {
119
+ appenders .add (Providers .of (pattern .substring (previousIndex )));
81
120
}
82
121
}
83
122
0 commit comments