View Javadoc
1 package jsdsi; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import jsdsi.sexp.Sexp; 8 import jsdsi.sexp.SexpList; 9 import jsdsi.sexp.SexpParseException; 10 import jsdsi.sexp.SexpUtil; 11 12 /*** 13 * A SDSI name: a principal and a sequence of strings. If the sequence 14 * contains just one string, this is a local name; otherwise this is an 15 * extended name. 16 * 17 * @author Sameer Ajmani 18 * @author Sean Radford 19 * @version $Revision: 1.3.2.1 $ $Date: 2005/11/08 03:12:52 $ 20 */ 21 public class Name extends Obj implements Subject { 22 23 private static final long serialVersionUID = 3430696653946782484L; 24 25 /*** 26 * Principal of this Name. 27 */ 28 private transient final Principal issuer; 29 30 /*** 31 * The name string(s) for this name (more than one string for an extended 32 * name). 33 */ 34 private transient final String[] names; 35 36 /*** 37 * Creates a new <code>Name</code> for a given principal and an array 38 * of name-strings. 39 * 40 * @param i principal to create the <code>Name</code> for. 41 * @param n array of string-names for this <code>Name</code> (if the 42 * length of <code>n</code> is greater than 1, an extended name 43 * will be created). 44 */ 45 public Name(Principal i, String[] n) { 46 assert(i != null) : "null issuer"; 47 assert(n != null) : "null names"; 48 assert(n.length > 0) : "empty names"; 49 issuer = i; 50 names = n; 51 } 52 53 /*** 54 * Creates a new local name from a given principal and name-string. 55 * 56 * @param i principal to create the <code>Name</code> for. 57 * @param n name-string to create the <code>Name</code> for. 58 */ 59 public Name(Principal i, String n) { 60 this(i, new String[] { n }); // XXX assert n != null 61 } 62 63 /*** 64 * Returns the principal of the <code>Name</code>. 65 * 66 * @return the principal of this <code>Name</code>. 67 */ 68 public Principal getIssuer() { 69 return issuer; 70 } 71 72 /*** 73 * Returns an array of name-strings of this <code>Name</code> (the 74 * array has a length of one in the case of a local name, and 75 * greater then 1 for extended names). 76 * 77 * @return an array of strings containing the name-string(s) of this 78 * name (more than one string for an extended string). 79 */ 80 public String[] getNames() { 81 return names; 82 } 83 84 /*** 85 * Checks if a given <code>Name</code> has the same issuer 86 * (principal) as this <code>Name</code> and if the name-strings are 87 * equal to this name's name-strings (a smaller number of 88 * name-strings are okay). 89 * 90 * @param n <code>Name</code> to compare with this <code>Name</code>. 91 * @return <code>false</code> if <code>n</code> has another issuer, 92 * a greater number of name-strings or if one name-string is not 93 * equal if a name-string from this <code>Name</code>, returns 94 * <code>true</code> otherwise. 95 */ 96 public boolean prefixOf(Name n) { 97 if (!issuer.equals(n.issuer)) { 98 return false; 99 } 100 if (names.length > n.names.length) { 101 return false; 102 } 103 for (int i = 0; i < names.length; i++) { 104 if (!names[i].equals(n.names[i])) { 105 return false; 106 } 107 } 108 return true; 109 } 110 111 /*** 112 * Creates a new <code>Name</code> from this <code>Name</code> using only 113 * the first <code>i</code> name-strings. 114 * 115 * @param i number of name-strings of this <code>Name</code> to use for 116 * creating a new <code>Name</code>. 117 * @return the new <code>Name</code> for the same principal as this 118 * <code>Name</code> using only the first <code>i</code> 119 * name-strings. 120 */ 121 public Name prefix(int i) { 122 assert(i <= names.length) : "prefix too long"; 123 String[] ns = new String[i]; 124 System.arraycopy(names, 0, ns, 0, i); 125 return new Name(issuer, ns); 126 } 127 128 /*** 129 * Returns the local name of this name, that is a <code>Name</code> 130 * for this <code>Name</code>s issuer with the first name-string of 131 * the list of names. 132 * 133 * @return the new local name for this <code>Name</code>. 134 */ 135 public Name prefix() { 136 return prefix(1); 137 } 138 139 /*** 140 * @see java.lang.Object#equals(Object) 141 */ 142 public boolean equals(Object o) { 143 if (o instanceof Name) { 144 Name n = (Name) o; 145 return issuer.equals(n.issuer) && Util.equals(names, n.names); 146 } 147 return false; 148 } 149 150 /*** 151 * @see java.lang.Object#hashCode() 152 */ 153 public int hashCode() { 154 return issuer.hashCode() ^ Util.hashCode(names); 155 } 156 157 public SexpList toSexp() { 158 // TODO: implement this method 159 return toSexp((Principal) null); 160 } 161 162 /*** 163 * Returns an <code>SexpList</code>-representation of the given 164 * <code>Principal</code>. 165 * 166 * @param iss the <code>Principal</code> to create an 167 * <code>SexpList</code> from. 168 * @return an <code>SexpList</code>-representation of <code>iss</code>. 169 */ 170 public SexpList toSexp(Principal iss) { 171 List l = new ArrayList(); 172 if (!getIssuer().samePrincipalAs(iss)) { 173 // make fully-qualified name 174 l.add(getIssuer().toSexp()); 175 } 176 for (int i = 0; i < names.length; i++) { 177 l.add(SexpUtil.toSexp(names[i])); 178 } 179 return SexpUtil.toSexp("name", l); 180 } 181 182 /*** 183 * Parses an <code>SexpList</code> to create a <code>Name</code> 184 * from it. 185 * 186 * @param l the <code>SexpList</code> that holds a <code>Name</code>. 187 * @return a <code>Name</code> created from the values in <code>l</code> 188 * @throws SexpParseException 189 */ 190 static Name parseName(SexpList l) throws SexpParseException { 191 return parseName(l, null); 192 } 193 194 /*** 195 * Parses a name from a given <code>SexpList</code> and 196 * <code>Principal</code>. 197 * 198 * @param l the <code>SexpList</code> to parse. 199 * @param issuerParam the <code>Principal</code> that is the issuer 200 * of the <code>Name</code>. 201 * @return the <code>Name</code> created from <code>l</code> and 202 * <code>issuerParm</code>. 203 * @throws SexpParseException 204 */ 205 static Name parseName(SexpList l, Principal issuerParam) 206 throws SexpParseException { 207 Iterator nbody = SexpUtil.getBody(l); 208 Sexp s = SexpUtil.getNext(nbody, "first name component"); 209 Principal issuer = null; 210 String[] names = null; 211 if (s instanceof SexpList) { 212 // name is fully-qualified 213 issuer = Principal.parsePrincipal(SexpUtil.getList(s)); 214 names = new String[l.size() - 2]; 215 for (int i = 0; i < names.length; i++) { 216 names[i] = SexpUtil.getNextString(nbody, "name string #" + i); 217 } 218 } else if (issuerParam != null) { 219 // name is relative; issuer provided as a parameter 220 issuer = issuerParam; 221 names = new String[l.size() - 1]; 222 names[0] = SexpUtil.getString(s); 223 for (int i = 1; i < names.length; i++) { 224 names[i] = SexpUtil.getNextString(nbody, "name string #" + i); 225 } 226 } else { 227 throw new SexpParseException("relative name used without issuer"); 228 } 229 SexpUtil.checkDone(nbody, "name"); // sanity check 230 return new Name(issuer, names); 231 } 232 233 // TODO: boolean sameNameAs(Name n) 234 // TODO: boolean sameNameAs(Principal i, String n) 235 }

This page was automatically generated by Maven