| 1 |
package ezvcard.io; |
| 2 |
|
| 3 |
import javax.xml.namespace.QName; |
| 4 |
|
| 5 |
import org.w3c.dom.Element; |
| 6 |
import org.w3c.dom.NodeList; |
| 7 |
|
| 8 |
import ezvcard.VCardDataType; |
| 9 |
import ezvcard.VCardVersion; |
| 10 |
import ezvcard.io.html.HCardElement; |
| 11 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 12 |
import ezvcard.io.text.WriteContext; |
| 13 |
import ezvcard.io.xml.XCardElement; |
| 14 |
import ezvcard.parameter.VCardParameters; |
| 15 |
import ezvcard.property.VCardProperty; |
| 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 |
public class LuckyNumProperty extends VCardProperty { |
| 52 |
public int luckyNum; |
| 53 |
|
| 54 |
public LuckyNumProperty(int luckyNum) { |
| 55 |
this.luckyNum = luckyNum; |
| 56 |
} |
| 57 |
|
| 58 |
public static class LuckyNumScribe extends VCardPropertyScribe<LuckyNumProperty> { |
| 59 |
public LuckyNumScribe() { |
| 60 |
super(LuckyNumProperty.class, "X-LUCKY-NUM", new QName("http://luckynum.com", "lucky-num")); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 65 |
return VCardDataType.INTEGER; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
protected String _writeText(LuckyNumProperty property, WriteContext context) { |
| 70 |
return property.luckyNum + ""; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
protected LuckyNumProperty _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 75 |
int luckyNum = Integer.parseInt(value); |
| 76 |
return new LuckyNumProperty(luckyNum); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
protected void _writeXml(LuckyNumProperty property, XCardElement parent) { |
| 81 |
int luckyNum = property.luckyNum; |
| 82 |
parent.element().setTextContent(luckyNum + ""); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
protected LuckyNumProperty _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) { |
| 87 |
NodeList nodeList = element.element().getElementsByTagNameNS(qname.getNamespaceURI(), "num"); |
| 88 |
if (nodeList.getLength() == 0) { |
| 89 |
return new LuckyNumProperty(0); |
| 90 |
} |
| 91 |
|
| 92 |
Element num = (Element) nodeList.item(0); |
| 93 |
int luckyNum = Integer.parseInt(num.getTextContent()); |
| 94 |
|
| 95 |
return new LuckyNumProperty(luckyNum); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
protected LuckyNumProperty _parseHtml(HCardElement element, ParseContext context) { |
| 100 |
return new LuckyNumProperty(Integer.parseInt(element.value())); |
| 101 |
} |
| 102 |
} |
| 103 |
} |