View Javadoc

1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.acegisecurity.acls;
16  
17  import org.springframework.util.Assert;
18  
19  
20  /**
21   * Utility methods for displaying ACL information.
22   *
23   * @author Ben Alex
24   * @version $Id: AclFormattingUtils.java 1784 2007-02-24 21:00:24Z luke_t $
25   */
26  public final class AclFormattingUtils {
27      //~ Constructors ===================================================================================================
28  
29      private AclFormattingUtils() {
30      }
31  
32      //~ Methods ========================================================================================================
33  
34      public static String demergePatterns(String original, String removeBits) {
35          Assert.notNull(original, "Original string required");
36          Assert.notNull(removeBits, "Bits To Remove string required");
37          Assert.isTrue(original.length() == removeBits.length(),
38              "Original and Bits To Remove strings must be identical length");
39  
40          char[] replacement = new char[original.length()];
41  
42          for (int i = 0; i < original.length(); i++) {
43              if (removeBits.charAt(i) == Permission.RESERVED_OFF) {
44                  replacement[i] = original.charAt(i);
45              } else {
46                  replacement[i] = Permission.RESERVED_OFF;
47              }
48          }
49  
50          return new String(replacement);
51      }
52  
53      public static String mergePatterns(String original, String extraBits) {
54          Assert.notNull(original, "Original string required");
55          Assert.notNull(extraBits, "Extra Bits string required");
56          Assert.isTrue(original.length() == extraBits.length(),
57              "Original and Extra Bits strings must be identical length");
58  
59          char[] replacement = new char[extraBits.length()];
60  
61          for (int i = 0; i < extraBits.length(); i++) {
62              if (extraBits.charAt(i) == Permission.RESERVED_OFF) {
63                  replacement[i] = original.charAt(i);
64              } else {
65                  replacement[i] = extraBits.charAt(i);
66              }
67          }
68  
69          return new String(replacement);
70      }
71  
72      private static String printBinary(int i, char on, char off) {
73          String s = Integer.toString(i, 2);
74          String pattern = Permission.THIRTY_TWO_RESERVED_OFF;
75          String temp2 = pattern.substring(0, pattern.length() - s.length()) + s;
76  
77          return temp2.replace('0', off).replace('1', on);
78      }
79  
80      /**
81       * Returns a representation of the active bits in the presented mask, with each active bit being denoted by
82       * character "".<p>Inactive bits will be denoted by character {@link Permission#RESERVED_OFF}.</p>
83       *
84       * @param i the integer bit mask to print the active bits for
85       *
86       * @return a 32-character representation of the bit mask
87       */
88      public static String printBinary(int i) {
89          return AclFormattingUtils.printBinary(i, '*', Permission.RESERVED_OFF);
90      }
91  
92      /**
93       * Returns a representation of the active bits in the presented mask, with each active bit being denoted by
94       * the passed character.<p>Inactive bits will be denoted by character {@link Permission#RESERVED_OFF}.</p>
95       *
96       * @param mask the integer bit mask to print the active bits for
97       * @param code the character to print when an active bit is detected
98       *
99       * @return a 32-character representation of the bit mask
100      */
101     public static String printBinary(int mask, char code) {
102         Assert.doesNotContain(new Character(code).toString(), new Character(Permission.RESERVED_ON).toString(),
103             Permission.RESERVED_ON + " is a reserved character code");
104         Assert.doesNotContain(new Character(code).toString(), new Character(Permission.RESERVED_OFF).toString(),
105             Permission.RESERVED_OFF + " is a reserved character code");
106 
107         return AclFormattingUtils.printBinary(mask, Permission.RESERVED_ON, Permission.RESERVED_OFF)
108                                  .replace(Permission.RESERVED_ON, code);
109     }
110 }