Revision 26048

View differences:

branches/v10/extensions/extI18n/src/org/gvsig/i18n/extension/preferences/table/LocaleTableModel.java
30 30

  
31 31
import javax.swing.table.AbstractTableModel;
32 32

  
33
import org.gvsig.i18n.I18nManager;
34
import org.gvsig.i18n.Messages;
33
import org.gvsig.i18n.*;
35 34

  
36 35
/**
37 36
 * TableModel to show the list of available Locales in gvSIG.
......
140 139
     * 
141 140
     * @param locale
142 141
     *            to remove
142
     * @throws I18nException
143
     *             if there is an error removing a Locale
143 144
     */
144
    public void removeLocale(Locale locale) {
145
    public void removeLocale(Locale locale) throws I18nException {
145 146
	int rowIndex = getLocaleRowIndex(locale);
146 147
	manager.uninstallLocale(locale);
147 148
	loadLocales();
branches/v10/extensions/extI18n/src/org/gvsig/i18n/extension/preferences/I18nPreferencePage.java
37 37
import javax.swing.table.TableColumn;
38 38

  
39 39
import org.gvsig.gui.beans.swing.JButton;
40
import org.gvsig.i18n.I18nManager;
41
import org.gvsig.i18n.Messages;
40
import org.gvsig.i18n.*;
42 41
import org.gvsig.i18n.extension.preferences.table.*;
43 42
import org.gvsig.i18n.impl.I18nManagerImpl;
44 43

  
......
166 165

  
167 166
	if (returnVal == JFileChooser.APPROVE_OPTION) {
168 167
	    File importFile = fileChooser.getSelectedFile();
169
	    Locale[] installedLocales = manager.installLocales(importFile);
170
	    if (installedLocales == null || installedLocales.length == 0) {
168
	    try {
169
		Locale[] installedLocales = manager.installLocales(importFile);
170
		if (installedLocales == null || installedLocales.length == 0) {
171 171
		JOptionPane
172 172
			.showMessageDialog(
173 173
				this,
......
176 176
				Messages
177 177
					.getText("I18nPreferencePage.error_instalar_idiomas"),
178 178
				JOptionPane.ERROR_MESSAGE);
179
	    }
180
	    else {
179
		}
180
		else {
181 181
		StringBuffer msg = new StringBuffer(Messages
182 182
			.getText("I18nPreferencePage.idiomas_instalados"));
183 183

  
......
191 191
		JOptionPane.showMessageDialog(this, msg.toString(), Messages
192 192
			.getText("I18nPreferencePage.instalar_idiomas"),
193 193
			JOptionPane.INFORMATION_MESSAGE);
194
		}
195
	    } catch (I18nException ex) {
196
		ex.showError();
194 197
	    }
195 198
	}
196 199
    }
......
238 241

  
239 242
	    if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
240 243
		File saveFile = fileChooser.getSelectedFile();
241
		manager.exportLocaleForUpdate(locale, selected.getLocale(),
242
			saveFile);
244
		try {
245
		    manager.exportLocaleForUpdate(locale, selected.getLocale(),
246
		    	saveFile);
243 247

  
244
		JOptionPane.showMessageDialog(this, Messages
245
			.getText("I18nPreferencePage.exportado_textos_idioma")
246
			+ " "
247
			+ manager.getDisplayName(locale)
248
			+ Messages.getText("I18nPreferencePage.al_archivo")
249
			+ saveFile, Messages
250
			.getText("I18nPreferencePage.exportar_idioma"),
251
			JOptionPane.INFORMATION_MESSAGE);
248
		    JOptionPane.showMessageDialog(this, Messages
249
		    	.getText("I18nPreferencePage.exportado_textos_idioma")
250
		    	+ " "
251
		    	+ manager.getDisplayName(locale)
252
		    	+ Messages.getText("I18nPreferencePage.al_archivo")
253
		    	+ saveFile, Messages
254
		    	.getText("I18nPreferencePage.exportar_idioma"),
255
		    	JOptionPane.INFORMATION_MESSAGE);
256
		} catch (I18nException ex) {
257
		    ex.showError();
258
		}
252 259
	    }
253 260
	}
