| 1 |
package ezvcard.io.chain; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.OutputStream; |
| 5 |
import java.io.Writer; |
| 6 |
import java.nio.file.Path; |
| 7 |
import java.util.Collection; |
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.Map; |
| 10 |
|
| 11 |
import javax.xml.transform.Transformer; |
| 12 |
import javax.xml.transform.TransformerException; |
| 13 |
|
| 14 |
import org.w3c.dom.Document; |
| 15 |
|
| 16 |
import ezvcard.Ezvcard; |
| 17 |
import ezvcard.VCard; |
| 18 |
import ezvcard.VCardDataType; |
| 19 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 20 |
import ezvcard.io.xml.XCardDocument; |
| 21 |
import ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter; |
| 22 |
import ezvcard.io.xml.XCardOutputProperties; |
| 23 |
import ezvcard.property.VCardProperty; |
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class ChainingXmlWriter extends ChainingWriter<ChainingXmlWriter> { |
| 57 |
private final XCardOutputProperties outputProperties = new XCardOutputProperties(); |
| 58 |
private final Map<String, VCardDataType> parameterDataTypes = new HashMap<>(0); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public ChainingXmlWriter(Collection<VCard> vcards) { |
| 64 |
super(vcards); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public ChainingXmlWriter indent(Integer indent) { |
| 75 |
outputProperties.setIndent(indent); |
| 76 |
return this; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public ChainingXmlWriter xmlVersion(String xmlVersion) { |
| 88 |
outputProperties.setXmlVersion(xmlVersion); |
| 89 |
return this; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public ChainingXmlWriter outputProperty(String name, String value) { |
| 100 |
outputProperties.put(name, value); |
| 101 |
return this; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public ChainingXmlWriter outputProperties(Map<String, String> outputProperties) { |
| 111 |
this.outputProperties.putAll(outputProperties); |
| 112 |
return this; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public ChainingXmlWriter prodId(boolean include) { |
| 117 |
return super.prodId(include); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public ChainingXmlWriter versionStrict(boolean versionStrict) { |
| 122 |
return super.versionStrict(versionStrict); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public ChainingXmlWriter register(VCardPropertyScribe<? extends VCardProperty> scribe) { |
| 127 |
return super.register(scribe); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public ChainingXmlWriter register(String parameterName, VCardDataType dataType) { |
| 138 |
parameterDataTypes.put(parameterName, dataType); |
| 139 |
return this; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public String go() { |
| 147 |
return createXCardDocument().write(outputProperties); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void go(OutputStream out) throws TransformerException { |
| 157 |
createXCardDocument().write(out, outputProperties); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public void go(Path file) throws IOException, TransformerException { |
| 167 |
createXCardDocument().write(file, outputProperties); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public void go(Writer writer) throws TransformerException { |
| 176 |
createXCardDocument().write(writer, outputProperties); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public Document dom() { |
| 184 |
return createXCardDocument().getDocument(); |
| 185 |
} |
| 186 |
|
| 187 |
private XCardDocument createXCardDocument() { |
| 188 |
XCardDocument document = new XCardDocument(); |
| 189 |
|
| 190 |
XCardDocumentStreamWriter writer = document.writer(); |
| 191 |
writer.setAddProdId(prodId); |
| 192 |
writer.setVersionStrict(versionStrict); |
| 193 |
for (Map.Entry<String, VCardDataType> entry : parameterDataTypes.entrySet()) { |
| 194 |
String parameterName = entry.getKey(); |
| 195 |
VCardDataType dataType = entry.getValue(); |
| 196 |
writer.registerParameterDataType(parameterName, dataType); |
| 197 |
} |
| 198 |
if (index != null) { |
| 199 |
writer.setScribeIndex(index); |
| 200 |
} |
| 201 |
|
| 202 |
for (VCard vcard : vcards) { |
| 203 |
writer.write(vcard); |
| 204 |
} |
| 205 |
|
| 206 |
return document; |
| 207 |
} |
| 208 |
} |