Debian Codegraph @

src/main/java/ezvcard/ValidationWarnings.java

raw symbols SCIP
1 package ezvcard;
2
3 import java.text.NumberFormat;
4 import java.util.ArrayList;
5 import java.util.IdentityHashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Locale;
9 import java.util.Map;
10 import java.util.Map.Entry;
11
12 import ezvcard.property.VCardProperty;
13 import ezvcard.util.ListMultimap;
14 import ezvcard.util.StringUtils;
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
41 /**
42 * <p>
43 * Holds the validation warnings of a vCard.
44 * </p>
45 * <p>
46 * <b>Examples:</b>
47 * </p>
48 *
49 * <pre class="brush:java">
50 * //validate a vCard object according to the rules of a specific version
51 * ValidationWarnings warnings = vcard.validate(VCardVersion.V3_0);
52 *
53 * //print all warnings to a string:
54 * System.out.println(warnings.toString());
55 * //sample output:
56 * //W01: A FormattedName property is required for vCard versions 3.0 and 4.0.
57 * //[Gender] | W02: Property is not supported in this vCard version. Supported versions are: [4.0]
58 *
59 * //iterate over the warnings
60 * for (Map.Entry&lt;VCardProperty, List&lt;Warning&gt;&gt; entry : warnings) {
61 * //the property that caused the warning(s)
62 * VCardProperty property = entry.getKey();
63 *
64 * //the list of warnings that belong to this property
65 * List&lt;Warning&gt; propWarnings = entry.getValue();
66 *
67 * if (property == null) {
68 * //it's a warning about the vCard as a whole
69 * }
70 *
71 * //each warning message has a numeric code
72 * //this allows you to programmatically respond to specific warning messages
73 * List&lt;Warning&gt; propWarnings = entry.getValue();
74 * for (Warning w : propWarnings) {
75 * System.out.println("Code: " + w.getCode());
76 * System.out.printkn("Message: " + w.getMessage());
77 * }
78 * }
79 *
80 * //you can also get the warnings of specific property classes
81 * List&lt;Warnings&gt; telWarnings = warnings.getByProperty(Telephone.class);
82 * </pre>
83 *
84 * @author Michael Angstadt
85 * @see VCard#validate
86 */
87 public class ValidationWarnings implements Iterable<Map.Entry<VCardProperty, List<ValidationWarning>>> {
88 private final ListMultimap<VCardProperty, ValidationWarning> warnings = new ListMultimap<>(new IdentityHashMap<>());
89
90 /**
91 * Adds a validation warning.
92 * @param property the property that caused the warning
93 * @param warning the warning
94 */
95 public void add(VCardProperty property, ValidationWarning warning) {
96 warnings.put(property, warning);
97 }
98
99 /**
100 * Adds a property's validation warnings.
101 * @param property the property that caused the warnings
102 * @param warnings the warnings
103 */
104 public void add(VCardProperty property, List<ValidationWarning> warnings) {
105 this.warnings.putAll(property, warnings);
106 }
107
108 /**
109 * Gets all of the validation warnings.
110 * @return the validation warnings
111 */
112 public ListMultimap<VCardProperty, ValidationWarning> getWarnings() {
113 return warnings;
114 }
115
116 /**
117 * Determines whether or not the warnings list is empty.
118 * @return true if there are no warnings, false if there is at least one
119 * warning
120 */
121 public boolean isEmpty() {
122 return warnings.isEmpty();
123 }
124
125 /**
126 * Gets all validation warnings that belong to a property of a specific
127 * class.
128 * @param propertyClass the property class (e.g. {@code Telephone.class}) or
129 * null to get the warnings that apply to the vCard as a whole
130 * @return the validation warnings
131 */
132 public List<ValidationWarning> getByProperty(Class<? extends VCardProperty> propertyClass) {
133 List<ValidationWarning> propWarnings = new ArrayList<>();
134 for (Map.Entry<VCardProperty, List<ValidationWarning>> entry : warnings) {
135 VCardProperty property = entry.getKey();
136
137 if ((property == null && propertyClass == null) || (property != null && propertyClass == property.getClass())) {
138 List<ValidationWarning> propViolations = entry.getValue();
139 propWarnings.addAll(propViolations);
140 }
141 }
142 return propWarnings;
143 }
144
145 /**
146 * <p>
147 * Outputs all validation warnings as a newline-delimited string. For
148 * example:
149 * </p>
150 *
151 * <pre>
152 * W01: A FormattedName property is required for vCard versions 3.0 and 4.0.
153 * [Gender] | W02: Property is not supported in this vCard version. Supported versions are: [4.0]
154 * </pre>
155 */
156 @Override
157 public String toString() {
158 NumberFormat nf = NumberFormat.getIntegerInstance(Locale.ROOT);
159 nf.setMinimumIntegerDigits(2);
160
161 StringBuilder sb = new StringBuilder();
162 for (Map.Entry<VCardProperty, List<ValidationWarning>> entry : warnings) {
163 VCardProperty property = entry.getKey();
164 List<ValidationWarning> propViolations = entry.getValue();
165
166 for (ValidationWarning propViolation : propViolations) {
167 if (property != null) {
168 sb.append('[');
169 sb.append(property.getClass().getSimpleName());
170 sb.append("] | ");
171 }
172
173 Integer code = propViolation.getCode();
174 if (code != null) {
175 sb.append('W');
176 sb.append(nf.format(code));
177 sb.append(": ");
178 }
179
180 sb.append(propViolation.getMessage());
181 sb.append(StringUtils.NEWLINE);
182 }
183 }
184
185 return sb.toString();
186 }
187
188 public Iterator<Entry<VCardProperty, List<ValidationWarning>>> iterator() {
189 return warnings.iterator();
190 }
191 }