Orbeon forms processsor for extracting locale from request

A simple processor for Orbeon forms that extracts locale information from request.

package fi.iki.juhap.xpl;

import java.util.Locale;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.orbeon.oxf.pipeline.api.ExternalContext;
import org.orbeon.oxf.pipeline.api.PipelineContext;
import org.orbeon.oxf.processor.ProcessorInputOutputInfo;
import org.orbeon.oxf.processor.SimpleProcessor;
import org.orbeon.oxf.xml.TransformerUtils;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

/**
 * Extract locale information from request.
 * 
 * Example output: 
 * 
 * <locale>
 * 		<language>en</language>
 * 		<country>US</country>
 * 		<variant />
 * </locale>
 *
 */
public class RequestLocaleProcessor extends SimpleProcessor {
	public RequestLocaleProcessor() {
		addOutputInfo(new ProcessorInputOutputInfo(OUTPUT_DATA));
	}
	
	public void  generateData(PipelineContext context, ContentHandler contentHandler) throws SAXException {
		ExternalContext externalContext  = (ExternalContext) 
				context.getAttribute(PipelineContext.EXTERNAL_CONTEXT);
		
		Locale locale = externalContext.getRequest().getLocale();

		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("locale");
		root.addElement("language").addText(locale.getLanguage());
		root.addElement("country").addText(locale.getCountry());
		root.addElement("variant").addText(locale.getVariant());
		
		TransformerUtils.writeDom4j(document, contentHandler);		
	}
}

In order to use this you need to add it to processors-local.xml file (WEB-INF/resources/config) like this:

<processors xmlns:jp="http://fi.iki.juhap/processors">
    <processor name="jp:request-locale">
        <class name="fi.iki.juhap.xpl.RequestLocaleProcessor" />
    </processor>
</processors>

Usage very simple. In XPL you need to declare the namespace and then use the custom processor as any other.

<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline"
          xmlns:jp="http://fi.iki.juhap/processors">
..
	<p:processor name="jp:request-locale">
		<p:output name="data" id="request-locale" />
	</p:processor>
..
</p:config>