| 1 |
package ezvcard.io.html; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.OutputStream; |
| 5 |
import java.io.OutputStreamWriter; |
| 6 |
import java.io.StringWriter; |
| 7 |
import java.io.UncheckedIOException; |
| 8 |
import java.io.Writer; |
| 9 |
import java.nio.file.Files; |
| 10 |
import java.nio.file.Path; |
| 11 |
import java.time.Instant; |
| 12 |
import java.time.ZoneId; |
| 13 |
import java.time.ZoneOffset; |
| 14 |
import java.time.format.DateTimeFormatter; |
| 15 |
import java.time.temporal.Temporal; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.HashMap; |
| 18 |
import java.util.List; |
| 19 |
import java.util.Map; |
| 20 |
import java.util.regex.Pattern; |
| 21 |
|
| 22 |
import ezvcard.Ezvcard; |
| 23 |
import ezvcard.VCard; |
| 24 |
import ezvcard.io.scribe.ScribeIndex; |
| 25 |
import ezvcard.parameter.ImageType; |
| 26 |
import ezvcard.property.Photo; |
| 27 |
import ezvcard.util.DataUri; |
| 28 |
import ezvcard.util.VCardDateFormat; |
| 29 |
import freemarker.template.Configuration; |
| 30 |
import freemarker.template.Template; |
| 31 |
import freemarker.template.TemplateException; |
| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public class HCardPage { |
| 86 |
private final Template template; |
| 87 |
private final List<VCard> vcards = new ArrayList<>(); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public HCardPage() { |
| 93 |
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23); |
| 94 |
cfg.setClassForTemplateLoading(HCardPage.class, ""); |
| 95 |
cfg.setWhitespaceStripping(true); |
| 96 |
try { |
| 97 |
template = cfg.getTemplate("hcard-template.html"); |
| 98 |
} catch (IOException e) { |
| 99 |
|
| 100 |
throw new UncheckedIOException(e); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public HCardPage(Template template) { |
| 111 |
this.template = template; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void add(VCard vcard) { |
| 119 |
vcards.add(vcard); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public String write() { |
| 127 |
StringWriter sw = new StringWriter(); |
| 128 |
try { |
| 129 |
write(sw); |
| 130 |
} catch (IOException e) { |
| 131 |
|
| 132 |
throw new UncheckedIOException(e); |
| 133 |
} |
| 134 |
return sw.toString(); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public void write(OutputStream out) throws IOException { |
| 143 |
write(new OutputStreamWriter(out)); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public void write(Path file) throws IOException { |
| 152 |
try (Writer writer = Files.newBufferedWriter(file)) { |
| 153 |
write(writer); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public void write(Writer writer) throws IOException { |
| 163 |
Map<String, Object> map = new HashMap<>(); |
| 164 |
map.put("vcards", vcards); |
| 165 |
map.put("utils", new TemplateUtils()); |
| 166 |
map.put("translucentBg", readImage("translucent-bg.png", ImageType.PNG)); |
| 167 |
map.put("noProfile", readImage("no-profile.png", ImageType.PNG)); |
| 168 |
map.put("ezVCardVersion", Ezvcard.VERSION); |
| 169 |
map.put("ezVCardUrl", Ezvcard.URL); |
| 170 |
map.put("scribeIndex", new ScribeIndex()); |
| 171 |
try { |
| 172 |
template.process(map, writer); |
| 173 |
} catch (TemplateException e) { |
| 174 |
throw new RuntimeException(e); |
| 175 |
} |
| 176 |
writer.flush(); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
private Photo readImage(String name, ImageType mediaType) throws IOException { |
| 187 |
return new Photo(getClass().getResourceAsStream(name), mediaType); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public static class TemplateUtils { |
| 194 |
private final Pattern newlineRegex = Pattern.compile("\\r\\n|\\r|\\n"); |
| 195 |
|
| 196 |
public String base64(String contentType, byte[] data) { |
| 197 |
return new DataUri(contentType, data).toString(); |
| 198 |
} |
| 199 |
|
| 200 |
public String lineBreaks(String value) { |
| 201 |
return newlineRegex.matcher(value).replaceAll("<br />"); |
| 202 |
} |
| 203 |
|
| 204 |
public String format(Temporal temporal, String format) { |
| 205 |
return format(temporal, format, ZoneOffset.UTC); |
| 206 |
} |
| 207 |
|
| 208 |
public String formatLocal(Temporal temporal, String format) { |
| 209 |
return format(temporal, format, ZoneId.systemDefault()); |
| 210 |
} |
| 211 |
|
| 212 |
private String format(Temporal temporal, String format, ZoneId offset) { |
| 213 |
if (temporal instanceof Instant) { |
| 214 |
temporal = ((Instant) temporal).atZone(offset); |
| 215 |
} |
| 216 |
return DateTimeFormatter.ofPattern(format).format(temporal); |
| 217 |
} |
| 218 |
|
| 219 |
public String format(ZoneOffset offset) { |
| 220 |
return VCardDateFormat.BASIC.format(offset); |
| 221 |
} |
| 222 |
} |
| 223 |
} |