| 1 |
package ezvcard.io.scribe; |
| 2 |
|
| 3 |
import java.util.Set; |
| 4 |
|
| 5 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues; |
| 6 |
|
| 7 |
import ezvcard.Messages; |
| 8 |
import ezvcard.VCard; |
| 9 |
import ezvcard.VCardDataType; |
| 10 |
import ezvcard.VCardVersion; |
| 11 |
import ezvcard.io.EmbeddedVCardException; |
| 12 |
import ezvcard.io.ParseContext; |
| 13 |
import ezvcard.io.SkipMeException; |
| 14 |
import ezvcard.io.html.HCardElement; |
| 15 |
import ezvcard.io.text.WriteContext; |
| 16 |
import ezvcard.parameter.VCardParameters; |
| 17 |
import ezvcard.property.Agent; |
| 18 |
import ezvcard.property.VCardProperty; |
| 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 |
public class AgentScribe extends VCardPropertyScribe<Agent> { |
| 50 |
public AgentScribe() { |
| 51 |
super(Agent.class, "AGENT"); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 56 |
return null; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
protected VCardDataType _dataType(Agent property, VCardVersion version) { |
| 61 |
if (property.getUrl() != null) { |
| 62 |
return (version == VCardVersion.V2_1) ? VCardDataType.URL : VCardDataType.URI; |
| 63 |
} |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
protected String _writeText(Agent property, WriteContext context) { |
| 69 |
String url = property.getUrl(); |
| 70 |
if (url != null) { |
| 71 |
return url; |
| 72 |
} |
| 73 |
|
| 74 |
VCard vcard = property.getVCard(); |
| 75 |
if (vcard != null) { |
| 76 |
throw new EmbeddedVCardException(vcard); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
throw new SkipMeException(Messages.INSTANCE.getValidationWarning(8)); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
protected Agent _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 85 |
Agent property = new Agent(); |
| 86 |
|
| 87 |
if (dataType == null) { |
| 88 |
throw new EmbeddedVCardException(new Injector(property)); |
| 89 |
} |
| 90 |
|
| 91 |
property.setUrl(VObjectPropertyValues.unescape(value)); |
| 92 |
return property; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
protected Agent _parseHtml(HCardElement element, ParseContext context) { |
| 97 |
Agent property = new Agent(); |
| 98 |
|
| 99 |
Set<String> classes = element.classNames(); |
| 100 |
if (classes.contains("vcard")) { |
| 101 |
throw new EmbeddedVCardException(new Injector(property)); |
| 102 |
} |
| 103 |
|
| 104 |
String url = element.absUrl("href"); |
| 105 |
if (url.isEmpty()) { |
| 106 |
url = element.value(); |
| 107 |
} |
| 108 |
property.setUrl(url); |
| 109 |
|
| 110 |
return property; |
| 111 |
} |
| 112 |
|
| 113 |
private static class Injector implements EmbeddedVCardException.InjectionCallback { |
| 114 |
private final Agent property; |
| 115 |
|
| 116 |
public Injector(Agent property) { |
| 117 |
this.property = property; |
| 118 |
} |
| 119 |
|
| 120 |
public void injectVCard(VCard vcard) { |
| 121 |
property.setVCard(vcard); |
| 122 |
} |
| 123 |
|
| 124 |
public VCardProperty getProperty() { |
| 125 |
return property; |
| 126 |
} |
| 127 |
} |
| 128 |
} |