| 1 | package ezvcard; |
| 2 | |
| 3 | import com.github.mangstadt.vinnie.SyntaxStyle; |
| 4 | |
| 5 | /* |
| 6 | * Copyright 2011 George El-Haddad. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without modification, are |
| 9 | * permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright notice, this list of |
| 12 | * conditions and the following disclaimer. |
| 13 | * |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 15 | * of conditions and the following disclaimer in the documentation and/or other materials |
| 16 | * provided with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY GEORGE EL-HADDAD ''AS IS'' AND ANY EXPRESS OR IMPLIED |
| 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GEORGE EL-HADDAD OR |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 24 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 26 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | * The views and conclusions contained in the software and documentation are those of the |
| 29 | * authors and should not be interpreted as representing official policies, either expressed |
| 30 | * or implied, of George El-Haddad. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | Copyright (c) 2012-2023, Michael Angstadt |
| 35 | All rights reserved. |
| 36 | |
| 37 | Redistribution and use in source and binary forms, with or without |
| 38 | modification, are permitted provided that the following conditions are met: |
| 39 | |
| 40 | 1. Redistributions of source code must retain the above copyright notice, this |
| 41 | list of conditions and the following disclaimer. |
| 42 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 43 | this list of conditions and the following disclaimer in the documentation |
| 44 | and/or other materials provided with the distribution. |
| 45 | |
| 46 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 47 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 48 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 49 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 50 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 51 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 52 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 53 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 54 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 55 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 56 | |
| 57 | The views and conclusions contained in the software and documentation are those |
| 58 | of the authors and should not be interpreted as representing official policies, |
| 59 | either expressed or implied, of the FreeBSD Project. |
| 60 | */ |
| 61 | |
| 62 | /** |
| 63 | * Contains the vCard versions that this library supports. |
| 64 | * @author George El-Haddad |
| 65 | * @author Michael Angstadt |
| 66 | */ |
| 67 | public enum VCardVersion { |
| 68 | /** |
| 69 | * The original vCard specification. |
| 70 | * @see <a href="http://www.imc.org/pdi/vcard-21.rtf">2.1 specs</a> |
| 71 | */ |
| 72 | V2_1("2.1", SyntaxStyle.OLD, null), |
| 73 | |
| 74 | /** |
| 75 | * Version 3.0. |
| 76 | * @see <a href="http://tools.ietf.org/html/rfc2426">RFC 2426</a> |
| 77 | */ |
| 78 | V3_0("3.0", SyntaxStyle.NEW, null), |
| 79 | |
| 80 | /** |
| 81 | * The lastest specification. |
| 82 | * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350</a> |
| 83 | */ |
| 84 | V4_0("4.0", SyntaxStyle.NEW, "urn:ietf:params:xml:ns:vcard-4.0"); |
| 85 | |
| 86 | private final String version; |
| 87 | private final SyntaxStyle syntaxStyle; |
| 88 | private final String xmlNamespace; |
| 89 | |
| 90 | /** |
| 91 | * @param version the text representation |
| 92 | * @param xmlNamespace the xCard namespace or null if not defined |
| 93 | */ |
| 94 | VCardVersion(String version, SyntaxStyle syntaxStyle, String xmlNamespace) { |
| 95 | this.version = version; |
| 96 | this.syntaxStyle = syntaxStyle; |
| 97 | this.xmlNamespace = xmlNamespace; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Gets the text representation of this version. |
| 102 | * @return the text representation |
| 103 | */ |
| 104 | public String getVersion() { |
| 105 | return version; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Gets the syntax style used by this version when writing to a plain-text |
| 110 | * data stream. |
| 111 | * @return the syntax style |
| 112 | */ |
| 113 | public SyntaxStyle getSyntaxStyle() { |
| 114 | return syntaxStyle; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Gets the XML namespace used by this version when writing to an xCard. |
| 119 | * @return the XML namespace or null if this version does not support xCard |
| 120 | */ |
| 121 | public String getXmlNamespace() { |
| 122 | return xmlNamespace; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Gets a {@link VCardVersion} instance based on the given text |
| 127 | * representation. |
| 128 | * @param value the text representation |
| 129 | * @return the object or null if not found |
| 130 | */ |
| 131 | public static VCardVersion valueOfByStr(String value) { |
| 132 | for (VCardVersion version : VCardVersion.values()) { |
| 133 | if (version.getVersion().equals(value)) { |
| 134 | return version; |
| 135 | } |
| 136 | } |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Gets a {@link VCardVersion} instance based on the given XML namespace. |
| 142 | * @param ns the XML namespace |
| 143 | * @return the object or null if not found |
| 144 | */ |
| 145 | public static VCardVersion valueOfByXmlNamespace(String ns) { |
| 146 | for (VCardVersion version : VCardVersion.values()) { |
| 147 | String versionNs = version.getXmlNamespace(); |
| 148 | if (versionNs != null && versionNs.equals(ns)) { |
| 149 | return version; |
| 150 | } |
| 151 | } |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public String toString() { |
| 157 | return version; |
| 158 | } |
| 159 | } |