예제 #1
0
 public void initRun(Writer target, GrailsWebRequest webRequest) {
   this.outputStack = GroovyPageOutputStack.currentStack(true, target, false, true);
   this.out = outputStack.getProxyWriter();
   this.webRequest = webRequest;
   final Map map = getBinding().getVariables();
   if (map.containsKey(APPLICATION_CONTEXT)) {
     final ApplicationContext applicationContext =
         (ApplicationContext) map.get(APPLICATION_CONTEXT);
     if (applicationContext != null
         && applicationContext.containsBean(GrailsPluginManager.BEAN_NAME)) {
       final GrailsPluginManager pluginManager =
           applicationContext.getBean(GrailsPluginManager.BEAN_NAME, GrailsPluginManager.class);
       this.pluginContextPath = pluginManager.getPluginPathForInstance(this);
     }
   }
   if (webRequest != null) {
     this.webRequest.setOut(this.out);
   }
   getBinding().setVariable(OUT, this.out);
 }
예제 #2
0
  public static Object captureTagOutput(
      TagLibraryLookup gspTagLibraryLookup,
      String namespace,
      String tagName,
      Map attrs,
      Object body,
      GrailsWebRequest webRequest) {
    if (!(attrs instanceof GroovyPageAttributes)) {
      attrs = new GroovyPageAttributes(attrs);
    }
    GroovyObject tagLib = lookupCachedTagLib(webRequest, gspTagLibraryLookup, namespace, tagName);

    boolean preferSubChunkWhenWritingToOtherBuffer = resolvePreferSubChunk(namespace, tagName);
    Closure actualBody =
        createOutputCapturingClosure(
            tagLib, body, webRequest, preferSubChunkWhenWritingToOtherBuffer);

    final GroovyPageTagWriter out = new GroovyPageTagWriter(preferSubChunkWhenWritingToOtherBuffer);
    try {
      GroovyPageOutputStack.currentStack().push(out);
      Object tagLibProp = tagLib.getProperty(tagName); // retrieve tag lib and create wrapper writer
      if (tagLibProp instanceof Closure) {
        Closure tag = (Closure) ((Closure) tagLibProp).clone();
        Object bodyResult = null;

        if (tag.getParameterTypes().length == 1) {
          bodyResult = tag.call(new Object[] {attrs});
          if (actualBody != null && actualBody != EMPTY_BODY_CLOSURE) {
            Object bodyResult2 = actualBody.call();
            if (bodyResult2 != null) {
              out.print(bodyResult2);
            }
          }
        } else if (tag.getParameterTypes().length == 2) {
          bodyResult = tag.call(new Object[] {attrs, actualBody});
        } else {
          throw new GrailsTagException(
              "Tag ["
                  + tagName
                  + "] does not specify expected number of params in tag library ["
                  + tagLib.getClass().getName()
                  + "]");
        }

        boolean returnsObject = gspTagLibraryLookup.doesTagReturnObject(namespace, tagName);

        if (returnsObject && bodyResult != null && !(bodyResult instanceof Writer)) {
          return bodyResult;
        }
        // add some method to always return string, configurable?
        return out.getBuffer();
      } else {
        throw new GrailsTagException(
            "Tag ["
                + tagName
                + "] does not exist in tag library ["
                + tagLib.getClass().getName()
                + "]");
      }
    } finally {
      GroovyPageOutputStack.currentStack().pop();
    }
  }
예제 #3
0
 public void cleanup() {
   outputStack.pop(true);
 }