16
16
import java .io .Reader ;
17
17
import java .io .StreamTokenizer ;
18
18
import java .util .Enumeration ;
19
+ import java .util .Optional ;
19
20
import java .util .Vector ;
20
21
21
22
public class PolicyParser {
22
23
23
- private final Vector <GrantNode > grantEntries = new Vector <>();
24
+ private final Vector <GrantEntry > grantEntries = new Vector <>();
24
25
private TokenStream tokenStream ;
25
26
26
27
public PolicyParser () {}
@@ -34,11 +35,7 @@ public void read(Reader policy) throws ParsingException, IOException {
34
35
35
36
while (!tokenStream .isEOF ()) {
36
37
if (peek ("grant" )) {
37
- GrantNode grantNode = parseGrantEntry ();
38
-
39
- if (grantNode != null ) {
40
- addGrantNode (grantNode );
41
- }
38
+ parseGrantEntry ().ifPresent (this ::addGrantEntry );
42
39
}
43
40
}
44
41
}
@@ -81,23 +78,24 @@ private String poll(String expected) throws IOException, ParsingException {
81
78
throw new ParsingException (token .line , expected , token .text );
82
79
}
83
80
84
- private GrantNode parseGrantEntry () throws ParsingException , IOException {
85
- GrantNode grantNode = new GrantNode ();
81
+ private Optional < GrantEntry > parseGrantEntry () throws ParsingException , IOException {
82
+ GrantEntry grantEntry = new GrantEntry ();
86
83
poll ("grant" );
87
84
88
85
while (!peek ("{" )) {
89
86
if (pollOnMatch ("Codebase" )) {
90
- if (grantNode .codeBase != null ) {
87
+ if (grantEntry .codeBase != null ) {
91
88
throw new ParsingException (tokenStream .line (), "Multiple Codebase expressions" );
92
89
}
93
90
94
91
String rawCodebase = poll (tokenStream .peek ().text );
95
92
try {
96
- grantNode .codeBase = PropertyExpander .expand (rawCodebase , true ).replace (File .separatorChar , '/' );
93
+ grantEntry .codeBase = PropertyExpander .expand (rawCodebase , true ).replace (File .separatorChar , '/' );
97
94
} catch (ExpandException e ) {
98
95
// skip this grant as expansion failed due to missing expansion property.
99
96
skipCurrentGrantBlock ();
100
- return null ;
97
+
98
+ return Optional .empty ();
101
99
}
102
100
pollOnMatch ("," );
103
101
} else {
@@ -109,8 +107,8 @@ private GrantNode parseGrantEntry() throws ParsingException, IOException {
109
107
110
108
while (!peek ("}" )) {
111
109
if (peek ("Permission" )) {
112
- PermissionNode permissionEntry = parsePermissionEntry ();
113
- grantNode .add (permissionEntry );
110
+ PermissionEntry permissionEntry = parsePermissionEntry ();
111
+ grantEntry .add (permissionEntry );
114
112
poll (";" );
115
113
} else {
116
114
throw new ParsingException (tokenStream .line (), "Expected permission entry" );
@@ -123,11 +121,11 @@ private GrantNode parseGrantEntry() throws ParsingException, IOException {
123
121
poll (";" );
124
122
}
125
123
126
- if (grantNode .codeBase != null ) {
127
- grantNode .codeBase = grantNode .codeBase .replace (File .separatorChar , '/' );
124
+ if (grantEntry .codeBase != null ) {
125
+ grantEntry .codeBase = grantEntry .codeBase .replace (File .separatorChar , '/' );
128
126
}
129
127
130
- return grantNode ;
128
+ return Optional . of ( grantEntry ) ;
131
129
}
132
130
133
131
private void skipCurrentGrantBlock () throws IOException , ParsingException {
@@ -161,8 +159,8 @@ private void skipCurrentGrantBlock() throws IOException, ParsingException {
161
159
}
162
160
}
163
161
164
- private PermissionNode parsePermissionEntry () throws ParsingException , IOException {
165
- PermissionNode permissionEntry = new PermissionNode ();
162
+ private PermissionEntry parsePermissionEntry () throws ParsingException , IOException {
163
+ PermissionEntry permissionEntry = new PermissionEntry ();
166
164
poll ("Permission" );
167
165
permissionEntry .permission = poll (tokenStream .peek ().text );
168
166
@@ -185,11 +183,11 @@ private boolean isQuotedToken(Token token) {
185
183
return token .type == '"' || token .type == '\'' ;
186
184
}
187
185
188
- public void addGrantNode ( GrantNode grantNode ) {
189
- grantEntries .addElement (grantNode );
186
+ public void addGrantEntry ( GrantEntry grantEntry ) {
187
+ grantEntries .addElement (grantEntry );
190
188
}
191
189
192
- public Enumeration <GrantNode > grantElements () {
190
+ public Enumeration <GrantEntry > grantElements () {
193
191
return grantEntries .elements ();
194
192
}
195
193
0 commit comments