Search in sources :

Example 1 with FastStringWriter

use of org.grails.buffer.FastStringWriter in project grails-core by grails.

the class GroovyPageParser method startTag.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void startTag() {
    if (!finalPass)
        return;
    tagIndex++;
    String text;
    StringBuilder buf = new StringBuilder(scan.getToken());
    String ns = scan.getNamespace();
    boolean emptyTag = false;
    state = scan.nextToken();
    while (state != HTML && state != GEND_TAG && state != GEND_EMPTY_TAG && state != EOF) {
        if (state == GTAG_EXPR) {
            buf.append("${");
            buf.append(scan.getToken().trim());
            buf.append("}");
        } else {
            buf.append(scan.getToken());
        }
        state = scan.nextToken();
    }
    if (state == GEND_EMPTY_TAG) {
        emptyTag = true;
    }
    doNextScan = false;
    text = buf.toString();
    String tagName;
    Map attrs = new LinkedHashMap();
    Matcher m = Pattern.compile("\\s").matcher(text);
    if (m.find()) {
        // ignores carriage returns and new lines
        tagName = text.substring(0, m.start());
        if (state != EOF) {
            String attrTokens = text.substring(m.start(), text.length());
            populateMapWithAttributes(attrs, attrTokens);
        }
    } else {
        tagName = text;
    }
    if (state == EOF) {
        throw new GrailsTagException("Unexpected end of file encountered parsing Tag [" + tagName + "] for " + className + ". Are you missing a closing brace '}'?", pageName, getCurrentOutputLineNumber());
    }
    flushTagBuffering();
    TagMeta tm = new TagMeta();
    tm.name = tagName;
    tm.namespace = ns;
    tm.hasAttributes = !attrs.isEmpty();
    tm.lineNumber = getCurrentOutputLineNumber();
    tm.emptyTag = emptyTag;
    tm.tagIndex = tagIndex;
    tagMetaStack.push(tm);
    if (GroovyPage.DEFAULT_NAMESPACE.equals(ns) && tagRegistry.isSyntaxTag(tagName)) {
        if (tagContext == null) {
            tagContext = new HashMap<Object, Object>();
            tagContext.put(GroovyPage.OUT, out);
            tagContext.put(GroovyPageParser.class, this);
        }
        GroovySyntaxTag tag = (GroovySyntaxTag) tagRegistry.newTag(tagName);
        tag.init(tagContext);
        tag.setAttributes(attrs);
        if (tag.isKeepPrecedingWhiteSpace() && currentlyBufferingWhitespace) {
            flushBufferedWhiteSpace();
        } else if (!tag.isAllowPrecedingContent() && previousContentWasNonWhitespace) {
            throw new GrailsTagException("Tag [" + tag.getName() + "] cannot have non-whitespace characters directly preceding it.", pageName, getCurrentOutputLineNumber());
        } else {
            // If tag does not specify buffering of WS, we swallow it here
            clearBufferedWhiteSpace();
        }
        tag.doStartTag();
        tm.instance = tag;
    } else {
        // Custom taglibs have to always flush the whitespace, there's no
        // "allowPrecedingWhitespace" property on tags yet
        flushBufferedWhiteSpace();
        if (attrs.size() > 0) {
            FastStringWriter buffer = new FastStringWriter();
            buffer.print("[");
            for (Iterator<?> i = attrs.keySet().iterator(); i.hasNext(); ) {
                String name = (String) i.next();
                String cleanedName = name;
                if (name.startsWith("\"") && name.endsWith("\"")) {
                    cleanedName = "'" + name.substring(1, name.length() - 1) + "'";
                }
                buffer.print(cleanedName);
                buffer.print(':');
                buffer.print(getExpressionText(attrs.get(name).toString()));
                if (i.hasNext()) {
                    buffer.print(',');
                } else {
                    buffer.print("]");
                }
            }
            attrsVarsMapDefinition.put(tagIndex, buffer.toString());
            buffer.close();
        }
        if (!emptyTag) {
            tm.bufferMode = true;
        }
    }
}
Also used : GrailsTagException(org.grails.taglib.GrailsTagException) Matcher(java.util.regex.Matcher) GroovySyntaxTag(org.grails.gsp.compiler.tags.GroovySyntaxTag) FastStringWriter(org.grails.buffer.FastStringWriter) ConfigMap(grails.config.ConfigMap)

