All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.milyn.json.JSONReader Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
	Milyn - Copyright (C) 2008

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License (version 2.1) as published by the Free Software
	Foundation.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	See the GNU Lesser General Public License for more details:
	http://www.gnu.org/licenses/lgpl.txt
*/

package org.milyn.json;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.milyn.cdr.Parameter;
import org.milyn.cdr.SmooksResourceConfiguration;
import org.milyn.cdr.annotation.Config;
import org.milyn.cdr.annotation.ConfigParam;
import org.milyn.cdr.annotation.ConfigParam.Use;
import org.milyn.container.ExecutionContext;
import org.milyn.delivery.annotation.Initialize;
import org.milyn.xml.SmooksXMLReader;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.helpers.AttributesImpl;

import javax.xml.XMLConstants;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Stack;
import java.util.Map;

/**
 * JSON to SAX event reader.
 * 

* This JSON Reader can be plugged into Smooks in order to convert a * JSON based message stream into a stream of SAX events to be consumed by the other * Smooks resources. * *

Configuration

*
 * <resource-config selector="org.xml.sax.driver">
 *  <resource>org.milyn.json.JSONReader</resource>
 *  <!--
 *      (Optional) The element name of the SAX document root. Default of 'json'.
 *  -->
 *  <param name="rootName"><root-name></param>
 *  <!--
 *      (Optional) The element name of a array element. Default of 'element'.
 *  -->
 *  <param name="arrayElementName"><array-element-name></param>
 *  <!--
 *      (Optional) The replacement string for JSON NULL values. Default is an empty string.
 *  -->
 *  <param name="nullValueReplacement"><null-value-replacement></param>
 *  <!--
 *      (Optional) The replacement character for whitespaces in a json map key. By default this not defined, so that the reader doesn't search for whitespaces.
 *  -->
 *  <param name="keyWhitspaceReplacement"><key-whitspace-replacement></param>
 *  <!--
 *      (Optional) The prefix character to add if the JSON node name starts with a number. By default this is not defined, so that the reader doesn't search for element names that start with a number.
 *  -->
 *  <param name="keyPrefixOnNumeric"><key-prefix-on-numeric></param>
 *  <!--
 *      (Optional) If illegal characters are encountered in a JSON element name then they are replaced with this value. By default this is not defined, so that the reader doesn't doesn't search for illegal characters.
 *  -->
 *  <param name="illegalElementNameCharReplacement"><illegal-element-name-char-replacement></param>
 *  <!--
 *      (Optional) Defines a map of keys and there replacement. The from key will be replaced with the to key or the contents of the element.
 *  -->
 *  <param name="keyMap">
 *   <key from="fromKey" to="toKey" />
 *   <key from="fromKey"><to></key>
 *  </param>
 *  <!--
 *      (Optional) The encoding of the input stream. Default of 'UTF-8'
 *  -->
 *  <param name="encoding"><encoding></param>
 *
 * </resource-config>
 * 
* *

Example Usage

* So the following configuration could be used to parse a JSON stream into * a stream of SAX events: *
 * <resource-config selector="org.xml.sax.driver">
 *  <resource>org.milyn.json.JSONReader</resource>
 * </smooks-resource>
* * The "Acme-Order-List" input JSON message: *
 * [
 *  {
 *   "name" : "Maurice Zeijen",
 *   "address" : "Netherlands",
 *   "item" : "V1234",
 *   "quantity" : 3
 *  },
 *  {
 *   "name" : "Joe Bloggs",
 *   "address" : "England",
 *   "item" : "D9123",
 *   "quantity" : 7
 *  },
 * ]
* * Within Smooks, the stream of SAX events generated by the "Acme-Order-List" message (and this reader) will generate * a DOM equivalent to the following: *
 * <json>
 *  <element>
 *   <name>Maurice Zeijen</name>
 *   <address>Netherlands</address>
 *   <item>V1234</item>
 *   <quantity>3</quantity>
 *  <element>
 *  <element>
 *   <name>Joe Bloggs</name>
 *   <address>England</address>
 *   <item>D9123</item>
 *   <quantity>7</quantity>
 *  <element>
 * </json>
*

* * @author [email protected] */ public class JSONReader implements SmooksXMLReader { private static Log logger = LogFactory.getLog(JSONReader.class); public static final String CONFIG_PARAM_KEY_MAP = "keyMap"; public static final String XML_ROOT = "json"; public static final String XML_ARRAY_ELEMENT_NAME = "element"; public static final String DEFAULT_NULL_VALUE_REPLACEMENT = ""; private static final Attributes EMPTY_ATTRIBS = new AttributesImpl(); private static final JsonFactory jsonFactory = new JsonFactory(); private ContentHandler contentHandler; private ExecutionContext executionContext; @ConfigParam(defaultVal = XML_ROOT) private String rootName; @ConfigParam(defaultVal = XML_ARRAY_ELEMENT_NAME) private String arrayElementName; @ConfigParam(use = Use.OPTIONAL) private String keyWhitspaceReplacement; @ConfigParam(use = Use.OPTIONAL) private String keyPrefixOnNumeric; @ConfigParam(use = Use.OPTIONAL) private String illegalElementNameCharReplacement; @ConfigParam(defaultVal = DEFAULT_NULL_VALUE_REPLACEMENT) private String nullValueReplacement; @ConfigParam(defaultVal = "UTF-8") private Charset encoding; @ConfigParam(defaultVal = "false") private boolean indent; private boolean doKeyReplacement = false; private boolean doKeyWhitspaceReplacement = false; private boolean doPrefixOnNumericKey = false; private boolean doIllegalElementNameCharReplacement = false; private HashMap keyMap = new HashMap(); @Config private SmooksResourceConfiguration config; private static enum Type { OBJECT, ARRAY } @Initialize public void initialize() { initKeyMap(); doKeyReplacement = !keyMap.isEmpty(); doKeyWhitspaceReplacement = keyWhitspaceReplacement != null; doPrefixOnNumericKey = keyPrefixOnNumeric != null; doIllegalElementNameCharReplacement = illegalElementNameCharReplacement != null; } /* * (non-Javadoc) * @see org.milyn.xml.SmooksXMLReader#setExecutionContext(org.milyn.container.ExecutionContext) */ public void setExecutionContext(ExecutionContext request) { this.executionContext = request; } /* * (non-Javadoc) * @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource) */ public void parse(InputSource csvInputSource) throws IOException, SAXException { if(contentHandler == null) { throw new IllegalStateException("'contentHandler' not set. Cannot parse JSON stream."); } if(executionContext == null) { throw new IllegalStateException("Smooks container 'executionContext' not set. Cannot parse JSON stream."); } try { // Get a reader for the JSON source... Reader jsonStreamReader = csvInputSource.getCharacterStream(); if(jsonStreamReader == null) { jsonStreamReader = new InputStreamReader(csvInputSource.getByteStream(), encoding); } // Create the JSON parser... JsonParser jp = null; try { if(logger.isTraceEnabled()) { logger.trace("Creating JSON parser"); } jp = jsonFactory.createJsonParser(jsonStreamReader); // Start the document and add the root "csv-set" element... contentHandler.startDocument(); startElement(rootName, 0); if(logger.isTraceEnabled()) { logger.trace("Starting JSON parsing"); } boolean first = true; Stack elementStack = new Stack(); Stack typeStack = new Stack(); JsonToken t; while ((t = jp.nextToken()) != null) { if(logger.isTraceEnabled()) { logger.trace("Token: " + t.name()); } switch(t) { case START_OBJECT: case START_ARRAY: if(!first) { if(!typeStack.empty() && typeStack.peek() == Type.ARRAY) { startElement(arrayElementName, typeStack.size()); } } typeStack.push(t == JsonToken.START_ARRAY ? Type.ARRAY : Type.OBJECT); break; case END_OBJECT: case END_ARRAY: typeStack.pop(); boolean typeStackPeekIsArray = !typeStack.empty() && typeStack.peek() == Type.ARRAY; if(!elementStack.empty() && !typeStackPeekIsArray) { endElement(elementStack.pop(), typeStack.size()); } if(typeStackPeekIsArray) { endElement(arrayElementName, typeStack.size()); } break; case FIELD_NAME: String text = jp.getText(); if(logger.isTraceEnabled()) { logger.trace("Field name: " + text); } String name = getElementName(text); startElement(name, typeStack.size()); elementStack.add(name); break; default: String value; if(t == JsonToken.VALUE_NULL) { value = nullValueReplacement; } else { value = jp.getText(); } if(typeStack.peek() == Type.ARRAY) { startElement(arrayElementName, typeStack.size()); } contentHandler.characters(value.toCharArray(), 0, value.length()); if(typeStack.peek() == Type.ARRAY) { endElement(arrayElementName); } else { endElement(elementStack.pop()); } break; } first = false; } endElement(rootName, 0); contentHandler.endDocument(); } finally { try { jp.close(); } catch (Exception e) { } } } finally { // These properties need to be reset for every execution (e.g. when reader is pooled). contentHandler = null; executionContext = null; } } private static char[] INDENT = new String("\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t").toCharArray(); private void startElement(String name, int indent) throws SAXException { indent(indent); contentHandler.startElement(XMLConstants.NULL_NS_URI, name, "", EMPTY_ATTRIBS); } private void endElement(String name, int indent) throws SAXException { indent(indent); endElement(name); } private void endElement(String name) throws SAXException { contentHandler.endElement(XMLConstants.NULL_NS_URI, name, ""); } private void indent(int indentAmount) throws SAXException { if(indent) { if(indentAmount > 0) { contentHandler.characters(INDENT, 0, indentAmount + 1); } else { contentHandler.characters(INDENT, 0, 1); } } } /** * @param text * @return */ private String getElementName(String text) { boolean replacedKey = false; if(doKeyReplacement) { String mappedKey = keyMap.get(text); replacedKey = mappedKey != null; if(replacedKey) { text = mappedKey; } } if(!replacedKey) { if(doKeyWhitspaceReplacement) { text = text.replace(" ", keyWhitspaceReplacement); } if(doPrefixOnNumericKey && Character.isDigit(text.charAt(0))) { text = keyPrefixOnNumeric + text; } if(doIllegalElementNameCharReplacement) { text = text.replaceAll("^[.]|[^a-zA-Z0-9_.-]", illegalElementNameCharReplacement); } } return text; } /** * */ private void initKeyMap() { Parameter keyMapParam = config.getParameter(CONFIG_PARAM_KEY_MAP); if (keyMapParam != null) { Object objValue = keyMapParam.getObjValue(); if(objValue instanceof Map) { keyMap = (HashMap) objValue; } else { Element keyMapParamElement = keyMapParam.getXml(); if(keyMapParamElement != null) { setKeyMap(KeyMapDigester.digest(keyMapParamElement)); } else { logger.error("Sorry, the key properties must be available as XML DOM. Please configure using XML."); } } } } public void setContentHandler(ContentHandler contentHandler) { this.contentHandler = contentHandler; } public ContentHandler getContentHandler() { return contentHandler; } /** * @return the keyMap */ public HashMap getKeyMap() { return keyMap; } /** * @param keyMap the keyMap to set */ public void setKeyMap(HashMap keyMap) { this.keyMap = keyMap; } /** * @return the rootName */ public String getRootName() { return rootName; } /** * @param rootName the rootName to set */ public void setRootName(String rootName) { this.rootName = rootName; } /** * @return the arrayElementName */ public String getArrayElementName() { return arrayElementName; } /** * @param arrayElementName the arrayElementName to set */ public void setArrayElementName(String arrayElementName) { this.arrayElementName = arrayElementName; } /** * @return the keyWhitspaceReplacement */ public String getKeyWhitspaceReplacement() { return keyWhitspaceReplacement; } /** * @param keyWhitspaceReplacement the keyWhitspaceReplacement to set */ public void setKeyWhitspaceReplacement(String keyWhitspaceReplacement) { this.keyWhitspaceReplacement = keyWhitspaceReplacement; } /** * @return the keyPrefixOnNumeric */ public String getKeyPrefixOnNumeric() { return keyPrefixOnNumeric; } /** * @param keyPrefixOnNumeric the keyPrefixOnNumeric to set */ public void setKeyPrefixOnNumeric(String keyPrefixOnNumeric) { this.keyPrefixOnNumeric = keyPrefixOnNumeric; } /** * @return the illegalElementNameCharReplacement */ public String getIllegalElementNameCharReplacement() { return illegalElementNameCharReplacement; } /** * @param illegalElementNameCharReplacement the illegalElementNameCharReplacement to set */ public void setIllegalElementNameCharReplacement( String illegalElementNameCharReplacement) { this.illegalElementNameCharReplacement = illegalElementNameCharReplacement; } /** * @return the nullValueReplacement */ public String getNullValueReplacement() { return nullValueReplacement; } /** * @param nullValueReplacement the nullValueReplacement to set */ public void setNullValueReplacement(String nullValueReplacement) { this.nullValueReplacement = nullValueReplacement; } /** * @return the encoding */ public Charset getEncoding() { return encoding; } /** * @param encoding the encoding to set */ public void setEncoding(Charset encoding) { this.encoding = encoding; } public void setIndent(boolean indent) { this.indent = indent; } /**************************************************************************** * * The following methods are currently unimplemnted... * ****************************************************************************/ public void parse(String systemId) throws IOException, SAXException { throw new UnsupportedOperationException("Operation not supports by this reader."); } public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { return false; } public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { } public DTDHandler getDTDHandler() { return null; } public void setDTDHandler(DTDHandler arg0) { } public EntityResolver getEntityResolver() { return null; } public void setEntityResolver(EntityResolver arg0) { } public ErrorHandler getErrorHandler() { return null; } public void setErrorHandler(ErrorHandler arg0) { } public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { return null; } public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException { } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy