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;
}
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();
}
}
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;
}
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);
}
}
}
}
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;
}
Aggregations