Search in sources :

Example 16 with OutboundVariable

use of org.directwebremoting.extend.OutboundVariable 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 17 with OutboundVariable

use of org.directwebremoting.extend.OutboundVariable 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)

Example 18 with OutboundVariable

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

the class MapConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    // First we just collect our converted children
    Map ovs = (Map) LocalUtil.classNewInstance("OrderedConvertOutbound", "java.util.LinkedHashMap", Map.class);
    if (ovs == null) {
        ovs = new HashMap();
    }
    ObjectOutboundVariable ov = new ObjectOutboundVariable(outctx);
    outctx.put(data, ov);
    // Loop through the map outputting any init code and collecting
    // converted outbound variables into the ovs map
    Map map = (Map) data;
    for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry entry = (Map.Entry) it.next();
        Object key = entry.getKey();
        Object value = entry.getValue();
        // It would be nice to check for Enums here
        if (!(key instanceof String) && !sentNonStringWarning) {
            log.warn("--Javascript does not support non string keys. Converting '" + key.getClass().getName() + "' using toString()");
            sentNonStringWarning = true;
        }
        String outkey = JavascriptUtil.escapeJavaScript(key.toString());
        // OutboundVariable ovkey = config.convertOutbound(key, outctx);
        // buffer.append(ovkey.getInitCode());
        // outkey = ovkey.getAssignCode();
        OutboundVariable nested = config.convertOutbound(value, outctx);
        ovs.put(outkey, nested);
    }
    ov.init(ovs, null);
    return ov;
}
Also used : ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) OutboundVariable(org.directwebremoting.extend.OutboundVariable) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) HashMap(java.util.HashMap) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with OutboundVariable

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

the class AbstractOutboundVariable method getChildDeclareCodes.

/**
 * Grab all the declare codes together
 * @return A declare string
 */
private String getChildDeclareCodes() {
    if (children == null) {
        return "";
    }
    StringBuffer buffer = new StringBuffer();
    // Make sure the nested things are declared
    for (Iterator it = children.iterator(); it.hasNext(); ) {
        OutboundVariable nested = (OutboundVariable) it.next();
        buffer.append(nested.getDeclareCode());
    }
    return buffer.toString();
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) Iterator(java.util.Iterator)

Example 20 with OutboundVariable

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

the class ObjectOutboundVariable method getNotInlineDefinition.

/* (non-Javadoc)
     * @see org.directwebremoting.dwrp.AbstractOutboundVariable#getNotInlineDefinition()
     */
protected NotInlineDefinition getNotInlineDefinition() {
    String declareCode;
    if (!isNamed) {
        declareCode = "var " + getVariableName() + "={};";
    } else {
        declareCode = "var " + getVariableName() + "=new " + scriptClassName + "();";
    }
    StringBuffer buildCode = new StringBuffer();
    for (Iterator it = ovs.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry entry = (Map.Entry) it.next();
        String name = (String) entry.getKey();
        OutboundVariable nested = (OutboundVariable) entry.getValue();
        String nestedAssignCode = nested.getAssignCode();
        String varName = getVariableName();
        // I dont think we need this check:  && !isRecursive()
        if (LocalUtil.isSimpleName(name)) {
            buildCode.append(varName);
            buildCode.append('.');
            buildCode.append(name);
            buildCode.append('=');
            buildCode.append(nestedAssignCode);
            buildCode.append(';');
        } else {
            buildCode.append(varName);
            buildCode.append("['");
            buildCode.append(name);
            buildCode.append("']=");
            buildCode.append(nestedAssignCode);
            buildCode.append(';');
        }
    }
    buildCode.append("\r\n");
    return new NotInlineDefinition(declareCode, buildCode.toString());
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) Iterator(java.util.Iterator) Map(java.util.Map)

Aggregations

OutboundVariable (org.directwebremoting.extend.OutboundVariable)21 MarshallException (org.directwebremoting.extend.MarshallException)12 Iterator (java.util.Iterator)10 Map (java.util.Map)7 ObjectOutboundVariable (org.directwebremoting.dwrp.ObjectOutboundVariable)6 SimpleOutboundVariable (org.directwebremoting.dwrp.SimpleOutboundVariable)6 TreeMap (java.util.TreeMap)5 Property (org.directwebremoting.extend.Property)5 StringWriter (java.io.StringWriter)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ArrayOutboundVariable (org.directwebremoting.dwrp.ArrayOutboundVariable)2 ErrorOutboundVariable (org.directwebremoting.dwrp.ErrorOutboundVariable)2 Converter (org.directwebremoting.extend.Converter)2 NamedConverter (org.directwebremoting.extend.NamedConverter)2 Collection (java.util.Collection)1 Entry (java.util.Map.Entry)1 Unit (javax.measure.unit.Unit)1 Source (javax.xml.transform.Source)1