1package org.unicode.cldr.icu; 2 3import java.io.IOException; 4import java.io.PrintWriter; 5import java.nio.charset.Charset; 6import java.util.ArrayList; 7import java.util.Collection; 8import java.util.Collections; 9import java.util.Comparator; 10import java.util.List; 11import java.util.regex.Pattern; 12 13import org.unicode.cldr.draft.FileUtilities; 14import org.unicode.cldr.util.CLDRFile; 15import org.unicode.cldr.util.FileCopier; 16import org.unicode.cldr.util.PatternCache; 17 18import com.google.common.collect.ImmutableMap; 19import com.ibm.icu.util.Calendar; 20 21/** 22 * Stores the content of a makefile and writes it to disk. 23 * @author jchye 24 */ 25class Makefile { 26 private static final Pattern VARIABLE = PatternCache.get("\\$\\([\\w_]++\\)"); 27 28 private String prefix; 29 private List<MakefileEntry> entries = new ArrayList<MakefileEntry>(); 30 31 private static final Comparator<String> valueComparator = new Comparator<String>() { 32 @Override 33 public int compare(String arg0, String arg1) { 34 return arg0.compareTo(arg1); 35 } 36 }; 37 38 class MakefileEntry { 39 String name; 40 String comment; 41 List<String> values = new ArrayList<String>(); 42 43 public MakefileEntry(String name, String comment) { 44 this.name = name; 45 this.comment = comment; 46 } 47 } 48 49 public Makefile(String prefix) { 50 this.prefix = prefix; 51 } 52 53 public MakefileEntry addEntry(String name, String comment) { 54 MakefileEntry entry = new MakefileEntry(name, comment); 55 entries.add(entry); 56 return entry; 57 } 58 59 public MakefileEntry addEntry(String name, String comment, Collection<String> values) { 60 MakefileEntry entry = addEntry(name, comment); 61 entry.values.addAll(values); 62 Collections.sort(entry.values, valueComparator); 63 return entry; 64 } 65 66 public void addSyntheticAlias(Collection<String> aliases) { 67 addEntry(prefix + "_SYNTHETIC_ALIAS", 68 "Aliases without a corresponding xx.xml file (see icu-config.xml & build.xml)", 69 aliases); 70 } 71 72 public void addAliasSource() { 73 MakefileEntry entry = addEntry(prefix + "_ALIAS_SOURCE", 74 "All aliases (to not be included under 'installed'), but not including root."); 75 entry.values.add("$(" + prefix + "_SYNTHETIC_ALIAS)"); 76 } 77 78 public void addSource(Collection<String> sources) { 79 addEntry(prefix + "_SOURCE", "Ordinary resources", sources); 80 } 81 82 public void print(String outputDir, String filename) throws IOException { 83 PrintWriter out = FileUtilities.openUTF8Writer(outputDir, filename); 84 ImmutableMap<String, String> params = ImmutableMap.of( 85 "%year%", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)), 86 "%prefix%", prefix, 87 "%local%", filename.replace("files.mk", "local.mk"), 88 "%version%", CLDRFile.GEN_VERSION); 89 90 FileCopier.copyAndReplace(NewLdml2IcuConverter.class, "makefile_header.txt", 91 Charset.forName("UTF-8"), 92 params, 93 out); 94 95 for (MakefileEntry entry : entries) { 96 out.println(); 97 out.append("# ").append(entry.comment).println(); 98 out.append(entry.name).append(" ="); 99 int lineCount = 0; 100 for (String value : entry.values) { 101 if (value.equals("root")) continue; 102 if (lineCount == 4) { 103 out.append('\\').println(); 104 } 105 out.append(' ').append(value); 106 if (!VARIABLE.matcher(value).matches()) { 107 out.append(".txt"); 108 } 109 lineCount = (lineCount + 1) % 5; 110 } 111 out.println(); 112 out.println(); 113 } 114 out.close(); 115 } 116}