Debian Codegraph @

src/main/java/ezvcard/io/chain/ChainingParser.java

raw symbols SCIP
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 import java.util.ArrayList;
8 import java.util.List;
9
10 import ezvcard.VCard;
11 import ezvcard.io.ParseWarning;
12 import ezvcard.io.StreamReader;
13 import ezvcard.io.scribe.ScribeIndex;
14 import ezvcard.io.scribe.VCardPropertyScribe;
15 import ezvcard.property.VCardProperty;
16
17 /*
18 Copyright (c) 2012-2023, Michael Angstadt
19 All rights reserved.
20
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met:
23
24 1. Redistributions of source code must retain the above copyright notice, this
25 list of conditions and the following disclaimer.
26 2. Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /**
43 * Parent class for all chaining parsers. This class is package-private in order
44 * to hide it from the generated Javadocs.
45 * @author Michael Angstadt
46 * @param <T> the object instance's type (for method chaining)
47 */
48 abstract class ChainingParser<T extends ChainingParser<?>> {
49 final String string;
50 final InputStream in;
51 final Reader reader;
52 final Path file;
53
54 ScribeIndex index;
55 List<List<ParseWarning>> warnings;
56
57 @SuppressWarnings("unchecked")
58 final T this_ = (T) this;
59
60 ChainingParser(String string) {
61 this(string, null, null, null);
62 }
63
64 ChainingParser(InputStream in) {
65 this(null, in, null, null);
66 }
67
68 ChainingParser(Reader reader) {
69 this(null, null, reader, null);
70 }
71
72 ChainingParser(Path file) {
73 this(null, null, null, file);
74 }
75
76 ChainingParser() {
77 this(null, null, null, null);
78 }
79
80 private ChainingParser(String string, InputStream in, Reader reader, Path file) {
81 this.string = string;
82 this.in = in;
83 this.reader = reader;
84 this.file = file;
85 }
86
87 /**
88 * Registers a property scribe.
89 * @param scribe the scribe
90 * @return this
91 */
92 public T register(VCardPropertyScribe<? extends VCardProperty> scribe) {
93 if (index == null) {
94 index = new ScribeIndex();
95 }
96 index.register(scribe);
97 return this_;
98 }
99
100 /**
101 * Provides a list object that any parser warnings will be put into.
102 * @param warnings the list object that will be populated with the warnings
103 * of each parsed vCard. Each element in the list is a list of warnings for
104 * one parsed vCard. Therefore, the size of this list will be equal to the
105 * number of parsed vCards. If a vCard does not have any warnings, then its
106 * warning list will be empty.
107 * @return this
108 */
109 public T warnings(List<List<ParseWarning>> warnings) {
110 this.warnings = warnings;
111 return this_;
112 }
113
114 /**
115 * Reads the first vCard from the stream.
116 * @return the vCard or null if there are no vCards
117 * @throws IOException if there's an I/O problem
118 */
119 public VCard first() throws IOException {
120 StreamReader reader = constructReader();
121 if (index != null) {
122 reader.setScribeIndex(index);
123 }
124
125 try {
126 VCard vcard = reader.readNext();
127 if (warnings != null) {
128 warnings.add(reader.getWarnings());
129 }
130 return vcard;
131 } finally {
132 if (closeWhenDone()) {
133 reader.close();
134 }
135 }
136 }
137
138 /**
139 * Reads all vCards from the stream.
140 * @return the parsed vCards
141 * @throws IOException if there's an I/O problem
142 */
143 public List<VCard> all() throws IOException {
144 StreamReader reader = constructReader();
145 if (index != null) {
146 reader.setScribeIndex(index);
147 }
148
149 try {
150 List<VCard> vcards = new ArrayList<>();
151 VCard vcard;
152 while ((vcard = reader.readNext()) != null) {
153 if (warnings != null) {
154 warnings.add(reader.getWarnings());
155 }
156 vcards.add(vcard);
157 }
158 return vcards;
159 } finally {
160 if (closeWhenDone()) {
161 reader.close();
162 }
163 }
164 }
165
166 abstract StreamReader constructReader() throws IOException;
167
168 private boolean closeWhenDone() {
169 return in == null && reader == null;
170 }
171 }