use of org.grails.taglib.encoder.OutputEncodingStack in project grails-core by grails.
the class MockGrailsApplication method testCodecAndNoCodecGRAILS8405.
@Test
public void testCodecAndNoCodecGRAILS8405() throws IOException {
FastStringWriter target = new FastStringWriter();
GrailsWebRequest webRequest = bindMockHttpRequest();
// Initialize out and codecOut as it is done in GroovyPage.initRun
OutputEncodingStack outputStack = OutputEncodingStack.currentStack(true, target, false, true);
GrailsPrintWriter out = outputStack.getOutWriter();
webRequest.setOut(out);
GrailsPrintWriter codecOut = new CodecPrintWriter(out, getEncoder(new MockGrailsApplication(), CodecWithClosureProperties.class), registry);
// print some output
codecOut.print("hola");
codecOut.flush();
out.print("1");
out.print("2");
out.print("3");
// similar as taglib call
FastStringWriter bufferWriter = new FastStringWriter();
GrailsPrintWriter out2 = new GrailsPrintWriter(bufferWriter);
outputStack.push(out2);
out.print("4");
codecOut.print("A");
codecOut.flush();
outputStack.pop();
// add output before appending "taglib output"
out.print("added");
codecOut.print("too");
codecOut.flush();
// append "taglib output"
out.leftShift(bufferWriter.getBuffer());
// print some more output
codecOut.print("B");
codecOut.flush();
out.print("5");
codecOut.print("C");
codecOut.flush();
// clear thread local
RequestContextHolder.resetRequestAttributes();
assertEquals("-> hola <-123added-> too <-4-> A <--> B <-5-> C <-", target.getValue());
codecOut.close();
}
use of org.grails.taglib.encoder.OutputEncodingStack 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();
}
}
Aggregations