001package ezvcard.property;
002
003import java.util.LinkedHashMap;
004import java.util.List;
005import java.util.Map;
006import java.util.Objects;
007
008import com.github.mangstadt.vinnie.SyntaxStyle;
009import com.github.mangstadt.vinnie.validate.AllowedCharacters;
010import com.github.mangstadt.vinnie.validate.VObjectValidator;
011
012import ezvcard.VCard;
013import ezvcard.VCardDataType;
014import ezvcard.VCardVersion;
015import ezvcard.ValidationWarning;
016import ezvcard.util.StringUtils;
017
018/*
019 Copyright (c) 2012-2026, Michael Angstadt
020 All rights reserved.
021
022 Redistribution and use in source and binary forms, with or without
023 modification, are permitted provided that the following conditions are met: 
024
025 1. Redistributions of source code must retain the above copyright notice, this
026 list of conditions and the following disclaimer. 
027 2. Redistributions in binary form must reproduce the above copyright notice,
028 this list of conditions and the following disclaimer in the documentation
029 and/or other materials provided with the distribution. 
030
031 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
032 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
033 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
034 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
035 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
036 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
037 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
038 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
039 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
040 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
041
042 The views and conclusions contained in the software and documentation are those
043 of the authors and should not be interpreted as representing official policies, 
044 either expressed or implied, of the FreeBSD Project.
045 */
046
047/**
048 * Holds the property value as-is. No escaping or unescaping is done on the
049 * value.
050 * @author Michael Angstadt
051 */
052public class RawProperty extends TextProperty {
053        private String propertyName;
054        private VCardDataType dataType;
055
056        /**
057         * Creates a raw property.
058         * @param propertyName the property name (e.g. "X-GENDER")
059         * @param value the property value
060         */
061        public RawProperty(String propertyName, String value) {
062                this(propertyName, value, null);
063        }
064
065        /**
066         * Creates a raw property.
067         * @param propertyName the property name (e.g. "X-GENDER")
068         * @param value the property value
069         * @param dataType the value's data type
070         */
071        public RawProperty(String propertyName, String value, VCardDataType dataType) {
072                super(value);
073                this.propertyName = propertyName;
074                this.dataType = dataType;
075        }
076
077        /**
078         * Copy constructor.
079         * @param original the property to make a copy of
080         */
081        public RawProperty(RawProperty original) {
082                super(original);
083                propertyName = original.propertyName;
084                dataType = original.dataType;
085        }
086
087        /**
088         * Gets the name of the property.
089         * @return the property name
090         */
091        public String getPropertyName() {
092                return propertyName;
093        }
094
095        /**
096         * Sets the name of the property.
097         * @param propertyName the property name
098         */
099        public void setPropertyName(String propertyName) {
100                this.propertyName = propertyName;
101        }
102
103        /**
104         * Gets the data type of the property's value.
105         * @return the data type or null if unknown
106         */
107        public VCardDataType getDataType() {
108                return dataType;
109        }
110
111        /**
112         * Sets the data type of the property's value.
113         * @param dataType the data type or null if unknown
114         */
115        public void setDataType(VCardDataType dataType) {
116                this.dataType = dataType;
117        }
118
119        @Override
120        protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
121                SyntaxStyle syntax = version.getSyntaxStyle();
122                AllowedCharacters allowed = VObjectValidator.allowedCharactersParameterName(syntax, true);
123                if (!allowed.check(propertyName)) {
124                        if (syntax == SyntaxStyle.OLD) {
125                                AllowedCharacters notAllowed = allowed.flip();
126                                warnings.add(new ValidationWarning(33, propertyName, notAllowed.toString(true)));
127                        } else {
128                                warnings.add(new ValidationWarning(24, propertyName));
129                        }
130                }
131        }
132
133        @Override
134        protected Map<String, Object> toStringValues() {
135                Map<String, Object> values = new LinkedHashMap<>();
136                values.put("propertyName", propertyName);
137                values.put("dataType", dataType);
138                values.put("value", value);
139                return values;
140        }
141
142        @Override
143        public RawProperty copy() {
144                return new RawProperty(this);
145        }
146
147        @Override
148        public int hashCode() {
149                final int prime = 31;
150                int result = super.hashCode();
151                result = prime * result + StringUtils.hashIgnoreCase(dataType, propertyName);
152                return result;
153        }
154
155        @Override
156        public boolean equals(Object obj) {
157                if (this == obj) return true;
158                if (!super.equals(obj)) return false;
159                if (getClass() != obj.getClass()) return false;
160                RawProperty other = (RawProperty) obj;
161                return Objects.equals(dataType, other.dataType) && StringUtils.equalsIgnoreCase(propertyName, other.propertyName);
162        }
163}