Debian Codegraph @

src/main/java/ezvcard/property/RawProperty.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import java.util.LinkedHashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import com.github.mangstadt.vinnie.SyntaxStyle;
8 import com.github.mangstadt.vinnie.validate.AllowedCharacters;
9 import com.github.mangstadt.vinnie.validate.VObjectValidator;
10
11 import ezvcard.VCard;
12 import ezvcard.VCardDataType;
13 import ezvcard.VCardVersion;
14 import ezvcard.ValidationWarning;
15
16 /*
17 Copyright (c) 2012-2023, Michael Angstadt
18 All rights reserved.
19
20 Redistribution and use in source and binary forms, with or without
21 modification, are permitted provided that the following conditions are met:
22
23 1. Redistributions of source code must retain the above copyright notice, this
24 list of conditions and the following disclaimer.
25 2. Redistributions in binary form must reproduce the above copyright notice,
26 this list of conditions and the following disclaimer in the documentation
27 and/or other materials provided with the distribution.
28
29 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
30 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
33 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
40 The views and conclusions contained in the software and documentation are those
41 of the authors and should not be interpreted as representing official policies,
42 either expressed or implied, of the FreeBSD Project.
43 */
44
45 /**
46 * Holds the property value as-is. No escaping or unescaping is done on the
47 * value.
48 * @author Michael Angstadt
49 */
50 public class RawProperty extends TextProperty {
51 private String propertyName;
52 private VCardDataType dataType;
53
54 /**
55 * Creates a raw property.
56 * @param propertyName the property name (e.g. "X-GENDER")
57 * @param value the property value
58 */
59 public RawProperty(String propertyName, String value) {
60 this(propertyName, value, null);
61 }
62
63 /**
64 * Creates a raw property.
65 * @param propertyName the property name (e.g. "X-GENDER")
66 * @param value the property value
67 * @param dataType the value's data type
68 */
69 public RawProperty(String propertyName, String value, VCardDataType dataType) {
70 super(value);
71 this.propertyName = propertyName;
72 this.dataType = dataType;
73 }
74
75 /**
76 * Copy constructor.
77 * @param original the property to make a copy of
78 */
79 public RawProperty(RawProperty original) {
80 super(original);
81 propertyName = original.propertyName;
82 dataType = original.dataType;
83 }
84
85 /**
86 * Gets the name of the property.
87 * @return the property name
88 */
89 public String getPropertyName() {
90 return propertyName;
91 }
92
93 /**
94 * Sets the name of the property.
95 * @param propertyName the property name
96 */
97 public void setPropertyName(String propertyName) {
98 this.propertyName = propertyName;
99 }
100
101 /**
102 * Gets the data type of the property's value.
103 * @return the data type or null if unknown
104 */
105 public VCardDataType getDataType() {
106 return dataType;
107 }
108
109 /**
110 * Sets the data type of the property's value.
111 * @param dataType the data type or null if unknown
112 */
113 public void setDataType(VCardDataType dataType) {
114 this.dataType = dataType;
115 }
116
117 @Override
118 protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
119 SyntaxStyle syntax = version.getSyntaxStyle();
120 AllowedCharacters allowed = VObjectValidator.allowedCharactersParameterName(syntax, true);
121 if (!allowed.check(propertyName)) {
122 if (syntax == SyntaxStyle.OLD) {
123 AllowedCharacters notAllowed = allowed.flip();
124 warnings.add(new ValidationWarning(33, propertyName, notAllowed.toString(true)));
125 } else {
126 warnings.add(new ValidationWarning(24, propertyName));
127 }
128 }
129 }
130
131 @Override
132 protected Map<String, Object> toStringValues() {
133 Map<String, Object> values = new LinkedHashMap<>();
134 values.put("propertyName", propertyName);
135 values.put("dataType", dataType);
136 values.put("value", value);
137 return values;
138 }
139
140 @Override
141 public RawProperty copy() {
142 return new RawProperty(this);
143 }
144
145 @Override
146 public int hashCode() {
147 final int prime = 31;
148 int result = super.hashCode();
149 result = prime * result + ((dataType == null) ? 0 : dataType.hashCode());
150 result = prime * result + ((propertyName == null) ? 0 : propertyName.toLowerCase().hashCode());
151 return result;
152 }
153
154 @Override
155 public boolean equals(Object obj) {
156 if (this == obj) return true;
157 if (!super.equals(obj)) return false;
158 RawProperty other = (RawProperty) obj;
159 if (dataType == null) {
160 if (other.dataType != null) return false;
161 } else if (!dataType.equals(other.dataType)) return false;
162 if (propertyName == null) {
163 if (other.propertyName != null) return false;
164 } else if (!propertyName.equalsIgnoreCase(other.propertyName)) return false;
165 return true;
166 }
167 }