ICU-2055 add option for generating supplemental localedata

X-SVN-Rev: 16252
This commit is contained in:
Ram Viswanadha 2004-09-03 18:28:01 +00:00
parent 592ed13f9a
commit 7c166871b7
3 changed files with 109 additions and 26 deletions

View file

@ -136,10 +136,10 @@ public class LDML2ICUConverter {
}
for (int i = 0; i < remainingArgc; i++) {
long start = System.currentTimeMillis();
int lastIndex = args[i].lastIndexOf(File.separator, args[i].length()) + 1 /* add 1 to skip past the separator */;
fileName = args[i].substring(lastIndex, args[i].length());
String xmlfileName = getFullPath(false,args[i]);
System.out.println("Processing file: "+xmlfileName);
/*
* debugging code
*
@ -155,11 +155,12 @@ public class LDML2ICUConverter {
* }
*/
// TODO : uncomment
System.out.println("INFO: Creating fully resolved LDML document for: " + sourceDir+File.separator+ args[i]);
fullyResolvedDoc = LDMLUtilities.getFullyResolvedLDML(sourceDir, args[i], false, false, false);
if(specialsDir!=null){
locName = args[i];
specialsDoc = LDMLUtilities.parse(sourceDir+File.separator+ args[i]);
System.out.println("INFO: Parsing LDML document for: " + specialsDir+File.separator+ args[i]);
specialsDoc = LDMLUtilities.parseAndResolveAliases(args[i], specialsDir, true);
int index = locName.indexOf(".xml");
if(index > -1){
locName = locName.substring(0,index);
@ -176,6 +177,8 @@ public class LDML2ICUConverter {
*/
}
createResourceBundle(xmlfileName);
long stop = System.currentTimeMillis();
System.out.println("Time taken: "+ (stop-start));
}
}
private String getFullPath(boolean fileType, String fName){
@ -215,8 +218,10 @@ public class LDML2ICUConverter {
}
private void createResourceBundle(String xmlfileName) {
try {
System.out.println("INFO: Parsing LDML document for: "+xmlfileName);
Document doc = LDMLUtilities.parse(xmlfileName);
// Create the Resource linked list which will hold the
// data after parsing
@ -233,9 +238,8 @@ public class LDML2ICUConverter {
catch (Throwable se) {
System.err.println("ERROR: " + se.toString());
se.printStackTrace();
System.exit(1);
}
}
}
private void writeAliasedResource(){
if(locName==null || writeDeprecated==false){
@ -299,13 +303,61 @@ public class LDML2ICUConverter {
//TODO: "FX", "RO", "TP", "ZR", /* obsolete country codes */
}
private ICUResourceWriter.Resource setNext(ICUResourceWriter.Resource current, ICUResourceWriter.ResourceTable table, ICUResourceWriter.Resource toSet){
if(current == null){
current = table.first = toSet;
}else{
current.next = toSet;
private ICUResourceWriter.Resource parseSupplemental(Node root){
ICUResourceWriter.ResourceTable table = null;
ICUResourceWriter.Resource current = null;
StringBuffer xpath = new StringBuffer();
xpath.append("//");
xpath.append(LDMLConstants.SUPPLEMENTAL_DATA);
int savedLength = xpath.length();
for(Node node=root.getFirstChild(); node!=null; node=node.getNextSibling()){
if(node.getNodeType()!=Node.ELEMENT_NODE){
continue;
}
String name = node.getNodeName();
ICUResourceWriter.Resource res = null;
if(name.equals(LDMLConstants.SUPPLEMENTAL_DATA) ){
if(LDMLUtilities.isNodeDraft(node) && writeDraft==false){
System.err.println("WARNING: The LDML file "+sourceDir+File.separator+locName+".xml is marked draft! Not producing ICU file. ");
System.exit(-1);
}
node=node.getFirstChild();
continue;
}else if (name.equals(LDMLConstants.SPECIAL)){
/*
* IGNORE SPECIALS
* FOR NOW
*/
node=node.getFirstChild();
continue;
}else if(name.equals(LDMLConstants.CURRENCY_DATA)){
res = parseCurrencyData(node, xpath);
}else if(name.indexOf("icu:")>-1|| name.indexOf("openOffice:")>-1){
//TODO: these are specials .. ignore for now ... figure out
// what to do later
}else{
System.err.println("Encountered unknown element: "+name);
System.exit(-1);
}
if(res!=null){
if(current == null){
table.first = res;
current = findLast(res);
}else{
current.next = res;
current = findLast(res);
}
res = null;
}
xpath.delete(savedLength,xpath.length());
}
return current.next;
return table;
}
private ICUResourceWriter.Resource parseCurrencyData(Node root, StringBuffer xpath){
return null;
}
private ICUResourceWriter.Resource parseBundle(Node root){
ICUResourceWriter.ResourceTable table = null;
@ -330,7 +382,7 @@ public class LDML2ICUConverter {
current = findLast(table.first);
continue;
}else if(name.equals(LDMLConstants.LDML) ){
if(LDMLUtilities.isDraft(node, xpath) && writeDraft==false){
if(LDMLUtilities.isNodeDraft(node) && writeDraft==false){
System.err.println("WARNING: The LDML file "+sourceDir+File.separator+locName+".xml is marked draft! Not producing ICU file. ");
System.exit(-1);
}
@ -414,9 +466,6 @@ public class LDML2ICUConverter {
}
return current;
}
private boolean isDraft(Node current, StringBuffer xpath){
return false;
}
private ICUResourceWriter.Resource parseAliasResource(Node node, StringBuffer xpath){
// TODO: dont know how
@ -2594,7 +2643,7 @@ public class LDML2ICUConverter {
FileOutputStream file = new FileOutputStream(outputFileName);
BufferedOutputStream writer = new BufferedOutputStream(file);
System.out.println("Creating file: "+outputFileName);
System.out.println("INFO: Creating ICU ResourceBundle: "+outputFileName);
//TODO: fix me
writeHeader(writer,sourceFileName);

View file

@ -157,5 +157,15 @@ public class LDMLConstants {
public static final String BEFORE = "before";
public static final String CONTEXT = "context";
public static final String EXTEND = "extend";
// supplemental data
public static final String SUPPLEMENTAL_DATA = "supplementalData";
public static final String CURRENCY_DATA = "currencyData";
public static final String FRACTIONS = "fractions";
public static final String INFO = "info";
public static final String ISO_4217 = "iso4217";
public static final String DIGITS = "digits";
public static final String REGION = "region";
public static final String ISO_3166 = "iso3166";
}

View file

@ -344,6 +344,12 @@ public class LDMLUtilities {
// element node :(
throw new IllegalArgumentException("Unknown Xpath fragment: " + token);
}
/**
*
* @param token XPath token fragment
* @param attrib attribute whose value must be fetched
* @return
*/
private static String getAttributeValue(String token, String attrib){
int attribStart = token.indexOf(attrib);
int valStart = token.indexOf('=', attribStart)+1/*skip past the separtor*/;
@ -387,7 +393,7 @@ public class LDMLUtilities {
* @param overide
* @return the merged document
*/
public static Node mergeLDMLDocuments(Document source, Node overide, StringBuffer xpath,
private static Node mergeLDMLDocuments(Document source, Node overide, StringBuffer xpath,
String thisName, String sourceDir){
if(source==null){
return overide;
@ -449,7 +455,7 @@ public class LDMLUtilities {
Node childToImport = source.importNode(child,true);
parentNodeInSource.replaceChild(childToImport, nodeInSource);
}else{
if(areChildrenElementNodes(child)){
if(areChildrenElementNodes(child) && areChildrenElementNodes(nodeInSource)){
//recurse to pickup any children!
mergeLDMLDocuments(source, child, xpath, thisName, sourceDir);
}else{
@ -613,7 +619,7 @@ public class LDMLUtilities {
// the default value is false if none specified
return false;
}
public boolean isNodeDraft(Node node){
public static boolean isNodeDraft(Node node){
String draft = getAttributeValue(node, LDMLConstants.DRAFT);
if(draft!=null ){
if(draft.equals("true")){
@ -636,6 +642,7 @@ public class LDMLUtilities {
return false;
}
}
current = current.getParentNode();
}
return false;
}
@ -811,14 +818,10 @@ public class LDMLUtilities {
*/
public static String getAttributeValue(Node sNode, String attribName){
String value=null;
Node node = sNode;
NamedNodeMap attributes = node.getAttributes();
Node attr = attributes.getNamedItem(attribName);
Node attr = sNode.getAttributes().getNamedItem(attribName);
if(attr!=null){
value = attr.getNodeValue();
}
return value;
}
/**
@ -853,7 +856,28 @@ public class LDMLUtilities {
String docURI = filenameToURL(filename);
return parse(new InputSource(docURI),filename);
}
public static Document parseAndResolveAliases(String locale, String sourceDir, boolean ignoreError){
try{
Document full = parse(sourceDir+File.separator+ "root.xml");
if(full!=null){
full = resolveAliases(full, sourceDir, "root");
}
/*
* Debugging
*
Node[] list = getNodeArray(full, LDMLConstants.ALIAS);
if(list.length>0){
System.err.println("Aliases not resolved!. list.getLength() returned "+ list.length);
}*/
return full;
}catch(RuntimeException ex){
if(!ignoreError){
throw ex;
}
}
return null;
}
public static Document parse(InputSource docSrc, String filename){
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();