| 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 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public T warnings(List<List<ParseWarning>> warnings) { |
| 110 |
this.warnings = warnings; |
| 111 |
return this_; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 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 |
} |