1 26 27 28 package org.objectweb.mobilitools.smi.api; 29 30 31 import java.io.Serializable ; 32 33 43 public class Location implements Serializable 44 { 45 static String regionPrefix = System.getProperty(Constants.regionPrefixProp, Constants.regionPrefixDefault); 46 static String agencyPrefix = System.getProperty(Constants.agencyPrefixProp, Constants.agencyPrefixDefault); 47 String my_region; 48 String my_agency; 49 String my_location; 50 51 52 55 public Location(String region, String agency) 56 { 57 my_region = region; 58 my_agency = agency; 59 my_location = 60 Constants.COSNAMINGURI + 61 regionPrefix + 62 "/" + 63 region + 64 "/" + 65 agencyPrefix + 66 "/" + 67 agency; 68 } 69 70 71 79 public Location(String location) 80 throws BadOperation 81 { 82 if (location == null) 83 { 84 throw new BadOperation(BadOperation.REJECTED, location + " is not a location string."); 85 } 86 my_location = location; 87 if (! location.startsWith(Constants.COSNAMINGURI)) 88 { 89 throw new BadOperation(BadOperation.REJECTED, location + " is not a location string."); 90 } 91 location = location.substring(Constants.COSNAMINGURI.length()); 92 if (! location.startsWith(regionPrefix + "/")) 93 { 94 throw new BadOperation(BadOperation.REJECTED, location + " is not a location string."); 95 } 96 if (regionPrefix.length() > 0) 97 { 98 location = location.substring(regionPrefix.length()+1); 99 } 100 int beginAgency = location.lastIndexOf("/"); 101 if (beginAgency == -1 || beginAgency == location.length()-1) 102 { 103 throw new BadOperation(BadOperation.REJECTED, location + " is not a location string."); 104 } 105 my_agency = location.substring(beginAgency + 1); 106 try 107 { 108 if (agencyPrefix.length() > 0) 109 { 110 my_region = location.substring(0, beginAgency - agencyPrefix.length() - 1); 111 } 112 else 113 { 114 my_region = location.substring(0, beginAgency); 115 } 116 } 117 catch (IndexOutOfBoundsException e) 118 { 119 throw new BadOperation(BadOperation.REJECTED, location + " is not a location string."); 120 } 121 } 122 123 124 127 public String getRegion() 128 { 129 return my_region; 130 } 131 132 133 136 public String getAgency() 137 { 138 return my_agency; 139 } 140 141 142 public String toString() 143 { 144 return my_location; 145 } 146 147 148 public boolean equals(Object other) 149 { 150 if (other instanceof Location) 151 { 152 return 153 my_region.equals(((Location)other).getRegion()) && 154 my_agency.equals(((Location)other).getAgency()); 155 } 156 else 157 { 158 return false; 159 } 160 } 161 162 163 166 public String getCosNamingName() 167 { 168 return my_location.substring(Constants.COSNAMINGURI.length()); 169 } 170 } 171 | Popular Tags |