Skip to content

Commit

Permalink
Use java 17 features
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Reinhart <patrick@reini.net>
  • Loading branch information
reinhapa committed Jun 10, 2024
1 parent 273c46e commit 42116b1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 85 deletions.
24 changes: 8 additions & 16 deletions src/main/java/org/xmldb/api/security/AclEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,13 @@ public Builder setPermissions(String modeStr) {
for (int index = 0; index < 3; index++) {
char chr = modeStr.charAt(index);
switch (chr) {
case 'r':
set.add(AclEntryPermission.READ);
break;
case 'w':
set.add(AclEntryPermission.WRITE);
break;
case 'x':
set.add(AclEntryPermission.EXECUTE);
break;
case '-':
break;
default:
throw new IllegalArgumentException(
"Unknown char '" + chr + "' in mode string '" + modeStr + "'");
case 'r' -> set.add(AclEntryPermission.READ);
case 'w' -> set.add(AclEntryPermission.WRITE);
case 'x' -> set.add(AclEntryPermission.EXECUTE);
case '-' -> {
}
default -> throw new IllegalArgumentException(
"Unknown char '" + chr + "' in mode string '" + modeStr + "'");
}
}
this.permissions = set;
Expand Down Expand Up @@ -330,8 +323,7 @@ public Set<AclEntryFlag> flags() {
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof AclEntry) {
AclEntry other = (AclEntry) obj;
if (obj instanceof AclEntry other) {
return principal.equals(other.principal) && type.equals(other.type)
&& permissions.equals(other.permissions) && flags.equals(other.flags);
}
Expand Down
139 changes: 70 additions & 69 deletions src/main/java/org/xmldb/api/security/Permissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,85 +199,86 @@ private static void setUnixSymbolicMode(final EnumSet<Permission> permissions,

for (final char c : whoose) {
switch (c) {
case ALL_CHAR:
if (clause.indexOf('+') > -1 || clause.indexOf('=') > -1) {
if (perms.containsKey(PermType.READ)) {
permissions.add(OWNER_READ);
permissions.add(GROUP_READ);
permissions.add(OTHERS_READ);
}
if (perms.containsKey(PermType.WRITE)) {
permissions.add(OWNER_WRITE);
permissions.add(GROUP_WRITE);
permissions.add(OTHERS_WRITE);
}
if (perms.containsKey(PermType.EXECUTE)) {
permissions.add(OWNER_EXECUTE);
permissions.add(GROUP_EXECUTE);
permissions.add(OTHERS_EXECUTE);
}
if (perms.containsKey(PermType.SETID)) {
permissions.add(SET_UID);
permissions.add(SET_GID);
}
if (perms.containsKey(PermType.STICKY)) {
permissions.add(STICKY_BIT);
}
}
break;
case USER_CHAR:
if (clause.indexOf('+') > -1 || clause.indexOf('=') > -1) {
setPermissions(permissions, perms, OWNER_READ, OWNER_WRITE, OWNER_EXECUTE);
if (perms.containsKey(PermType.SETID)) {
permissions.add(SET_UID);
}
}
break;
case GROUP_CHAR:
if (clause.indexOf('+') > -1 || clause.indexOf('=') > -1) {
setPermissions(permissions, perms, GROUP_READ, GROUP_WRITE, GROUP_EXECUTE);
if (perms.containsKey(PermType.SETID)) {
permissions.add(SET_GID);
}
}
break;
case OTHER_CHAR:
if (clause.indexOf('+') > -1 || clause.indexOf('=') > -1) {
setPermissions(permissions, perms, OTHERS_READ, OTHERS_WRITE, OTHERS_EXECUTE);
if (perms.containsKey(PermType.STICKY)) {
permissions.add(STICKY_BIT);
}
}
break;
default:
throw new IllegalArgumentException("Unrecognised mode char '" + c + "'");
case ALL_CHAR -> handleAllChar(permissions, clause, perms);
case USER_CHAR -> handleUserChar(permissions, clause, perms);
case GROUP_CHAR -> handleGroupChar(permissions, clause, perms);
case OTHER_CHAR -> handleOtherChar(permissions, clause, perms);
default -> throw new IllegalArgumentException("Unrecognised mode char '" + c + "'");
}
}
perms.clear();
}
}

private static void handleOtherChar(EnumSet<Permission> permissions, String clause,
EnumMap<PermType, Boolean> perms) {
if (clause.indexOf('+') > -1 || clause.indexOf('=') > -1) {
setPermissions(permissions, perms, OTHERS_READ, OTHERS_WRITE, OTHERS_EXECUTE);
if (perms.containsKey(PermType.STICKY)) {
permissions.add(STICKY_BIT);
}
}
}

private static void handleGroupChar(EnumSet<Permission> permissions, String clause,
EnumMap<PermType, Boolean> perms) {
if (clause.indexOf('+') > -1 || clause.indexOf('=') > -1) {
setPermissions(permissions, perms, GROUP_READ, GROUP_WRITE, GROUP_EXECUTE);
if (perms.containsKey(PermType.SETID)) {
permissions.add(SET_GID);
}
}
}

private static void handleUserChar(EnumSet<Permission> permissions, String clause,
EnumMap<PermType, Boolean> perms) {
if (clause.indexOf('+') > -1 || clause.indexOf('=') > -1) {
setPermissions(permissions, perms, OWNER_READ, OWNER_WRITE, OWNER_EXECUTE);
if (perms.containsKey(PermType.SETID)) {
permissions.add(SET_UID);
}
}
return;
}

private static void handleAllChar(EnumSet<Permission> permissions, String clause,
EnumMap<PermType, Boolean> perms) {
if (clause.indexOf('+') > -1 || clause.indexOf('=') > -1) {
if (perms.containsKey(PermType.READ)) {
permissions.add(OWNER_READ);
permissions.add(GROUP_READ);
permissions.add(OTHERS_READ);
}
if (perms.containsKey(PermType.WRITE)) {
permissions.add(OWNER_WRITE);
permissions.add(GROUP_WRITE);
permissions.add(OTHERS_WRITE);
}
if (perms.containsKey(PermType.EXECUTE)) {
permissions.add(OWNER_EXECUTE);
permissions.add(GROUP_EXECUTE);
permissions.add(OTHERS_EXECUTE);
}
if (perms.containsKey(PermType.SETID)) {
permissions.add(SET_UID);
permissions.add(SET_GID);
}
if (perms.containsKey(PermType.STICKY)) {
permissions.add(STICKY_BIT);
}
}
}

private static void parseOperation(final String[] whoPerm,
final EnumMap<PermType, Boolean> perms) {
for (final char c : whoPerm[1].toCharArray()) {
switch (c) {
case READ_CHAR:
perms.put(PermType.READ, TRUE);
break;
case WRITE_CHAR:
perms.put(PermType.WRITE, TRUE);
break;
case EXECUTE_CHAR:
perms.put(PermType.EXECUTE, TRUE);
break;
case SETID_CHAR:
perms.put(PermType.SETID, TRUE);
break;
case STICKY_CHAR:
perms.put(PermType.STICKY, TRUE);
break;
default:
throw new IllegalArgumentException("Unrecognised mode char '" + c + "'");
case READ_CHAR -> perms.put(PermType.READ, TRUE);
case WRITE_CHAR -> perms.put(PermType.WRITE, TRUE);
case EXECUTE_CHAR -> perms.put(PermType.EXECUTE, TRUE);
case SETID_CHAR -> perms.put(PermType.SETID, TRUE);
case STICKY_CHAR -> perms.put(PermType.STICKY, TRUE);
default -> throw new IllegalArgumentException("Unrecognised mode char '" + c + "'");
}
}
}
Expand Down

0 comments on commit 42116b1

Please sign in to comment.