0001: package com.xoetrope.carousel.langed;
0002:
0003: import com.xoetrope.carousel.build.BuildProperties;
0004: import java.io.BufferedWriter;
0005: import java.io.File;
0006: import java.io.FileInputStream;
0007: import java.io.FileWriter;
0008: import java.io.IOException;
0009: import java.net.MalformedURLException;
0010: import java.net.URL;
0011: import java.util.Enumeration;
0012: import java.util.Properties;
0013: import java.util.Vector;
0014:
0015: import java.awt.Component;
0016: import java.io.BufferedReader;
0017: import java.io.InputStream;
0018: import java.io.InputStreamReader;
0019: import java.util.PropertyResourceBundle;
0020: import java.util.ResourceBundle;
0021: import javax.swing.JPanel;
0022:
0023: import jxl.Sheet;
0024: import jxl.Workbook;
0025: import jxl.write.Label;
0026: import jxl.write.WritableSheet;
0027: import jxl.write.WritableWorkbook;
0028: import net.xoetrope.debug.DebugLogger;
0029:
0030: import net.xoetrope.editor.project.XEditorProject;
0031: import net.xoetrope.editor.project.XEditorProjectManager;
0032: import net.xoetrope.optional.langmgr.EncodedResourceBundle;
0033:
0034: /**
0035: * LangManager manages the language resources on behalf of the language editor.
0036: * <p>Copyright: Xoetrope Ltd. (c) 2001-2006.
0037: * This software is licensed under the GNU Public License (GPL) see license.txt
0038: * for more details on licensing</p>
0039: */
0040: public class EdLangMgr {
0041: private XEditorProject currentProject;
0042: private String projectPath;
0043:
0044: public EdLangMgr(boolean _bIsStandalone, URL _url) {
0045: bIsStandalone = _bIsStandalone;
0046: url = _url;
0047: currentProject = (XEditorProject) XEditorProjectManager
0048: .getCurrentProject();
0049: try {
0050: init(url);
0051: } catch (IOException ex) {
0052: }
0053: }
0054:
0055: public void init(URL _url) throws IOException {
0056: usesDatabaseSource = false;
0057: url = _url;
0058: langList = new Vector(5);
0059:
0060: if ((url != null)
0061: && (url.getFile().toLowerCase().indexOf(
0062: "languagelist.properties") > 0))
0063: readLanguageList();
0064: else if (langList.size() == 0) {
0065: addLang("reference", "Reference", null, false);
0066: addLang("EN", "English", null, false);
0067: refLang = ((EdLangName) langList.elementAt(0)).edLang;
0068: }
0069: }
0070:
0071: private EdLangName readLang(String id, String code, String name)
0072: throws IOException {
0073: String str = url.getPath();
0074: String newStr = newStr = str.substring(0,
0075: str.lastIndexOf('/') + 1)
0076: + code.toLowerCase() + ".properties";
0077: try {
0078: File langFile = new File(newStr);
0079: if (langFile.exists())
0080: return importPropertiesFile(new File(newStr), code,
0081: name);
0082: } catch (MalformedURLException excep) {
0083: System.out.println("Malformed URL: " + excep);
0084: } catch (IOException excep) {
0085: System.out.println("IO Error: " + excep);
0086: }
0087:
0088: return null;
0089: }
0090:
0091: public String getProjectPath() {
0092: if (currentProject != null)
0093: return currentProject.getPath();
0094:
0095: return projectPath;
0096: }
0097:
0098: public void setProjectPath(String pp) {
0099: projectPath = pp;
0100: }
0101:
0102: private void readLanguageList() {
0103: Properties languageList = new Properties();
0104: try {
0105: languageList.load(url.openStream());
0106:
0107: int numLangs = languageList.keySet().size();
0108: langList.ensureCapacity(numLangs + 3);
0109:
0110: EdLangName commentsLang = readLang("1000", "cm", "Comments");
0111: boolean readRef = false;
0112: if (commentsLang == null) {
0113: commentsLang = readLang("1000", "reference",
0114: "Reference");
0115: readRef = true;
0116: }
0117:
0118: if (refLang == null) {
0119: addLang("reference", "Reference", null, false);
0120: refLang = ((EdLangName) langList.elementAt(readRef ? 0
0121: : 1)).edLang;
0122: }
0123:
0124: EdLangName hintsLang = readLang("1001", "HT", "Hints");
0125:
0126: int id = 0;
0127: Enumeration languageKeys = languageList.keys();
0128: while (languageKeys.hasMoreElements()) {
0129: String languageName = (String) languageKeys
0130: .nextElement();
0131: String languageCode = (String) languageList
0132: .get(languageName);
0133: String languageNameLwr = languageName.toLowerCase();
0134: if (languageNameLwr.equals("comments")
0135: || languageNameLwr.equals("hints")
0136: || languageNameLwr.equals("reference"))
0137: continue;
0138: readLang(Integer.toString(id++), languageCode,
0139: languageName);
0140: }
0141:
0142: //loadedFromExportedFiles = ( s != null ? ( s.compareTo( "1" ) == 0 ) : false );
0143: } catch (IOException ioe) {
0144: ioe.printStackTrace();
0145: } finally {
0146: if (langList.size() == 0
0147: || !languageList.containsKey("Reference")) {
0148: addLang("reference", "Reference", null, false);
0149: refLang = ((EdLangName) langList.elementAt(0)).edLang;
0150: }
0151: }
0152: }
0153:
0154: public void init(Vector languages, Vector translations)
0155: throws IOException {
0156: usesDatabaseSource = true;
0157: langList = new Vector(5);
0158:
0159: int numLangs = languages.size();
0160: langList.ensureCapacity(numLangs);
0161: for (int i = 0; i < numLangs; i++) {
0162: // LanguageObject lo = (LanguageObject)languages.elementAt( i );
0163: // EdLangName ln = new EdLangName();
0164: // ln.id = ( int ) lo.getId();
0165: // ln.code = lo.getCode();
0166: // ln.name = lo.getName();
0167: //
0168: // ln.edLang = read( translations, TranslationObject.FIELD_REFERENCE + i );
0169: // ln.edLang.setLangCode( ln.code );
0170: // ln.edLang.setLangName( ln.name );
0171: //
0172: // langList.addElement( ln );
0173: }
0174: currentLang = ((EdLangName) langList.elementAt(0)).edLang;
0175: refLang = ((EdLangName) langList.elementAt(0)).edLang;
0176: loadedFromExportedFiles = false;
0177: }
0178:
0179: public EdLangName importPropertiesFile(File theFile, String code,
0180: String name) throws IOException {
0181: usesDatabaseSource = false;
0182:
0183: // Add the two prerequisite langauges
0184: int id = langList.size();
0185: EdLangName ln;
0186:
0187: if (!theFile.exists())
0188: return null;
0189:
0190: String path = theFile.getParent();//path.substring( 0, path.indexOf( theFile.getName() ) );
0191: ResourceBundle props = null;
0192: String fileName = theFile.toString();
0193:
0194: // Check for a file encoding
0195: String encoding = null;
0196: fileName = fileName.substring(0, fileName.lastIndexOf('.'));
0197: Properties encodingProperties = new Properties();
0198: File encodingFile = new File(fileName + ".property_encodings");
0199: if (encodingFile.exists()) {
0200: try {
0201: encodingProperties.load(new FileInputStream(
0202: encodingFile));
0203: } catch (Exception ex) {
0204: if (BuildProperties.DEBUG)
0205: DebugLogger
0206: .logWarning("Could not load the property_encodings file for: "
0207: + fileName);
0208: encodingProperties = null;
0209: }
0210:
0211: // Check the file encoding and load the properties based on the encoding
0212: String fileEncoding = (String) encodingProperties
0213: .get("encoding");
0214: if ((fileEncoding != null) && (fileEncoding.length() > 0))
0215: encoding = fileEncoding;
0216: }
0217:
0218: // Now read the file
0219: InputStream fis = new FileInputStream(theFile);
0220: if (encoding != null) {
0221: InputStreamReader isr = new InputStreamReader(fis, encoding);
0222: props = new EncodedResourceBundle(new BufferedReader(isr),
0223: encodingProperties);
0224: } else
0225: props = new PropertyResourceBundle(fis);
0226:
0227: ln = new EdLangName();
0228: ln.id = ++id;
0229: ln.code = code;
0230: ln.name = name;
0231: ln.edLang = currentLang = new EdLanguage(this );
0232: ln.encoding = encoding;
0233:
0234: if (code.equals("reference"))
0235: refLang = currentLang;
0236:
0237: langList.addElement(ln);
0238:
0239: Enumeration enumeration = props.getKeys();
0240: while (enumeration.hasMoreElements()) {
0241: String key = (String) enumeration.nextElement();
0242: String value = (String) props.getObject(key);
0243:
0244: // Check that the string exists in the reference language
0245: int stringId = refLang.findString(key);
0246: if (stringId < 0)
0247: stringId = refLang.addString(key, key);
0248:
0249: if (currentLang != refLang)
0250: currentLang.addString(stringId, key, value);
0251: }
0252: return ln;
0253: }
0254:
0255: public boolean hasLang(String code) {
0256: int numLangs = langList.size();
0257: for (int i = 0; i < numLangs; i++) {
0258: if (((EdLangName) langList.elementAt(i)).code.equals(code))
0259: return true;
0260: }
0261: return false;
0262: }
0263:
0264: public void importFile(JPanel parent, File theFile)
0265: throws IOException {
0266: // Vector impLangList = new Vector( 5 );
0267: //
0268: // BufferedReader is;
0269: // is = new BufferedReader( new InputStreamReader( theFile.toURL().openStream() ) );
0270: //
0271: // String s = is.readLine();
0272: // StringTokenizer token = new StringTokenizer( s, "|" );
0273: // int numImpLangs = Integer.parseInt( token.nextToken() );
0274: //
0275: // langList.ensureCapacity( numImpLangs );
0276: // for ( int i = 0; i < numImpLangs; i++ ) {
0277: // s = is.readLine();
0278: // token = new StringTokenizer( s, "|" );
0279: // EdLangName ln = new EdLangName();
0280: // ln.id = Integer.parseInt( token.nextToken() );
0281: // ln.code = token.nextToken();
0282: // ln.name = token.nextToken();
0283: //
0284: // impLangList.addElement( ln );
0285: // }
0286: //
0287: // int maxStrId = 0;
0288: // try {
0289: // s = is.readLine();
0290: // maxStrId = Integer.parseInt( s );
0291: // }
0292: // catch ( Exception ex ) {
0293: // return;
0294: // }
0295: //
0296: // // Now attempt to merge the languages.
0297: // for ( int i = 0; i < numImpLangs; i++ ) {
0298: // EdLangName tempLang = ( ( EdLangName ) impLangList.elementAt( i ) );
0299: // EdLanguage source = null;
0300: // if ( JOptionPane.showConfirmDialog( parent, "Import and merge the language: " + tempLang.code, "Language Import", JOptionPane.YES_NO_OPTION ) ==
0301: // JOptionPane.YES_OPTION ) {
0302: // String str = theFile.getPath();
0303: // String fs = System.getProperty( "file.separator" );
0304: // String newStr = str.substring( 0, str.lastIndexOf( fs ) + 1 ) + tempLang.code.toLowerCase() + ".jln";
0305: // try {
0306: // source = tempLang.edLang = read( new File( newStr ).toURL() );
0307: // }
0308: // catch ( MalformedURLException excep ) {
0309: // System.out.println( "Malformed URL: " + excep );
0310: // }
0311: // catch ( Exception e ) {
0312: // e.printStackTrace();
0313: // }
0314: //
0315: // // Find the matching language
0316: // int numLangs = langList.size();
0317: // for ( int j = 0; j < numLangs; j++ ) {
0318: // EdLangName mergeTargetLang = ( ( EdLangName ) langList.elementAt( j ) );
0319: // if ( mergeTargetLang.id == tempLang.id ) {
0320: // // Ask if the language is to be merged
0321: // EdLanguage target = mergeTargetLang.edLang;
0322: // int numStrings = source.getNumStrings();
0323: // for ( int k = 0; k < numStrings; k++ ) {
0324: // LangItem srcItem = ( LangItem ) source.getElementAt( k );
0325: // if ( srcItem.id < maxStrId ) {
0326: // // Update the strings where an entry already exists
0327: // target.setString( srcItem.id, srcItem.keyStr, srcItem.langStr );
0328: // }
0329: // else {
0330: // // Insert the strings where an no entry already exists
0331: // int newId = refLang.addString( srcItem.keyStr, srcItem.langStr );
0332: // if ( target != refLang )
0333: // target.addString( newId, srcItem.keyStr, srcItem.langStr );
0334: // }
0335: // }
0336: // }
0337: // }
0338: // }
0339: // }
0340: }
0341:
0342: /**
0343: * Export content to a file based language resource
0344: * @param theFile
0345: * @param selectedLangs the list of languages to export
0346: * @throws IOException
0347: */
0348: public void exportFile(File theFile, char delimiterStartChar,
0349: char delimiterEndChar, String[] selectedLangs)
0350: throws IOException {
0351: URL theUrl = url;
0352: url = theFile.toURL();
0353: try {
0354: Vector selection = new Vector();
0355: int numSelectedLangs = selectedLangs.length;
0356: int numLangs = langList.size();
0357: for (int i = 0; i < numSelectedLangs; i++) {
0358: String selectedLang = selectedLangs[i];
0359: for (int j = 0; j < numLangs; j++) {
0360: EdLangName eln = (EdLangName) langList.elementAt(j);
0361: if (selectedLang.equals(eln.name)) {
0362: selection.add(eln);
0363: break;
0364: }
0365: }
0366: }
0367:
0368: saveSelected(delimiterStartChar, delimiterEndChar, false,
0369: true, selection);
0370: } catch (Exception ex) {
0371: ex.printStackTrace();
0372: } finally {
0373: // Restore the original URL
0374: url = theUrl;
0375: }
0376: }
0377:
0378: /**
0379: * Export content to an excel file based language resource. This assumes that
0380: * the first language exported is the master language containing all the keys
0381: * @param theFile
0382: * @param selectedLangs the list of languages to export
0383: * @throws IOException
0384: */
0385: public void exportExcel(File theFile, String[] selectedLangs)
0386: throws IOException {
0387: URL theUrl = url;
0388: url = theFile.toURL();
0389: String sql;
0390:
0391: WritableWorkbook workbook = Workbook.createWorkbook(theFile);
0392: WritableSheet sheet = workbook.createSheet("Translations", 0);
0393: Vector selection = new Vector();
0394: int numSelectedLangs = selectedLangs.length;
0395: int numLangs = langList.size();
0396: if (numSelectedLangs == 0)
0397: return;
0398:
0399: try {
0400: sheet.addCell(new Label(0, 0, "id"));
0401: for (int i = 0; i < numSelectedLangs; i++) {
0402: String selectedLang = selectedLangs[i];
0403: for (int j = 0; j < numLangs; j++) {
0404: EdLangName eln = (EdLangName) langList.elementAt(j);
0405: if (selectedLang.equals(eln.name)) {
0406: Label label = new Label(i + 1, 0, selectedLang);
0407: sheet.addCell(label);
0408: selection.add(eln);
0409: break;
0410: }
0411: }
0412: }
0413:
0414: int numStrings = ((EdLangName) langList.elementAt(0)).edLang
0415: .getNumStrings();
0416: for (int i = 0; i < numStrings; i++) {
0417: sheet
0418: .addCell(new jxl.write.Number(0, i + 1,
0419: (double) i));
0420: for (int j = 0; j < numSelectedLangs; j++) {
0421: EdLangName eln = (EdLangName) selection
0422: .elementAt(j);
0423: String txt = eln.edLang.getString(i);
0424: if (txt == null) {
0425: txt = "";
0426: }
0427: sheet.addCell(new Label(j + 1, i + 1, txt));
0428: }
0429: }
0430:
0431: workbook.write();
0432: workbook.close();
0433: } catch (Exception ex) {
0434: }
0435: /* try {
0436:
0437: try {
0438: Object c = Class.forName("com.nilostep.xlsql.jdbc.xlDriver");
0439: String test = System.getProperty("user.dir");
0440: String url="jdbc:nilostep:excel:" + theFile.getParent();
0441: // boolean b = ((java.sql.Driver)c).acceptsURL( url );
0442: Connection con = DriverManager.getConnection( url );
0443: Statement stm = con.createStatement();
0444: stm.setEscapeProcessing( true );
0445: sql = "DROP TABLE \"translation.carousel\" IF EXISTS;";
0446: stm.execute(sql);
0447:
0448: // Setup the table
0449: sql = "CREATE TABLE \"translation.carousel\" (";
0450: String sql2 = "INSERT INTO \"translation.carousel\" VALUES (";
0451: Vector selection = new Vector();
0452: int numSelectedLangs = selectedLangs.length;
0453: int numLangs = langList.size();
0454: if ( numSelectedLangs == 0 )
0455: return;
0456:
0457: for (int i = 0; i < numSelectedLangs; i++) {
0458: String selectedLang = selectedLangs[ i ];
0459: for (int j = 0; j < numLangs; j++) {
0460: EdLangName eln = (EdLangName)langList.elementAt( j );
0461: if ( selectedLang.equals( eln.name )) {
0462: if ( i > 0 ) {
0463: sql += ",";
0464: sql2 += ",";
0465: }
0466: sql += eln.name + " varchar";
0467: sql2 += "?";
0468: selection.add( eln );
0469: break;
0470: }
0471: }
0472: }
0473: sql += ");";
0474: sql2 += ");";
0475: stm.execute(sql);
0476:
0477: PreparedStatement ps = con.prepareStatement( sql2 );
0478: int numStrings = ((EdLangName)langList.elementAt( 0 )).edLang.getNumStrings();
0479: for ( int i = 0; i < numStrings; i++ ) {
0480: for ( int j = 0; j < numSelectedLangs; j++ ) {
0481: EdLangName eln = (EdLangName)selection.elementAt( j );
0482: String txt = eln.edLang.getString( i );
0483: if ( txt == null )
0484: txt = "";
0485: ps.setString( j + 1, txt );
0486: }
0487: ps.execute();
0488: }
0489: con.close();
0490: }
0491: catch (Exception e) {
0492: System.out.println("xlSQL: Something is wrong...");
0493: e.printStackTrace();
0494: }
0495: }
0496: catch ( Exception ex ) {
0497: ex.printStackTrace();
0498: }
0499: finally {
0500: // Restore the original URL
0501: url = theUrl;
0502: }*/
0503: }
0504:
0505: /**
0506: * Import content to an excel file based language resource. This assumes that
0507: * the first language exported is the master language containing all the keys
0508: * @param theFile
0509: * @param selectedLangs the list of languages to export
0510: * @throws IOException
0511: */
0512: public void importExcel(Component parent, File theFile)
0513: throws IOException {
0514: Vector selectedLangs = new Vector();
0515:
0516: URL theUrl = url;
0517: url = theFile.toURL();
0518: String sql;
0519: try {
0520:
0521: Workbook workbook = Workbook.getWorkbook(theFile);
0522: Sheet sheet = workbook.getSheet(0);
0523: Vector selectedLanguages = new Vector();
0524:
0525: int numLangs = sheet.getColumns() - 1;
0526: for (int i = 1; i <= numLangs; i++) {
0527: selectedLangs.addElement(sheet.getCell(i, 0)
0528: .getContents());
0529: selectedLanguages.addElement(new EdLanguage(this ));
0530: }
0531:
0532: EdLanguage idLang = getLang(0);
0533: int numRows = sheet.getRows();
0534: for (int i = 1; i < numRows; i++) {
0535: // Lookup the comment and get its ID to cater for row deletions in the spreadsheet
0536: String keyStr = sheet.getCell(0, i).getContents();
0537: if (keyStr != null) {
0538: int id;
0539: if (idLang != null)
0540: id = idLang.findString(keyStr);
0541: else if (keyStr.length() > 0)
0542: id = Integer.parseInt(keyStr);
0543: else
0544: break;
0545:
0546: // No new strings allowed
0547: if (id >= 0) {
0548: for (int j = 0; j < numLangs; j++) {
0549: EdLanguage el = (EdLanguage) selectedLanguages
0550: .elementAt(j);
0551: el.addString(id, null, sheet.getCell(j + 1,
0552: i).getContents());
0553: }
0554: }
0555: }
0556: }
0557:
0558: // Replace the existing languages
0559: int nextId = 0;
0560: int numExistingLangs = langList.size();
0561: for (int i = 0; i < numLangs; i++) {
0562: String selectedLang = ((String) selectedLangs
0563: .elementAt(i)).toLowerCase();
0564: int j = 0;
0565: for (; j < numExistingLangs; j++) {
0566: EdLangName eln = (EdLangName) langList.elementAt(j);
0567: nextId = Math.max(nextId, eln.id);
0568: if (selectedLang.equals(eln.name.toLowerCase())) {
0569: eln.edLang = (EdLanguage) selectedLanguages
0570: .elementAt(i);
0571: break;
0572: }
0573: }
0574:
0575: // If the language hasn't been replaced
0576: if (j == numExistingLangs) {
0577: EdLangName eln = new EdLangName();
0578: if ((i == 0) && (nextId > 0))
0579: nextId++;
0580: eln.id = nextId++;
0581: eln.code = selectedLang;
0582: eln.name = selectedLang;
0583: eln.edLang = (EdLanguage) selectedLanguages
0584: .elementAt(i);
0585: langList.add(eln);
0586: }
0587: }
0588:
0589: if (numLangs > 0) {
0590: EdLangName tempLang = ((EdLangName) langList
0591: .elementAt(0));
0592: currentLang = tempLang.edLang;
0593: refLang = currentLang;
0594: }
0595: /*
0596: try {
0597: Object c = Class.forName("com.nilostep.xlsql.jdbc.xlDriver");
0598: String test = System.getProperty("user.dir");
0599: String url="jdbc:nilostep:excel:" + theFile.getParent();
0600: Connection con = DriverManager.getConnection( url );
0601: Statement stm = con.createStatement();
0602: stm.setEscapeProcessing( true );
0603: String excelFile = theFile.getName();
0604: excelFile = excelFile.substring( 0, excelFile.length() - 3 );
0605:
0606: // Query the table
0607: sql = "SELECT * FROM \"" + excelFile + "carousel\"";
0608: ResultSet rs = stm.executeQuery( sql );
0609: ResultSetMetaData rsmd = rs.getMetaData();
0610: for ( int i = 0; i < rsmd.getColumnCount(); i++ )
0611: selectedLangs.addElement( rsmd.getColumnName( i + 1 ));
0612: String[] selectedLangNames = LangEdDesktop.getSelectedLanguages( parent, selectedLangs.toArray(), false );
0613:
0614: // Build the query for the selected languages
0615: Vector selectedLanguages = new Vector();
0616: int numSelectedLangs = selectedLangNames.length;
0617: int numLangs = langList.size();
0618: sql = "SELECT ";
0619: for ( int i = 0; i < numSelectedLangs; i++ ) {
0620: String selectedLang = selectedLangNames[ i ];
0621: if ( i > 0 )
0622: sql += ",";
0623:
0624: selectedLanguages.addElement( new EdLanguage( this ) );
0625: sql += selectedLang;
0626: }
0627: sql += " FROM \"translation.carousel\";";
0628: ResultSet rs2 = stm.executeQuery(sql);
0629:
0630: // Read the languages
0631: int numStrings = ((EdLangName)langList.elementAt( 0 )).edLang.getNumStrings();
0632: numSelectedLangs = selectedLanguages.size();
0633: EdLanguage commentLang = getLang( 0 );
0634: while ( rs2.next()) {
0635: // Lookup the comment and get its ID to cater for row deletions in the spreadsheet
0636: String keyStr = rs2.getString( 1 );
0637: if ( keyStr != null ) {
0638: int id = commentLang.findString( keyStr );
0639: // No new strings allowed
0640: if ( id >= 0 ) {
0641: for ( int j = 1; j < numSelectedLangs; j++ ) {
0642: EdLanguage el = ( EdLanguage )selectedLanguages.elementAt( j );
0643: el.addString( id, rs2.getString( j + 1 ) );
0644: }
0645: }
0646: }
0647: }
0648: con.close();
0649:
0650: // Replace the existing languages
0651: for ( int i = 1; i < numSelectedLangs; i++ ) {
0652: String selectedLang = selectedLangNames[ i ].toLowerCase();
0653: for ( int j = 0; j < numLangs; j++ ) {
0654: EdLangName eln = ( EdLangName )langList.elementAt( j );
0655: if ( selectedLang.equals( eln.name.toLowerCase() ) ) {
0656: eln.edLang = (EdLanguage)selectedLanguages.elementAt( i );
0657: break;
0658: }
0659: }
0660: }
0661: }
0662: catch (Exception e) {
0663: System.out.println("xlSQL: Something is wrong...");
0664: e.printStackTrace();
0665: }*/
0666: } catch (Exception ex) {
0667: ex.printStackTrace();
0668: } finally {
0669: // Restore the original URL
0670: url = theUrl;
0671: }
0672: }
0673:
0674: public boolean isLoadedFromExportedFiles() {
0675: return loadedFromExportedFiles;
0676: }
0677:
0678: /**
0679: * Sets the URL to which the languages will be saved.
0680: * @param url The URL for the file save.
0681: */
0682: void setUrl(URL _url) {
0683: url = _url;
0684: }
0685:
0686: /**
0687: * Gets the URL to which the languages will be saved.
0688: * @param url The URL for the file save.
0689: */
0690: URL getUrl() {
0691: return url;
0692: }
0693:
0694: /**
0695: * Adds a new language
0696: * This function is intended primarily for internal use by the
0697: * catalogue viewer.
0698: * @param code The ISO code for a locale.
0699: * @param name A plain text name for the language in the national language.
0700: */
0701: public void addLang(String code, String name, String encoding,
0702: boolean useDb) {
0703: if (langList == null) {
0704: langList = new Vector(5);
0705: currentLang = null;
0706: }
0707:
0708: int numLangs = langList.size();
0709:
0710: int id = 0;
0711: for (int i = 0; i < numLangs; i++) {
0712: EdLangName tempLang = ((EdLangName) langList.elementAt(i));
0713: id = Math.max(id, tempLang.id);
0714: }
0715:
0716: EdLangName ln = new EdLangName();
0717: ln.id = ++id;
0718: ln.code = code;
0719: ln.name = name;
0720: ln.edLang = new EdLanguage(this );
0721: ln.encoding = encoding;
0722:
0723: if (currentLang == null)
0724: currentLang = ln.edLang;
0725:
0726: langList.add(ln);
0727:
0728: if (useDb) {
0729: // LanguageObject lo = new LanguageObject();
0730: // lo.setStatus( lo.NEW_ITEM );
0731: // lo.setField( 1, name );
0732: // lo.setField( 2, code );
0733: // lo.insert();
0734: //
0735: // new TranslationObject().addLanguage( code );
0736: }
0737: }
0738:
0739: /**
0740: * Returns a language from the specified index in the list.
0741: * @param index <CODE>int</CODE> specifing the index
0742: * @return the returned <CODE>EdLanguage</CODE> instance
0743: */
0744: public EdLanguage getLangFromList(int index) {
0745: int items = langList.size();
0746: if (index < items) {
0747: EdLangName tempLangName = (EdLangName) langList
0748: .elementAt(index);
0749: if (tempLangName.edLang.getLangCode() == null) {
0750: tempLangName.edLang.setLangCode(tempLangName.code);
0751: tempLangName.edLang.setLangName(tempLangName.name);
0752: }
0753: return tempLangName.edLang;
0754: }
0755: return currentLang;
0756: }
0757:
0758: /**
0759: * Loads a language and sets it as the default language.
0760: * This function is intended primarily for internal use by the
0761: * catalogue viewer.
0762: * @param index the index for the language.
0763: */
0764: public EdLanguage getLang(int index) {
0765: int items = langList.size();
0766: if (index < items) {
0767: EdLanguage backupLang = currentLang;
0768: // try {
0769: EdLangName tempLangName = (EdLangName) langList
0770: .elementAt(index);
0771: // if ( tempLangName.edLang == null ) {
0772: // String str = url.toString();
0773: // String newStr = str.substring( 0, str.lastIndexOf( '/' ) + 1 ) + tempLangName.code.toLowerCase() + ".jln";
0774: // tempLangName.edLang = read( new URL( newStr ) );
0775: // tempLangName.edLang.setLangName( tempLangName.name );
0776: // tempLangName.edLang.setLangCode( tempLangName.code );
0777: // return tempLangName.edLang;
0778: // }
0779: if (tempLangName.edLang.getLangCode() == null) {
0780: tempLangName.edLang.setLangCode(tempLangName.code);
0781: tempLangName.edLang.setLangName(tempLangName.name);
0782: }
0783: currentLang = tempLangName.edLang;
0784: // }
0785: // catch ( MalformedURLException excep ) {
0786: // System.out.println( "Malformed URL: " + excep );
0787: // }
0788: // catch ( IOException e ) {
0789: // System.out.println( "IO Exception: " + e );
0790: // }
0791: }
0792: return currentLang;
0793: }
0794:
0795: /**
0796: * Loads a language and sets it as the default language.
0797: * This function is intended primarily for internal use by the
0798: * catalogue viewer.
0799: * @param index the index for the language.
0800: */
0801: public EdLanguage getLang(String langName) {
0802: int items = langList.size();
0803: for (int i = 0; i < items; i++) {
0804: if (((EdLangName) langList.elementAt(i)).name == langName) {
0805: EdLanguage backupLang = currentLang;
0806: EdLangName tempLangName = (EdLangName) langList
0807: .elementAt(i);
0808: // if ( tempLangName.edLang == null ) {
0809: // String str = url.toString();
0810: // String newStr = str.substring( 0, str.lastIndexOf( '/' ) + 1 ) + ( ( EdLangName ) langList.elementAt( i ) ).code.toLowerCase() + ".jln";
0811: // try {
0812: // tempLangName.edLang = read( new URL( newStr ) );
0813: // tempLangName.edLang.setLangName( ( ( EdLangName ) langList.elementAt( i ) ).name );
0814: // tempLangName.edLang.setLangCode( ( ( EdLangName ) langList.elementAt( i ) ).code );
0815: // return tempLangName.edLang;
0816: // }
0817: // catch ( MalformedURLException excep ) {
0818: // System.out.println( "Malformed URL: " + excep );
0819: // }
0820: // catch ( IOException e ) {
0821: // System.out.println( "IO Exception: " + e );
0822: // }
0823: // currentLang = backupLang;
0824: // }
0825: // else {
0826: if (tempLangName.edLang.getLangCode() == null) {
0827: tempLangName.edLang.setLangCode(tempLangName.code);
0828: tempLangName.edLang.setLangName(tempLangName.name);
0829: }
0830: return tempLangName.edLang;
0831: // }
0832: }
0833: }
0834: return currentLang;
0835: }
0836:
0837: /**
0838: * Returns the current language... the comments language
0839: */
0840: public EdLanguage getCurrentLang() {
0841: return currentLang;
0842: }
0843:
0844: /**
0845: * Gets the key corresponding to an ID
0846: * @param id
0847: * @return
0848: */
0849: public String getKey(int id) {
0850: String src = refLang.getString(id);
0851: if (usesReferenceLang)
0852: return src.replaceAll(" ", "_");
0853:
0854: return "str_" + Integer.toString(id);
0855: }
0856:
0857: public void setId(String id) {
0858: refLang.addString("", id);
0859: }
0860:
0861: /**
0862: * Reads a language from the vector
0863: */
0864: private EdLanguage read(Vector translations, int field) {
0865: currentLang = new EdLanguage(this );
0866:
0867: currentLang.read(translations, field, false);
0868: return currentLang;
0869: }
0870:
0871: /**
0872: * Returns the number of languages
0873: */
0874: public int getNumLangs() {
0875: return langList == null ? 0 : langList.size();
0876: }
0877:
0878: /**
0879: * Returns the name of the indexed language
0880: */
0881: public String getLangName(int index) {
0882: int items = langList.size();
0883: if ((items > 0) && (index < items))
0884: return ((EdLangName) langList.elementAt(index)).name;
0885:
0886: return "";
0887: }
0888:
0889: /**
0890: * Returns the name of the indexed language
0891: */
0892: public String getLangCode(int index) {
0893: int items = langList.size();
0894: if ((items > 0) && (index < items))
0895: return ((EdLangName) langList.elementAt(index)).code;
0896:
0897: return "";
0898: }
0899:
0900: public void setCurrentLang(String langName) {
0901: currentLang = getLang(langName);
0902: }
0903:
0904: public void saveFile(char delimiterStartChar, char delimiterEndChar) {
0905: saveSelected(delimiterStartChar, delimiterEndChar, true, false,
0906: langList);
0907: }
0908:
0909: /**
0910: * Saves all languages back to their original URL.
0911: */
0912: private void saveSelected(char delimiterStartChar,
0913: char delimiterEndChar, boolean doDatabaseSave,
0914: boolean bExported, Vector selectedList) {
0915: if (langList == null)
0916: return;
0917:
0918: int maxStrId = 0;
0919: int items = langList.size();
0920: if (items == 0)
0921: return;
0922:
0923: try {
0924: String projectPath = getProjectPath();
0925: if (projectPath != null) {
0926: BufferedWriter os;
0927: File file = new File(projectPath + File.separator
0928: + "lang" + File.separator
0929: + "LanguageList.properties");
0930: if (!file.exists())
0931: file.createNewFile();
0932: os = new BufferedWriter(new FileWriter(file));
0933:
0934: int numLangs = langList.size();
0935: for (int i = 0; i < numLangs; i++) {
0936: EdLangName tempLang = ((EdLangName) langList
0937: .elementAt(i));
0938: String s = tempLang.name + "=" + tempLang.code
0939: + "\r\n";
0940: os.write(s);
0941:
0942: // Save the language as a properties file.
0943: String str = projectPath + File.separator + "lang"
0944: + File.separator
0945: + tempLang.code.toLowerCase()
0946: + ".properties";
0947: if (tempLang.edLang != null)
0948: tempLang.edLang.saveProperties(str,
0949: tempLang.encoding);
0950: }
0951:
0952: os.close();
0953: }
0954: } catch (IOException exp) {
0955: System.out.println("IO Exception: " + exp);
0956: }
0957: }
0958:
0959: private void saveToDatabase() {
0960: // try {
0961: // int numLangs = langList.size();
0962: // int numStrings = refLang.getNumStrings();
0963: // Vector mappings = new Vector();
0964: //
0965: // for ( int i = 0; i < numStrings; i++ ) {
0966: // TranslationObject tm = new TranslationObject();
0967: //
0968: // int id = refLang.getStringId( i );
0969: // tm.setField( tm.FIELD_ID, id );
0970: // int status = -1;
0971: // for ( int j = 0; j < numLangs; j++ ) {
0972: // EdLanguage el = ( ( EdLangName ) langList.elementAt( j ) ).edLang;
0973: // if ( el != null ) {
0974: // LangItem li = el.getLangItem( id );
0975: // if ( li != null ) {
0976: // tm.setField( 1 + j, li.langStr );
0977: // int langItemStatus = li.getStatus();
0978: // switch ( langItemStatus ) {
0979: // case TranslationObject.NEW_ITEM:
0980: //
0981: // // If the object exists already it will have a modified flag.
0982: // if ( ( status < 0 ) || ( status == TranslationObject.NEW_ITEM ) )
0983: // status = TranslationObject.NEW_ITEM;
0984: // else
0985: // status = TranslationObject.MODIFIED;
0986: // break;
0987: // case TranslationObject.MODIFIED:
0988: //
0989: //// if ( status != TranslationObject.NEW_ITEM )
0990: // status = TranslationObject.MODIFIED;
0991: // break;
0992: // default:
0993: // status = 0;
0994: // break;
0995: // }
0996: // li.status = li.OK;
0997: // }
0998: // }
0999: // }
1000: // if ( status > TranslationObject.OK ) {
1001: // tm.setStatus( status );
1002: // mappings.add( tm );
1003: // }
1004: // }
1005: //
1006: // TranslationObject.getPrototype().updateList( mappings );
1007: // }
1008: // catch ( Exception exp ) {
1009: // exp.printStackTrace();
1010: // }
1011: }
1012:
1013: public void newFile() {
1014: EdLangName ln = new EdLangName();
1015: ln.id = 0;
1016: ln.code = "CM";
1017: ln.name = "Comments";
1018: ln.edLang = new EdLanguage(this );
1019:
1020: langList = new Vector();
1021: langList.addElement(ln);
1022: }
1023:
1024: private EdLanguage currentLang, refLang;
1025: private Vector langList;
1026: private String langName;
1027: private URL url;
1028: private boolean bIsStandalone = true;
1029: private boolean usesDatabaseSource = false;
1030: private boolean usesReferenceLang = true;
1031: private boolean loadedFromExportedFiles = false;
1032: }
1033:
1034: class EdLangName {
1035: public EdLanguage edLang;
1036:
1037: public int id;
1038: public String code;
1039: public String name;
1040: public String encoding;
1041:
1042: public EdLangName() {
1043: edLang = null;
1044: encoding = null;
1045: }
1046:
1047: public String toString() {
1048: return name;
1049: }
1050: }
|