254 261
    }
......
295 302

  
296 303
	    if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
297 304
		File saveFile = fileChooser.getSelectedFile();
298
		manager.exportLocaleForTranslation(selected.getLocale(),
299
			refLocale, saveFile);
305
		try {
306
		    manager.exportLocaleForTranslation(selected.getLocale(),
307
		    	refLocale, saveFile);
300 308

  
301
		JOptionPane
302
			.showMessageDialog(
303
				this,
304
				Messages
305
					.getText("I18nPreferencePage.idioma_preparado_traducir")
306
					+ manager.getDisplayName(selected
307
						.getLocale())
308
					+ Messages
309
						.getText("I18nPreferencePage.en_archivo")
310
					+ saveFile,
311
				Messages
312
					.getText("I18nPreferencePage.exportar_idioma"),
313
				JOptionPane.INFORMATION_MESSAGE);
309
		    JOptionPane
310
		    	.showMessageDialog(
311
		    		this,
312
		    		Messages
313
		    			.getText("I18nPreferencePage.idioma_preparado_traducir")
314
		    			+ manager.getDisplayName(selected
315
		    				.getLocale())
316
		    			+ Messages
317
		    				.getText("I18nPreferencePage.en_archivo")
318
		    			+ saveFile,
319
		    		Messages
320
		    			.getText("I18nPreferencePage.exportar_idioma"),
321
		    		JOptionPane.INFORMATION_MESSAGE);
322
		} catch (I18nException ex) {
323
		    ex.showError();
324
		}
314 325
	    }
315 326
	}
316 327

  
......
408 419
				    .getText("I18nPreferencePage.confirmar_desinstalar_idioma"),
409 420
			    JOptionPane.YES_NO_OPTION);
410 421
	    if (option == JOptionPane.YES_OPTION) {
411
		tableModel.removeLocale(locale);
422
		try {
423
		    tableModel.removeLocale(locale);
424
		} catch (I18nException ex) {
425
		    ex.showError();
426
		}
412 427
	    }
413 428
	}
414 429
    }
branches/v10/extensions/extI18n/src/org/gvsig/i18n/I18nException.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 {DiSiD Technologies}  {New extension for installation and update of text translations}
26
 */
27
package org.gvsig.i18n;
28

  
29
import com.iver.andami.messages.NotificationManager;
30

  
31
/**
32
 * Exception for errors of the I18nManager.
33
 * 
34
 * TODO: hacer que extienda BaseException cuando se pase a gvSIG trunk.
35
 * 
36
 * @author <a href="mailto:dcervera@disid.com">David Cervera</a>
37
 */
