Search in sources :

Example 1 with Encoder

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

the class BasicCodecLookup method registerCodecFactory.

public void registerCodecFactory(CodecFactory codecFactory) {
    Encoder encoder = codecFactory.getEncoder();
    if (encoder != null) {
        registerEncoder(encoder);
    }
    Decoder decoder = codecFactory.getDecoder();
    if (decoder != null) {
        registerDecoder(decoder);
    }
}
Also used : StreamingEncoder(org.grails.encoder.StreamingEncoder) Encoder(org.grails.encoder.Encoder) ChainedEncoder(org.grails.encoder.ChainedEncoder) Decoder(org.grails.encoder.Decoder) ChainedDecoder(org.grails.encoder.ChainedDecoder)

Example 2 with Encoder

use of org.grails.encoder.Encoder 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 3 with Encoder

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

the class StreamCharBuffer method createEncodingInitializer.

public LazyInitializingWriter createEncodingInitializer(final EncoderAware encoderLookup, final EncodingStateRegistryLookup encodingStateRegistryLookup, final LazyInitializingWriter... writers) {
    LazyInitializingWriter encodingWriterInitializer = new LazyInitializingMultipleWriter() {

        Writer lazyWriter;

        public Writer getWriter() throws IOException {
            return lazyWriter;
        }

        public LazyInitializingWriter[] initializeMultiple(StreamCharBuffer buffer, boolean autoFlushMode) throws IOException {
            Encoder encoder = encoderLookup.getEncoder();
            if (encoder != null) {
                EncodingStateRegistry encodingStateRegistry = encodingStateRegistryLookup.lookup();
                StreamCharBuffer encodeBuffer = new StreamCharBuffer(chunkSize, growProcent, maxChunkSize);
                encodeBuffer.setAllowSubBuffers(false);
                lazyWriter = encodeBuffer.getWriterForEncoder(encoder, encodingStateRegistry);
                for (LazyInitializingWriter w : writers) {
                    encodeBuffer.connectTo(w, autoFlushMode);
                }
                return new LazyInitializingWriter[] { this };
            } else {
                return writers;
            }
        }
    };
    return encodingWriterInitializer;
}
Also used : EncodingStateRegistry(org.grails.encoder.EncodingStateRegistry) Encoder(org.grails.encoder.Encoder) StreamingEncoder(org.grails.encoder.StreamingEncoder) Writer(java.io.Writer) EncodedAppenderWriter(org.grails.encoder.EncodedAppenderWriter) OutputStreamWriter(java.io.OutputStreamWriter) EncodesToWriter(org.grails.encoder.EncodesToWriter)

Example 4 with Encoder

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

the class TagOutput method captureTagOutput.

@SuppressWarnings("rawtypes")
public static final Object captureTagOutput(TagLibraryLookup gspTagLibraryLookup, String namespace, String tagName, Map attrs, Object body, OutputContext outputContext) {
    GroovyObject tagLib = lookupCachedTagLib(gspTagLibraryLookup, namespace, tagName);
    if (tagLib == null) {
        throw new GrailsTagException("Tag [" + tagName + "] does not exist. No corresponding tag library found.");
    }
    if (!(attrs instanceof GroovyPageAttributes)) {
        attrs = new GroovyPageAttributes(attrs, false);
    }
    ((GroovyPageAttributes) attrs).setGspTagSyntaxCall(false);
    Closure actualBody = createOutputCapturingClosure(tagLib, body, outputContext);
    final GroovyPageTagWriter tagOutput = new GroovyPageTagWriter();
    OutputEncodingStack outputStack = null;
    try {
        outputStack = OutputEncodingStack.currentStack(outputContext, false);
        if (outputStack == null) {
            outputStack = OutputEncodingStack.currentStack(outputContext, true, tagOutput, true, true);
        }
        Map<String, Object> defaultEncodeAs = gspTagLibraryLookup.getEncodeAsForTag(namespace, tagName);
        Map<String, Object> codecSettings = createCodecSettings(namespace, tagName, attrs, defaultEncodeAs);
        OutputEncodingStackAttributes.Builder builder = WithCodecHelper.createOutputStackAttributesBuilder(codecSettings, outputContext.getGrailsApplication());
        builder.topWriter(tagOutput);
        outputStack.push(builder.build());
        // retrieve tag lib and create wrapper writer
        Object tagLibProp = tagLib.getProperty(tagName);
        if (tagLibProp instanceof Closure) {
            Closure tag = (Closure) ((Closure) tagLibProp).clone();
            Object bodyResult;
            switch(tag.getParameterTypes().length) {
                case 1:
                    bodyResult = tag.call(new Object[] { attrs });
                    if (actualBody != null && actualBody != EMPTY_BODY_CLOSURE) {
                        Object bodyResult2 = actualBody.call();
                        if (bodyResult2 != null) {
                            if (actualBody instanceof ConstantClosure) {
                                outputStack.getStaticWriter().print(bodyResult2);
                            } else {
                                outputStack.getTaglibWriter().print(bodyResult2);
                            }
                        }
                    }
                    break;
                case 2:
                    bodyResult = tag.call(new Object[] { attrs, actualBody });
                    break;
                default:
                    throw new GrailsTagException("Tag [" + tagName + "] does not specify expected number of params in tag library [" + tagLib.getClass().getName() + "]");
            }
            Encoder taglibEncoder = outputStack.getTaglibEncoder();
            boolean returnsObject = gspTagLibraryLookup.doesTagReturnObject(namespace, tagName);
            if (returnsObject && bodyResult != null && !(bodyResult instanceof Writer)) {
                if (taglibEncoder != null) {
                    bodyResult = taglibEncoder.encode(bodyResult);
                }
                return bodyResult;
            }
            // add some method to always return string, configurable?
            if (taglibEncoder != null) {
                return taglibEncoder.encode(tagOutput.getBuffer());
            } else {
                return tagOutput.getBuffer();
            }
        }
        throw new GrailsTagException("Tag [" + tagName + "] does not exist in tag library [" + tagLib.getClass().getName() + "]");
    } finally {
        if (outputStack != null)
            outputStack.pop();
    }
}
Also used : Closure(groovy.lang.Closure) GroovyObject(groovy.lang.GroovyObject) Encoder(org.grails.encoder.Encoder) OutputEncodingStackAttributes(org.grails.taglib.encoder.OutputEncodingStackAttributes) GroovyObject(groovy.lang.GroovyObject) OutputEncodingStack(org.grails.taglib.encoder.OutputEncodingStack) Writer(java.io.Writer)

Example 5 with Encoder

use of org.grails.encoder.Encoder 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)

Aggregations

Encoder (org.grails.encoder.Encoder)10 StreamingEncoder (org.grails.encoder.StreamingEncoder)7 Writer (java.io.Writer)3 GroovyObject (groovy.lang.GroovyObject)2 OutputStreamWriter (java.io.OutputStreamWriter)2 ChainedDecoder (org.grails.encoder.ChainedDecoder)2 ChainedEncoder (org.grails.encoder.ChainedEncoder)2 EncodedAppenderWriter (org.grails.encoder.EncodedAppenderWriter)2 EncodesToWriter (org.grails.encoder.EncodesToWriter)2 Closure (groovy.lang.Closure)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 CodecPrintWriter (org.grails.buffer.CodecPrintWriter)1 DefaultGrailsCodecClass (org.grails.commons.DefaultGrailsCodecClass)1