Search in sources :

Example 1 with StreamingEncoder

use of org.grails.encoder.StreamingEncoder in project grails-core by grails.

the class BasicCodecLookup method createChainedCodecInstance.

protected <T extends CodecIdentifierProvider> T createChainedCodecInstance(String codecName, ConcurrentMap<String, T> map, Class<T> returnType) {
    String[] codecs = codecName.split(",");
    List<T> codecInstances = new ArrayList<T>(codecs.length);
    for (int i = 0; i < codecs.length; i++) {
        T codecInstance = map.get(codecs[i]);
        if (codecInstance != null) {
            codecInstances.add(codecInstance);
        }
    }
    if (returnType == Encoder.class) {
        List<StreamingEncoder> streamingEncoders = ChainedEncoders.toStreamingEncoders((List<Encoder>) codecInstances);
        if (streamingEncoders == null) {
            throw new RuntimeException("ChainedEncoder only supports StreamingEncoder instances. Couldn't build chained encoder for '" + codecName + "'");
        } else {
            return (T) ChainedEncoder.createFor(streamingEncoders);
        }
    } else {
        Collections.reverse(codecInstances);
        return (T) new ChainedDecoder(codecInstances.toArray(new Decoder[codecInstances.size()]));
    }
}
Also used : StreamingEncoder(org.grails.encoder.StreamingEncoder) Encoder(org.grails.encoder.Encoder) ChainedEncoder(org.grails.encoder.ChainedEncoder) ArrayList(java.util.ArrayList) ChainedDecoder(org.grails.encoder.ChainedDecoder) StreamingEncoder(org.grails.encoder.StreamingEncoder)

Example 2 with StreamingEncoder

use of org.grails.encoder.StreamingEncoder in project grails-core by grails.

the class GroovyPagesTemplateRenderer method wrapWriterWithEncoder.

private Writer wrapWriterWithEncoder(GrailsWebRequest webRequest, Map<String, Object> attrs, Writer out) {
    Object encodeAs = attrs.get(GroovyPage.ENCODE_AS_ATTRIBUTE_NAME);
    if (encodeAs != null) {
        Map<String, Object> codecSettings = WithCodecHelper.makeSettingsCanonical(encodeAs);
        String codecForTaglibs = (String) codecSettings.get(OutputEncodingSettings.TAGLIB_CODEC_NAME);
        if (codecForTaglibs != null) {
            Encoder encoder = WithCodecHelper.lookupEncoder(webRequest.getAttributes().getGrailsApplication(), codecForTaglibs);
            if (out instanceof EncodedAppenderWriterFactory) {
                out = ((EncodedAppenderWriterFactory) out).getWriterForEncoder(encoder, webRequest.getEncodingStateRegistry());
            } else if (encoder instanceof StreamingEncoder) {
                out = new StreamingEncoderWriter(out, (StreamingEncoder) encoder, webRequest.getEncodingStateRegistry());
            } else {
                out = new CodecPrintWriter(out, encoder, webRequest.getEncodingStateRegistry());
            }
        }
    }
    return out;
}
Also used : Encoder(org.grails.encoder.Encoder) StreamingEncoder(org.grails.encoder.StreamingEncoder) StreamingEncoderWriter(org.grails.encoder.StreamingEncoderWriter) GroovyObject(groovy.lang.GroovyObject) StreamingEncoder(org.grails.encoder.StreamingEncoder) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) EncodedAppenderWriterFactory(org.grails.encoder.EncodedAppenderWriterFactory)

Example 3 with StreamingEncoder

use of org.grails.encoder.StreamingEncoder in project grails-core by grails.

the class DefaultGrailsCodecClass method initializeCodec.

private void initializeCodec() {
    if (initialized)
        return;
    initialized = true;
    Integer orderSetting = getStaticPropertyValue("order", Integer.class);
    if (orderSetting != null) {
        order = orderSetting;
    }
    Object instance = getReferenceInstance();
    if (Encoder.class.isAssignableFrom(getClazz())) {
        encoder = (Encoder) instance;
        encoder = (Encoder) autowireCodecBean(encoder);
        if (encoder instanceof Ordered) {
            order = ((Ordered) encoder).getOrder();
        }
    }
    if (Decoder.class.isAssignableFrom(getClazz())) {
        decoder = (Decoder) instance;
        decoder = (Decoder) autowireCodecBean(decoder);
        if (decoder instanceof Ordered) {
            order = ((Ordered) decoder).getOrder();
        }
    }
    if (encoder == null && decoder == null) {
        CodecFactory codecFactory = null;
        if (CodecFactory.class.isAssignableFrom(getClazz())) {
            codecFactory = (CodecFactory) instance;
            codecFactory = (CodecFactory) autowireCodecBean(codecFactory);
        }
        if (codecFactory == null) {
            codecFactory = getStaticPropertyValue("codecFactory", CodecFactory.class);
            codecFactory = (CodecFactory) autowireCodecBean(codecFactory);
        }
        if (codecFactory == null) {
            codecFactory = new ClosureCodecFactory(instance);
        }
        encoder = codecFactory.getEncoder();
        decoder = codecFactory.getDecoder();
        if (codecFactory instanceof Ordered) {
            order = ((Ordered) codecFactory).getOrder();
        }
    }
    if (encoder != null) {
        if (encoder instanceof StreamingEncoder) {
            encoder = new StreamingStateAwareEncoderWrapper((StreamingEncoder) encoder);
        } else {
            encoder = new StateAwareEncoderWrapper(encoder);
        }
    }
}
Also used : Ordered(org.springframework.core.Ordered) CodecFactory(org.grails.encoder.CodecFactory) StreamingEncoder(org.grails.encoder.StreamingEncoder)

Aggregations

StreamingEncoder (org.grails.encoder.StreamingEncoder)3 Encoder (org.grails.encoder.Encoder)2 GroovyObject (groovy.lang.GroovyObject)1 ArrayList (java.util.ArrayList)1 CodecPrintWriter (org.grails.buffer.CodecPrintWriter)1 ChainedDecoder (org.grails.encoder.ChainedDecoder)1 ChainedEncoder (org.grails.encoder.ChainedEncoder)1 CodecFactory (org.grails.encoder.CodecFactory)1 EncodedAppenderWriterFactory (org.grails.encoder.EncodedAppenderWriterFactory)1 StreamingEncoderWriter (org.grails.encoder.StreamingEncoderWriter)1 Ordered (org.springframework.core.Ordered)1