| 1 | package ezvcard.io; |
| 2 | |
| 3 | import java.io.Closeable; |
| 4 | import java.io.IOException; |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.HashSet; |
| 7 | import java.util.List; |
| 8 | import java.util.Set; |
| 9 | |
| 10 | import ezvcard.VCard; |
| 11 | import ezvcard.io.scribe.ScribeIndex; |
| 12 | import ezvcard.io.scribe.VCardPropertyScribe; |
| 13 | import ezvcard.parameter.AddressType; |
| 14 | import ezvcard.property.Address; |
| 15 | import ezvcard.property.Label; |
| 16 | import ezvcard.property.VCardProperty; |
| 17 | |
| 18 | /* |
| 19 | Copyright (c) 2012-2023, Michael Angstadt |
| 20 | All rights reserved. |
| 21 | |
| 22 | Redistribution and use in source and binary forms, with or without |
| 23 | modification, are permitted provided that the following conditions are met: |
| 24 | |
| 25 | 1. Redistributions of source code must retain the above copyright notice, this |
| 26 | list of conditions and the following disclaimer. |
| 27 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 28 | this list of conditions and the following disclaimer in the documentation |
| 29 | and/or other materials provided with the distribution. |
| 30 | |
| 31 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 32 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 33 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 34 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 35 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 36 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 37 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 38 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 39 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 40 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 41 | |
| 42 | The views and conclusions contained in the software and documentation are those |
| 43 | of the authors and should not be interpreted as representing official policies, |
| 44 | either expressed or implied, of the FreeBSD Project. |
| 45 | */ |
| 46 | |
| 47 | /** |
| 48 | * Parses vCards from a data stream. |
| 49 | * @author Michael Angstadt |
| 50 | */ |
| 51 | public abstract class StreamReader implements Closeable { |
| 52 | protected final List<ParseWarning> warnings = new ArrayList<>(); |
| 53 | protected ScribeIndex index = new ScribeIndex(); |
| 54 | protected ParseContext context; |
| 55 | |
| 56 | /** |
| 57 | * Reads all vCards from the data stream. |
| 58 | * @return the vCards |
| 59 | * @throws IOException if there's a problem reading from the stream |
| 60 | */ |
| 61 | public List<VCard> readAll() throws IOException { |
| 62 | List<VCard> vcards = new ArrayList<>(); |
| 63 | VCard vcard; |
| 64 | while ((vcard = readNext()) != null) { |
| 65 | vcards.add(vcard); |
| 66 | } |
| 67 | return vcards; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Reads the next vCard from the data stream. |
| 72 | * @return the next vCard or null if there are no more |
| 73 | * @throws IOException if there's a problem reading from the stream |
| 74 | */ |
| 75 | public VCard readNext() throws IOException { |
| 76 | warnings.clear(); |
| 77 | context = new ParseContext(); |
| 78 | return _readNext(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Reads the next vCard from the data stream. |
| 83 | * @return the next vCard or null if there are no more |
| 84 | * @throws IOException if there's a problem reading from the stream |
| 85 | */ |
| 86 | protected abstract VCard _readNext() throws IOException; |
| 87 | |
| 88 | /** |
| 89 | * Matches up a list of {@link Label} properties with their corresponding |
| 90 | * {@link Address} properties. If no match can be found, then the LABEL |
| 91 | * property itself is assigned to the vCard. |
| 92 | * @param vcard the vCard that the properties belong to |
| 93 | * @param labels the LABEL properties |
| 94 | */ |
| 95 | protected void assignLabels(VCard vcard, List<Label> labels) { |
| 96 | List<Address> adrs = vcard.getAddresses(); |
| 97 | for (Label label : labels) { |
| 98 | boolean orphaned = true; |
| 99 | Set<AddressType> labelTypes = new HashSet<>(label.getTypes()); |
| 100 | for (Address adr : adrs) { |
| 101 | if (adr.getLabel() != null) { |
| 102 | //a label has already been assigned to it |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | Set<AddressType> adrTypes = new HashSet<>(adr.getTypes()); |
| 107 | if (adrTypes.equals(labelTypes)) { |
| 108 | adr.setLabel(label.getValue()); |
| 109 | orphaned = false; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | if (orphaned) { |
| 114 | vcard.addOrphanedLabel(label); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * <p> |
| 121 | * Registers a property scribe. This is the same as calling: |
| 122 | * </p> |
| 123 | * <p> |
| 124 | * {@code getScribeIndex().register(scribe)} |
| 125 | * </p> |
| 126 | * @param scribe the scribe to register |
| 127 | */ |
| 128 | public void registerScribe(VCardPropertyScribe<? extends VCardProperty> scribe) { |
| 129 | index.register(scribe); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Gets the scribe index. |
| 134 | * @return the scribe index |
| 135 | */ |
| 136 | public ScribeIndex getScribeIndex() { |
| 137 | return index; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Sets the scribe index. |
| 142 | * @param index the scribe index |
| 143 | */ |
| 144 | public void setScribeIndex(ScribeIndex index) { |
| 145 | this.index = index; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Gets the warnings from the last vCard that was unmarshalled. This list is |
| 150 | * reset every time a new vCard is read. |
| 151 | * @return the warnings or empty list if there were no warnings |
| 152 | */ |
| 153 | public List<ParseWarning> getWarnings() { |
| 154 | return new ArrayList<>(warnings); |
| 155 | } |
| 156 | } |