Example 2 with FastStringWriter

use of org.grails.buffer.FastStringWriter 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();
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) GrailsPrintWriter(org.grails.buffer.GrailsPrintWriter) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) OutputEncodingStack(org.grails.taglib.encoder.OutputEncodingStack) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) Test(org.junit.Test)

Example 3 with FastStringWriter

use of org.grails.buffer.FastStringWriter in project grails-core by grails.

the class MockGrailsApplication method testPrintString.

@Test
public void testPrintString() {
    FastStringWriter stringwriter = new FastStringWriter();
    CodecPrintWriter writer = new CodecPrintWriter(stringwriter, getEncoder(new MockGrailsApplication(), HTMLCodec.class), registry);
    writer.print("&&");
    writer.flush();
    assertEquals("&amp;&amp;", stringwriter.getValue());
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) HTMLCodec(org.grails.plugins.codecs.HTMLCodec) Test(org.junit.Test)

Example 4 with FastStringWriter

use of org.grails.buffer.FastStringWriter in project grails-core by grails.

the class XML method toString.

@Override
public String toString() {
    FastStringWriter strw = new FastStringWriter();
    render(strw);
    strw.flush();
    return strw.toString();
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter)

Example 5 with FastStringWriter

use of org.grails.buffer.FastStringWriter in project grails-core by grails.

the class GroovyPagesTemplateRenderer method generateScaffoldedTemplate.

private Template generateScaffoldedTemplate(GrailsWebRequest webRequest, String uri) throws IOException {
    Template t = null;
    Collection<String> controllerActions = scaffoldedActionMap.get(webRequest.getControllerName());
    if (controllerActions != null && controllerActions.contains(webRequest.getActionName())) {
        GrailsDomainClass domainClass = controllerToScaffoldedDomainClassMap.get(webRequest.getControllerName());
        if (domainClass != null) {
            int i = uri.lastIndexOf('/');
            String scaffoldedtemplateName = i > -1 ? uri.substring(i) : uri;
            if (scaffoldedtemplateName.toLowerCase().endsWith(".gsp")) {
                scaffoldedtemplateName = scaffoldedtemplateName.substring(0, scaffoldedtemplateName.length() - 4);
            }
            FastStringWriter sw = new FastStringWriter();
            ReflectionUtils.invokeMethod(generateViewMethod, scaffoldingTemplateGenerator, domainClass, scaffoldedtemplateName, sw);
            t = groovyPagesTemplateEngine.createTemplate(new ByteArrayResource(sw.toString().getBytes("UTF-8"), uri), false);
        }
    }
    return t;
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) GrailsDomainClass(grails.core.GrailsDomainClass) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Template(groovy.text.Template)

Aggregations

FastStringWriter (org.grails.buffer.FastStringWriter)12 CodecPrintWriter (org.grails.buffer.CodecPrintWriter)5 Test (org.junit.Test)5 StreamCharBuffer (org.grails.buffer.StreamCharBuffer)2 HTMLCodec (org.grails.plugins.codecs.HTMLCodec)2 ConfigMap (grails.config.ConfigMap)1 GrailsDomainClass (grails.core.GrailsDomainClass)1 Template (groovy.text.Template)1 Matcher (java.util.regex.Matcher)1 GrailsPrintWriter (org.grails.buffer.GrailsPrintWriter)1 GroovySyntaxTag (org.grails.gsp.compiler.tags.GroovySyntaxTag)1 GrailsTagException (org.grails.taglib.GrailsTagException)1 OutputEncodingStack (org.grails.taglib.encoder.OutputEncodingStack)1 DefaultRequestStateLookupStrategy (org.grails.web.servlet.mvc.DefaultRequestStateLookupStrategy)1 GrailsRequestStateLookupStrategy (org.grails.web.servlet.mvc.GrailsRequestStateLookupStrategy)1 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1