001package ezvcard.io; 002 003import ezvcard.Messages; 004 005/* 006Copyright (c) 2012-2026, Michael Angstadt 007All rights reserved. 008 009Redistribution and use in source and binary forms, with or without 010modification, are permitted provided that the following conditions are met: 011 0121. Redistributions of source code must retain the above copyright notice, this 013list of conditions and the following disclaimer. 0142. Redistributions in binary form must reproduce the above copyright notice, 015this list of conditions and the following disclaimer in the documentation 016and/or other materials provided with the distribution. 017 018THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 019ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 020WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 021DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 022ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 023(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 024LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 025ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 026(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 027SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 028 029The views and conclusions contained in the software and documentation are those 030of the authors and should not be interpreted as representing official policies, 031either expressed or implied, of the FreeBSD Project. 032*/ 033 034/** 035 * Represents a warning that occurred during the parsing of a vCard. 036 * @author Michael Angstadt 037 */ 038public class ParseWarning { 039 private final Integer code; 040 private final Integer lineNumber; 041 private final String propertyName; 042 private final String message; 043 044 private ParseWarning(Integer lineNumber, String propertyName, Integer code, String message) { 045 this.lineNumber = lineNumber; 046 this.propertyName = propertyName; 047 this.code = code; 048 this.message = message; 049 } 050 051 /** 052 * Gets the warning code. 053 * @return the warning code or null if no code was specified 054 */ 055 public Integer getCode() { 056 return code; 057 } 058 059 /** 060 * Gets the line number the warning occurred on. 061 * @return the line number or null if not applicable 062 */ 063 public Integer getLineNumber() { 064 return lineNumber; 065 } 066 067 /** 068 * Gets the warning message 069 * @return the warning message 070 */ 071 public String getMessage() { 072 return message; 073 } 074 075 /** 076 * Gets the name of the property that the warning occurred on. 077 * @return the property name (e.g. "DTSTART") or null if not applicable 078 */ 079 public String getPropertyName() { 080 return propertyName; 081 } 082 083 @Override 084 public String toString() { 085 String message = this.message; 086 if (code != null) { 087 message = "(" + code + ") " + message; 088 } 089 090 if (lineNumber == null && propertyName == null) { 091 return message; 092 } 093 094 int code; 095 if (lineNumber == null && propertyName != null) { 096 code = 35; 097 } else if (lineNumber != null && propertyName == null) { 098 code = 37; 099 } else { 100 code = 36; 101 } 102 103 return Messages.INSTANCE.getParseMessage(code, lineNumber, propertyName, message); 104 } 105 106 /** 107 * Constructs instances of the {@link ParseWarning} class. 108 * @author Michael Angstadt 109 */ 110 public static class Builder { 111 private Integer lineNumber; 112 private Integer code; 113 private String propertyName; 114 private String message; 115 116 /** 117 * Creates an empty builder. 118 */ 119 public Builder() { 120 //empty 121 } 122 123 /** 124 * Initializes the builder with data from the parse context. 125 * @param context the parse context 126 */ 127 public Builder(ParseContext context) { 128 lineNumber(context.getLineNumber()); 129 propertyName(context.getPropertyName()); 130 } 131 132 /** 133 * Sets the name of the property that the warning occurred on. 134 * @param propertyName the property name (e.g. "DTSTART") or null if not 135 * applicable 136 * @return this 137 */ 138 public Builder propertyName(String propertyName) { 139 this.propertyName = propertyName; 140 return this; 141 } 142 143 /** 144 * Sets the line number that the warning occurred on. 145 * @param lineNumber the line number or null if not applicable 146 * @return this 147 */ 148 public Builder lineNumber(Integer lineNumber) { 149 this.lineNumber = lineNumber; 150 return this; 151 } 152 153 /** 154 * Sets the warning message. 155 * @param code the message code 156 * @param args the message arguments 157 * @return this 158 */ 159 public Builder message(int code, Object... args) { 160 this.code = code; 161 message = Messages.INSTANCE.getParseMessage(code, args); 162 return this; 163 } 164 165 /** 166 * Sets the warning message. 167 * @param message the warning message 168 * @return this 169 */ 170 public Builder message(String message) { 171 code = null; 172 this.message = message; 173 return this; 174 } 175 176 /** 177 * Sets the warning message, based on the contents of a 178 * {@link CannotParseException}. 179 * @param exception the exception 180 * @return this 181 */ 182 public Builder message(CannotParseException exception) { 183 return message(exception.getCode(), exception.getArgs()); 184 } 185 186 /** 187 * Builds the {@link ParseWarning} object. 188 * @return the {@link ParseWarning} object 189 */ 190 public ParseWarning build() { 191 return new ParseWarning(lineNumber, propertyName, code, message); 192 } 193 } 194}