Package com.espertech.esper.event.xml

Source Code of com.espertech.esper.event.xml.DOMConvertingArrayGetter

package com.espertech.esper.event.xml;

import com.espertech.esper.client.EventPropertyGetter;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.PropertyAccessException;
import com.espertech.esper.util.SimpleTypeParser;
import com.espertech.esper.util.SimpleTypeParserFactory;
import org.w3c.dom.Node;

import java.lang.reflect.Array;

/**
* Getter for converting a Node child nodes into an array.
*/
public class DOMConvertingArrayGetter implements EventPropertyGetter
{
    private final DOMPropertyGetter getter;
    private final Class componentType;
    private final SimpleTypeParser parser;

    /**
     * Ctor.
     * @param domPropertyGetter getter
     * @param returnType component type
     */
    public DOMConvertingArrayGetter(DOMPropertyGetter domPropertyGetter, Class returnType)
    {
        this.getter = domPropertyGetter;
        this.componentType = returnType;
        this.parser = SimpleTypeParserFactory.getParser(returnType);
    }

    public Object get(EventBean obj) throws PropertyAccessException
    {
        // The underlying is expected to be a map
        if (!(obj.getUnderlying() instanceof Node))
        {
            throw new PropertyAccessException("Mismatched property getter to event bean type, " +
                    "the underlying data object is not of type Node");
        }

        Node node = (Node) obj.getUnderlying();

        Node[] result = getter.getValueAsNodeArray(node);
        if (result == null)
        {
            return null;
        }

        Object array = Array.newInstance(componentType, result.length);
        for (int i = 0; i < result.length; i++)
        {
            String text = result[i].getTextContent();
            if ((text == null) || (text.length() == 0))
            {
                continue;
            }

            Object parseResult = parser.parse(text);
            Array.set(array, i, parseResult);
        }

        return array;
    }

    public boolean isExistsProperty(EventBean eventBean)
    {
        return true;
    }

    public Object getFragment(EventBean eventBean) throws PropertyAccessException
    {
        return null;
    }
}
TOP

Related Classes of com.espertech.esper.event.xml.DOMConvertingArrayGetter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.