1 package org.unicode.cldr.tool; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.io.StringWriter; 6 import java.util.ArrayList; 7 import java.util.Date; 8 import java.util.Set; 9 import java.util.TreeSet; 10 11 import org.unicode.cldr.draft.FileUtilities; 12 import org.unicode.cldr.test.HelpMessages; 13 import org.unicode.cldr.util.ArrayComparator; 14 import org.unicode.cldr.util.CLDRPaths; 15 import org.unicode.cldr.util.CldrUtility; 16 17 import com.ibm.icu.text.Collator; 18 import com.ibm.icu.util.ICUUncheckedIOException; 19 import com.ibm.icu.util.ULocale; 20 21 public class FormattedFileWriter extends java.io.Writer { 22 public static final String CHART_TARGET_DIR = CLDRPaths.CHART_DIRECTORY + "/supplemental/"; 23 public static final Collator COL = 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 + "<table>" + Chart.LS); // 43 StringBuffer contents = new StringBuffer("<div id='chits'>" + Chart.LS); // 44 ArrayList<String[]> anchorList = new ArrayList<>(anchors); // flatten 45 for (String[] item : anchorList) { 46 String title = item[0]; 47 String fileName = item[1]; 48 String explanation = item[2]; 49 contents 50 .append("\t<div class='chit'><a name='" + FileUtilities.anchorize(title) + "' href='" + fileName + "'>" + title + "</a></div>" + Chart.LS); 51 if (hasExplanations) { 52 contents.append("\t<div class='chit'>" + explanation + "</div>" + Chart.LS); 53 } 54 } 55 // int columns = hasExplanations ? 2 : 4; 56 // int rows = 1 + (anchorList.size() - 1) / columns; 57 // String td = "<td class='plain' style='width:" + (100 / columns) + "%'>"; 58 // for (int row = 0; row < rows; ++row) { 59 // contents.append("<tr>" + Chart.LS); 60 // for (int column = 0; column < columns; ++column) { 61 // int index = column * rows + row; 62 // String linkedTitle = ""; 63 // String explanation = ""; 64 // if (index < anchorList.size()) { 65 // String[] item = anchorList.get(index); 66 // String title = item[0]; 67 // String fileName = item[1]; 68 // explanation = item[2]; 69 // linkedTitle = "<a name='" + FileUtilities.anchorize(title) + "' href='" + fileName + "'>" + title + "</a>"; 70 // } 71 // contents.append(td + linkedTitle + "</td>" + Chart.LS); 72 // if (hasExplanations) { 73 // contents.append(td + explanation + "</td>" + Chart.LS); 74 // } 75 // } 76 // contents.append("</tr>" + Chart.LS); 77 // td = "<td class='plain'>"; // only need width on first row 78 // } 79 contents.append("</div>" + Chart.LS); 80 // contents.append("</table>" + Chart.LS + "</div>" + Chart.LS); 81 return contents.toString(); 82 } 83 add(String title, String fileName, String explanation)84 public void add(String title, String fileName, String explanation) { 85 anchors.add(new String[] { title, fileName, explanation }); 86 if (explanation != null) { 87 hasExplanations = true; 88 } 89 } 90 } 91 92 private Anchors localeAnchors; 93 94 private String dir; 95 96 private String title; 97 private String filename; 98 99 private String indexLink = "index.html"; 100 private String indexTitle = "Index"; 101 102 private String explanation; 103 private boolean showDate = true; 104 105 private StringWriter out = new StringWriter(); 106 FormattedFileWriter(String baseFileName, String title, String explanation, Anchors anchors)107 public FormattedFileWriter(String baseFileName, String title, String explanation, Anchors anchors) 108 throws IOException { 109 // we set up a bunch of variables, but we won't actually use them unless there is generate content. See close() 110 if (baseFileName == null) { 111 baseFileName = FileUtilities.anchorize(title); 112 } 113 this.dir = FormattedFileWriter.CHART_TARGET_DIR; 114 this.filename = baseFileName; 115 this.title = title; 116 this.explanation = explanation; 117 this.localeAnchors = anchors; 118 } 119 getBaseFileName()120 public String getBaseFileName() { 121 return filename; 122 } 123 getDir()124 public String getDir() { 125 return dir; 126 } 127 setDirectory(String dir)128 public FormattedFileWriter setDirectory(String dir) { 129 this.dir = dir; 130 return this; 131 } 132 setShowDate(boolean showDate)133 public FormattedFileWriter setShowDate(boolean showDate) { 134 this.showDate = showDate; 135 return this; 136 } 137 138 @Override close()139 public void close() { 140 String contents = out.toString(); 141 if (contents.isEmpty()) { 142 return; // skip writing if there are no contents 143 } 144 if (explanation == null) { 145 explanation = HelpMessages.getChartMessages(filename); 146 } 147 if (explanation != null) { 148 contents = explanation + contents; 149 } 150 String targetFileName = filename + ".html"; 151 if (localeAnchors != null) { 152 localeAnchors.add(title, targetFileName, null); 153 } 154 final String templateFileName = "chart-template.html"; 155 String[] replacements = { "%header%", "", 156 "%title%", title, 157 "%index%", indexLink, 158 "%index-title%", indexTitle, 159 "%body%", contents }; 160 writeTargetWithReplacements(dir, targetFileName, templateFileName, replacements); 161 } 162 writeTargetWithReplacements(String targetdir, String targetFileName, final String templateFileName, String[] replacements)163 public static void writeTargetWithReplacements(String targetdir, String targetFileName, final String templateFileName, String[] replacements) { 164 try { 165 PrintWriter pw2 = org.unicode.cldr.draft.FileUtilities.openUTF8Writer(targetdir, targetFileName); 166 FileUtilities.appendBufferedReader(ToolUtilities.getUTF8Data(templateFileName), pw2, replacements); 167 pw2.close(); 168 } catch (IOException e) { 169 throw new ICUUncheckedIOException(e); 170 } 171 } 172 copyIncludeHtmls(String targetDirectory)173 public static void copyIncludeHtmls (String targetDirectory) { 174 copyIncludeHtmls(targetDirectory, false); 175 } 176 copyIncludeHtmls(String targetDirectory, boolean addPrevVersion)177 public static void copyIncludeHtmls (String targetDirectory, boolean addPrevVersion) { 178 String[] replacements = { 179 "%version%", ToolConstants.CHART_DISPLAY_VERSION + (addPrevVersion ? " – " + ToolConstants.PREV_CHART_VERSION_WITH0 : ""), 180 "%date%", CldrUtility.isoFormatDateOnly(new Date()) 181 }; 182 writeTargetWithReplacements(targetDirectory, "include-date.html", "include-date.html", replacements); 183 writeTargetWithReplacements(targetDirectory, "include-version.html", "include-version.html", replacements); 184 } 185 getDateValue()186 private String getDateValue() { 187 return showDate ? CldrUtility.isoFormatDateOnly(new Date()) : ""; 188 } 189 190 @Override write(char[] cbuf, int off, int len)191 public void write(char[] cbuf, int off, int len) throws IOException { 192 out.write(cbuf, off, len); 193 } 194 195 @Override flush()196 public void flush() throws IOException { 197 out.flush(); 198 } 199 setIndex(String indexTitle_, String indexLink_)200 public FormattedFileWriter setIndex(String indexTitle_, String indexLink_) { 201 indexLink = indexLink_; 202 indexTitle = indexTitle_; 203 return this; 204 } 205 }