001package ezvcard.io;
002
003import ezvcard.Messages;
004
005/*
006Copyright (c) 2012-2023, 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, lineNumber;
040        private final String propertyName, message;
041
042        private ParseWarning(Integer lineNumber, String propertyName, Integer code, String message) {
043                this.lineNumber = lineNumber;
044                this.propertyName = propertyName;
045                this.code = code;
046                this.message = message;
047        }
048
049        /**
050         * Gets the warning code.
051         * @return the warning code or null if no code was specified
052         */
053        public Integer getCode() {
054                return code;
055        }
056
057        /**
058         * Gets the line number the warning occurred on.
059         * @return the line number or null if not applicable
060         */
061        public Integer getLineNumber() {
062                return lineNumber;
063        }
064
065        /**
066         * Gets the warning message
067         * @return the warning message
068         */
069        public String getMessage() {
070                return message;
071        }
072
073        /**
074         * Gets the name of the property that the warning occurred on.
075         * @return the property name (e.g. "DTSTART") or null if not applicable
076         */
077        public String getPropertyName() {
078                return propertyName;
079        }
080
081        @Override
082        public String toString() {
083                String message = this.message;
084                if (code != null) {
085                        message = "(" + code + ") " + message;
086                }
087
088                if (lineNumber == null && propertyName == null) {
089                        return message;
090                }
091
092                int code;
093                if (lineNumber == null && propertyName != null) {
094                        code = 35;
095                } else if (lineNumber != null && propertyName == null) {
096                        code = 37;
097                } else {
098                        code = 36;
099                }
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}