1 32 33 package com.knowgate.hipergate; 34 35 import java.util.ArrayList ; 36 37 import com.knowgate.debug.DebugFile; 38 import com.knowgate.misc.Gadgets; 39 40 45 public class MenuOption { 46 47 private String name; 48 private String mask; 49 private String title; 50 private String href; 51 private String forbid; 52 private ArrayList suboptions; 53 54 56 59 public MenuOption() { 60 mask = "0"; 61 forbid = href = title = name = null; 62 suboptions = new ArrayList (); 63 } 64 65 67 73 public MenuOption getSubOption(int nIndex) throws ArrayIndexOutOfBoundsException { 74 return (MenuOption) suboptions.get(nIndex); 75 } 76 77 79 85 public MenuOption getSubOption(String sName) throws ArrayIndexOutOfBoundsException { 86 final int nOptCount = countSubOptions(); 87 for (int o=0;o<nOptCount;o++) { 88 if (((MenuOption)suboptions.get(o)).getName().equalsIgnoreCase(sName)) 89 return (MenuOption) suboptions.get(o); 90 } throw new ArrayIndexOutOfBoundsException (sName + " menu option not found"); 92 } 93 94 96 100 public int countSubOptions() { 101 return suboptions.size(); 102 } 103 104 106 110 public String getName() { 111 return name; 112 } 113 114 116 123 public Object getMask() throws NumberFormatException { 124 if (null==mask) 125 return null; 126 else if (mask.length()==0) 127 return null; 128 else if (mask.equalsIgnoreCase("admin")) 129 return "admin"; 130 else if (mask.equalsIgnoreCase("owner")) 131 return "owner"; 132 else if (mask.indexOf(",")>0) { 133 String [] aMask = Gadgets.split(mask,','); 134 ArrayList oMask = new ArrayList (aMask.length); 135 for (int m=0; m<aMask.length; m++) 136 oMask.add(new Integer (aMask[m])); 137 return oMask; 138 } else 139 return new Integer (mask); 140 } 142 144 152 public int getBitMask() { 153 int iMask; 154 if (null==mask) 155 return 0; 156 else if (mask.equalsIgnoreCase("admin") || mask.equalsIgnoreCase("owner")) 157 return -1; 158 else if (mask.indexOf(",")>0) { 159 String [] aMask = Gadgets.split(mask,','); 160 iMask = 0; 161 for (int m=0; m<aMask.length; m++) 162 iMask |= 1<<Integer.parseInt(aMask[m]); 163 return iMask; 164 } else { 165 try { 166 iMask=1<<Integer.parseInt(mask); 167 } 168 catch (NumberFormatException ignore) { 169 if (DebugFile.trace) DebugFile.writeln("MenuOption.getBitMask() NumberFormatException "+mask); 170 iMask=0; 171 } 172 return iMask; 173 } 174 } 176 178 208 public boolean hasAccess(int iAppMask, boolean bAdmin, boolean bOwner) { 209 if (DebugFile.trace) DebugFile.writeln("Begin MenuOption.hasAccess("+ 210 String.valueOf(iAppMask)+","+ 211 String.valueOf(bAdmin)+","+ 212 String.valueOf(bOwner)+")"); 213 final int iBitMask = getBitMask(); 214 boolean bRetVal; 215 if (0==iBitMask) { 216 bRetVal = true; 217 } 218 else if (-1==iBitMask) { 219 if (DebugFile.trace) DebugFile.writeln(" mask="+mask); 220 bRetVal = (mask.equalsIgnoreCase("admin") && bAdmin) || (mask.equalsIgnoreCase("owner") && bOwner); 221 } else { 222 if (DebugFile.trace) DebugFile.writeln(" bitmask="+String.valueOf(iBitMask)); 223 bRetVal = ((iBitMask&iAppMask)!=0); 224 } 225 if (DebugFile.trace) DebugFile.writeln("End MenuOption.hasAccess() : "+String.valueOf(bRetVal)); 226 return bRetVal; 227 } 229 231 235 public String getTitle() { 236 if (null!=title) 237 return title; 238 else 239 return ""; 240 } 241 242 244 248 public String getHRef() { 249 if (null!=href) 250 return href; 251 else 252 return ""; 253 } 254 255 257 263 public String getForbid() { 264 if (null!=forbid) 265 return forbid; 266 else 267 return ""; 268 } 269 } 270 | Popular Tags |