001package ezvcard;
002
003import java.util.Arrays;
004
005import com.github.mangstadt.vinnie.SyntaxStyle;
006
007/*
008 * Copyright 2011 George El-Haddad. All rights reserved.
009 * 
010 * Redistribution and use in source and binary forms, with or without modification, are
011 * permitted provided that the following conditions are met:
012 * 
013 *    1. Redistributions of source code must retain the above copyright notice, this list of
014 *       conditions and the following disclaimer.
015 * 
016 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
017 *       of conditions and the following disclaimer in the documentation and/or other materials
018 *       provided with the distribution.
019 * 
020 * THIS SOFTWARE IS PROVIDED BY GEORGE EL-HADDAD ''AS IS'' AND ANY EXPRESS OR IMPLIED
021 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
022 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GEORGE EL-HADDAD OR
023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
027 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
028 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 * 
030 * The views and conclusions contained in the software and documentation are those of the
031 * authors and should not be interpreted as representing official policies, either expressed
032 * or implied, of George El-Haddad.
033 */
034
035/*
036 Copyright (c) 2012-2026, Michael Angstadt
037 All rights reserved.
038
039 Redistribution and use in source and binary forms, with or without
040 modification, are permitted provided that the following conditions are met: 
041
042 1. Redistributions of source code must retain the above copyright notice, this
043 list of conditions and the following disclaimer. 
044 2. Redistributions in binary form must reproduce the above copyright notice,
045 this list of conditions and the following disclaimer in the documentation
046 and/or other materials provided with the distribution. 
047
048 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
049 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
050 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
051 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
052 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
053 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
054 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
055 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
056 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
057 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
058
059 The views and conclusions contained in the software and documentation are those
060 of the authors and should not be interpreted as representing official policies, 
061 either expressed or implied, of the FreeBSD Project.
062 */
063
064/**
065 * Contains the vCard versions that this library supports.
066 * @author George El-Haddad
067 * @author Michael Angstadt
068 */
069public enum VCardVersion {
070        /**
071         * The original vCard specification.
072         * @see <a href="http://www.imc.org/pdi/vcard-21.rtf">2.1 specs</a>
073         */
074        V2_1("2.1", SyntaxStyle.OLD, null),
075
076        /**
077         * Version 3.0.
078         * @see <a href="http://tools.ietf.org/html/rfc2426">RFC 2426</a>
079         */
080        V3_0("3.0", SyntaxStyle.NEW, null),
081
082        /**
083         * The lastest specification.
084         * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350</a>
085         */
086        V4_0("4.0", SyntaxStyle.NEW, "urn:ietf:params:xml:ns:vcard-4.0");
087
088        private final String version;
089        private final SyntaxStyle syntaxStyle;
090        private final String xmlNamespace;
091
092        /**
093         * @param version the text representation
094         * @param xmlNamespace the xCard namespace or null if not defined
095         */
096        VCardVersion(String version, SyntaxStyle syntaxStyle, String xmlNamespace) {
097                this.version = version;
098                this.syntaxStyle = syntaxStyle;
099                this.xmlNamespace = xmlNamespace;
100        }
101
102        /**
103         * Gets the text representation of this version.
104         * @return the text representation
105         */
106        public String getVersion() {
107                return version;
108        }
109
110        /**
111         * Gets the syntax style used by this version when writing to a plain-text
112         * data stream.
113         * @return the syntax style
114         */
115        public SyntaxStyle getSyntaxStyle() {
116                return syntaxStyle;
117        }
118
119        /**
120         * Gets the XML namespace used by this version when writing to an xCard.
121         * @return the XML namespace or null if this version does not support xCard
122         */
123        public String getXmlNamespace() {
124                return xmlNamespace;
125        }
126
127        /**
128         * Gets a {@link VCardVersion} instance based on the given text
129         * representation.
130         * @param value the text representation
131         * @return the object or null if not found
132         */
133        public static VCardVersion valueOfByStr(String value) {
134                //@formatter:off
135                return Arrays.stream(VCardVersion.values())
136                        .filter(version -> version.getVersion().equals(value))
137                .findFirst().orElse(null);
138                //@formatter:on
139        }
140
141        /**
142         * Gets a {@link VCardVersion} instance based on the given XML namespace.
143         * @param ns the XML namespace
144         * @return the object or null if not found
145         */
146        public static VCardVersion valueOfByXmlNamespace(String ns) {
147                //@formatter:off
148                return Arrays.stream(VCardVersion.values())
149                        .filter(version -> version.getXmlNamespace() != null)
150                        .filter(version -> version.getXmlNamespace().equals(ns))
151                .findFirst().orElse(null);
152                //@formatter:on
153        }
154
155        @Override
156        public String toString() {
157                return version;
158        }
159}