[Unicode]   Common Locale Data Repository : Bug Tracking Home | Site Map | Search
 

Changeset 4332


Ignore:
Timestamp:
10/13/09 12:57:09 (9 years ago)
Author:
dougfelt
Message:

cldrbug 2387: reindent

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/java/org/unicode/cldr/icu/DeprecatedConverter.java

    r4330 r4332  
    4545  private final File depDir; 
    4646  private final File dstDir; 
    47    
     47 
    4848  private static final String DEPRECATED_LIST = "icu-config.xml & build.xml"; 
    4949 
     
    5454    this.dstDir = dstDir; 
    5555  } 
    56    
     56 
    5757  public void write(ICUWriter writer, List<String> emptyLocaleList, Map<String, Alias> aliasMap, 
    5858      List<String> aliasLocaleList, boolean parseDraft, boolean parseSubLocale) { 
    59   String myTreeName = depDir.getName(); 
    60   final File[] destFiles = dstDir.listFiles(); 
    61  
    62   // parse a bunch of locales? 
    63   boolean parseThem = parseDraft || parseSubLocale; 
    64  
    65   // ex: "ji" -> "yi" 
    66   TreeMap<String, String> fromToMap = new TreeMap<String, String>(); 
    67  
    68   // ex:  "th_TH_TRADITIONAL" -> "@some xpath.." 
    69   TreeMap<String, String> fromXpathMap = new TreeMap<String, String>(); 
    70  
    71   // ex:  "mt.xml" -> File .  Ordinary XML source files 
    72   Map<String, File> fromFiles = new TreeMap<String, File>(); 
    73  
    74   // ex:  "en_US.xml" -> File .  empty files generated by validSubLocales 
    75   Map<String, File> emptyFromFiles = new TreeMap<String, File>(); 
    76  
    77   // ex:  th_TH_TRADITIONAL.xml -> File  Files generated directly from the alias list 
    78   // (no XML actually exists). 
    79   Map<String, File> generatedAliasFiles = new TreeMap<String, File>(); 
    80  
    81   // ex: zh_MO.xml -> File  Files which actually exist in LDML and contain aliases 
    82   Map<String, File> aliasFromFiles = new TreeMap<String, File>(); 
    83  
    84   // en -> "en_US en_GB ..." 
    85   TreeMap<String, String> validSubMap = new TreeMap<String, String>(); 
    86  
    87   // for in -> id where id is a synthetic alias 
    88   TreeMap<String, String> maybeValidAlias = new TreeMap<String, String>(); 
    89  
    90   // 1. get the list of input XML files 
    91   FileFilter myFilter = new FileFilter() { 
    92     public boolean accept(File f) { 
    93       String n = f.getName(); 
    94       return !f.isDirectory() 
    95           && n.endsWith(".xml") 
    96           && !n.startsWith("supplementalData") // not a locale 
    97           /* &&!n.startsWith("root") */ 
    98           && isInDest(n); // root is implied, will be included elsewhere. 
    99     } 
    100  
    101     public boolean isInDest(String n) { 
    102       String name = n.substring(0, n.indexOf('.') + 1); 
    103       for (int i = 0; i < destFiles.length; i++) { 
    104         String dest = destFiles[i].getName(); 
    105         if (dest.indexOf(name) == 0) { 
    106           return true; 
    107         } 
    108       } 
    109  
    110       return false; 
    111     } 
    112   }; 
    113  
    114   File inFiles[] = depDir.listFiles(myFilter); 
    115  
    116   int nrInFiles = inFiles.length; 
    117   if (parseThem) { 
     59    String myTreeName = depDir.getName(); 
     60    final File[] destFiles = dstDir.listFiles(); 
     61 
     62    // parse a bunch of locales? 
     63    boolean parseThem = parseDraft || parseSubLocale; 
     64 
     65    // ex: "ji" -> "yi" 
     66    TreeMap<String, String> fromToMap = new TreeMap<String, String>(); 
     67 
     68    // ex:  "th_TH_TRADITIONAL" -> "@some xpath.." 
     69    TreeMap<String, String> fromXpathMap = new TreeMap<String, String>(); 
     70 
     71    // ex:  "mt.xml" -> File .  Ordinary XML source files 
     72    Map<String, File> fromFiles = new TreeMap<String, File>(); 
     73 
     74    // ex:  "en_US.xml" -> File .  empty files generated by validSubLocales 
     75    Map<String, File> emptyFromFiles = new TreeMap<String, File>(); 
     76 
     77    // ex:  th_TH_TRADITIONAL.xml -> File  Files generated directly from the alias list 
     78    // (no XML actually exists). 
     79    Map<String, File> generatedAliasFiles = new TreeMap<String, File>(); 
     80 
     81    // ex: zh_MO.xml -> File  Files which actually exist in LDML and contain aliases 
     82    Map<String, File> aliasFromFiles = new TreeMap<String, File>(); 
     83 
     84    // en -> "en_US en_GB ..." 
     85    TreeMap<String, String> validSubMap = new TreeMap<String, String>(); 
     86 
     87    // for in -> id where id is a synthetic alias 
     88    TreeMap<String, String> maybeValidAlias = new TreeMap<String, String>(); 
     89 
     90    // 1. get the list of input XML files 
     91    FileFilter myFilter = new FileFilter() { 
     92      public boolean accept(File f) { 
     93        String n = f.getName(); 
     94        return !f.isDirectory() 
     95        && n.endsWith(".xml") 
     96        && !n.startsWith("supplementalData") // not a locale 
     97        /* &&!n.startsWith("root") */ 
     98        && isInDest(n); // root is implied, will be included elsewhere. 
     99      } 
     100 
     101      public boolean isInDest(String n) { 
     102        String name = n.substring(0, n.indexOf('.') + 1); 
     103        for (int i = 0; i < destFiles.length; i++) { 
     104          String dest = destFiles[i].getName(); 
     105          if (dest.indexOf(name) == 0) { 
     106            return true; 
     107          } 
     108        } 
     109 
     110        return false; 
     111      } 
     112    }; 
     113 
     114    File inFiles[] = depDir.listFiles(myFilter); 
     115 
     116    int nrInFiles = inFiles.length; 
     117    if (parseThem) { 
     118      log.setStatus(null); 
     119      log.log("Parsing: " + nrInFiles + " LDML locale files to check " 
     120          + (parseDraft ? "draft, " : "") 
     121          + (parseSubLocale ? "valid-sub-locales, " : "")); 
     122    } 
     123 
     124    for (int i = 0; i < nrInFiles; i++) { 
     125      if (i > 0 && (i % 60 == 0)) { 
     126        System.out.println(" " + i); 
     127        System.out.flush(); 
     128      } 
     129      boolean thisOK = true; 
     130      String localeName = inFiles[i].getName(); 
     131      localeName = localeName.substring(0, localeName.indexOf('.')); 
     132      log.setStatus(localeName); 
     133      if (parseThem) { 
     134        try { 
     135          Document doc2 = LDMLUtilities.parse(inFiles[i].toString(), false); 
     136          // TODO: figure out if this is really required 
     137          if (parseDraft && LDMLUtilities.isLocaleDraft(doc2)) { 
     138            thisOK = false; 
     139          } 
     140          if (thisOK && parseSubLocale) { 
     141            Node collations = LDMLUtilities.getNode(doc2, "//ldml/collations"); 
     142            if (collations != null) { 
     143              String vsl = LDMLUtilities.getAttributeValue(collations, "validSubLocales"); 
     144              if (vsl != null && vsl.length() > 0) { 
     145                validSubMap.put(localeName, vsl); 
     146                log.info(localeName + " <- " + vsl); 
     147              } 
     148            } 
     149          } 
     150        } catch (Throwable t) { 
     151          log.error(t.getMessage(), t); 
     152          System.exit(-1); // TODO: should be full 'parser error'stuff. 
     153        } 
     154      } 
     155 
     156      if (!localeName.equals("root")) { 
     157        if (thisOK) { 
     158          System.out.print("."); // regular file 
     159          fromFiles.put(inFiles[i].getName(), inFiles[i]); // add to hash 
     160        } else { 
     161          if (services.isDraftStatusOverridable(localeName)) { 
     162            fromFiles.put(inFiles[i].getName(), inFiles[i]); // add to hash 
     163            System.out.print("o"); // override 
     164            // System.out.print("[o:" + localeName + "]"); 
     165          } else { 
     166            System.out.print("d"); // draft 
     167            // System.out.print("[d:" + localeName + "]"); 
     168          } 
     169        } 
     170      } else { 
     171        System.out.print("_"); 
     172      } 
     173      log.setStatus(null); 
     174    } 
     175 
     176    if (parseThem == true) { 
     177      // end the debugging line 
     178      System.out.println(); 
     179    } 
     180 
     181    // End of parsing all XML files. 
     182    if (emptyLocaleList != null && emptyLocaleList.size() > 0) { 
     183      for (int i = 0; i < emptyLocaleList.size(); i++) { 
     184        String loc = emptyLocaleList.get(i); 
     185        writeSimpleLocaleAlias(writer, loc, loc, null, "empty locale file for dependency checking"); 
     186        // we do not want these files to show up in installed locales list! 
     187        generatedAliasFiles.put(loc + ".xml", new File(depDir, loc + ".xml")); 
     188      } 
     189    } 
     190 
     191    // interpret the deprecated locales list 
     192    if (aliasMap != null && aliasMap.size() > 0) { 
     193      for (Iterator<String> i = aliasMap.keySet().iterator(); i.hasNext();) { 
     194        String from = i.next(); 
     195        log.setStatus(String.valueOf(from)); 
     196        Alias value = aliasMap.get(from); 
     197        String to = value.to; 
     198        String xpath = value.xpath; 
     199        if (to.indexOf('@') != -1 && xpath == null) { 
     200          log.error("Malformed alias - '@' but no xpath: from=\"" + from + "\" to=\"" + to + "\""); 
     201          System.exit(-1); 
     202          return; // NOTREACHED 
     203        } 
     204 
     205        if (from == null || to == null) { 
     206          log.error("Malformed alias - no 'from' or 'to': from=\"" + from + "\" to=\"" + to + "\""); 
     207          System.exit(-1); 
     208          return; // NOTREACHED 
     209        } 
     210 
     211        String toFileName = to; 
     212        if (xpath != null) { 
     213          toFileName = to.substring(0, to.indexOf('@')); 
     214        } 
     215        if (fromFiles.containsKey(from + ".xml")) { 
     216          throw new IllegalArgumentException( 
     217              "Can't be both a synthetic alias locale and a real xml file - " 
     218              + "consider using <aliasLocale locale=\"" + from + "\"/> instead. "); 
     219        } 
     220 
     221        ULocale fromLocale = new ULocale(from); 
     222        String fromLocaleName = fromLocale.toString(); 
     223        if (!fromFiles.containsKey(toFileName + ".xml")) { 
     224          maybeValidAlias.put(toFileName, from); 
     225        } else { 
     226          generatedAliasFiles.put(from, new File(depDir, from + ".xml")); 
     227          fromToMap.put(fromLocale.toString(), to); 
     228          if (xpath != null) { 
     229            fromXpathMap.put(fromLocale.toString(), xpath); 
     230 
     231            CLDRFile fakeFile = CLDRFile.make(fromLocaleName); 
     232            fakeFile.add(xpath, ""); 
     233            fakeFile.freeze(); 
     234            Resource res = services.parseBundle(fakeFile); 
     235 
     236            if (res != null && ((ResourceTable) res).first != null) { 
     237              res.name = fromLocaleName; 
     238              writer.writeResource(res, DEPRECATED_LIST); 
     239            } else { 
     240              // parse error? 
     241              log.error("Failed to write out alias bundle " + fromLocaleName + " from " + xpath 
     242                  + " - XML list follows:"); 
     243              fakeFile.write(new PrintWriter(System.out)); 
     244            } 
     245 
     246          } else { 
     247            String toLocaleName = new ULocale(to).toString(); 
     248            writeSimpleLocaleAlias(writer, from, fromLocaleName, toLocaleName, null); 
     249          } 
     250        } 
     251      } 
     252      log.setStatus(null); 
     253    } 
     254 
     255    if (aliasLocaleList != null && aliasLocaleList.size() > 0) { 
     256      for (int i = 0; i < aliasLocaleList.size(); i++) { 
     257        String source = aliasLocaleList.get(i) + ".xml"; 
     258        log.setStatus(source); 
     259        if (!fromFiles.containsKey(source)) { 
     260          log.warning("Alias file named in deprecates list but not present. Ignoring alias entry."); 
     261        } else { 
     262          aliasFromFiles.put(source, new File(depDir, source)); 
     263          fromFiles.remove(source); 
     264        } 
     265      } 
     266      log.setStatus(null); 
     267    } 
     268 
     269    // Post process: calculate any 'valid sub locales' (empty locales 
     270    // generated due to validSubLocales attribute) 
     271    if (!validSubMap.isEmpty() && parseSubLocale) { 
     272      log.info("Writing valid sub locs for: " + validSubMap.toString()); 
     273 
     274      for (Iterator<String> e = validSubMap.keySet().iterator(); e.hasNext();) { 
     275        String actualLocale = e.next(); 
     276        log.setStatus(actualLocale + ".xml"); 
     277        String list = validSubMap.get(actualLocale); 
     278        String validSubs[] = list.split(" "); 
     279        for (int i = 0; i < validSubs.length; i++) { 
     280          String aSub = validSubs[i]; 
     281          String testSub; 
     282 
     283          for (testSub = aSub; 
     284          testSub != null && !testSub.equals("root") && !testSub.equals(actualLocale); 
     285          testSub = LDMLUtilities.getParent(testSub)) { 
     286 
     287            if (fromFiles.containsKey(testSub + ".xml")) { 
     288              log.warning( 
     289                  "validSubLocale=" + aSub + " overridden because  " + testSub + ".xml  exists."); 
     290              testSub = null; 
     291              break; 
     292            } 
     293 
     294            if (generatedAliasFiles.containsKey(testSub)) { 
     295              log.warning( 
     296                  "validSubLocale=" + aSub + " overridden because an alias locale " + testSub 
     297                  + ".xml  exists."); 
     298              testSub = null; 
     299              break; 
     300            } 
     301          } 
     302 
     303          if (testSub != null) { 
     304            emptyFromFiles.put(aSub + ".xml", new File(depDir, aSub + ".xml")); 
     305            if (maybeValidAlias.containsKey(aSub)) { 
     306              String from = maybeValidAlias.get(aSub); 
     307              writeSimpleLocaleAlias(writer, from, from, aSub, null); 
     308              maybeValidAlias.remove(aSub); 
     309              generatedAliasFiles.put(from, new File(depDir, from + ".xml")); 
     310            } 
     311            writeSimpleLocaleAlias(writer, aSub, aSub, null, "validSubLocale of \"" + actualLocale  
     312                + "\""); 
     313          } 
     314        } 
     315      } 
     316      log.setStatus(null); 
     317    } 
     318 
     319    if (!maybeValidAlias.isEmpty()) { 
     320      Set<String> keys = maybeValidAlias.keySet(); 
     321      Iterator<String> iter = keys.iterator(); 
     322      while (iter.hasNext()) { 
     323        String to = iter.next(); 
     324        String from = maybeValidAlias.get(to); 
     325        log.warning("Alias from \"" + from 
     326            + "\" not generated, because it would point to a nonexistent LDML file " + to + ".xml"); 
     327      } 
     328    } 
     329 
     330    String inFileText = fileMapToList(fromFiles); 
     331    String emptyFileText = null; 
     332    if (!emptyFromFiles.isEmpty()) { 
     333      emptyFileText = fileMapToList(emptyFromFiles); 
     334    } 
     335    String aliasFilesList = fileMapToList(aliasFromFiles); 
     336    String generatedAliasList = fileMapToList(generatedAliasFiles); 
     337 
     338    // Now- write the actual items (resfiles.mk, etc) 
     339    String[] brkArray = new String[2]; 
     340    if (myTreeName.equals("brkitr")) { 
     341      getBrkCtdFilesList(depDir, brkArray); 
     342    } 
     343 
     344    writeResourceMakefile(myTreeName, generatedAliasList, aliasFilesList, 
     345        inFileText, emptyFileText, brkArray[0], brkArray[1]); 
     346 
    118347    log.setStatus(null); 
    119     log.log("Parsing: " + nrInFiles + " LDML locale files to check " 
    120         + (parseDraft ? "draft, " : "") 
    121         + (parseSubLocale ? "valid-sub-locales, " : "")); 
     348    log.log("WriteDeprecated done."); 
    122349  } 
    123350 
    124   for (int i = 0; i < nrInFiles; i++) { 
    125     if (i > 0 && (i % 60 == 0)) { 
    126       System.out.println(" " + i); 
    127       System.out.flush(); 
    128     } 
    129     boolean thisOK = true; 
    130     String localeName = inFiles[i].getName(); 
    131     localeName = localeName.substring(0, localeName.indexOf('.')); 
    132     log.setStatus(localeName); 
    133     if (parseThem) { 
    134       try { 
    135         Document doc2 = LDMLUtilities.parse(inFiles[i].toString(), false); 
    136         // TODO: figure out if this is really required 
    137         if (parseDraft && LDMLUtilities.isLocaleDraft(doc2)) { 
    138           thisOK = false; 
    139         } 
    140         if (thisOK && parseSubLocale) { 
    141           Node collations = LDMLUtilities.getNode(doc2, "//ldml/collations"); 
    142           if (collations != null) { 
    143             String vsl = LDMLUtilities.getAttributeValue(collations, "validSubLocales"); 
    144             if (vsl != null && vsl.length() > 0) { 
    145               validSubMap.put(localeName, vsl); 
    146               log.info(localeName + " <- " + vsl); 
    147             } 
    148           } 
    149         } 
    150       } catch (Throwable t) { 
    151         log.error(t.getMessage(), t); 
    152         System.exit(-1); // TODO: should be full 'parser error'stuff. 
    153       } 
    154     } 
    155  
    156     if (!localeName.equals("root")) { 
    157       if (thisOK) { 
    158         System.out.print("."); // regular file 
    159         fromFiles.put(inFiles[i].getName(), inFiles[i]); // add to hash 
     351  private String fileMapToList(Map<String, File> files) { 
     352    return ICUWriter.fileIteratorToList(files.values().iterator()); 
     353  } 
     354 
     355  private String[] getBrkCtdFilesList(File directory, String[] brkArray) { 
     356    // read all xml files in the directory and create ctd file list and brk file list 
     357    FilenameFilter myFilter = new FilenameFilter() { 
     358      public boolean accept(File f, String name) { 
     359        return !f.isFile() 
     360        && name.endsWith(".xml") 
     361        && !name.startsWith("supplementalData"); // not a locale 
     362        // root is implied, will be included elsewhere. 
     363      } 
     364    }; 
     365 
     366    File[] files = directory.listFiles(myFilter); 
     367    StringBuilder brkList = new StringBuilder(); 
     368    StringBuilder ctdList = new StringBuilder(); 
     369 
     370    // open each file and create the list of files for brk and ctd 
     371    for (File file : files) { 
     372      String fileName = file.getName(); 
     373      String filePath = file.getAbsolutePath(); 
     374      Document doc = LDMLUtilities.parse(filePath, false); 
     375      log.setStatus(fileName); 
     376      for(Node node = doc.getFirstChild(); node != null; node = node.getNextSibling()) { 
     377        if (node.getNodeType() != Node.ELEMENT_NODE) { 
     378          continue; 
     379        } 
     380 
     381        String name = node.getNodeName(); 
     382        if (name.equals(LDMLConstants.LDML)) { 
     383          node = node.getFirstChild(); 
     384          continue; 
     385        } 
     386 
     387        if (name.equals(LDMLConstants.IDENTITY)) { 
     388          continue; 
     389        } 
     390 
     391        if (name.equals(LDMLConstants.SPECIAL)) { 
     392          node = node.getFirstChild(); 
     393          continue; 
     394        } 
     395 
     396        if (name.equals(ICU_BRKITR_DATA)) { 
     397          node = node.getFirstChild(); 
     398          continue; 
     399        } 
     400 
     401        if (name.equals(ICU_BOUNDARIES)) { 
     402          for (Node cn = node.getFirstChild(); cn != null; cn = cn.getNextSibling()) { 
     403            if (cn.getNodeType() != Node.ELEMENT_NODE) { 
     404              continue; 
     405            } 
     406            String cnName = cn.getNodeName(); 
     407 
     408            if (cnName.equals(ICU_GRAPHEME) 
     409                || cnName.equals(ICU_WORD) 
     410                || cnName.equals(ICU_TITLE) 
     411                || cnName.equals(ICU_SENTENCE) 
     412                || cnName.equals(ICU_XGC) 
     413                || cnName.equals(ICU_LINE)) { 
     414 
     415              String val = LDMLUtilities.getAttributeValue(cn, ICU_DEPENDENCY); 
     416              if (val != null) { 
     417                brkList.append(val.substring(0, val.indexOf('.'))); 
     418                brkList.append(".txt "); 
     419              } 
     420            } else { 
     421              log.error("Encountered unknown <" + name + "> subelement: " + cnName); 
     422              System.exit(-1); 
     423            } 
     424          } 
     425        } else if (name.equals(ICU_DICTIONARIES)) { 
     426          for (Node cn = node.getFirstChild(); cn != null; cn = cn.getNextSibling()) { 
     427            if (cn.getNodeType() != Node.ELEMENT_NODE) { 
     428              continue; 
     429            } 
     430            String cnName = cn.getNodeName(); 
     431 
     432            if (cnName.equals(ICU_DICTIONARY)) { 
     433              String val = LDMLUtilities.getAttributeValue(cn, ICU_DEPENDENCY); 
     434              if (val != null) { 
     435                ctdList.append(val.substring(0, val.indexOf('.'))); 
     436                ctdList.append(".txt "); 
     437              } 
     438            } else { 
     439              log.error("Encountered unknown <" + name + "> subelement: " + cnName); 
     440              System.exit(-1); 
     441            } 
     442          } 
     443        } else { 
     444          log.error("Encountered unknown <" + doc.getNodeName() + "> subelement: " + name); 
     445          System.exit(-1); 
     446        } 
     447      } 
     448    } 
     449 
     450    if (brkList.length() > 0) { 
     451      brkArray[0] = brkList.toString(); 
     452    } 
     453 
     454    if (ctdList.length() > 0) { 
     455      brkArray[1] = ctdList.toString(); 
     456    } 
     457 
     458    return brkArray; 
     459  } 
     460 
     461  private void writeResourceMakefile( 
     462      String myTreeName, String generatedAliasList, String aliasFilesList, String inFileText, 
     463      String emptyFileText, String brkFilesList, String ctdFilesList) { 
     464 
     465    String stub = "UNKNOWN"; 
     466    String shortstub = "unk"; 
     467 
     468    if (myTreeName.equals("main")) { 
     469      stub = "GENRB"; // GENRB_SOURCE, GENRB_ALIAS_SOURCE 
     470      shortstub = "res"; // resfiles.mk 
     471    } else if (myTreeName.equals("collation")) { 
     472      stub = "COLLATION"; // COLLATION_ALIAS_SOURCE, COLLATION_SOURCE 
     473      shortstub = "col"; // colfiles.mk 
     474    } else if (myTreeName.equals("brkitr")) { 
     475      stub = "BRK_RES"; // BRK_SOURCE, BRK_CTD_SOURCE BRK_RES_SOURCE 
     476      shortstub = "brk"; // brkfiles.mk 
     477    } else if (myTreeName.equals("rbnf")) { 
     478      stub = "RBNF"; // RBNF_SOURCE, RBNF_ALIAS_SOURCE 
     479      shortstub = "rbnf"; // brkfiles.mk 
     480    } else { 
     481      log.error("Unknown tree name in writeResourceMakefile: " + myTreeName); 
     482      System.exit(-1); 
     483    } 
     484 
     485    String resfiles_mk_name = dstDir + "/" + shortstub + "files.mk"; 
     486    try { 
     487      log.info("Writing ICU build file: " + resfiles_mk_name); 
     488      PrintStream resfiles_mk = new PrintStream(new FileOutputStream(resfiles_mk_name)); 
     489      Calendar c = Calendar.getInstance(); 
     490      resfiles_mk.println( 
     491          "# *   Copyright (C) 1998-" + c.get(Calendar.YEAR) + ", International Business Machines"); 
     492      resfiles_mk.println("# *   Corporation and others.  All Rights Reserved."); 
     493      resfiles_mk.println(stub + "_CLDR_VERSION = " + CLDRFile.GEN_VERSION); 
     494      resfiles_mk.println("# A list of txt's to build"); 
     495      resfiles_mk.println("# Note: "); 
     496      resfiles_mk.println("#"); 
     497      resfiles_mk.println("#   If you are thinking of modifying this file, READ THIS."); 
     498      resfiles_mk.println("#"); 
     499      resfiles_mk.println("# Instead of changing this file [unless you want to check it back in],"); 
     500      resfiles_mk.println( 
     501          "# you should consider creating a '" + shortstub 
     502          + "local.mk' file in this same directory."); 
     503      resfiles_mk.println("# Then, you can have your local changes remain even if you upgrade or"); 
     504      resfiles_mk.println("# reconfigure ICU."); 
     505      resfiles_mk.println("#"); 
     506      resfiles_mk.println("# Example '" + shortstub + "local.mk' files:"); 
     507      resfiles_mk.println("#"); 
     508      resfiles_mk .println("#  * To add an additional locale to the list: "); 
     509      resfiles_mk .println("#    _____________________________________________________"); 
     510      resfiles_mk.println("#    |  " + stub + "_SOURCE_LOCAL =   myLocale.txt ..."); 
     511      resfiles_mk.println("#"); 
     512      resfiles_mk.println("#  * To REPLACE the default list and only build with a few"); 
     513      resfiles_mk.println("#     locale:"); 
     514      resfiles_mk.println("#    _____________________________________________________"); 
     515      resfiles_mk.println("#    |  " + stub + "_SOURCE = ar.txt ar_AE.txt en.txt de.txt zh.txt"); 
     516      resfiles_mk.println("#"); 
     517      resfiles_mk.println("#"); 
     518      resfiles_mk .println("# Generated by LDML2ICUConverter, from LDML source files. "); 
     519      resfiles_mk.println(""); 
     520      resfiles_mk .println( 
     521          "# Aliases which do not have a corresponding xx.xml file (see " + DEPRECATED_LIST + ")"); 
     522      resfiles_mk.println( 
     523          stub + "_SYNTHETIC_ALIAS =" + generatedAliasList); // note: lists start with a space. 
     524      resfiles_mk.println(""); 
     525      resfiles_mk.println(""); 
     526      resfiles_mk.println( 
     527      "# All aliases (to not be included under 'installed'), but not including root."); 
     528      resfiles_mk.println( 
     529          stub + "_ALIAS_SOURCE = $(" + stub + "_SYNTHETIC_ALIAS)" + aliasFilesList); 
     530      resfiles_mk.println(""); 
     531      resfiles_mk.println(""); 
     532 
     533      if (ctdFilesList != null) { 
     534        resfiles_mk.println("# List of compact trie dictionary files (ctd)."); 
     535        resfiles_mk.println("BRK_CTD_SOURCE = " + ctdFilesList); 
     536        resfiles_mk.println(""); 
     537        resfiles_mk.println(""); 
     538      } 
     539 
     540      if (brkFilesList != null) { 
     541        resfiles_mk.println("# List of break iterator files (brk)."); 
     542        resfiles_mk.println("BRK_SOURCE = " + brkFilesList); 
     543        resfiles_mk.println(""); 
     544        resfiles_mk.println(""); 
     545      } 
     546 
     547      if (emptyFileText != null) { 
     548        resfiles_mk.println("# Empty locales, used for validSubLocale fallback."); 
     549        // note: lists start with a space. 
     550        resfiles_mk.println(stub + "_EMPTY_SOURCE =" + emptyFileText); 
     551        resfiles_mk.println(""); 
     552        resfiles_mk.println(""); 
     553      } 
     554 
     555      resfiles_mk.println("# Ordinary resources"); 
     556      if (emptyFileText == null) { 
     557        resfiles_mk.print(stub + "_SOURCE =" + inFileText); 
    160558      } else { 
    161         if (services.isDraftStatusOverridable(localeName)) { 
    162           fromFiles.put(inFiles[i].getName(), inFiles[i]); // add to hash 
    163           System.out.print("o"); // override 
    164           // System.out.print("[o:" + localeName + "]"); 
    165         } else { 
    166           System.out.print("d"); // draft 
    167           // System.out.print("[d:" + localeName + "]"); 
    168         } 
    169       } 
    170     } else { 
    171       System.out.print("_"); 
    172     } 
    173     log.setStatus(null); 
     559        resfiles_mk.print(stub + "_SOURCE = $(" + stub + "_EMPTY_SOURCE)" + inFileText); 
     560      } 
     561      resfiles_mk.println(""); 
     562      resfiles_mk.println(""); 
     563 
     564      resfiles_mk.close(); 
     565    } catch(IOException e) { 
     566      log.error("While writing " + resfiles_mk_name, e); 
     567      System.exit(1); 
     568    } 
    174569  } 
    175570 
    176   if (parseThem == true) { 
    177     // end the debugging line 
    178     System.out.println(); 
    179   } 
    180    
    181   // End of parsing all XML files. 
    182   if (emptyLocaleList != null && emptyLocaleList.size() > 0) { 
    183     for (int i = 0; i < emptyLocaleList.size(); i++) { 
    184       String loc = emptyLocaleList.get(i); 
    185       DeprecatedConverter.writeSimpleLocaleAlias(writer, loc, loc, null, 
    186           "empty locale file for dependency checking"); 
    187       // we do not want these files to show up in installed locales list! 
    188       generatedAliasFiles.put(loc + ".xml", new File(depDir, loc + ".xml")); 
    189     } 
    190   } 
    191  
    192   // interpret the deprecated locales list 
    193   if (aliasMap != null && aliasMap.size() > 0) { 
    194     for (Iterator<String> i = aliasMap.keySet().iterator(); i.hasNext();) { 
    195       String from = i.next(); 
    196       log.setStatus(String.valueOf(from)); 
    197       Alias value = aliasMap.get(from); 
    198       String to = value.to; 
    199       String xpath = value.xpath; 
    200       if (to.indexOf('@') != -1 && xpath == null) { 
    201         log.error("Malformed alias - '@' but no xpath: from=\"" + from + "\" to=\"" + to + "\""); 
    202         System.exit(-1); 
    203         return; // NOTREACHED 
    204       } 
    205  
    206       if (from == null || to == null) { 
    207         log.error("Malformed alias - no 'from' or 'to': from=\"" + from + "\" to=\"" + to + "\""); 
    208         System.exit(-1); 
    209         return; // NOTREACHED 
    210       } 
    211  
    212       String toFileName = to; 
    213       if (xpath != null) { 
    214         toFileName = to.substring(0, to.indexOf('@')); 
    215       } 
    216       if (fromFiles.containsKey(from + ".xml")) { 
    217         throw new IllegalArgumentException( 
    218             "Can't be both a synthetic alias locale and a real xml file - " 
    219             + "consider using <aliasLocale locale=\"" + from + "\"/> instead. "); 
    220       } 
    221  
    222       ULocale fromLocale = new ULocale(from); 
    223       String fromLocaleName = fromLocale.toString(); 
    224       if (!fromFiles.containsKey(toFileName + ".xml")) { 
    225         maybeValidAlias.put(toFileName, from); 
    226       } else { 
    227         generatedAliasFiles.put(from, new File(depDir, from + ".xml")); 
    228         fromToMap.put(fromLocale.toString(), to); 
    229         if (xpath != null) { 
    230           fromXpathMap.put(fromLocale.toString(), xpath); 
    231            
    232           CLDRFile fakeFile = CLDRFile.make(fromLocaleName); 
    233           fakeFile.add(xpath, ""); 
    234           fakeFile.freeze(); 
    235           Resource res = services.parseBundle(fakeFile); 
    236            
    237           if (res != null && ((ResourceTable) res).first != null) { 
    238             res.name = fromLocaleName; 
    239             writer.writeResource(res, DEPRECATED_LIST); 
    240           } else { 
    241             // parse error? 
    242             log.error("Failed to write out alias bundle " + fromLocaleName + " from " + xpath 
    243                 + " - XML list follows:"); 
    244             fakeFile.write(new PrintWriter(System.out)); 
    245           } 
    246  
    247         } else { 
    248           String toLocaleName = new ULocale(to).toString(); 
    249           DeprecatedConverter.writeSimpleLocaleAlias(writer, from, fromLocaleName, 
    250               toLocaleName, null); 
    251         } 
    252       } 
    253     } 
    254     log.setStatus(null); 
    255   } 
    256  
    257   if (aliasLocaleList != null && aliasLocaleList.size() > 0) { 
    258     for (int i = 0; i < aliasLocaleList.size(); i++) { 
    259       String source = aliasLocaleList.get(i) + ".xml"; 
    260       log.setStatus(source); 
    261       if (!fromFiles.containsKey(source)) { 
    262         log.warning("Alias file named in deprecates list but not present. Ignoring alias entry."); 
    263       } else { 
    264         aliasFromFiles.put(source, new File(depDir, source)); 
    265         fromFiles.remove(source); 
    266       } 
    267     } 
    268     log.setStatus(null); 
    269   } 
    270  
    271   // Post process: calculate any 'valid sub locales' (empty locales 
    272   // generated due to validSubLocales attribute) 
    273   if (!validSubMap.isEmpty() && parseSubLocale) { 
    274     log.info("Writing valid sub locs for: " + validSubMap.toString()); 
    275  
    276     for (Iterator<String> e = validSubMap.keySet().iterator(); e.hasNext();) { 
    277       String actualLocale = e.next(); 
    278       log.setStatus(actualLocale + ".xml"); 
    279       String list = validSubMap.get(actualLocale); 
    280       String validSubs[] = list.split(" "); 
    281       for (int i = 0; i < validSubs.length; i++) { 
    282         String aSub = validSubs[i]; 
    283         String testSub; 
    284  
    285         for (testSub = aSub; 
    286              testSub != null && !testSub.equals("root") && !testSub.equals(actualLocale); 
    287              testSub = LDMLUtilities.getParent(testSub)) { 
    288  
    289           if (fromFiles.containsKey(testSub + ".xml")) { 
    290             log.warning( 
    291                 "validSubLocale=" + aSub + " overridden because  " + testSub + ".xml  exists."); 
    292             testSub = null; 
    293             break; 
    294           } 
    295  
    296           if (generatedAliasFiles.containsKey(testSub)) { 
    297             log.warning( 
    298                 "validSubLocale=" + aSub + " overridden because an alias locale " + testSub 
    299                 + ".xml  exists."); 
    300             testSub = null; 
    301             break; 
    302           } 
    303         } 
    304  
    305         if (testSub != null) { 
    306           emptyFromFiles.put(aSub + ".xml", new File(depDir, aSub + ".xml")); 
    307           if (maybeValidAlias.containsKey(aSub)) { 
    308             String from = maybeValidAlias.get(aSub); 
    309             DeprecatedConverter.writeSimpleLocaleAlias(writer, from, from, 
    310                 aSub, null); 
    311             maybeValidAlias.remove(aSub); 
    312             generatedAliasFiles.put(from, new File(depDir, from + ".xml")); 
    313           } 
    314           DeprecatedConverter.writeSimpleLocaleAlias(writer, aSub, aSub, null, 
    315               "validSubLocale of \"" + actualLocale + "\""); 
    316         } 
    317       } 
    318     } 
    319     log.setStatus(null); 
    320   } 
    321  
    322   if (!maybeValidAlias.isEmpty()) { 
    323     Set<String> keys = maybeValidAlias.keySet(); 
    324     Iterator<String> iter = keys.iterator(); 
    325     while (iter.hasNext()) { 
    326       String to = iter.next(); 
    327       String from = maybeValidAlias.get(to); 
    328       log.warning("Alias from \"" + from 
    329           + "\" not generated, because it would point to a nonexistent LDML file " + to + ".xml"); 
    330     } 
    331   } 
    332    
    333   String inFileText = fileMapToList(fromFiles); 
    334   String emptyFileText = null; 
    335   if (!emptyFromFiles.isEmpty()) { 
    336     emptyFileText = fileMapToList(emptyFromFiles); 
    337   } 
    338   String aliasFilesList = fileMapToList(aliasFromFiles); 
    339   String generatedAliasList = fileMapToList(generatedAliasFiles); 
    340  
    341   // Now- write the actual items (resfiles.mk, etc) 
    342   String[] brkArray = new String[2]; 
    343   if (myTreeName.equals("brkitr")) { 
    344     getBrkCtdFilesList(depDir, brkArray); 
    345   } 
    346    
    347   writeResourceMakefile(myTreeName, generatedAliasList, aliasFilesList, 
    348           inFileText, emptyFileText, brkArray[0], brkArray[1]); 
    349  
    350   log.setStatus(null); 
    351   log.log("WriteDeprecated done."); 
    352 } 
    353  
    354 private String fileMapToList(Map<String, File> files) { 
    355   return ICUWriter.fileIteratorToList(files.values().iterator()); 
    356 } 
    357  
    358 private String[] getBrkCtdFilesList(File directory, String[] brkArray) { 
    359   // read all xml files in the directory and create ctd file list and brk file list 
    360   FilenameFilter myFilter = new FilenameFilter() { 
    361     public boolean accept(File f, String name) { 
    362       return !f.isFile() 
    363           && name.endsWith(".xml") 
    364           && !name.startsWith("supplementalData"); // not a locale 
    365       // root is implied, will be included elsewhere. 
    366     } 
    367   }; 
    368  
    369   File[] files = directory.listFiles(myFilter); 
    370   StringBuilder brkList = new StringBuilder(); 
    371   StringBuilder ctdList = new StringBuilder(); 
    372  
    373   // open each file and create the list of files for brk and ctd 
    374   for (File file : files) { 
    375     String fileName = file.getName(); 
    376     String filePath = file.getAbsolutePath(); 
    377     Document doc = LDMLUtilities.parse(filePath, false); 
    378     log.setStatus(fileName); 
    379     for(Node node = doc.getFirstChild(); node != null; node = node.getNextSibling()) { 
    380       if (node.getNodeType() != Node.ELEMENT_NODE) { 
    381         continue; 
    382       } 
    383  
    384       String name = node.getNodeName(); 
    385       if (name.equals(LDMLConstants.LDML)) { 
    386         node = node.getFirstChild(); 
    387         continue; 
    388       } 
    389  
    390       if (name.equals(LDMLConstants.IDENTITY)) { 
    391         continue; 
    392       } 
    393  
    394       if (name.equals(LDMLConstants.SPECIAL)) { 
    395         node = node.getFirstChild(); 
    396         continue; 
    397       } 
    398  
    399       if (name.equals(ICU_BRKITR_DATA)) { 
    400         node = node.getFirstChild(); 
    401         continue; 
    402       } 
    403  
    404       if (name.equals(ICU_BOUNDARIES)) { 
    405         for (Node cn = node.getFirstChild(); cn != null; cn = cn.getNextSibling()) { 
    406           if (cn.getNodeType() != Node.ELEMENT_NODE) { 
    407             continue; 
    408           } 
    409           String cnName = cn.getNodeName(); 
    410  
    411           if (cnName.equals(ICU_GRAPHEME) 
    412               || cnName.equals(ICU_WORD) 
    413               || cnName.equals(ICU_TITLE) 
    414               || cnName.equals(ICU_SENTENCE) 
    415               || cnName.equals(ICU_XGC) 
    416               || cnName.equals(ICU_LINE)) { 
    417  
    418             String val = LDMLUtilities.getAttributeValue(cn, ICU_DEPENDENCY); 
    419             if (val != null) { 
    420               brkList.append(val.substring(0, val.indexOf('.'))); 
    421               brkList.append(".txt "); 
    422             } 
    423           } else { 
    424             log.error("Encountered unknown <" + name + "> subelement: " + cnName); 
    425             System.exit(-1); 
    426           } 
    427         } 
    428       } else if (name.equals(ICU_DICTIONARIES)) { 
    429         for (Node cn = node.getFirstChild(); cn != null; cn = cn.getNextSibling()) { 
    430           if (cn.getNodeType() != Node.ELEMENT_NODE) { 
    431             continue; 
    432           } 
    433           String cnName = cn.getNodeName(); 
    434  
    435           if (cnName.equals(ICU_DICTIONARY)) { 
    436             String val = LDMLUtilities.getAttributeValue(cn, ICU_DEPENDENCY); 
    437             if (val != null) { 
    438               ctdList.append(val.substring(0, val.indexOf('.'))); 
    439               ctdList.append(".txt "); 
    440             } 
    441           } else { 
    442             log.error("Encountered unknown <" + name + "> subelement: " + cnName); 
    443             System.exit(-1); 
    444           } 
    445         } 
    446       } else { 
    447         log.error("Encountered unknown <" + doc.getNodeName() + "> subelement: " + name); 
    448         System.exit(-1); 
    449       } 
    450     } 
    451   } 
    452  
    453   if (brkList.length() > 0) { 
    454     brkArray[0] = brkList.toString(); 
    455   } 
    456  
    457   if (ctdList.length() > 0) { 
    458     brkArray[1] = ctdList.toString(); 
    459   } 
    460  
    461   return brkArray; 
    462 } 
    463  
    464 private void writeResourceMakefile( 
    465     String myTreeName, String generatedAliasList, String aliasFilesList, String inFileText, 
    466     String emptyFileText, String brkFilesList, String ctdFilesList) { 
    467  
    468   String stub = "UNKNOWN"; 
    469   String shortstub = "unk"; 
    470  
    471   if (myTreeName.equals("main")) { 
    472     stub = "GENRB"; // GENRB_SOURCE, GENRB_ALIAS_SOURCE 
    473     shortstub = "res"; // resfiles.mk 
    474   } else if (myTreeName.equals("collation")) { 
    475     stub = "COLLATION"; // COLLATION_ALIAS_SOURCE, COLLATION_SOURCE 
    476     shortstub = "col"; // colfiles.mk 
    477   } else if (myTreeName.equals("brkitr")) { 
    478     stub = "BRK_RES"; // BRK_SOURCE, BRK_CTD_SOURCE BRK_RES_SOURCE 
    479     shortstub = "brk"; // brkfiles.mk 
    480   } else if (myTreeName.equals("rbnf")) { 
    481     stub = "RBNF"; // RBNF_SOURCE, RBNF_ALIAS_SOURCE 
    482     shortstub = "rbnf"; // brkfiles.mk 
    483   } else { 
    484     log.error("Unknown tree name in writeResourceMakefile: " + myTreeName); 
    485     System.exit(-1); 
    486   } 
    487  
    488   String resfiles_mk_name = dstDir + "/" + shortstub + "files.mk"; 
    489   try { 
    490     log.info("Writing ICU build file: " + resfiles_mk_name); 
    491     PrintStream resfiles_mk = new PrintStream(new FileOutputStream(resfiles_mk_name)); 
    492     Calendar c = Calendar.getInstance(); 
    493     resfiles_mk.println( 
    494         "# *   Copyright (C) 1998-" + c.get(Calendar.YEAR) + ", International Business Machines"); 
    495     resfiles_mk.println("# *   Corporation and others.  All Rights Reserved."); 
    496     resfiles_mk.println(stub + "_CLDR_VERSION = " + CLDRFile.GEN_VERSION); 
    497     resfiles_mk.println("# A list of txt's to build"); 
    498     resfiles_mk.println("# Note: "); 
    499     resfiles_mk.println("#"); 
    500     resfiles_mk.println("#   If you are thinking of modifying this file, READ THIS."); 
    501     resfiles_mk.println("#"); 
    502     resfiles_mk.println("# Instead of changing this file [unless you want to check it back in],"); 
    503     resfiles_mk.println( 
    504         "# you should consider creating a '" + shortstub 
    505         + "local.mk' file in this same directory."); 
    506     resfiles_mk.println("# Then, you can have your local changes remain even if you upgrade or"); 
    507     resfiles_mk.println("# reconfigure ICU."); 
    508     resfiles_mk.println("#"); 
    509     resfiles_mk.println("# Example '" + shortstub + "local.mk' files:"); 
    510     resfiles_mk.println("#"); 
    511     resfiles_mk .println("#  * To add an additional locale to the list: "); 
    512     resfiles_mk .println("#    _____________________________________________________"); 
    513     resfiles_mk.println("#    |  " + stub + "_SOURCE_LOCAL =   myLocale.txt ..."); 
    514     resfiles_mk.println("#"); 
    515     resfiles_mk.println("#  * To REPLACE the default list and only build with a few"); 
    516     resfiles_mk.println("#     locale:"); 
    517     resfiles_mk.println("#    _____________________________________________________"); 
    518     resfiles_mk.println("#    |  " + stub + "_SOURCE = ar.txt ar_AE.txt en.txt de.txt zh.txt"); 
    519     resfiles_mk.println("#"); 
    520     resfiles_mk.println("#"); 
    521     resfiles_mk .println("# Generated by LDML2ICUConverter, from LDML source files. "); 
    522     resfiles_mk.println(""); 
    523     resfiles_mk .println( 
    524         "# Aliases which do not have a corresponding xx.xml file (see " + DEPRECATED_LIST + ")"); 
    525     resfiles_mk.println( 
    526         stub + "_SYNTHETIC_ALIAS =" + generatedAliasList); // note: lists start with a space. 
    527     resfiles_mk.println(""); 
    528     resfiles_mk.println(""); 
    529     resfiles_mk.println( 
    530         "# All aliases (to not be included under 'installed'), but not including root."); 
    531     resfiles_mk.println(stub + "_ALIAS_SOURCE = $(" + stub 
    532             + "_SYNTHETIC_ALIAS)" + aliasFilesList); 
    533     resfiles_mk.println(""); 
    534     resfiles_mk.println(""); 
    535  
    536     if (ctdFilesList != null) { 
    537       resfiles_mk.println("# List of compact trie dictionary files (ctd)."); 
    538       resfiles_mk.println("BRK_CTD_SOURCE = " + ctdFilesList); 
    539       resfiles_mk.println(""); 
    540       resfiles_mk.println(""); 
    541     } 
    542  
    543     if (brkFilesList != null) { 
    544       resfiles_mk.println("# List of break iterator files (brk)."); 
    545       resfiles_mk.println("BRK_SOURCE = " + brkFilesList); 
    546       resfiles_mk.println(""); 
    547       resfiles_mk.println(""); 
    548     } 
    549  
    550     if (emptyFileText != null) { 
    551       resfiles_mk.println("# Empty locales, used for validSubLocale fallback."); 
    552       // note: lists start with a space. 
    553       resfiles_mk.println(stub + "_EMPTY_SOURCE =" + emptyFileText); 
    554       resfiles_mk.println(""); 
    555       resfiles_mk.println(""); 
    556     } 
    557  
    558     resfiles_mk.println("# Ordinary resources"); 
    559     if (emptyFileText == null) { 
    560       resfiles_mk.print(stub + "_SOURCE =" + inFileText); 
    561     } else { 
    562       resfiles_mk.print(stub + "_SOURCE = $(" + stub + "_EMPTY_SOURCE)" + inFileText); 
    563     } 
    564     resfiles_mk.println(""); 
    565     resfiles_mk.println(""); 
    566  
    567     resfiles_mk.close(); 
    568   } catch(IOException e) { 
    569     log.error("While writing " + resfiles_mk_name, e); 
    570     System.exit(1); 
     571  private static void writeSimpleLocaleAlias( 
     572      ICUWriter writer, String fileName, String fromLocale, String toLocale, String comment) { 
     573    writer.writeSimpleLocaleAlias(fileName + ".txt", fromLocale, toLocale, DEPRECATED_LIST,  
     574        comment); 
    571575  } 
    572576} 
    573  
    574 private static void writeSimpleLocaleAlias(ICUWriter writer, String fileName, String fromLocale, 
    575     String toLocale, String comment) { 
    576   writer.writeSimpleLocaleAlias(fileName + ".txt", fromLocale, toLocale, DEPRECATED_LIST,  
    577       comment); 
    578 } 
    579 } 
Note: See TracChangeset for help on using the changeset viewer.