View Javadoc
1   /*
2    * Copyright (c) 2012, Dienst Landelijk Gebied - Ministerie van Economische Zaken
3    * 
4    * Gepubliceerd onder de BSD 2-clause licentie, 
5    * zie https://github.com/MinELenI/CBSviewer/blob/master/LICENSE.md voor de volledige licentie.
6    */
7   package nl.mineleni.cbsviewer.util;
8   
9   import java.util.Enumeration;
10  import java.util.MissingResourceException;
11  import java.util.PropertyResourceBundle;
12  import java.util.ResourceBundle;
13  
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  /**
18   * Resourcebundel bundle voor de applicatie.
19   * 
20   * @author prinsmc
21   * @since 1.6
22   */
23  public class LabelsBundle extends ResourceBundle {
24  
25  	/** LOGGER. */
26  	private static final Logger LOGGER = LoggerFactory
27  			.getLogger(LabelsBundle.class);
28  
29  	/** resource bundle. */
30  	private PropertyResourceBundle resBundle;
31  
32  	/**
33  	 * Instantiates a new labels bundle.
34  	 */
35  
36  	public LabelsBundle() {
37  		try {
38  			this.resBundle = (PropertyResourceBundle) PropertyResourceBundle
39  					.getBundle(
40  							"LabelsBundle",
41  							ResourceBundle.Control
42  									.getControl(ResourceBundle.Control.FORMAT_PROPERTIES));
43  		} catch (final NullPointerException e) {
44  			LOGGER.error("baseName of control is null.", e);
45  		} catch (final MissingResourceException e) {
46  			LOGGER.error(
47  					"De resourcebundel (LabelsBundle.properties) is niet gevonden",
48  					e);
49  		} catch (final IllegalArgumentException e) {
50  			LOGGER.error(
51  					"ResourceBundle.Control heeft geen locale kunnen achterhalen.",
52  					e);
53  		}
54  	}
55  
56  	/**
57  	 * Geeft sleutel/waarde paren als een javascript OpenLayers object.
58  	 * Vooralsnog wordt alleen de nl taal ondersteund.
59  	 * 
60  	 * @return the open layers lang bundle
61  	 */
62  	public String getOpenLayersLangBundle() {
63  		final StringBuilder sb = new StringBuilder(
64  				"\n/* <![CDATA[ */ OpenLayers.Lang.nl = OpenLayers.Util.extend({");
65  		for (final Enumeration<String> keys = this.getKeys(); keys
66  				.hasMoreElements();) {
67  			final String key = keys.nextElement();
68  			if (LOGGER.isDebugEnabled()) {
69  				sb.append("\n");
70  			}
71  			sb.append("'");
72  			sb.append(key);
73  			sb.append("'");
74  			sb.append(":'");
75  			sb.append(this.getString(key).replaceAll("\\{[0-9]\\}*", "\\$$0"));
76  			sb.append("',");
77  		}
78  		sb.deleteCharAt(sb.length() - 1);
79  		sb.append("}, OpenLayers.Lang.nl);/* ]]> */");
80  		return sb.toString();
81  	}
82  
83  	@Override
84  	public Enumeration<String> getKeys() {
85  		return this.resBundle.getKeys();
86  	}
87  
88  	@Override
89  	protected Object handleGetObject(final String key) {
90  		return this.resBundle.handleGetObject(key);
91  	}
92  }