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