예제 #1
0
 /**
  * Write to the outputstream ONLY if the string is not blank, else we hold it back in case it is
  * to be swallowed between tags
  */
 @SuppressWarnings("unused")
 private void bufferedPrintlnToResponse(String s) {
   if (currentlyBufferingWhitespace) {
     whitespaceBuffer.append(s);
   } else {
     flushTagBuffering();
     out.printlnToResponse(s);
   }
 }
예제 #2
0
  private void script(boolean gsp) {
    flushTagBuffering();
    if (!finalPass) return;

    LOG.debug("parse: script");

    out.println();
    write(scan.getToken().trim(), gsp);
    out.println();
    out.println();
  }
예제 #3
0
  private void htmlPartPrintlnToResponse(int partNumber) {
    if (!tagMetaStack.isEmpty()) {
      TagMeta tm = tagMetaStack.peek();
      if (tm.bufferMode && tm.bufferPartNumber == -1) {
        tm.bufferPartNumber = partNumber;
        return;
      }
    }

    flushTagBuffering();

    htmlPartPrintlnRaw(partNumber);
  }
예제 #4
0
  @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;
      }
    }
  }