Search in sources :

Example 1 with XMLConverter

use of lucee.runtime.converter.XMLConverter in project Lucee by lucee.

the class Props method _writeOut.

private static void _writeOut(PageContext pc, Props props, Object queryFormat, Object rtn, Charset cs, boolean setFormat) throws ConverterException, PageException, IOException {
    // return type XML ignore WDDX
    if (props.type == CFTypes.TYPE_XML) {
        // if(UDF.RETURN_FORMAT_WDDX==format) format=UDF.RETURN_FORMAT_PLAIN;
        rtn = Caster.toString(Caster.toXML(rtn));
    } else
        // function does no real cast, only check it
        rtn = Caster.castTo(pc, (short) props.type, props.strType, rtn);
    if (setFormat)
        setFormat(pc.getHttpServletResponse(), props.format, cs);
    // WDDX
    if (UDF.RETURN_FORMAT_WDDX == props.format) {
        WDDXConverter converter = new WDDXConverter(pc.getTimeZone(), false, false);
        converter.setTimeZone(pc.getTimeZone());
        pc.forceWrite(converter.serialize(rtn));
    } else // JSON
    if (UDF.RETURN_FORMAT_JSON == props.format) {
        boolean byColumn = false;
        if (queryFormat instanceof String) {
            String strQF = ((String) queryFormat).trim();
            if (strQF.equalsIgnoreCase("row"))
                ;
            else if (strQF.equalsIgnoreCase("column"))
                byColumn = true;
            else
                throw new ApplicationException("invalid queryformat definition [" + strQF + "], valid formats are [row,column]");
        }
        JSONConverter converter = new JSONConverter(false, cs);
        String prefix = "";
        if (props.secureJson) {
            prefix = pc.getApplicationContext().getSecureJsonPrefix();
            if (prefix == null)
                prefix = "";
        }
        pc.forceWrite(prefix + converter.serialize(pc, rtn, byColumn));
    } else // CFML
    if (UDF.RETURN_FORMAT_SERIALIZE == props.format) {
        ScriptConverter converter = new ScriptConverter(false);
        pc.forceWrite(converter.serialize(rtn));
    } else // XML
    if (UDF.RETURN_FORMAT_XML == props.format) {
        XMLConverter converter = new XMLConverter(pc.getTimeZone(), false);
        converter.setTimeZone(pc.getTimeZone());
        pc.forceWrite(converter.serialize(rtn));
    } else // Plain
    if (UDF.RETURN_FORMAT_PLAIN == props.format) {
        pc.forceWrite(Caster.toString(rtn));
    } else // JAVA
    if (UDF.RETURN_FORMAT_JAVA == props.format) {
        writeOut(pc, rtn, MimeType.APPLICATION_JAVA, new JavaConverter());
    } else
        throw new IOException("invalid return format defintion:" + props.format);
}
Also used : WDDXConverter(lucee.runtime.converter.WDDXConverter) ApplicationException(lucee.runtime.exp.ApplicationException) ScriptConverter(lucee.runtime.converter.ScriptConverter) XMLConverter(lucee.runtime.converter.XMLConverter) JavaConverter(lucee.runtime.converter.JavaConverter) IOException(java.io.IOException) JSONConverter(lucee.runtime.converter.JSONConverter)

Example 2 with XMLConverter

use of lucee.runtime.converter.XMLConverter in project Lucee by lucee.

the class Props method callCFCMetaData.

