
nz.co.senanque.localemanagement.XMLMessageSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of madura-objects Show documentation
Show all versions of madura-objects Show documentation
This is essentially a JAXB/XJC plugin that adds automatic validation to the generated Java classes.
You define business objects in an XSD file, pass it through XJC and the plugin will add the validation code.
It uses information in the XSD to pick up validation, and you can specify extensions to that in the XSD. The resulting
Java classes check for validity when the setter is called and they reject attempts to set invalid values (this is a difference from other validation frameworks). The Java classes also expose a metadata interface to make it easy for UIs to generate, say, lists of options for a select box.
The validation framework handles single field validation but you can inject a rule engine (or several) to handle cross field validation.
But to any Java code the objects still look like ordinary Java beans. The surrounding application is unaware that they are anything different until they throw an exception. This makes it easy to use with frameworks that expect Java beans, and most of them do.
/*******************************************************************************
* Copyright (c)2014 Prometheus Consulting
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package nz.co.senanque.localemanagement;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.core.io.Resource;
/**
*
* Acts as a message source, drawing data from an XML file. This is used for testing rather
* than production because you can use .properties files just as well as XML.
* But it is useful as a template for getting the information from a database
*
* @author Roger Parkinson
* @version $Revision:$
*/
public class XMLMessageSource extends AbstractMessageSource implements InitializingBean
{
private static final Logger logger = LoggerFactory.getLogger(XMLMessageSource.class);
private Resource m_resource;
private Map> m_map = new HashMap>();
/* (non-Javadoc)
* @see org.springframework.context.support.AbstractMessageSource#resolveCode(java.lang.String, java.util.Locale)
*/
@Override
protected MessageFormat resolveCode(String code, Locale locale)
{
String ret = null;
String country = locale.getCountry();
String language = locale.getLanguage();
String variant = locale.getVariant();
logger.debug("Code {} Initial locale {}",code,locale.toString());
Locale thisLocale = null;
if (!StringUtils.isEmpty(variant))
{
thisLocale = new Locale(language,country,variant);
Map m = m_map.get(thisLocale);
if (m != null)
{
ret = m.get(code);
}
logger.debug("tried locale {} result: {}",thisLocale.toString(),ret);
}
if (ret == null)
{
if (!StringUtils.isEmpty(country))
{
thisLocale = new Locale(language,country);
Map m = m_map.get(thisLocale);
if (m != null)
{
ret = m.get(code);
}
logger.debug("tried locale {} result: {}",thisLocale.toString(),ret);
}
}
if (ret == null)
{
if (!StringUtils.isEmpty(language))
{
thisLocale = new Locale(language);
Map m = m_map.get(thisLocale);
if (m != null)
{
ret = m.get(code);
}
logger.debug("tried locale {} result: {}",thisLocale.toString(),ret);
}
}
if (ret == null)
{
thisLocale = Locale.getDefault();
Map m = m_map.get(thisLocale);
if (m != null)
{
ret = m.get(code);
}
logger.debug("tried locale {} result: {}",thisLocale.toString(),ret);
}
if (ret == null)
{
return null;
}
return new MessageFormat(ret, locale);
}
public Resource getResource()
{
return m_resource;
}
public void setResource(Resource resource)
{
m_resource = resource;
}
@SuppressWarnings("unchecked")
@Override
public void afterPropertiesSet() throws Exception
{
m_resource.getInputStream();
Document doc = null;
try {
InputStream in = getResource().getInputStream();
SAXBuilder sax = new SAXBuilder();
doc = sax.build(in);
}
catch (Exception e) {
throw new RuntimeException(e);
}
Locale defaultLocale = Locale.getDefault();
for (Element e: (List)doc.getRootElement().getChildren("locale"))
{
String l = e.getAttributeValue("name");
if (l == null)
{
l = defaultLocale.toString();
}
for (Locale locale: Locale.getAvailableLocales())
{
if (locale.toString().equals(l))
{
Map values = new HashMap();
m_map.put(locale, values);
for (Element e1: (List)e.getChildren("entry"))
{
values.put(e1.getAttributeValue("name"), e1.getAttributeValue("value"));
}
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy