[919] | 1 | /**
|
---|
| 2 | * Permission.java
|
---|
| 3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
[109] | 19 | package org.expeditee.items;
|
---|
| 20 |
|
---|
[376] | 21 | public enum Permission {
|
---|
[109] | 22 | none, followLinks, copy, createFrames, full;
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
[376] | 25 | * Converts the given Expeditee permission code into a int corresponding to
|
---|
| 26 | * the constants defined in Item.
|
---|
[109] | 27 | *
|
---|
| 28 | * @param permissionCode
|
---|
[376] | 29 | * The Expeditee permission code to convert
|
---|
[109] | 30 | * @return The resulting int corresponding to one of the constants defined
|
---|
| 31 | * in Item
|
---|
| 32 | */
|
---|
[427] | 33 | public static Permission convertString(String permissionCode) {
|
---|
[109] | 34 | return getPermission(permissionCode, Permission.full);
|
---|
| 35 | }
|
---|
[376] | 36 |
|
---|
| 37 | public static Permission getPermission(String permissionCode,
|
---|
| 38 | Permission defaultPermission) {
|
---|
[109] | 39 | if (permissionCode == null)
|
---|
| 40 | return defaultPermission;
|
---|
[376] | 41 |
|
---|
[109] | 42 | permissionCode = permissionCode.trim().toLowerCase();
|
---|
| 43 | if (permissionCode.length() == 0)
|
---|
| 44 | return defaultPermission;
|
---|
[376] | 45 |
|
---|
[109] | 46 | // if it is a single char just match the first character
|
---|
[376] | 47 | try {
|
---|
[109] | 48 | return values()[Integer.parseInt(permissionCode)];
|
---|
| 49 | // Otherwise match the whole string
|
---|
[376] | 50 | } catch (Exception ex) {
|
---|
| 51 | try {
|
---|
| 52 | return valueOf(permissionCode);
|
---|
| 53 | } catch (Exception e) {
|
---|
[109] | 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | // default permission
|
---|
| 58 | return defaultPermission;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public int getCode() {
|
---|
| 62 | return ordinal();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public String toString() {
|
---|
| 66 | return this.name();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public static Permission min(Permission p1, Permission p2) {
|
---|
| 70 | return p1.ordinal() < p2.ordinal() ? p1 : p2;
|
---|
| 71 | }
|
---|
[376] | 72 |
|
---|
[115] | 73 | public static Permission max(Permission p1, Permission p2) {
|
---|
| 74 | return p1.ordinal() > p2.ordinal() ? p1 : p2;
|
---|
| 75 | }
|
---|
[109] | 76 | }
|
---|