1616import java .io .Reader ;
1717import java .io .StreamTokenizer ;
1818import java .util .Enumeration ;
19+ import java .util .Optional ;
1920import java .util .Vector ;
2021
2122public class PolicyParser {
2223
23- private final Vector <GrantNode > grantEntries = new Vector <>();
24+ private final Vector <GrantEntry > grantEntries = new Vector <>();
2425 private TokenStream tokenStream ;
2526
2627 public PolicyParser () {}
@@ -34,11 +35,7 @@ public void read(Reader policy) throws ParsingException, IOException {
3435
3536 while (!tokenStream .isEOF ()) {
3637 if (peek ("grant" )) {
37- GrantNode grantNode = parseGrantEntry ();
38-
39- if (grantNode != null ) {
40- addGrantNode (grantNode );
41- }
38+ parseGrantEntry ().ifPresent (this ::addGrantEntry );
4239 }
4340 }
4441 }
@@ -81,23 +78,24 @@ private String poll(String expected) throws IOException, ParsingException {
8178 throw new ParsingException (token .line , expected , token .text );
8279 }
8380
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 ();
8683 poll ("grant" );
8784
8885 while (!peek ("{" )) {
8986 if (pollOnMatch ("Codebase" )) {
90- if (grantNode .codeBase != null ) {
87+ if (grantEntry .codeBase != null ) {
9188 throw new ParsingException (tokenStream .line (), "Multiple Codebase expressions" );
9289 }
9390
9491 String rawCodebase = poll (tokenStream .peek ().text );
9592 try {
96- grantNode .codeBase = PropertyExpander .expand (rawCodebase , true ).replace (File .separatorChar , '/' );
93+ grantEntry .codeBase = PropertyExpander .expand (rawCodebase , true ).replace (File .separatorChar , '/' );
9794 } catch (ExpandException e ) {
9895 // skip this grant as expansion failed due to missing expansion property.
9996 skipCurrentGrantBlock ();
100- return null ;
97+
98+ return Optional .empty ();
10199 }
102100 pollOnMatch ("," );
103101 } else {
@@ -109,8 +107,8 @@ private GrantNode parseGrantEntry() throws ParsingException, IOException {
109107
110108 while (!peek ("}" )) {
111109 if (peek ("Permission" )) {
112- PermissionNode permissionEntry = parsePermissionEntry ();
113- grantNode .add (permissionEntry );
110+ PermissionEntry permissionEntry = parsePermissionEntry ();
111+ grantEntry .add (permissionEntry );
114112 poll (";" );
115113 } else {
116114 throw new ParsingException (tokenStream .line (), "Expected permission entry" );
@@ -123,11 +121,11 @@ private GrantNode parseGrantEntry() throws ParsingException, IOException {
123121 poll (";" );
124122 }
125123
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 , '/' );
128126 }
129127
130- return grantNode ;
128+ return Optional . of ( grantEntry ) ;
131129 }
132130
133131 private void skipCurrentGrantBlock () throws IOException , ParsingException {
@@ -161,8 +159,8 @@ private void skipCurrentGrantBlock() throws IOException, ParsingException {
161159 }
162160 }
163161
164- private PermissionNode parsePermissionEntry () throws ParsingException , IOException {
165- PermissionNode permissionEntry = new PermissionNode ();
162+ private PermissionEntry parsePermissionEntry () throws ParsingException , IOException {
163+ PermissionEntry permissionEntry = new PermissionEntry ();
166164 poll ("Permission" );
167165 permissionEntry .permission = poll (tokenStream .peek ().text );
168166
@@ -185,11 +183,11 @@ private boolean isQuotedToken(Token token) {
185183 return token .type == '"' || token .type == '\'' ;
186184 }
187185
188- public void addGrantNode ( GrantNode grantNode ) {
189- grantEntries .addElement (grantNode );
186+ public void addGrantEntry ( GrantEntry grantEntry ) {
187+ grantEntries .addElement (grantEntry );
190188 }
191189
192- public Enumeration <GrantNode > grantElements () {
190+ public Enumeration <GrantEntry > grantElements () {
193191 return grantEntries .elements ();
194192 }
195193
0 commit comments