ICU-2899 check the target-language and source-language attribute values
X-SVN-Rev: 13503
This commit is contained in:
parent
ebbf031deb
commit
41d28075fa
@ -6,7 +6,7 @@
|
|||||||
*
|
*
|
||||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/tool/localeconverter/XLIFF2ICUConverter.java,v $
|
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/tool/localeconverter/XLIFF2ICUConverter.java,v $
|
||||||
* $Date: 2003/05/19
|
* $Date: 2003/05/19
|
||||||
* $Revision: 1.7 $
|
* $Revision: 1.8 $
|
||||||
*
|
*
|
||||||
******************************************************************************
|
******************************************************************************
|
||||||
*/
|
*/
|
||||||
@ -30,9 +30,6 @@ import java.util.*;
|
|||||||
|
|
||||||
public final class XLIFF2ICUConverter {
|
public final class XLIFF2ICUConverter {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* These must be kept in sync with getOptions().
|
* These must be kept in sync with getOptions().
|
||||||
*/
|
*/
|
||||||
@ -65,6 +62,7 @@ public final class XLIFF2ICUConverter {
|
|||||||
private static final String TS = "ts";
|
private static final String TS = "ts";
|
||||||
private static final String ORIGINAL = "original";
|
private static final String ORIGINAL = "original";
|
||||||
private static final String SOURCELANGUAGE = "source-language";
|
private static final String SOURCELANGUAGE = "source-language";
|
||||||
|
private static final String TARGETLANGUAGE = "target-language";
|
||||||
private static final String TARGET = "target";
|
private static final String TARGET = "target";
|
||||||
private static final String SOURCE = "source";
|
private static final String SOURCE = "source";
|
||||||
private static final String NOTE = "note";
|
private static final String NOTE = "note";
|
||||||
@ -93,12 +91,11 @@ public final class XLIFF2ICUConverter {
|
|||||||
private static final String QUOTE = "\"";
|
private static final String QUOTE = "\"";
|
||||||
private static final String COMMENTSTART = "/**";
|
private static final String COMMENTSTART = "/**";
|
||||||
private static final String COMMENTEND = " */";
|
private static final String COMMENTEND = " */";
|
||||||
private static final String TAG = " * @ ";
|
private static final String TAG = " * @";
|
||||||
private static final String COMMENTMIDDLE = " * ";
|
private static final String COMMENTMIDDLE = " * ";
|
||||||
private static final String SPACE = " ";
|
private static final String SPACE = " ";
|
||||||
private static final String INDENT = " ";
|
private static final String INDENT = " ";
|
||||||
private static final String EMPTY = "";
|
private static final String EMPTY = "";
|
||||||
private static final String SOURCE_LANGUAGE = "source-language";
|
|
||||||
private static final String ID = "id";
|
private static final String ID = "id";
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -200,11 +197,6 @@ public final class XLIFF2ICUConverter {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String crc = "";
|
|
||||||
private static int transID = -1;
|
|
||||||
private static boolean isIntvector = false;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility method to translate a String filename to URL.
|
* Utility method to translate a String filename to URL.
|
||||||
*
|
*
|
||||||
@ -270,7 +262,45 @@ public final class XLIFF2ICUConverter {
|
|||||||
return "file:///" + tmp;
|
return "file:///" + tmp;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
private boolean isXmlLang (String lang){
|
||||||
|
|
||||||
|
int suffix;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
if (lang.length () < 2){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
c = lang.charAt(1);
|
||||||
|
if (c == '-') {
|
||||||
|
c = lang.charAt(0);
|
||||||
|
if (!(c == 'i' || c == 'I' || c == 'x' || c == 'X')){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
suffix = 1;
|
||||||
|
} else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
|
||||||
|
c = lang.charAt(0);
|
||||||
|
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
suffix = 2;
|
||||||
|
} else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
while (suffix < lang.length ()) {
|
||||||
|
c = lang.charAt(suffix);
|
||||||
|
if (c != '-'){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
while (++suffix < lang.length ()) {
|
||||||
|
c = lang.charAt(suffix);
|
||||||
|
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ((lang.length() == suffix) && (c != '-'));
|
||||||
|
}
|
||||||
|
|
||||||
private void createRB(String xmlfileName) {
|
private void createRB(String xmlfileName) {
|
||||||
|
|
||||||
@ -304,16 +334,32 @@ public final class XLIFF2ICUConverter {
|
|||||||
if(nlist.getLength()>1){
|
if(nlist.getLength()>1){
|
||||||
throw new RuntimeException("Multiple <file> elements in the XLIFF file not supported.");
|
throw new RuntimeException("Multiple <file> elements in the XLIFF file not supported.");
|
||||||
}
|
}
|
||||||
|
// get the value of source-language attribute
|
||||||
|
String sourceLang = getLanguageName(doc, SOURCELANGUAGE);
|
||||||
|
// get the value of target-language attribute
|
||||||
|
String targetLang = getLanguageName(doc, TARGETLANGUAGE);
|
||||||
|
|
||||||
|
// get the list of <source> elements
|
||||||
NodeList sourceList = doc.getElementsByTagName(SOURCE);
|
NodeList sourceList = doc.getElementsByTagName(SOURCE);
|
||||||
|
// get the list of target elements
|
||||||
NodeList targetList = doc.getElementsByTagName(TARGET);
|
NodeList targetList = doc.getElementsByTagName(TARGET);
|
||||||
|
|
||||||
|
// check if the xliff file has source elements in multiple languages
|
||||||
|
// the source-language value should be the same as xml:lang values
|
||||||
|
// of all the source elements.
|
||||||
|
checkLangAttribute(sourceList, sourceLang);
|
||||||
|
|
||||||
|
// check if the xliff file has target elements in multiple languages
|
||||||
|
// the target-language value should be the same as xml:lang values
|
||||||
|
// of all the target elements.
|
||||||
|
checkLangAttribute(targetList, targetLang);
|
||||||
|
|
||||||
Resource[] set = new Resource[2];
|
Resource[] set = new Resource[2];
|
||||||
set[0] = new ResourceTable();
|
set[0] = new ResourceTable();
|
||||||
set[1] = new ResourceTable();
|
set[1] = new ResourceTable();
|
||||||
set[0].name = getSourceBundleName(doc);
|
set[0].name = sourceLang.replace('-','_');
|
||||||
|
set[1].name = targetLang.replace('-','_');
|
||||||
|
|
||||||
// check if the xliff file is translated into multiple languages
|
|
||||||
checkLangAttribute(sourceList);
|
|
||||||
set[1].name = checkLangAttribute(targetList);
|
|
||||||
|
|
||||||
// check if any <alt-trans> elements are present
|
// check if any <alt-trans> elements are present
|
||||||
NodeList altTrans = doc.getElementsByTagName(ALTTRANS);
|
NodeList altTrans = doc.getElementsByTagName(ALTTRANS);
|
||||||
@ -321,8 +367,14 @@ public final class XLIFF2ICUConverter {
|
|||||||
throw new RuntimeException("<alt-trans> elements in XLIFF format are not supported.");
|
throw new RuntimeException("<alt-trans> elements in XLIFF format are not supported.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get all the group elements
|
||||||
NodeList list = doc.getElementsByTagName(GROUPS);
|
NodeList list = doc.getElementsByTagName(GROUPS);
|
||||||
|
|
||||||
|
// process the first group element. The first group element is
|
||||||
|
// the base table that must be parsed recursively
|
||||||
parseTable(list.item(0), set);
|
parseTable(list.item(0), set);
|
||||||
|
|
||||||
|
// write out the bundle
|
||||||
writeResource(set, xmlfileName);
|
writeResource(set, xmlfileName);
|
||||||
}
|
}
|
||||||
catch (Throwable se) {
|
catch (Throwable se) {
|
||||||
@ -369,18 +421,20 @@ public final class XLIFF2ICUConverter {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private String getSourceBundleName(Document doc){
|
|
||||||
|
private String getLanguageName(Document doc, String lang){
|
||||||
if(doc!=null){
|
if(doc!=null){
|
||||||
NodeList list = doc.getElementsByTagName(FILE);
|
NodeList list = doc.getElementsByTagName(FILE);
|
||||||
Node node = list.item(0);
|
Node node = list.item(0);
|
||||||
NamedNodeMap attr = node.getAttributes();
|
NamedNodeMap attr = node.getAttributes();
|
||||||
Node orig = attr.getNamedItem(SOURCE_LANGUAGE);
|
Node orig = attr.getNamedItem(lang);
|
||||||
String name = orig.getNodeValue();
|
String name = orig.getNodeValue();
|
||||||
|
|
||||||
NodeList groupList = doc.getElementsByTagName(GROUPS);
|
NodeList groupList = doc.getElementsByTagName(GROUPS);
|
||||||
Node group = groupList.item(0);
|
Node group = groupList.item(0);
|
||||||
NamedNodeMap groupAtt = group.getAttributes();
|
NamedNodeMap groupAtt = group.getAttributes();
|
||||||
Node id = groupAtt.getNamedItem(ID);
|
Node id = groupAtt.getNamedItem(ID);
|
||||||
|
if(id!=null){
|
||||||
String idVal = id.getNodeValue();
|
String idVal = id.getNodeValue();
|
||||||
|
|
||||||
if(!name.equals(idVal)){
|
if(!name.equals(idVal)){
|
||||||
@ -388,7 +442,12 @@ public final class XLIFF2ICUConverter {
|
|||||||
"Please compare the output with the orignal " +
|
"Please compare the output with the orignal " +
|
||||||
"ICU ResourceBundle before proceeding.");
|
"ICU ResourceBundle before proceeding.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if(!isXmlLang(name)){
|
||||||
|
System.err.println("The attribute "+ lang + "=\""+ name +
|
||||||
|
"\" of <file> element does not satisfy RFC 1766 conditions.");
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -399,20 +458,25 @@ public final class XLIFF2ICUConverter {
|
|||||||
// as the child of <trans-unit> but the attributes of the
|
// as the child of <trans-unit> but the attributes of the
|
||||||
// <target> element may different across <trans-unit> elements
|
// <target> element may different across <trans-unit> elements
|
||||||
// check for it. Similar is the case with <source> elements
|
// check for it. Similar is the case with <source> elements
|
||||||
private String checkLangAttribute(NodeList list){
|
private String checkLangAttribute(NodeList list, String origName){
|
||||||
String oldLangName=null;
|
String oldLangName=origName;
|
||||||
for(int i = 0 ;i<list.getLength(); i++){
|
for(int i = 0 ;i<list.getLength(); i++){
|
||||||
Node node = list.item(i);
|
Node node = list.item(i);
|
||||||
NamedNodeMap attr = node.getAttributes();
|
NamedNodeMap attr = node.getAttributes();
|
||||||
Node lang = attr.getNamedItem(XMLLANG);
|
Node lang = attr.getNamedItem(XMLLANG);
|
||||||
// the source file sometimes may not contain xml:lang attribute
|
String langName = null;
|
||||||
if(lang==null && node.getNodeName().equals(SOURCE)){
|
// the target element should always contain xml:lang attribute
|
||||||
continue;
|
if(lang==null ){
|
||||||
|
if(origName==null){
|
||||||
|
System.err.println("Encountered <target> element without xml:lang attribute. Please fix the below element in the XLIFF file.\n"+ node.toString());
|
||||||
|
System.exit(-1);
|
||||||
|
}else{
|
||||||
|
langName = origName;
|
||||||
}
|
}
|
||||||
String langName = lang.getNodeValue();
|
}else{
|
||||||
if(i==0){
|
langName = lang.getNodeValue();
|
||||||
oldLangName = langName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(oldLangName!=null && langName!=null && !langName.equals(oldLangName)){
|
if(oldLangName!=null && langName!=null && !langName.equals(oldLangName)){
|
||||||
throw new RuntimeException("The <trans-unit> elements must be bilingual, multilingual tranlations not supported. xml:lang = " + oldLangName +
|
throw new RuntimeException("The <trans-unit> elements must be bilingual, multilingual tranlations not supported. xml:lang = " + oldLangName +
|
||||||
" and xml:lang = " + langName);
|
" and xml:lang = " + langName);
|
||||||
@ -1038,6 +1102,7 @@ public final class XLIFF2ICUConverter {
|
|||||||
writeLine(writer, buffer.toString());
|
writeLine(writer, buffer.toString());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeBOM(OutputStream buffer) {
|
private void writeBOM(OutputStream buffer) {
|
||||||
try {
|
try {
|
||||||
byte[] bytes = BOM.getBytes(CHARSET);
|
byte[] bytes = BOM.getBytes(CHARSET);
|
||||||
|
Loading…
Reference in New Issue
Block a user