1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 /* 24 * Created on 2005 25 * by Christian Schmidt 26 */ 27 package com.sun.star.tooling.languageResolver; 28 29 import java.io.BufferedReader; 30 import java.io.IOException; 31 import java.io.InputStream; 32 import java.io.InputStreamReader; 33 import java.util.ArrayList; 34 import java.util.ListIterator; 35 36 /** 37 * Translate language codes into another format 38 * between ISO, RFC3066 and numeric 39 * 40 * @author Christian Schmidt 2005 41 * 42 */ 43 public class LanguageResolver { 44 private final static int ISO =2; 45 private final static int LANGID =0; 46 private final static int LANGNAME =1; 47 private final static int RFC3066 =3; 48 49 ArrayList languages=new ArrayList(); 50 51 // public static void main(String[] args){ 52 // try { 53 // LanguageResolver lr=new LanguageResolver(); 54 // } catch (IOException e) { 55 // // 56 // e.printStackTrace(); 57 // } 58 // } 59 60 /** 61 * Create a new Instance of LanguageResolver 62 * 63 * @throws IOException 64 */ LanguageResolver()65 public LanguageResolver() throws IOException{ 66 String lang = "com/sun/star/tooling/languageResolver/lang.map"; 67 ClassLoader cl = this.getClass().getClassLoader(); 68 InputStream in = cl.getResourceAsStream(lang); 69 BufferedReader languageTable= new BufferedReader(new InputStreamReader(in)); 70 71 String line; 72 73 while((line=(languageTable.readLine()))!=null){ 74 languages.add(line.split(",")); 75 } 76 } 77 /** 78 * Get the numeric value of the given ISO Language Code 79 * 80 * @param isoCode the ISO Language Code to find 81 * @return numeric value of the given isoCode 82 * @throws LanguageResolvingException if the Language ISO Code is not known 83 */ getNrFromISO(String isoCode)84 public String getNrFromISO(String isoCode) throws LanguageResolvingException{ 85 if("".equals(isoCode)) return ""; 86 ListIterator iter=languages.listIterator(); 87 String[] line=new String[5]; 88 while(isoCode!="" && iter.hasNext()){ 89 line=(String[]) iter.next(); 90 if(line[ISO].equals(isoCode)) return line[LANGID]; 91 } 92 throw new LanguageResolvingException("Can not find ISO Code: "+isoCode ); 93 94 } 95 96 /** 97 * Get the ISO Language Code corresponding with the given Language ID 98 * 99 * @param ID the numeric language id to find 100 * @return the ISO Language Code corresponding with the given Language ID 101 * @throws LanguageResolvingException if the Language ID is not known 102 */ getISOfromNr(String ID)103 public String getISOfromNr(String ID) throws LanguageResolvingException{ 104 if("".equals(ID)) return ""; 105 ListIterator iter=languages.listIterator(); 106 String[] line=new String[5]; 107 while(iter.hasNext()){ 108 line=(String[]) iter.next(); 109 if(line[LANGID].equals(ID)) return line[ISO]; 110 } 111 throw new LanguageResolvingException("Can not find Language Id: "+ID ); 112 } 113 114 /** 115 * Get the RFC3066 value of the given ISO Language Code 116 * 117 * @param isoCode the ISO Language Code to find 118 * @return RFC3066 value of the given isoCode 119 * @throws LanguageResolvingException if the Language ISO Code is not known 120 */ getRFCFromISO(String isoCode)121 public String getRFCFromISO(String isoCode) throws LanguageResolvingException{ 122 if("".equals(isoCode)) return ""; 123 ListIterator iter=languages.listIterator(); 124 String[] line=new String[5]; 125 while(iter.hasNext()){ 126 line=(String[]) iter.next(); 127 if(line[ISO].equals(isoCode)) return line[RFC3066]; 128 } 129 throw new LanguageResolvingException("Can not find ISO Code: "+isoCode ); 130 } 131 132 /** 133 * Get the ISO Language Code corresponding with the given RFC3066 code 134 * 135 * @param RFC RFC3066 language id to find 136 * @return the ISO Language Code corresponding with the given RFC3066 code 137 * @throws LanguageResolvingException if the RFC3066 code is not known 138 */ getISOFromRFC(String RFC)139 public String getISOFromRFC(String RFC) throws LanguageResolvingException{ 140 if("".equals(RFC)) return ""; 141 ListIterator iter=languages.listIterator(); 142 String[] line=new String[5]; 143 while(iter.hasNext()){ 144 line=(String[]) iter.next(); 145 if(line[RFC3066].equals(RFC)) return line[ISO]; 146 } 147 throw new LanguageResolvingException("Can not find Language Id: "+RFC ); 148 } 149 150 151 /** 152 * This Exception is thrown if a Language Identfier is unknown 153 * 154 * @author Christian Schmidt 2005 155 * 156 */ 157 public class LanguageResolvingException extends Exception { 158 159 /** 160 * 161 */ LanguageResolvingException()162 public LanguageResolvingException() { 163 super(); 164 // 165 } 166 167 /** 168 * @param arg0 169 */ LanguageResolvingException(String arg0)170 public LanguageResolvingException(String arg0) { 171 super(arg0); 172 // 173 } 174 175 /** 176 * @param arg0 177 * @param arg1 178 */ LanguageResolvingException(String arg0, Throwable arg1)179 public LanguageResolvingException(String arg0, Throwable arg1) { 180 super(arg0, arg1); 181 // 182 } 183 184 /** 185 * @param arg0 186 */ LanguageResolvingException(Throwable arg0)187 public LanguageResolvingException(Throwable arg0) { 188 super(arg0); 189 // 190 } 191 192 } 193 194 } 195