001package ezvcard.io.chain;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.io.Reader;
006import java.nio.file.Path;
007
008import ezvcard.Ezvcard;
009import ezvcard.io.StreamReader;
010import ezvcard.io.text.VCardReader;
011
012/*
013 Copyright (c) 2012-2023, Michael Angstadt
014 All rights reserved.
015
016 Redistribution and use in source and binary forms, with or without
017 modification, are permitted provided that the following conditions are met: 
018
019 1. Redistributions of source code must retain the above copyright notice, this
020 list of conditions and the following disclaimer. 
021 2. Redistributions in binary form must reproduce the above copyright notice,
022 this list of conditions and the following disclaimer in the documentation
023 and/or other materials provided with the distribution. 
024
025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
028 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
029 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
032 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
034 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
035 */
036
037/**
038 * Chainer class for parsing traditional, plain-text vCards.
039 * @see Ezvcard#parse(InputStream)
040 * @see Ezvcard#parse(Path)
041 * @see Ezvcard#parse(Reader)
042 * @author Michael Angstadt
043 */
044public class ChainingTextParser<T extends ChainingTextParser<?>> extends ChainingParser<T> {
045        private boolean caretDecoding = true;
046
047        public ChainingTextParser(String string) {
048                super(string);
049        }
050
051        public ChainingTextParser(InputStream in) {
052                super(in);
053        }
054
055        public ChainingTextParser(Reader reader) {
056                super(reader);
057        }
058
059        public ChainingTextParser(Path file) {
060                super(file);
061        }
062
063        /**
064         * Sets whether the reader will decode characters in parameter values that
065         * use circumflex accent encoding (enabled by default).
066         * 
067         * @param enable true to use circumflex accent decoding, false not to
068         * @return this
069         * @see VCardReader#setCaretDecodingEnabled(boolean)
070         * @see <a href="http://tools.ietf.org/html/rfc6868">RFC 6868</a>
071         */
072        public T caretDecoding(boolean enable) {
073                caretDecoding = enable;
074                return this_;
075        }
076
077        @Override
078        StreamReader constructReader() throws IOException {
079                VCardReader reader = newReader();
080                reader.setCaretDecodingEnabled(caretDecoding);
081                return reader;
082        }
083
084        private VCardReader newReader() throws IOException {
085                if (string != null) {
086                        return new VCardReader(string);
087                }
088                if (in != null) {
089                        return new VCardReader(in);
090                }
091                if (reader != null) {
092                        return new VCardReader(reader);
093                }
094                return new VCardReader(file);
095        }
096}