| 1 | package ezvcard.io.chain; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.InputStream; |
| 5 | import java.io.Reader; |
| 6 | import java.nio.file.Path; |
| 7 | |
| 8 | import ezvcard.Ezvcard; |
| 9 | import ezvcard.io.StreamReader; |
| 10 | import ezvcard.io.text.VCardReader; |
| 11 | |
| 12 | /* |
| 13 | Copyright (c) 2012-2023, Michael Angstadt |
| 14 | All rights reserved. |
| 15 | |
| 16 | Redistribution and use in source and binary forms, with or without |
| 17 | modification, are permitted provided that the following conditions are met: |
| 18 | |
| 19 | 1. Redistributions of source code must retain the above copyright notice, this |
| 20 | list of conditions and the following disclaimer. |
| 21 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 22 | this list of conditions and the following disclaimer in the documentation |
| 23 | and/or other materials provided with the distribution. |
| 24 | |
| 25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 26 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 27 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 29 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 30 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 32 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * Chainer class for parsing traditional, plain-text vCards. |
| 39 | * @see Ezvcard#parse(InputStream) |
| 40 | * @see Ezvcard#parse(Path) |
| 41 | * @see Ezvcard#parse(Reader) |
| 42 | * @author Michael Angstadt |
| 43 | */ |
| 44 | public class ChainingTextParser<T extends ChainingTextParser<?>> extends ChainingParser<T> { |
| 45 | private boolean caretDecoding = true; |
| 46 | |
| 47 | public ChainingTextParser(String string) { |
| 48 | super(string); |
| 49 | } |
| 50 | |
| 51 | public ChainingTextParser(InputStream in) { |
| 52 | super(in); |
| 53 | } |
| 54 | |
| 55 | public ChainingTextParser(Reader reader) { |
| 56 | super(reader); |
| 57 | } |
| 58 | |
| 59 | public ChainingTextParser(Path file) { |
| 60 | super(file); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Sets whether the reader will decode characters in parameter values that |
| 65 | * use circumflex accent encoding (enabled by default). |
| 66 | * |
| 67 | * @param enable true to use circumflex accent decoding, false not to |
| 68 | * @return this |
| 69 | * @see VCardReader#setCaretDecodingEnabled(boolean) |
| 70 | * @see <a href="http://tools.ietf.org/html/rfc6868">RFC 6868</a> |
| 71 | */ |
| 72 | public T caretDecoding(boolean enable) { |
| 73 | caretDecoding = enable; |
| 74 | return this_; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | StreamReader constructReader() throws IOException { |
| 79 | VCardReader reader = newReader(); |
| 80 | reader.setCaretDecodingEnabled(caretDecoding); |
| 81 | return reader; |
| 82 | } |
| 83 | |
| 84 | private VCardReader newReader() throws IOException { |
| 85 | if (string != null) { |
| 86 | return new VCardReader(string); |
| 87 | } |
| 88 | if (in != null) { |
| 89 | return new VCardReader(in); |
| 90 | } |
| 91 | if (reader != null) { |
| 92 | return new VCardReader(reader); |
| 93 | } |
| 94 | return new VCardReader(file); |
| 95 | } |
| 96 | } |