Search in sources :

Example 6 with Encoder

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

the class StreamCharBuffer method writeToEncodedAppender.

private static boolean writeToEncodedAppender(StreamCharBuffer source, Writer target, EncodedAppender notAllowedAppender, boolean flush) throws IOException {
    if (target instanceof EncodedAppenderFactory) {
        EncodedAppenderFactory eaw = (EncodedAppenderFactory) target;
        EncodedAppender appender = eaw.getEncodedAppender();
        if (appender != null) {
            if (appender == notAllowedAppender) {
                throw new IllegalArgumentException("Cannot write buffer to itself.");
            }
            Encoder encoder = null;
            if (target instanceof EncoderAware) {
                encoder = ((EncoderAware) target).getEncoder();
            }
            if (encoder == null && appender instanceof EncoderAware) {
                encoder = ((EncoderAware) appender).getEncoder();
            }
            source.encodeTo(appender, encoder);
            if (flush) {
                appender.flush();
            }
            return true;
        }
    }
    return false;
}
Also used : WriterEncodedAppender(org.grails.encoder.WriterEncodedAppender) AbstractEncodedAppender(org.grails.encoder.AbstractEncodedAppender) EncodedAppender(org.grails.encoder.EncodedAppender) Encoder(org.grails.encoder.Encoder) StreamingEncoder(org.grails.encoder.StreamingEncoder) EncodedAppenderFactory(org.grails.encoder.EncodedAppenderFactory) EncoderAware(org.grails.encoder.EncoderAware)

Example 7 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 8 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)

Example 9 with Encoder

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

the class FilteringCodecsByContentTypeSettings method initialize.

@SuppressWarnings("rawtypes")
public void initialize(GrailsApplication grailsApplication) {
    contentTypeToEncoderMapping = null;
    contentTypePatternToEncoderMapping = null;
    Map codecForContentTypeConfig = getConfigSettings(grailsApplication.getConfig());
    if (codecForContentTypeConfig != null) {
        contentTypeToEncoderMapping = new LinkedHashMap<String, Encoder>();
        contentTypePatternToEncoderMapping = new LinkedHashMap<Pattern, Encoder>();
        Map codecForContentTypeMapping = (Map) codecForContentTypeConfig;
        for (Iterator i = codecForContentTypeMapping.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry entry = (Map.Entry) i.next();
            Encoder encoder = CodecLookupHelper.lookupEncoder(grailsApplication, String.valueOf(entry.getValue()));
            if (entry.getKey() instanceof Pattern) {
                contentTypePatternToEncoderMapping.put((Pattern) entry.getKey(), encoder);
            } else {
                contentTypeToEncoderMapping.put(String.valueOf(entry.getKey()), encoder);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Encoder(org.grails.encoder.Encoder) Iterator(java.util.Iterator) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with Encoder

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

the class MockGrailsApplication method getEncoder.

private Encoder getEncoder(GrailsApplication grailsApplication, Class<?> codecClass) {
    Encoder encoder = null;
    if (grailsApplication != null && codecClass != null) {
        GrailsCodecClass codecArtefact = (GrailsCodecClass) grailsApplication.getArtefact("Codec", codecClass.getName());
        encoder = codecArtefact.getEncoder();
    }
    return encoder;
}
Also used : Encoder(org.grails.encoder.Encoder) GrailsCodecClass(org.grails.commons.GrailsCodecClass) DefaultGrailsCodecClass(org.grails.commons.DefaultGrailsCodecClass)

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