001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.acegisecurity.acls;
016:
017: import org.springframework.util.Assert;
018:
019: /**
020: * Utility methods for displaying ACL information.
021: *
022: * @author Ben Alex
023: * @version $Id: AclFormattingUtils.java 1784 2007-02-24 21:00:24Z luke_t $
024: */
025: public final class AclFormattingUtils {
026: //~ Constructors ===================================================================================================
027:
028: private AclFormattingUtils() {
029: }
030:
031: //~ Methods ========================================================================================================
032:
033: public static String demergePatterns(String original,
034: String removeBits) {
035: Assert.notNull(original, "Original string required");
036: Assert.notNull(removeBits, "Bits To Remove string required");
037: Assert
038: .isTrue(original.length() == removeBits.length(),
039: "Original and Bits To Remove strings must be identical length");
040:
041: char[] replacement = new char[original.length()];
042:
043: for (int i = 0; i < original.length(); i++) {
044: if (removeBits.charAt(i) == Permission.RESERVED_OFF) {
045: replacement[i] = original.charAt(i);
046: } else {
047: replacement[i] = Permission.RESERVED_OFF;
048: }
049: }
050:
051: return new String(replacement);
052: }
053:
054: public static String mergePatterns(String original, String extraBits) {
055: Assert.notNull(original, "Original string required");
056: Assert.notNull(extraBits, "Extra Bits string required");
057: Assert
058: .isTrue(original.length() == extraBits.length(),
059: "Original and Extra Bits strings must be identical length");
060:
061: char[] replacement = new char[extraBits.length()];
062:
063: for (int i = 0; i < extraBits.length(); i++) {
064: if (extraBits.charAt(i) == Permission.RESERVED_OFF) {
065: replacement[i] = original.charAt(i);
066: } else {
067: replacement[i] = extraBits.charAt(i);
068: }
069: }
070:
071: return new String(replacement);
072: }
073:
074: private static String printBinary(int i, char on, char off) {
075: String s = Integer.toString(i, 2);
076: String pattern = Permission.THIRTY_TWO_RESERVED_OFF;
077: String temp2 = pattern.substring(0, pattern.length()
078: - s.length())
079: + s;
080:
081: return temp2.replace('0', off).replace('1', on);
082: }
083:
084: /**
085: * Returns a representation of the active bits in the presented mask, with each active bit being denoted by
086: * character "".<p>Inactive bits will be denoted by character {@link Permission#RESERVED_OFF}.</p>
087: *
088: * @param i the integer bit mask to print the active bits for
089: *
090: * @return a 32-character representation of the bit mask
091: */
092: public static String printBinary(int i) {
093: return AclFormattingUtils.printBinary(i, '*',
094: Permission.RESERVED_OFF);
095: }
096:
097: /**
098: * Returns a representation of the active bits in the presented mask, with each active bit being denoted by
099: * the passed character.<p>Inactive bits will be denoted by character {@link Permission#RESERVED_OFF}.</p>
100: *
101: * @param mask the integer bit mask to print the active bits for
102: * @param code the character to print when an active bit is detected
103: *
104: * @return a 32-character representation of the bit mask
105: */
106: public static String printBinary(int mask, char code) {
107: Assert.doesNotContain(new Character(code).toString(),
108: new Character(Permission.RESERVED_ON).toString(),
109: Permission.RESERVED_ON
110: + " is a reserved character code");
111: Assert.doesNotContain(new Character(code).toString(),
112: new Character(Permission.RESERVED_OFF).toString(),
113: Permission.RESERVED_OFF
114: + " is a reserved character code");
115:
116: return AclFormattingUtils.printBinary(mask,
117: Permission.RESERVED_ON, Permission.RESERVED_OFF)
118: .replace(Permission.RESERVED_ON, code);
119: }
120: }
|