| 1 | package ezvcard.io.xml; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Map; |
| 5 | |
| 6 | import ezvcard.VCardDataType; |
| 7 | import ezvcard.VCardVersion; |
| 8 | import ezvcard.io.StreamWriter; |
| 9 | import ezvcard.parameter.VCardParameters; |
| 10 | |
| 11 | /* |
| 12 | Copyright (c) 2012-2023, Michael Angstadt |
| 13 | All rights reserved. |
| 14 | |
| 15 | Redistribution and use in source and binary forms, with or without |
| 16 | modification, are permitted provided that the following conditions are met: |
| 17 | |
| 18 | 1. Redistributions of source code must retain the above copyright notice, this |
| 19 | list of conditions and the following disclaimer. |
| 20 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 21 | this list of conditions and the following disclaimer in the documentation |
| 22 | and/or other materials provided with the distribution. |
| 23 | |
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 25 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 28 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | |
| 35 | The views and conclusions contained in the software and documentation are those |
| 36 | of the authors and should not be interpreted as representing official policies, |
| 37 | either expressed or implied, of the FreeBSD Project. |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * Base class for xCard writers. |
| 42 | * @author Michael Angstadt |
| 43 | */ |
| 44 | abstract class XCardWriterBase extends StreamWriter { |
| 45 | protected final VCardVersion targetVersion = VCardVersion.V4_0; //xCard only supports 4.0 |
| 46 | |
| 47 | /** |
| 48 | * Defines the names (data types) of the XML elements that are used to hold |
| 49 | * each parameter's value. |
| 50 | */ |
| 51 | protected final Map<String, VCardDataType> parameterDataTypes = new HashMap<>(); |
| 52 | { |
| 53 | registerParameterDataType(VCardParameters.ALTID, VCardDataType.TEXT); |
| 54 | registerParameterDataType(VCardParameters.CALSCALE, VCardDataType.TEXT); |
| 55 | //registerParameterDataType(VCardParameters.CHARSET, VCardDataType.TEXT); //not 4.0 |
| 56 | //registerParameterDataType(VCardParameters.ENCODING, VCardDataType.TEXT); //not 4.0 |
| 57 | registerParameterDataType(VCardParameters.GEO, VCardDataType.URI); |
| 58 | registerParameterDataType(VCardParameters.INDEX, VCardDataType.INTEGER); |
| 59 | registerParameterDataType(VCardParameters.LABEL, VCardDataType.TEXT); |
| 60 | registerParameterDataType(VCardParameters.LANGUAGE, VCardDataType.LANGUAGE_TAG); |
| 61 | registerParameterDataType(VCardParameters.LEVEL, VCardDataType.TEXT); |
| 62 | registerParameterDataType(VCardParameters.MEDIATYPE, VCardDataType.TEXT); |
| 63 | registerParameterDataType(VCardParameters.PID, VCardDataType.TEXT); |
| 64 | registerParameterDataType(VCardParameters.PREF, VCardDataType.INTEGER); |
| 65 | registerParameterDataType(VCardParameters.SORT_AS, VCardDataType.TEXT); |
| 66 | registerParameterDataType(VCardParameters.TYPE, VCardDataType.TEXT); |
| 67 | registerParameterDataType(VCardParameters.TZ, VCardDataType.URI); |
| 68 | //registerParameterDataType(VCardParameters.VALUE, VCardDataType.TEXT); //not used by xCard |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected VCardVersion getTargetVersion() { |
| 73 | return targetVersion; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Removes parameters that are not supported by xCard. |
| 78 | * @param parameters the property parameters |
| 79 | */ |
| 80 | protected void removeUnsupportedParameters(VCardParameters parameters) { |
| 81 | parameters.setCharset(null); |
| 82 | parameters.setEncoding(null); |
| 83 | parameters.setValue(null); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Registers the data type of an experimental parameter. Experimental |
| 88 | * parameters use the "unknown" data type by default. |
| 89 | * @param parameterName the parameter name (e.g. "x-foo") |
| 90 | * @param dataType the data type or null to remove |
| 91 | */ |
| 92 | public void registerParameterDataType(String parameterName, VCardDataType dataType) { |
| 93 | parameterName = parameterName.toLowerCase(); |
| 94 | if (dataType == null) { |
| 95 | parameterDataTypes.remove(parameterName); |
| 96 | } else { |
| 97 | parameterDataTypes.put(parameterName, dataType); |
| 98 | } |
| 99 | } |
| 100 | } |