38
public class I18nException extends Exception {
39

  
40
    private static final long serialVersionUID = 2877056472517898602L;
41
    
42
    private String key;
43

  
44
    /**
45
     * Creates a new exception with an error message.
46
     * 
47
     * @param message
48
     *            the error message
49
     * @param key
50
     *            the i18n key to localize the exception message
51
     */
52
    public I18nException(String message, String key) {
53
	super(message);
54
	this.key = key;
55
    }
56

  
57
    /**
58
     * Creates a new exception with an error message and a cause exception.
59
     * 
60
     * @param message
61
     *            the error message
62
     * @param key
63
     *            the i18n key to localize the exception message
64
     * @param cause
65
     *            the error that caused the exception
66
     */
67
    public I18nException(String message, String key, Throwable cause) {
68
	super(message, cause);
69
	this.key = key;
70
    }
71
    
72
    public String getLocalizedMessage() {
73
	String msg = Messages.getText(key);
74
	if (msg.equals(key)) {
75
	    msg = getMessage();
76
	}
77
	return msg;
78
    }
79

  
80
    /**
81
     * Show the exception error in through the NotificationManager.
82
     */
83
    public void showError() {
84
	NotificationManager.addError(getLocalizedMessage(), this);
85
    }
86
}
branches/v10/extensions/extI18n/src/org/gvsig/i18n/impl/InstallLocalesException.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Gobernment (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 {DiSiD Technologies}  {New extension for installation and update of text translations}
26
*/
27
package org.gvsig.i18n.impl;
28

  
29
import java.io.File;
30

  
31
import org.gvsig.i18n.I18nException;
32

  
33
/**
34
 * Exception for errors installing a locale or locales from an import file.
35
 * 
36
 * @author <a href="mailto:dcervera@disid.com">David Cervera</a>
37
 */
38
public class InstallLocalesException extends I18nException {
39

  
40
    private static final long serialVersionUID = 8426119666919876086L;
41

  
42
    private static final String MSG = "Unable to install locales from the imported file";
43

  
44
    private static final String KEY = "_InstallLocalesException";
45
    
46
    private final File importFile;
47

  
48
    public InstallLocalesException(File importFile) {
49
	super(MSG, KEY);
50
	this.importFile = importFile;
51
    }
52

  
53
    public InstallLocalesException(File importFile, Throwable cause) {
54
	super(MSG, KEY, cause);
55
	this.importFile = importFile;
56
    }
57
 
58
    public String getLocalizedMessage() {
59
	return super.getLocalizedMessage() + ": " + importFile;
60
    }
61
}
branches/v10/extensions/extI18n/src/org/gvsig/i18n/impl/UninstallLocaleException.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Gobernment (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 {DiSiD Technologies}  {New extension for installation and update of text translations}
26
*/
27
package org.gvsig.i18n.impl;
28

  
29
import java.util.Locale;
30

  
31
import org.gvsig.i18n.I18nException;
32

  
33
/**
34
 * Exception for errors uninstalling a {@link Locale}?
35
 * 
36
 * @author <a href="mailto:dcervera@disid.com">David Cervera</a>
37
 */
38
public class UninstallLocaleException extends I18nException {
39

  
40
    private static final long serialVersionUID = -7147574247950810955L;
41

  
42
    private static final String MSG = "The locale "
43
	    + " is the current locale or a reference locale, "
44
	    + "can't be unsintalled";
45

  
46
    private static final String KEY = "_UninstallLocaleException";
47
    
48
    private final Locale locale;
49

  
50
    /**
51
     */
52
    public UninstallLocaleException(Locale locale) {
53
	super(MSG, KEY);
54
	this.locale = locale;
55
    }
56

  
57
    /**
58
     */
59
    public UninstallLocaleException(Locale locale, Throwable cause) {
60
	super(MSG, KEY, cause);
61
	this.locale = locale;
62
    }
63
 
64
    public String getLocalizedMessage() {
65
	return super.getLocalizedMessage() + ": " + locale;
66
    }
67
}
branches/v10/extensions/extI18n/src/org/gvsig/i18n/impl/I18nManagerImpl.java
32 32
import java.util.jar.*;
33 33
import java.util.zip.ZipEntry;
34 34

  
35
import org.gvsig.i18n.I18nManager;
36
import org.gvsig.i18n.Messages;
35
import org.gvsig.i18n.*;
37 36

  
38 37
import com.iver.andami.Launcher;
39 38
import com.iver.andami.PluginServices;
40 39
import com.iver.andami.config.generate.AndamiConfig;
40
import com.iver.utiles.StringUtilities;
41 41
import com.iver.utiles.XMLEntity;
42 42

  
43 43
/**
......
154 154
		.toArray(new Locale[registeredLocales.size()]);
155 155
    }
156 156

  
157
    public void uninstallLocale(Locale locale) {
157
    public void uninstallLocale(Locale locale) throws I18nException {
158 158
	if (getCurrentLocale().equals(locale) || isReferenceLocale(locale)) {
159
	    // TODO: Exception
160
	    // throw
159
	    throw new UninstallLocaleException(locale);
161 160
	}
162 161

  
163 162
	if (registeredLocales.remove(locale)) {
......
248 247
	config.setLocaleVariant(locale.getVariant());
249 248
    }
250 249

  
251
    public Locale[] installLocales(File importFile) {
250
    public Locale[] installLocales(File importFile) throws I18nException {
252 251
	List importLocales = new ArrayList();
253 252

  
254 253
	try {
......
296 295
		}
297 296
	    }
298 297

  
299
	} catch (FileNotFoundException e) {
300
	    // TODO Auto-generated catch block
301
	    e.printStackTrace();
302
	} catch (IOException e) {
303
	    // TODO Auto-generated catch block
304
	    e.printStackTrace();
298
	} catch (Exception ex) {
299
	    throw new InstallLocalesException(importFile, ex);
305 300
	}
306 301

  
307 302
	return (Locale[]) importLocales
......
309 304
    }
310 305

  
311 306
    public void exportLocaleForUpdate(Locale locale, Locale referenceLocale,
312
	    File exportFile) {
307
	    File exportFile) throws I18nException {
313 308
	Manifest manifest = new Manifest();
314 309

  
315 310
	// First, add an entry for the locale to update
......
358 353
	    ps.close();
359 354
	    jaros.close();
360 355
	    fos.close();
361
	} catch (IOException e) {
362
	    // TODO Auto-generated catch block
363
	    e.printStackTrace();
356
	} catch (IOException ex) {
357
	    throw new ExportLocaleException(locale, ex);
364 358
	}
365 359
    }
366 360

  
367 361
    public void exportLocaleForTranslation(Locale locale,
368
	    Locale referenceLocale, File exportFile) {
362
	    Locale referenceLocale, File exportFile) throws I18nException {
369 363
	Manifest manifest = new Manifest();
370 364

  
371 365
	// First, add an entry for the locale to update
......
412 406
	    // bos.close();
413 407
	    jaros.close();
414 408
	    fos.close();
415
	} catch (IOException e) {
416
	    // TODO Auto-generated catch block
417
	    e.printStackTrace();
409
	} catch (IOException ex) {
410
	    throw new ExportLocaleException(locale, ex);
418 411
	}
419 412
    }
420 413

  
......
470 463
	for (Iterator iterator = texts.entrySet().iterator(); iterator
471 464
		.hasNext();) {
472 465
	    Entry entry = (Entry) iterator.next();
473
	    ps.print((String) entry.getKey());
466
	    String keyEncoded = escape((String) entry.getKey(), true);
467
	    ps.print(keyEncoded);
474 468
	    ps.print("=");
475 469
	    if (withValue) {
476
		ps.println((String) entry.getValue());
470
		String valueEncoded = escape((String) entry.getValue(), false);
471
		ps.println(valueEncoded);
477 472
	    }
478 473
	    else {
479 474
		ps.println();
......
627 622

  
628 623
	getI18nPersistence().addChild(localesEntity);
629 624
    }
625

  
626
    private String escape(String value, boolean replaceBlanks) {
627
	// First replace non printable characters
628
	if (replaceBlanks) {
629
	    value = StringUtilities.replace(value, " ", "\\ ");
630
	}
631
	value = StringUtilities.replace(value, ":", "\\:");
632
	value = StringUtilities.replace(value, "\n", "\\n");
633
	value = StringUtilities.replace(value, "\t", "\\t");
634
	value = StringUtilities.replace(value, "\b", "\\b");
635
	value = StringUtilities.replace(value, "\f", "\\f");
636
	value = StringUtilities.replace(value, "\r", "\\r");
637
	// value = StringUtilities.replace(value, "\\", "\\\\");
638
	value = StringUtilities.replace(value, "\'", "\\\'");
639
	value = StringUtilities.replace(value, "\"", "\\\"");
640

  
641
	// Next, encode in raw-unicode-escape
642
	return toRawUnicodeEncoded(value);
643
    }
644

  
645
    private String toRawUnicodeEncoded(String value) {
646
	StringBuffer sb = new StringBuffer();
647
	for (int i = 0; i < value.length(); i++) {
648
	    char c = value.charAt(i);
649
	    if (c <= 0x80) {
650
		sb.append(c);
651
	    }
652
	    else {
653
		sb.append("\\u");
654
		String hexValue = Integer.toHexString((int) c);
655
		// Append 0 if the hex value has less than 4 digits
656
		for (int j = hexValue.length(); j < 4; j++) {
657
		    sb.append('0');
658
		}
659
		sb.append(hexValue);
660
	    }
661
	}
662
	return sb.toString();
663
    }
630 664
}
branches/v10/extensions/extI18n/src/org/gvsig/i18n/impl/ExportLocaleException.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Gobernment (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 {DiSiD Technologies}  {New extension for installation and update of text translations}
26
*/
27
package org.gvsig.i18n.impl;
28

  
29
import java.util.Locale;
30

  
31
import org.gvsig.i18n.I18nException;
32

  
33
/**
34
 * Exception for errors exporting a Locale for translation or update.
35
 * 
36
 * @author <a href="mailto:dcervera@disid.com">David Cervera</a>
37
 */
38
public class ExportLocaleException extends I18nException {
39

  
40
    private static final long serialVersionUID = -1386427767068278421L;
41

  
42
    private static final String MSG = "Unable to export the locale";
43

  
44
    private static final String KEY = "_ExportLocaleException";
45
    
46
    private final Locale locale;
47

  
48
    public ExportLocaleException(Locale locale) {
49
	super(MSG, KEY);
50
	this.locale = locale;
51
    }
52

  
53
    public ExportLocaleException(Locale locale, Throwable cause) {
54
	super(MSG, KEY, cause);
55
	this.locale = locale;
56
    }
57
 
58
    public String getLocalizedMessage() {
59
	return super.getLocalizedMessage() + ": " + locale;
60
    }
61
}
branches/v10/extensions/extI18n/src/org/gvsig/i18n/I18nManager.java
111 111
     *            the jar or zip file which contains the locales and the
112 112
     *            translations as resource bundle files
113 113
     * @return the list of installed or updated locales
114
     * @throws I18nException
115
     *             if the locales could'n be installed
114 116
     */
115
    Locale[] installLocales(File importFile);
117
    Locale[] installLocales(File importFile) throws I18nException;
116 118

  
117 119
    /**
118 120
     * Uninstalls a locale and its translation.
119 121
     * 
120 122
     * @param locale
121 123
     *            to uninstall
124
     * @throws I18nException
125
     *             if the locale can't be uninstalled
122 126
     */
123
    void uninstallLocale(Locale locale);
127
    void uninstallLocale(Locale locale) throws I18nException;
124 128

  
125 129
    /**
126 130
     * Exports the translations of a locale to update or complete its
......
134 138
     *            the locale to be used as reference
135 139
     * @param exportFile
136 140
     *            the jar file to export to
141
     * @throws I18nException
142
     *             if the locale could not be exported for update
137 143
     */
138 144
    void exportLocaleForUpdate(Locale locale, Locale referenceLocale,
139
	    File exportFile);
145
	    File exportFile) throws I18nException;
140 146

  
141 147
    /**
142 148
     * Exports the keys of text to translate to a new locale to a jar file. Into
......
149 155
     *            the locale to be used as reference
150 156
     * @param exportFile
151 157
     *            the jar file to export to
158
     * @throws I18nException
159
     *             if the locale could not be exported for translation
152 160
     */
153 161
    void exportLocaleForTranslation(Locale locale, Locale referenceLocale,
154
	    File exportFile);
162
	    File exportFile) throws I18nException;
155 163

  
156 164
    /**
157 165
     * Returns the list of default locales to use as reference when exporting to

Also available in: Unified diff