| 1 |
package ezvcard.io.scribe; |
| 2 |
|
| 3 |
import java.net.URI; |
| 4 |
import java.net.URISyntaxException; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Collections; |
| 7 |
import java.util.List; |
| 8 |
import java.util.regex.Matcher; |
| 9 |
import java.util.regex.Pattern; |
| 10 |
|
| 11 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues; |
| 12 |
|
| 13 |
import ezvcard.VCard; |
| 14 |
import ezvcard.VCardDataType; |
| 15 |
import ezvcard.VCardVersion; |
| 16 |
import ezvcard.io.CannotParseException; |
| 17 |
import ezvcard.io.ParseContext; |
| 18 |
import ezvcard.io.html.HCardElement; |
| 19 |
import ezvcard.io.json.JCardValue; |
| 20 |
import ezvcard.io.text.WriteContext; |
| 21 |
import ezvcard.io.xml.XCardElement; |
| 22 |
import ezvcard.parameter.VCardParameters; |
| 23 |
import ezvcard.property.Impp; |
| 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 |
public class ImppScribe extends VCardPropertyScribe<Impp> { |
| 55 |
public static final String AIM = "aim"; |
| 56 |
public static final String ICQ = "icq"; |
| 57 |
public static final String IRC = "irc"; |
| 58 |
public static final String MSN = "msnim"; |
| 59 |
public static final String SIP = "sip"; |
| 60 |
public static final String SKYPE = "skype"; |
| 61 |
public static final String XMPP = "xmpp"; |
| 62 |
public static final String YAHOO = "ymsgr"; |
| 63 |
|
| 64 |
public ImppScribe() { |
| 65 |
super(Impp.class, "IMPP"); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
protected void _prepareParameters(Impp property, VCardParameters copy, VCardVersion version, VCard vcard) { |
| 70 |
handlePrefParam(property, copy, version, vcard); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 75 |
return VCardDataType.URI; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
protected String _writeText(Impp property, WriteContext context) { |
| 80 |
return write(property); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
protected Impp _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 85 |
value = VObjectPropertyValues.unescape(value); |
| 86 |
return parse(value); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
protected void _writeXml(Impp property, XCardElement parent) { |
| 91 |
parent.append(VCardDataType.URI, write(property)); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
protected Impp _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) { |
| 96 |
String value = element.first(VCardDataType.URI); |
| 97 |
if (value != null) { |
| 98 |
return parse(value); |
| 99 |
} |
| 100 |
|
| 101 |
throw missingXmlElements(VCardDataType.URI); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
protected Impp _parseHtml(HCardElement element, ParseContext context) { |
| 106 |
String href = element.attr("href"); |
| 107 |
if (href.isEmpty()) { |
| 108 |
href = element.value(); |
| 109 |
} |
| 110 |
|
| 111 |
try { |
| 112 |
URI uri = parseHtmlLink(href); |
| 113 |
if (uri == null) { |
| 114 |
throw new IllegalArgumentException(); |
| 115 |
} |
| 116 |
return new Impp(uri); |
| 117 |
} catch (IllegalArgumentException e) { |
| 118 |
throw new CannotParseException(14, href); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
protected JCardValue _writeJson(Impp property) { |
| 124 |
return JCardValue.single(write(property)); |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
protected Impp _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 129 |
return parse(value.asSingle()); |
| 130 |
} |
| 131 |
|
| 132 |
private String write(Impp property) { |
| 133 |
URI uri = property.getUri(); |
| 134 |
return (uri == null) ? "" : uri.toASCIIString(); |
| 135 |
} |
| 136 |
|
| 137 |
private Impp parse(String value) { |
| 138 |
if (value == null || value.isEmpty()) { |
| 139 |
return new Impp((URI) null); |
| 140 |
} |
| 141 |
|
| 142 |
try { |
| 143 |
return new Impp(value); |
| 144 |
} catch (IllegalArgumentException e) { |
| 145 |
throw new CannotParseException(15, value, e.getMessage()); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private static final List<HtmlLinkFormat> htmlLinkFormats; |
| 154 |
static { |
| 155 |
List<HtmlLinkFormat> list = new ArrayList<>(); |
| 156 |
|
| 157 |
|
| 158 |
list.add(new HtmlLinkFormat(AIM, "(goim|addbuddy)\\?.*?\\bscreenname=(.*?)(&|$)", 2, "goim?screenname=%s")); |
| 159 |
|
| 160 |
|
| 161 |
list.add(new HtmlLinkFormat(YAHOO, "(sendim|addfriend|sendfile|call)\\?(.*)", 2, "sendim?%s")); |
| 162 |
|
| 163 |
|
| 164 |
list.add(new HtmlLinkFormat(SKYPE, "(.*?)(\\?|$)", 1, "%s")); |
| 165 |
|
| 166 |
|
| 167 |
list.add(new HtmlLinkFormat(MSN, "(chat|add|voice|video)\\?contact=(.*?)(&|$)", 2, "chat?contact=%s")); |
| 168 |
|
| 169 |
|
| 170 |
list.add(new HtmlLinkFormat(XMPP, "(.*?)(\\?|$)", 1, "%s?message")); |
| 171 |
|
| 172 |
|
| 173 |
list.add(new HtmlLinkFormat(ICQ, "message\\?uin=(\\d+)", 1, "message?uin=%s")); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
list.add(new HtmlLinkFormat(SIP)); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
list.add(new HtmlLinkFormat(IRC)); |
| 182 |
|
| 183 |
htmlLinkFormats = Collections.unmodifiableList(list); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public URI parseHtmlLink(String linkUri) { |
| 192 |
for (HtmlLinkFormat format : htmlLinkFormats) { |
| 193 |
String handle = format.parseHandle(linkUri); |
| 194 |
if (handle == null) { |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
try { |
| 199 |
return new URI(format.getProtocol(), handle, null); |
| 200 |
} catch (URISyntaxException e) { |
| 201 |
throw new IllegalArgumentException(e); |
| 202 |
} |
| 203 |
} |
| 204 |
return null; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public String writeHtmlLink(Impp property) { |
| 214 |
URI uri = property.getUri(); |
| 215 |
if (uri == null) { |
| 216 |
return null; |
| 217 |
} |
| 218 |
|
| 219 |
String protocol = uri.getScheme(); |
| 220 |
String handle = uri.getSchemeSpecificPart(); |
| 221 |
|
| 222 |
for (HtmlLinkFormat format : htmlLinkFormats) { |
| 223 |
if (protocol.equals(format.getProtocol())) { |
| 224 |
return format.buildLink(handle); |
| 225 |
} |
| 226 |
} |
| 227 |
return uri.toASCIIString(); |
| 228 |
} |
| 229 |
|
| 230 |
private static class HtmlLinkFormat { |
| 231 |
private final Pattern parseRegex; |
| 232 |
private final String protocol; |
| 233 |
private final int handleGroup; |
| 234 |
private final String linkFormat; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public HtmlLinkFormat(String protocol) { |
| 240 |
this(protocol, "(.*)", 1, "%s"); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public HtmlLinkFormat(String protocol, String linkRegex, int handleGroup, String linkFormat) { |
| 251 |
this.parseRegex = Pattern.compile('^' + protocol + ':' + linkRegex, Pattern.CASE_INSENSITIVE); |
| 252 |
this.protocol = protocol; |
| 253 |
this.handleGroup = handleGroup; |
| 254 |
this.linkFormat = protocol + ':' + linkFormat; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public String parseHandle(String linkUri) { |
| 263 |
Matcher m = parseRegex.matcher(linkUri); |
| 264 |
return m.find() ? m.group(handleGroup) : null; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public String buildLink(String handle) { |
| 273 |
return String.format(linkFormat, handle); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public String getProtocol() { |
| 281 |
return protocol; |
| 282 |
} |
| 283 |
} |
| 284 |
} |