| 1 |
package ezvcard.property; |
| 2 |
|
| 3 |
import java.text.NumberFormat; |
| 4 |
import java.util.LinkedHashMap; |
| 5 |
import java.util.List; |
| 6 |
import java.util.Locale; |
| 7 |
import java.util.Map; |
| 8 |
|
| 9 |
import ezvcard.SupportedVersions; |
| 10 |
import ezvcard.VCard; |
| 11 |
import ezvcard.VCardVersion; |
| 12 |
import ezvcard.ValidationWarnings; |
| 13 |
import ezvcard.ValidationWarning; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@SupportedVersions({ VCardVersion.V2_1, VCardVersion.V3_0 }) |
| 100 |
public class Agent extends VCardProperty { |
| 101 |
private String url; |
| 102 |
private VCard vcard; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public Agent() { |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public Agent(String url) { |
| 116 |
setUrl(url); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public Agent(VCard vcard) { |
| 124 |
setVCard(vcard); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public Agent(Agent original) { |
| 132 |
super(original); |
| 133 |
url = original.url; |
| 134 |
vcard = (original.vcard == null) ? null : new VCard(original.vcard); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public String getUrl() { |
| 142 |
return url; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public void setUrl(String url) { |
| 150 |
this.url = url; |
| 151 |
vcard = null; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public VCard getVCard() { |
| 159 |
return vcard; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public void setVCard(VCard vcard) { |
| 167 |
this.vcard = vcard; |
| 168 |
url = null; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) { |
| 173 |
if (url == null && this.vcard == null) { |
| 174 |
warnings.add(new ValidationWarning(8)); |
| 175 |
} |
| 176 |
|
| 177 |
if (this.vcard != null) { |
| 178 |
NumberFormat nf = NumberFormat.getIntegerInstance(Locale.ROOT); |
| 179 |
nf.setMinimumIntegerDigits(2); |
| 180 |
|
| 181 |
ValidationWarnings validationWarnings = this.vcard.validate(version); |
| 182 |
for (Map.Entry<VCardProperty, List<ValidationWarning>> entry : validationWarnings) { |
| 183 |
VCardProperty property = entry.getKey(); |
| 184 |
List<ValidationWarning> propViolations = entry.getValue(); |
| 185 |
|
| 186 |
for (ValidationWarning propViolation : propViolations) { |
| 187 |
String className = (property == null) ? "" : property.getClass().getSimpleName(); |
| 188 |
|
| 189 |
int code = propViolation.getCode(); |
| 190 |
String codeStr = (code >= 0) ? "W" + nf.format(code) : ""; |
| 191 |
String message = propViolation.getMessage(); |
| 192 |
warnings.add(new ValidationWarning(10, className, codeStr, message)); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
protected Map<String, Object> toStringValues() { |
| 200 |
Map<String, Object> values = new LinkedHashMap<>(); |
| 201 |
values.put("url", url); |
| 202 |
values.put("vcard", vcard); |
| 203 |
return values; |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public Agent copy() { |
| 208 |
return new Agent(this); |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public int hashCode() { |
| 213 |
final int prime = 31; |
| 214 |
int result = super.hashCode(); |
| 215 |
result = prime * result + ((url == null) ? 0 : url.hashCode()); |
| 216 |
result = prime * result + ((vcard == null) ? 0 : vcard.hashCode()); |
| 217 |
return result; |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public boolean equals(Object obj) { |
| 222 |
if (this == obj) return true; |
| 223 |
if (!super.equals(obj)) return false; |
| 224 |
Agent other = (Agent) obj; |
| 225 |
if (url == null) { |
| 226 |
if (other.url != null) return false; |
| 227 |
} else if (!url.equals(other.url)) return false; |
| 228 |
if (vcard == null) { |
| 229 |
if (other.vcard != null) return false; |
| 230 |
} else if (!vcard.equals(other.vcard)) return false; |
| 231 |
return true; |
| 232 |
} |
| 233 |
} |