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.ULocale; 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 = Collator.getInstance(ULocale.ROOT).setStrength2(Collator.IDENTICAL); 23 //public static final PairComparator<String,String> PC = new PairComparator(COL, null); 24 public static final ArrayComparator PC = new ArrayComparator(COL); 25 26 ///Comparator<Pair<>> 27 28 public static class Anchors { 29 boolean hasExplanations = false; 30 private Set<String[]> anchors = new TreeSet<String[]>(PC); 31 32 @Override toString()33 public String toString() { 34 /* 35 <div id="chits"> 36 <div class='chit chGold'> 37 <a name='g002C' title='U+002C , COMMA' href='#g002C'> 38 <img class='chitImg' src='/consortium/aacimg/002C.png' alt=','></a>Mark Davis and Anne Gundelfinger 39 </div> 40 */ 41 //StringBuffer contents = new StringBuffer("<div align='center'>" + Chart.LS + "<table>" + Chart.LS); // 42 StringBuffer contents = new StringBuffer("<div id='chits'>" + Chart.LS); // 43 ArrayList<String[]> anchorList = new ArrayList<>(anchors); // flatten 44 for (String[] item : anchorList) { 45 String title = item[0]; 46 String fileName = item[1]; 47 String explanation = item[2]; 48 contents 49 .append("\t<div class='chit'><a name='" + FileUtilities.anchorize(title) + "' href='" + fileName + "'>" + title + "</a></div>" + Chart.LS); 50 if (hasExplanations) { 51 contents.append("\t<div class='chit'>" + explanation + "</div>" + Chart.LS); 52 } 53 } 54 // int columns = hasExplanations ? 2 : 4; 55 // int rows = 1 + (anchorList.size() - 1) / columns; 56 // String td = "<td class='plain' style='width:" + (100 / columns) + "%'>"; 57 // for (int row = 0; row < rows; ++row) { 58 // contents.append("<tr>" + Chart.LS); 59 // for (int column = 0; column < columns; ++column) { 60 // int index = column * rows + row; 61 // String linkedTitle = ""; 62 // String explanation = ""; 63 // if (index < anchorList.size()) { 64 // String[] item = anchorList.get(index); 65 // String title = item[0]; 66 // String fileName = item[1]; 67 // explanation = item[2]; 68 // linkedTitle = "<a name='" + FileUtilities.anchorize(title) + "' href='" + fileName + "'>" + title + "</a>"; 69 // } 70 // contents.append(td + linkedTitle + "</td>" + Chart.LS); 71 // if (hasExplanations) { 72 // contents.append(td + explanation + "</td>" + Chart.LS); 73 // } 74 // } 75 // contents.append("</tr>" + Chart.LS); 76 // td = "<td class='plain'>"; // only need width on first row 77 // } 78 contents.append("</div>" + Chart.LS); 79 // contents.append("</table>" + Chart.LS + "</div>" + Chart.LS); 80 return contents.toString(); 81 } 82 add(String title, String fileName, String explanation)83 public void add(String title, String fileName, String explanation) { 84 anchors.add(new String[] { title, fileName, explanation }); 85 if (explanation != null) { 86 hasExplanations = true; 87 } 88 } 89 } 90 91 private Anchors localeAnchors; 92 93 private String dir; 94 95 private String title; 96 private String filename; 97 98 private String indexLink = "index.html"; 99 private String indexTitle = "Index"; 100 101 private String explanation; 102 private boolean showDate = true; 103 104 private StringWriter out = new StringWriter(); 105 FormattedFileWriter(String baseFileName, String title, String explanation, Anchors anchors)106 public FormattedFileWriter(String baseFileName, String title, String explanation, Anchors anchors) 107 throws IOException { 108 // we set up a bunch of variables, but we won't actually use them unless there is generate content. See close() 109 if (baseFileName == null) { 110 baseFileName = FileUtilities.anchorize(title); 111 } 112 this.dir = FormattedFileWriter.CHART_TARGET_DIR; 113 this.filename = baseFileName; 114 this.title = title; 115 this.explanation = explanation; 116 this.localeAnchors = anchors; 117 } 118 getBaseFileName()119 public String getBaseFileName() { 120 return filename; 121 } 122 getDir()123 public String getDir() { 124 return dir; 125 } 126 setDirectory(String dir)127 public FormattedFileWriter setDirectory(String dir) { 128 this.dir = dir; 129 return this; 130 } 131 setShowDate(boolean showDate)132 public FormattedFileWriter setShowDate(boolean showDate) { 133 this.showDate = showDate; 134 return this; 135 } 136 close()137 public void close() throws IOException { 138 String contents = out.toString(); 139 if (contents.isEmpty()) { 140 return; // skip writing if there are no contents 141 } 142 if (explanation == null) { 143 explanation = HelpMessages.getChartMessages(filename); 144 } 145 if (explanation != null) { 146 contents = explanation + contents; 147 } 148 if (localeAnchors != null) { 149 localeAnchors.add(title, filename + ".html", null); 150 } 151 PrintWriter pw2 = org.unicode.cldr.draft.FileUtilities.openUTF8Writer(dir, filename + ".html"); 152 String[] replacements = { "%header%", "", 153 "%title%", title, 154 "%version%", ToolConstants.CHART_DISPLAY_VERSION, 155 "%index%", indexLink, 156 "%index-title%", indexTitle, 157 "%date%", getDateValue(), 158 "%body%", contents }; 159 final String templateFileName = "chart-template.html"; 160 FileUtilities.appendBufferedReader(ToolUtilities.getUTF8Data(templateFileName), pw2, replacements); 161 pw2.close(); 162 } 163 getDateValue()164 private String getDateValue() { 165 return showDate ? CldrUtility.isoFormatDateOnly(new Date()) : ""; 166 } 167 write(char[] cbuf, int off, int len)168 public void write(char[] cbuf, int off, int len) throws IOException { 169 out.write(cbuf, off, len); 170 } 171 flush()172 public void flush() throws IOException { 173 out.flush(); 174 } 175 setIndex(String indexTitle_, String indexLink_)176 public FormattedFileWriter setIndex(String indexTitle_, String indexLink_) { 177 indexLink = indexLink_; 178 indexTitle = indexTitle_; 179 return this; 180 } 181 }