| 1 |
package ezvcard.io.chain; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.OutputStream; |
| 5 |
import java.io.StringWriter; |
| 6 |
import java.io.UncheckedIOException; |
| 7 |
import java.io.Writer; |
| 8 |
import java.nio.file.Path; |
| 9 |
import java.util.Collection; |
| 10 |
|
| 11 |
import ezvcard.Ezvcard; |
| 12 |
import ezvcard.VCard; |
| 13 |
import ezvcard.io.json.JCardWriter; |
| 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 |
public class ChainingJsonWriter extends ChainingWriter<ChainingJsonWriter> { |
| 49 |
private boolean prettyPrint = false; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public ChainingJsonWriter(Collection<VCard> vcards) { |
| 55 |
super(vcards); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public ChainingJsonWriter prettyPrint(boolean prettyPrint) { |
| 65 |
this.prettyPrint = prettyPrint; |
| 66 |
return this; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public ChainingJsonWriter prodId(boolean include) { |
| 71 |
return super.prodId(include); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public ChainingJsonWriter versionStrict(boolean versionStrict) { |
| 76 |
return super.versionStrict(versionStrict); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public ChainingJsonWriter register(VCardPropertyScribe<? extends VCardProperty> scribe) { |
| 81 |
return super.register(scribe); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public String go() { |
| 89 |
StringWriter sw = new StringWriter(); |
| 90 |
try { |
| 91 |
go(sw); |
| 92 |
} catch (IOException e) { |
| 93 |
|
| 94 |
throw new UncheckedIOException(e); |
| 95 |
} |
| 96 |
return sw.toString(); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public void go(OutputStream out) throws IOException { |
| 105 |
go(new JCardWriter(out, wrapInArray())); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public void go(Path file) throws IOException { |
| 114 |
try (JCardWriter writer = new JCardWriter(file, wrapInArray())) { |
| 115 |
go(writer); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public void go(Writer writer) throws IOException { |
| 125 |
go(new JCardWriter(writer, wrapInArray())); |
| 126 |
} |
| 127 |
|
| 128 |
private void go(JCardWriter writer) throws IOException { |
| 129 |
writer.setAddProdId(prodId); |
| 130 |
writer.setPrettyPrint(prettyPrint); |
| 131 |
writer.setVersionStrict(versionStrict); |
| 132 |
if (index != null) { |
| 133 |
writer.setScribeIndex(index); |
| 134 |
} |
| 135 |
try { |
| 136 |
for (VCard vcard : vcards) { |
| 137 |
writer.write(vcard); |
| 138 |
writer.flush(); |
| 139 |
} |
| 140 |
} finally { |
| 141 |
writer.closeJsonStream(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
private boolean wrapInArray() { |
| 146 |
return vcards.size() > 1; |
| 147 |
} |
| 148 |
} |