1 package org.unicode.cldr.tool; 2 3 import com.ibm.icu.text.Collator; 4 import com.ibm.icu.util.ICUUncheckedIOException; 5 import com.ibm.icu.util.ULocale; 6 import java.io.IOException; 7 import java.io.PrintWriter; 8 import java.io.StringWriter; 9 import java.nio.file.Paths; 10 import java.util.ArrayList; 11 import java.util.Date; 12 import java.util.Set; 13 import java.util.TreeSet; 14 import org.unicode.cldr.draft.FileUtilities; 15 import org.unicode.cldr.test.HelpMessages; 16 import org.unicode.cldr.util.ArrayComparator; 17 import org.unicode.cldr.util.CLDRPaths; 18 import org.unicode.cldr.util.CldrUtility; 19 20 public class FormattedFileWriter extends java.io.Writer { 21 public static final String CHART_TARGET_DIR = CLDRPaths.CHART_DIRECTORY + "/supplemental/"; 22 public static final Collator COL = 23 Collator.getInstance(ULocale.ROOT).setStrength2(Collator.IDENTICAL); 24 // public static final PairComparator<String,String> PC = new PairComparator(COL, null); 25 public static final ArrayComparator PC = new ArrayComparator(COL); 26 27 /// Comparator<Pair<>> 28 29 public static class Anchors { 30 boolean hasExplanations = false; 31 private Set<String[]> anchors = new TreeSet<String[]>(PC); 32 33 @Override toString()34 public String toString() { 35 /* 36 <div id="chits"> 37 <div class='chit chGold'> 38 <a name='g002C' title='U+002C , COMMA' href='#g002C'> 39 <img class='chitImg' src='/consortium/aacimg/002C.png' alt=','></a>Mark Davis and Anne Gundelfinger 40 </div> 41 */ 42 // StringBuffer contents = new StringBuffer("<div align='center'>" + Chart.LS + 43 // "<table>" + Chart.LS); // 44 StringBuffer contents = new StringBuffer("<div id='chits'>" + Chart.LS); // 45 ArrayList<String[]> anchorList = new ArrayList<>(anchors); // flatten 46 for (String[] item : anchorList) { 47 String title = item[0]; 48 String fileName = item[1]; 49 String explanation = item[2]; 50 contents.append( 51 "\t<div class='chit'><a name='" 52 + FileUtilities.anchorize(title) 53 + "' href='" 54 + fileName 55 + "'>" 56 + title 57 + "</a></div>" 58 + Chart.LS); 59 if (hasExplanations) { 60 contents.append("\t<div class='chit'>" + explanation + "</div>" + Chart.LS); 61 } 62 } 63 // int columns = hasExplanations ? 2 : 4; 64 // int rows = 1 + (anchorList.size() - 1) / columns; 65 // String td = "<td class='plain' style='width:" + (100 / columns) + "%'>"; 66 // for (int row = 0; row < rows; ++row) { 67 // contents.append("<tr>" + Chart.LS); 68 // for (int column = 0; column < columns; ++column) { 69 // int index = column * rows + row; 70 // String linkedTitle = ""; 71 // String explanation = ""; 72 // if (index < anchorList.size()) { 73 // String[] item = anchorList.get(index); 74 // String title = item[0]; 75 // String fileName = item[1]; 76 // explanation = item[2]; 77 // linkedTitle = "<a name='" + FileUtilities.anchorize(title) + 78 // "' href='" + fileName + "'>" + title + "</a>"; 79 // } 80 // contents.append(td + linkedTitle + "</td>" + Chart.LS); 81 // if (hasExplanations) { 82 // contents.append(td + explanation + "</td>" + Chart.LS); 83 // } 84 // } 85 // contents.append("</tr>" + Chart.LS); 86 // td = "<td class='plain'>"; // only need width on first row 87 // } 88 contents.append("</div>" + Chart.LS); 89 // contents.append("</table>" + Chart.LS + "</div>" + Chart.LS); 90 return contents.toString(); 91 } 92 add(String title, String fileName, String explanation)93 public void add(String title, String fileName, String explanation) { 94 anchors.add(new String[] {title, fileName, explanation}); 95 if (explanation != null) { 96 hasExplanations = true; 97 } 98 } 99 } 100 101 private Anchors localeAnchors; 102 103 private String dir; 104 105 private String title; 106 private String filename; 107 108 private String indexLink = "index.html"; 109 private String indexTitle = "Index"; 110 111 private String explanation; 112 private boolean showDate = true; 113 114 private StringWriter out = new StringWriter(); 115 getStringWriter()116 protected StringWriter getStringWriter() { 117 return out; 118 } 119 FormattedFileWriter( String baseFileName, String title, String explanation, Anchors anchors)120 public FormattedFileWriter( 121 String baseFileName, String title, String explanation, Anchors anchors) 122 throws IOException { 123 // we set up a bunch of variables, but we won't actually use them unless there is generate 124 // content. See close() 125 if (baseFileName == null) { 126 baseFileName = FileUtilities.anchorize(title); 127 } 128 this.dir = FormattedFileWriter.CHART_TARGET_DIR; 129 this.filename = baseFileName; 130 this.title = title; 131 this.explanation = explanation; 132 this.localeAnchors = anchors; 133 } 134 getBaseFileName()135 public String getBaseFileName() { 136 return filename; 137 } 138 getDir()139 public String getDir() { 140 return dir; 141 } 142 setDirectory(String dir)143 public FormattedFileWriter setDirectory(String dir) { 144 this.dir = dir; 145 return this; 146 } 147 setShowDate(boolean showDate)148 public FormattedFileWriter setShowDate(boolean showDate) { 149 this.showDate = showDate; 150 return this; 151 } 152 153 @Override close()154 public void close() { 155 String contents = out.toString(); 156 if (contents.isEmpty()) { 157 return; // skip writing if there are no contents 158 } 159 if (explanation == null) { 160 explanation = HelpMessages.getChartMessages(filename); 161 } 162 if (explanation != null) { 163 contents = explanation + contents; 164 } 165 String targetFileName = filename + ".html"; 166 if (localeAnchors != null) { 167 localeAnchors.add(title, targetFileName, null); 168 } 169 final String templateFileName = "chart-template.html"; 170 String[] replacements = { 171 "%header%", 172 "", 173 "%title%", 174 title, 175 "%index%", 176 indexLink, 177 "%index-title%", 178 indexTitle, 179 "%body%", 180 contents, 181 "%analytics%", 182 Chart.AnalyticsID.CLDR.getScript() 183 }; 184 writeTargetWithReplacements(dir, targetFileName, templateFileName, replacements); 185 } 186 writeTargetWithReplacements( String targetdir, String targetFileName, final String templateFileName, String[] replacements)187 public static void writeTargetWithReplacements( 188 String targetdir, 189 String targetFileName, 190 final String templateFileName, 191 String[] replacements) { 192 try { 193 PrintWriter pw2 = 194 org.unicode.cldr.draft.FileUtilities.openUTF8Writer(targetdir, targetFileName); 195 System.err.println("Writing: " + Paths.get(targetdir, targetFileName)); 196 FileUtilities.appendBufferedReader( 197 ToolUtilities.getUTF8Data(templateFileName), pw2, replacements); 198 pw2.close(); 199 } catch (IOException e) { 200 throw new ICUUncheckedIOException(e); 201 } 202 } 203 copyIncludeHtmls(String targetDirectory)204 public static void copyIncludeHtmls(String targetDirectory) { 205 copyIncludeHtmls(targetDirectory, false); 206 } 207 copyIncludeHtmls(String targetDirectory, boolean addPrevVersion)208 public static void copyIncludeHtmls(String targetDirectory, boolean addPrevVersion) { 209 String[] replacements = { 210 "%version%", 211 ToolConstants.CHART_DISPLAY_VERSION 212 + (addPrevVersion ? " – " + ToolConstants.PREV_CHART_VERSION_WITH0 : ""), 213 "%date%", 214 CldrUtility.isoFormatDateOnly(new Date()) 215 }; 216 writeTargetWithReplacements( 217 targetDirectory, "include-date.html", "include-date.html", replacements); 218 writeTargetWithReplacements( 219 targetDirectory, "include-version.html", "include-version.html", replacements); 220 } 221 getDateValue()222 private String getDateValue() { 223 return showDate ? CldrUtility.isoFormatDateOnly(new Date()) : ""; 224 } 225 226 @Override write(char[] cbuf, int off, int len)227 public void write(char[] cbuf, int off, int len) throws IOException { 228 out.write(cbuf, off, len); 229 } 230 231 @Override flush()232 public void flush() throws IOException { 233 out.flush(); 234 } 235 setIndex(String indexTitle_, String indexLink_)236 public FormattedFileWriter setIndex(String indexTitle_, String indexLink_) { 237 indexLink = indexLink_; 238 indexTitle = indexTitle_; 239 return this; 240 } 241 } 242