Search in sources :

Example 36 with MarshallException

use of org.directwebremoting.extend.MarshallException in project ma-core-public by infiniteautomation.

the class DOMConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     */
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = LocalUtil.decode(iv.getValue());
    try {
        if (buildFactory == null) {
            buildFactory = DocumentBuilderFactory.newInstance();
        }
        DocumentBuilder builder = buildFactory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(value));
        Document doc = builder.parse(is);
        if (paramType == Document.class) {
            return doc;
        } else if (paramType == Element.class) {
            return doc.getDocumentElement();
        }
        throw new MarshallException(paramType);
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) MarshallException(org.directwebremoting.extend.MarshallException) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) MarshallException(org.directwebremoting.extend.MarshallException)

Example 37 with MarshallException

use of org.directwebremoting.extend.MarshallException in project ma-core-public by infiniteautomation.

the class DOMConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    try {
        Transformer transformer = xslFact.newTransformer();
        // Using XSLT to convert to a stream. Setup the source
        Source source;
        if (data instanceof Node) {
            Node node = (Node) data;
            source = new DOMSource(node);
        } else {
            throw new MarshallException(data.getClass());
        }
        // Setup the destination
        StringWriter xml = new StringWriter();
        StreamResult result = new StreamResult(xml);
        transformer.transform(source, result);
        xml.flush();
        String script = EnginePrivate.xmlStringToJavascriptDom(xml.toString());
        OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false);
        outctx.put(data, ov);
        return ov;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(data.getClass(), ex);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) OutboundVariable(org.directwebremoting.extend.OutboundVariable) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Node(org.w3c.dom.Node) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) MarshallException(org.directwebremoting.extend.MarshallException)

Example 38 with MarshallException

use of org.directwebremoting.extend.MarshallException in project ma-core-public by infiniteautomation.

the class DateConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    long millis;
    if (data instanceof Calendar) {
        Calendar cal = (Calendar) data;
        millis = cal.getTime().getTime();
    } else if (data instanceof Date) {
        Date date = (Date) data;
        millis = date.getTime();
    } else {
        throw new MarshallException(data.getClass());
    }
    return new SimpleOutboundVariable("new Date(" + millis + ")", outctx, true);
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) Calendar(java.util.Calendar) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) Date(java.util.Date)

Example 39 with MarshallException

use of org.directwebremoting.extend.MarshallException in project ma-core-public by infiniteautomation.

the class EnumConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     */
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = LocalUtil.decode(iv.getValue());
    Object[] values;
    try {
        Method getter = paramType.getMethod("values", new Class[0]);
        values = (Object[]) getter.invoke(paramType, (Object[]) null);
    } catch (NoSuchMethodException ex) {
        // But this catch block has the same effect
        throw new MarshallException(paramType);
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
    for (int i = 0; i < values.length; i++) {
        Object en = values[i];
        if (value.equals(en.toString())) {
            return en;
        }
    }
    throw new MarshallException(paramType);
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) Method(java.lang.reflect.Method) MarshallException(org.directwebremoting.extend.MarshallException)

Example 40 with MarshallException

use of org.directwebremoting.extend.MarshallException in project ma-core-public by infiniteautomation.

the class JDOMConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    try {
        Format outformat = Format.getCompactFormat();
        outformat.setEncoding("UTF-8");
        // Setup the destination
        StringWriter xml = new StringWriter();
        XMLOutputter writer = new XMLOutputter(outformat);
        // Using XSLT to convert to a stream. Setup the source
        if (data instanceof Document) {
            Document doc = (Document) data;
            writer.output(doc, xml);
        } else if (data instanceof Element) {
            Element ele = (Element) data;
            writer.output(ele, xml);
        } else {
            throw new MarshallException(data.getClass());
        }
        xml.flush();
        String script = EnginePrivate.xmlStringToJavascriptDom(xml.toString());
        OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false);
        outctx.put(data, ov);
        return ov;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(data.getClass(), ex);
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) Format(org.jdom.output.Format) StringWriter(java.io.StringWriter) OutboundVariable(org.directwebremoting.extend.OutboundVariable) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Element(org.jdom.Element) Document(org.jdom.Document) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException)

Aggregations

MarshallException (org.directwebremoting.extend.MarshallException)43 Map (java.util.Map)16 OutboundVariable (org.directwebremoting.extend.OutboundVariable)12 Iterator (java.util.Iterator)10 HashMap (java.util.HashMap)9 TreeMap (java.util.TreeMap)9 Property (org.directwebremoting.extend.Property)9 SimpleOutboundVariable (org.directwebremoting.dwrp.SimpleOutboundVariable)8 InboundVariable (org.directwebremoting.extend.InboundVariable)8 TypeHintContext (org.directwebremoting.extend.TypeHintContext)7 ObjectOutboundVariable (org.directwebremoting.dwrp.ObjectOutboundVariable)5 IntrospectionException (java.beans.IntrospectionException)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 StringTokenizer (java.util.StringTokenizer)4 Converter (org.directwebremoting.extend.Converter)4 NamedConverter (org.directwebremoting.extend.NamedConverter)4 BeanInfo (java.beans.BeanInfo)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 StringWriter (java.io.StringWriter)3