Debian Codegraph @

src/test/java/ezvcard/io/LuckyNumProperty.java

raw symbols SCIP
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 Copyright (c) 2012-2023, Michael Angstadt
19 All rights reserved.
20
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met:
23
24 1. Redistributions of source code must retain the above copyright notice, this
25 list of conditions and the following disclaimer.
26 2. Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 The views and conclusions contained in the software and documentation are those
42 of the authors and should not be interpreted as representing official policies,
43 either expressed or implied, of the FreeBSD Project.
44 */
45
46 /**
47 * A property class class used for testing whose scribe DOES contain XML
48 * marshalling methods and a QName (unlike {@link AgeProperty}, which does not).
49 * @author Michael Angstadt
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 }