| 1 | package ezvcard.io; |
| 2 | |
| 3 | import ezvcard.Messages; |
| 4 | |
| 5 | /* |
| 6 | Copyright (c) 2012-2023, Michael Angstadt |
| 7 | All rights reserved. |
| 8 | |
| 9 | Redistribution and use in source and binary forms, with or without |
| 10 | modification, are permitted provided that the following conditions are met: |
| 11 | |
| 12 | 1. Redistributions of source code must retain the above copyright notice, this |
| 13 | list of conditions and the following disclaimer. |
| 14 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | this list of conditions and the following disclaimer in the documentation |
| 16 | and/or other materials provided with the distribution. |
| 17 | |
| 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | The views and conclusions contained in the software and documentation are those |
| 30 | of the authors and should not be interpreted as representing official policies, |
| 31 | either expressed or implied, of the FreeBSD Project. |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Represents a warning that occurred during the parsing of a vCard. |
| 36 | * @author Michael Angstadt |
| 37 | */ |
| 38 | public class ParseWarning { |
| 39 | private final Integer code, lineNumber; |
| 40 | private final String propertyName, message; |
| 41 | |
| 42 | private ParseWarning(Integer lineNumber, String propertyName, Integer code, String message) { |
| 43 | this.lineNumber = lineNumber; |
| 44 | this.propertyName = propertyName; |
| 45 | this.code = code; |
| 46 | this.message = message; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets the warning code. |
| 51 | * @return the warning code or null if no code was specified |
| 52 | */ |
| 53 | public Integer getCode() { |
| 54 | return code; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Gets the line number the warning occurred on. |
| 59 | * @return the line number or null if not applicable |
| 60 | */ |
| 61 | public Integer getLineNumber() { |
| 62 | return lineNumber; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Gets the warning message |
| 67 | * @return the warning message |
| 68 | */ |
| 69 | public String getMessage() { |
| 70 | return message; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Gets the name of the property that the warning occurred on. |
| 75 | * @return the property name (e.g. "DTSTART") or null if not applicable |
| 76 | */ |
| 77 | public String getPropertyName() { |
| 78 | return propertyName; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public String toString() { |
| 83 | String message = this.message; |
| 84 | if (code != null) { |
| 85 | message = "(" + code + ") " + message; |
| 86 | } |
| 87 | |
| 88 | if (lineNumber == null && propertyName == null) { |
| 89 | return message; |
| 90 | } |
| 91 | |
| 92 | int code; |
| 93 | if (lineNumber == null && propertyName != null) { |
| 94 | code = 35; |
| 95 | } else if (lineNumber != null && propertyName == null) { |
| 96 | code = 37; |
| 97 | } else { |
| 98 | code = 36; |
| 99 | } |
| 100 | |
| 101 | return Messages.INSTANCE.getParseMessage(code, lineNumber, propertyName, message); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Constructs instances of the {@link ParseWarning} class. |
| 106 | * @author Michael Angstadt |
| 107 | */ |
| 108 | public static class Builder { |
| 109 | private Integer lineNumber, code; |
| 110 | private String propertyName, message; |
| 111 | |
| 112 | /** |
| 113 | * Creates an empty builder. |
| 114 | */ |
| 115 | public Builder() { |
| 116 | //empty |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Initializes the builder with data from the parse context. |
| 121 | * @param context the parse context |
| 122 | */ |
| 123 | public Builder(ParseContext context) { |
| 124 | lineNumber(context.getLineNumber()); |
| 125 | propertyName(context.getPropertyName()); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Sets the name of the property that the warning occurred on. |
| 130 | * @param propertyName the property name (e.g. "DTSTART") or null if not |
| 131 | * applicable |
| 132 | * @return this |
| 133 | */ |
| 134 | public Builder propertyName(String propertyName) { |
| 135 | this.propertyName = propertyName; |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets the line number that the warning occurred on. |
| 141 | * @param lineNumber the line number or null if not applicable |
| 142 | * @return this |
| 143 | */ |
| 144 | public Builder lineNumber(Integer lineNumber) { |
| 145 | this.lineNumber = lineNumber; |
| 146 | return this; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Sets the warning message. |
| 151 | * @param code the message code |
| 152 | * @param args the message arguments |
| 153 | * @return this |
| 154 | */ |
| 155 | public Builder message(int code, Object... args) { |
| 156 | this.code = code; |
| 157 | message = Messages.INSTANCE.getParseMessage(code, args); |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Sets the warning message. |
| 163 | * @param message the warning message |
| 164 | * @return this |
| 165 | */ |
| 166 | public Builder message(String message) { |
| 167 | code = null; |
| 168 | this.message = message; |
| 169 | return this; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Sets the warning message, based on the contents of a |
| 174 | * {@link CannotParseException}. |
| 175 | * @param exception the exception |
| 176 | * @return this |
| 177 | */ |
| 178 | public Builder message(CannotParseException exception) { |
| 179 | return message(exception.getCode(), exception.getArgs()); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Builds the {@link ParseWarning} object. |
| 184 | * @return the {@link ParseWarning} object |
| 185 | */ |
| 186 | public ParseWarning build() { |
| 187 | return new ParseWarning(lineNumber, propertyName, code, message); |
| 188 | } |
| 189 | } |
| 190 | } |