private void callCFCMetaData(PageContext pc, Component cfc, int format) throws IOException, PageException, ConverterException {
    ComponentSpecificAccess cw = new ComponentSpecificAccess(Component.ACCESS_REMOTE, cfc);
    ComponentScope scope = cw.getComponentScope();
    Struct udfs = new StructImpl(), sctUDF, sctArg;
    Array arrArg;
    Iterator<Object> it = scope.valueIterator();
    Object v;
    UDF udf;
    FunctionArgument[] args;
    while (it.hasNext()) {
        v = it.next();
        // UDF
        if (v instanceof UDF) {
            udf = (UDF) v;
            sctUDF = new StructImpl();
            arrArg = new ArrayImpl();
            udfs.setEL(udf.getFunctionName(), sctUDF);
            args = udf.getFunctionArguments();
            for (int i = 0; i < args.length; i++) {
                sctArg = new StructImpl();
                arrArg.appendEL(sctArg);
                sctArg.setEL(KeyConstants._name, args[i].getName().getString());
                sctArg.setEL(KeyConstants._type, args[i].getTypeAsString());
                sctArg.setEL(KeyConstants._required, args[i].isRequired());
                if (!StringUtil.isEmpty(args[i].getHint()))
                    sctArg.setEL(KeyConstants._hint, args[i].getHint());
            }
            sctUDF.set(KeyConstants._arguments, arrArg);
            sctUDF.set(KeyConstants._returntype, udf.getReturnTypeAsString());
        }
    }
    Struct rtn = new StructImpl();
    rtn.set(KeyConstants._functions, udfs);
    rtn.set(ACCEPT_ARG_COLL_FORMATS, "cfml,json");
    InputStream is;
    Charset cs = null;
    // WDDX
    if (UDF.RETURN_FORMAT_WDDX == format) {
        WDDXConverter converter = new WDDXConverter(pc.getTimeZone(), false, false);
        converter.setTimeZone(pc.getTimeZone());
        String str = converter.serialize(rtn);
        cs = getCharset(pc);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // JSON
    if (UDF.RETURN_FORMAT_JSON == format) {
        boolean byColumn = false;
        cs = getCharset(pc);
        JSONConverter converter = new JSONConverter(false, cs);
        String str = converter.serialize(pc, rtn, byColumn);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // CFML
    if (UDF.RETURN_FORMAT_SERIALIZE == format) {
        ScriptConverter converter = new ScriptConverter(false);
        String str = converter.serialize(rtn);
        cs = getCharset(pc);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // XML
    if (UDF.RETURN_FORMAT_XML == format) {
        XMLConverter converter = new XMLConverter(pc.getTimeZone(), false);
        converter.setTimeZone(pc.getTimeZone());
        String str = converter.serialize(rtn);
        cs = getCharset(pc);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // Plain
    if (UDF.RETURN_FORMAT_PLAIN == format) {
        String str = Caster.toString(rtn);
        cs = getCharset(pc);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // Java
    if (UDF.RETURN_FORMAT_JAVA == format) {
        byte[] bytes = JavaConverter.serializeAsBinary(rtn);
        is = new ByteArrayInputStream(bytes);
    } else
        throw new IOException("invalid format defintion:" + format);
    OutputStream os = null;
    try {
        os = pc.getResponseStream();
        setFormat(pc.getHttpServletResponse(), format, cs);
        IOUtil.copy(is, os, false, false);
    } finally {
        IOUtil.flushEL(os);
        IOUtil.closeEL(os);
        ((PageContextImpl) pc).getRootOut().setClosed(true);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayImpl(lucee.runtime.type.ArrayImpl) OutputStream(java.io.OutputStream) XMLConverter(lucee.runtime.converter.XMLConverter) Charset(java.nio.charset.Charset) IOException(java.io.IOException) StaticStruct(lucee.runtime.component.StaticStruct) Struct(lucee.runtime.type.Struct) Array(lucee.runtime.type.Array) WDDXConverter(lucee.runtime.converter.WDDXConverter) StructImpl(lucee.runtime.type.StructImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) UDF(lucee.runtime.type.UDF) ScriptConverter(lucee.runtime.converter.ScriptConverter) FunctionArgument(lucee.runtime.type.FunctionArgument) JSONConverter(lucee.runtime.converter.JSONConverter)

Aggregations

IOException (java.io.IOException)2 JSONConverter (lucee.runtime.converter.JSONConverter)2 ScriptConverter (lucee.runtime.converter.ScriptConverter)2 WDDXConverter (lucee.runtime.converter.WDDXConverter)2 XMLConverter (lucee.runtime.converter.XMLConverter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Charset (java.nio.charset.Charset)1 StaticStruct (lucee.runtime.component.StaticStruct)1 JavaConverter (lucee.runtime.converter.JavaConverter)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 Array (lucee.runtime.type.Array)1 ArrayImpl (lucee.runtime.type.ArrayImpl)1 FunctionArgument (lucee.runtime.type.FunctionArgument)1 Struct (lucee.runtime.type.Struct)1 StructImpl (lucee.runtime.type.StructImpl)1 UDF (lucee.runtime.type.UDF)1