ICU-2055 empty locales

X-SVN-Rev: 16797
This commit is contained in:
Steven R. Loomis 2004-11-08 21:53:50 +00:00
parent a4d034cfb4
commit e3fad60ba4
2 changed files with 137 additions and 41 deletions

View file

@ -137,6 +137,15 @@ public class LDML2ICUConverter {
if(options[WRITE_DRAFT].doesOccur) {
writeDraft = true;
}
if(options[SUPPLEMENTAL].doesOccur) {
writeSupplemental = true;
}
if(options[VERBOSE].doesOccur) {
verbose = true;
}
if(destDir==null){
destDir = ".";
}
if(options[WRITE_DEPRECATED].doesOccur) {
writeDeprecated = true;
if(remainingArgc>0) {
@ -151,15 +160,6 @@ public class LDML2ICUConverter {
printInfo("Reading alias table searching for draft overrides");
writeDeprecated(); // actually just reads the alias
}
if(options[SUPPLEMENTAL].doesOccur) {
writeSupplemental = true;
}
if(options[VERBOSE].doesOccur) {
verbose = true;
}
if(destDir==null){
destDir = ".";
}
if(remainingArgc==0){
printError("", "Either the file name to be processed is not "+
"specified or the it is specified after the -t/-c \n"+
@ -2773,6 +2773,8 @@ public class LDML2ICUConverter {
}
private ICUResourceWriter.Resource parseValidSubLocales(Node root, StringBuffer xpath){
return null;
/*
String loc = LDMLUtilities.getAttributeValue(root,LDMLConstants.VALID_SUBLOCALE);
if(loc!=null){
String[] locales = loc.split("\u0020");
@ -2795,6 +2797,7 @@ public class LDML2ICUConverter {
}
}
return null;
*/
}
private ICUResourceWriter.Resource parseCollation(Node root, StringBuffer xpath){
ICUResourceWriter.ResourceTable table = new ICUResourceWriter.ResourceTable();
@ -3565,15 +3568,19 @@ public class LDML2ICUConverter {
String treeName = LDMLUtilities.getAttributeValue(node, "type");
// System.out.println("TreeName = " + treeName);
boolean parseDraft = !writeDraft; // parse for draft status?
boolean parseSubLocale = treeName.equals("collation");
boolean parseThem = (parseDraft||parseSubLocale); // parse a bunch of locales?
if(treeName.equals(myTreeName)) {
// System.out.println("Match!");
HashMap fromToMap = new HashMap(); // ex: "ji" -> "yi"
HashMap fromXpathMap = new HashMap(); // ex: "th_TH_TRADITIONAL" -> "@some xpath.."
Map fromFiles = new TreeMap(); // ex: "mt.xml" -> File . Ordinary XML source files
Map emptyFromFiles = new TreeMap(); // ex: "en_US.xml" -> File . empty files generated by validSubLocales
Map generatedAliasFiles = new TreeMap(); // ex: th_TH_TRADITIONAL.xml -> File Files generated directly from the alias list. (no XML actually exists)
Map aliasFromFiles = new TreeMap(); // ex: zh_MO.xml -> File Files which actually exist in LDML and contain aliases
HashMap validSubMap = new HashMap(); // en -> "en_US en_GB ..."
// 1. get the list of input XML files
FileFilter myFilter = new FileFilter() {
@ -3582,24 +3589,38 @@ public class LDML2ICUConverter {
return(!f.isDirectory()
&&n.endsWith(".xml")
&&!n.startsWith("supplementalData") // not a locale
&&!n.startsWith("root")); // root is implied, will be included elsewhere.
/*&&!n.startsWith("root")*/); // root is implied, will be included elsewhere.
}
};
File inFiles[] = depF.listFiles(myFilter);
int nrInFiles = inFiles.length;
if(writeDraft == false) {
System.out.print("Parsing: " + nrInFiles + " LDML locale files to check draft status: ");
if(parseThem) {
System.out.print("Parsing: " + nrInFiles + " LDML locale files to check " +
(parseDraft?"draft, ":"") +
(parseSubLocale?"valid-sub-locales, ":"") );
}
for(int i=0;i<nrInFiles;i++) {
boolean thisOK = true;
if(writeDraft == false) {
String localeName = inFiles[i].getName();
localeName = localeName.substring(0,localeName.indexOf('.'));
if(parseThem) {
//System.out.print(" " + inFiles[i].getName() + ":" );
try {
Document doc2 = LDMLUtilities.parse(inFiles[i].toString(), false);
if(LDMLUtilities.isDraft(doc2,new StringBuffer("//ldml"))) {
if(parseDraft && LDMLUtilities.isDraft(doc2,new StringBuffer("//ldml"))) {
thisOK = false;
}
if(thisOK && parseSubLocale) {
Node collations = LDMLUtilities.getNode(doc2, "//ldml/collations");
if(collations != null) {
String vsl = LDMLUtilities.getAttributeValue(collations,"validSubLocales");
if((vsl!=null)&&(vsl.length()>0)) {
validSubMap.put(localeName, vsl);
printInfo(localeName + " <- " + vsl);
}
}
}
} catch(Throwable t) {
System.err.println("While parsing " + inFiles[i].toString() + " - ");
System.err.println(t.toString());
@ -3607,27 +3628,30 @@ public class LDML2ICUConverter {
System.exit(-1); // TODO: should be full 'parser error' stuff.
}
}
String localeName = inFiles[i].getName();
localeName = localeName.substring(0,localeName.indexOf('.'));
// System.out.println("FN put " + inFiles[i].getName());
if(thisOK) {
System.out.print("."); // regular file
fromFiles.put(inFiles[i].getName(),inFiles[i]); // add to hash
} else {
if(overrideMap.containsKey(localeName)) {
fromFiles.put(inFiles[i].getName(),inFiles[i]); // add to hash
System.out.print("o"); // override
// System.out.print("[o:"+localeName+"]");
} else {
System.out.print("d"); //draft
// System.out.print("[d:"+localeName+"]" );
}
}
if(!localeName.equals("root")) {
// System.out.println("FN put " + inFiles[i].getName());
if(thisOK) {
System.out.print("."); // regular file
fromFiles.put(inFiles[i].getName(),inFiles[i]); // add to hash
} else {
if(overrideMap.containsKey(localeName)) {
fromFiles.put(inFiles[i].getName(),inFiles[i]); // add to hash
System.out.print("o"); // override
// System.out.print("[o:"+localeName+"]");
} else {
System.out.print("d"); //draft
// System.out.print("[d:"+localeName+"]" );
}
}
}
}
if(writeDraft==false) {
if(parseThem==true) { // end the debugging line
System.out.println("");
}
// End of parsing all XML files.
// Now, parse the deprecatedLocales list
for(Node alias=node.getFirstChild();alias!=null;alias=alias.getNextSibling()){
if(alias.getNodeType()!=Node.ELEMENT_NODE){
continue;
@ -3671,7 +3695,7 @@ public class LDML2ICUConverter {
}
// write an individual file
writeDeprecatedLocale(from+".txt", fromLocale, new ULocale(to), xpath);
writeSimpleLocale(from+".txt", fromLocale, new ULocale(to), xpath,null);
}
} else if(aliasKind.equals("aliasLocale")) {
String source = LDMLUtilities.getAttributeValue(alias,"locale");
@ -3692,14 +3716,56 @@ public class LDML2ICUConverter {
System.exit(1);
}
}
// Post process: calculate any 'valid sub locales' (empty locales generated due to validSubLocales attribute)
if(!validSubMap.isEmpty() && treeName.equals("collation")) {
printInfo("Writing valid sub locs for : " + validSubMap.toString());
for(Iterator e = validSubMap.keySet().iterator();e.hasNext();) {
String actualLocale = (String)e.next();
String list = (String)validSubMap.get(actualLocale);
String validSubs[]= list.split(" ");
//printInfo(actualLocale + " .. ");
for(int i=0;i<validSubs.length;i++) {
String aSub = validSubs[i];
String testSub;
//printInfo(" " + aSub);
for(testSub = aSub;(testSub!=null) &&
!testSub.equals("root") &&
(!testSub.equals(actualLocale));testSub=LDMLUtilities.getParent(testSub)) {
//printInfo(" trying " + testSub);
if(fromFiles.containsKey(testSub + ".xml")) {
printWarning(actualLocale+".xml", " validSubLocale=" + aSub + " overridden because " + testSub + ".xml exists.");
testSub = null;
break;
}
if(generatedAliasFiles.containsKey(testSub)) {
printWarning(actualLocale+".xml", " validSubLocale=" + aSub + " overridden because an alias locale " + testSub + ".xml exists.");
testSub = null;
break;
}
}
if(testSub != null) {
emptyFromFiles.put(aSub + ".xml", new File(depF,aSub+".xml"));
writeSimpleLocale(aSub + ".txt", new ULocale(aSub), null, null,
"validSubLocale of \"" + actualLocale + "\"");
}
}
}
}
// System.out.println("In Files: " + inFileText);
String inFileText = fileMapToList(fromFiles);
String emptyFileText=null;
if(!emptyFromFiles.isEmpty()) {
emptyFileText = fileMapToList(emptyFromFiles);
}
String aliasFilesList = fileMapToList(aliasFromFiles);
String generatedAliasList = fileMapToList(generatedAliasFiles);
// Now- write the actual items (resfiles.mk, etc)
writeResourceMakefile(myTreeName,generatedAliasList,aliasFilesList,inFileText);
writeResourceMakefile(myTreeName,generatedAliasList,aliasFilesList,inFileText,emptyFileText);
System.exit(0);
return;
@ -3734,7 +3800,7 @@ public class LDML2ICUConverter {
return out;
}
private void writeDeprecatedLocale(String fileName, ULocale fromLocale, ULocale toLocale, String xpath)
private void writeSimpleLocale(String fileName, ULocale fromLocale, ULocale toLocale, String xpath, String comment)
{
if(xpath != null) {
@ -3781,13 +3847,17 @@ public class LDML2ICUConverter {
try {
ICUResourceWriter.ResourceTable table = new ICUResourceWriter.ResourceTable();
table.name = fromLocale.toString();
if(xpath == null) {
if(toLocale != null &&
xpath == null) {
ICUResourceWriter.ResourceString str = new ICUResourceWriter.ResourceString();
str.name = "\"%%ALIAS\"";
str.val = toLocale.toString();
table.first = str;
}
set = table;
if( comment != null ) {
set.comment = comment;
}
} catch (Throwable e) {
printError("","building synthetic locale tree for " + outputFileName + ": " +e.toString());
e.printStackTrace();
@ -3796,7 +3866,13 @@ public class LDML2ICUConverter {
try {
printInfo("Writing deprecated locale: " + outputFileName);
String info;
if(toLocale!=null) {
info = "(alias to " + toLocale.toString() + ")";
} else {
info = comment;
}
printInfo("Writing synthetic: " + outputFileName + " " + info);
FileOutputStream file = new FileOutputStream(outputFileName);
BufferedOutputStream writer = new BufferedOutputStream(file);
writeHeader(writer,"deprecatedList.xml");
@ -3816,14 +3892,14 @@ public class LDML2ICUConverter {
writer.flush();
writer.close();
}catch( IOException e) {
System.err.println("ERROR: While writing deprecated locale " + outputFileName + ": " +e.toString());
System.err.println("ERROR: While writing synthetic locale " + outputFileName + ": " +e.toString());
e.printStackTrace();
System.exit(1);
}
}
}
private void writeResourceMakefile(String myTreeName, String generatedAliasList, String aliasFilesList, String inFileText)
private void writeResourceMakefile(String myTreeName, String generatedAliasList, String aliasFilesList, String inFileText, String emptyFileText)
{
/// Write resfiles.mk
String stub = "UNKNOWN";
@ -3877,9 +3953,19 @@ public class LDML2ICUConverter {
resfiles_mk.println("# All aliases (to not be included under 'installed'), but not including root.");
resfiles_mk.println( stub + "_ALIAS_SOURCE = $(" + stub + "_SYNTHETIC_ALIAS)" + aliasFilesList );
resfiles_mk.println( "" );
resfiles_mk.println( "" );
if(emptyFileText != null) {
resfiles_mk.println("# Empty locales, used for validSubLocale fallback.");
resfiles_mk.println( stub + "_EMPTY_SOURCE =" + emptyFileText ); // note: lists start with a space.
}
resfiles_mk.println( "" );
resfiles_mk.println( "" );
resfiles_mk.println( "# Ordinary resources");
resfiles_mk.print( stub + "_SOURCE =" + inFileText );
if(emptyFileText == null) {
resfiles_mk.print( stub + "_SOURCE =" + inFileText );
} else {
resfiles_mk.print( stub + "_SOURCE = $(" + stub + "_EMPTY_SOURCE)" + inFileText );
}
resfiles_mk.println( "" );
resfiles_mk.println( "" );

View file

@ -1447,4 +1447,14 @@ System.err.println(filename2 + ":" + e.getLineNumber() + (col>=0?":" + col:"")
return str;
}
// This should be in ULocale!
public static String getParent(String locale) {
int pos = locale.lastIndexOf('_');
if (pos >= 0) {
return locale.substring(0,pos);
}
if (!locale.equals("root")) return "root";
return null;
}
}