001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jasper.compiler;
018:
019: import org.apache.jasper.JasperException;
020: import org.apache.jasper.Options;
021:
022: /**
023: */
024: public class TextOptimizer {
025:
026: /**
027: * A visitor to concatenate contiguous template texts.
028: */
029: static class TextCatVisitor extends Node.Visitor {
030:
031: private Options options;
032: private PageInfo pageInfo;
033: private int textNodeCount = 0;
034: private Node.TemplateText firstTextNode = null;
035: private StringBuffer textBuffer;
036: private final String emptyText = new String("");
037:
038: public TextCatVisitor(Compiler compiler) {
039: options = compiler.getCompilationContext().getOptions();
040: pageInfo = compiler.getPageInfo();
041: }
042:
043: public void doVisit(Node n) throws JasperException {
044: collectText();
045: }
046:
047: /*
048: * The following directis are ignored in text concatenation
049: */
050:
051: public void visit(Node.PageDirective n) throws JasperException {
052: }
053:
054: public void visit(Node.TagDirective n) throws JasperException {
055: }
056:
057: public void visit(Node.TaglibDirective n)
058: throws JasperException {
059: }
060:
061: public void visit(Node.AttributeDirective n)
062: throws JasperException {
063: }
064:
065: public void visit(Node.VariableDirective n)
066: throws JasperException {
067: }
068:
069: /*
070: * Don't concatenate text across body boundaries
071: */
072: public void visitBody(Node n) throws JasperException {
073: super .visitBody(n);
074: collectText();
075: }
076:
077: public void visit(Node.TemplateText n) throws JasperException {
078: if ((options.getTrimSpaces() || pageInfo
079: .isTrimDirectiveWhitespaces())
080: && n.isAllSpace()) {
081: n.setText(emptyText);
082: return;
083: }
084:
085: if (textNodeCount++ == 0) {
086: firstTextNode = n;
087: textBuffer = new StringBuffer(n.getText());
088: } else {
089: // Append text to text buffer
090: textBuffer.append(n.getText());
091: n.setText(emptyText);
092: }
093: }
094:
095: /**
096: * This method breaks concatenation mode. As a side effect it copies
097: * the concatenated string to the first text node
098: */
099: private void collectText() {
100:
101: if (textNodeCount > 1) {
102: // Copy the text in buffer into the first template text node.
103: firstTextNode.setText(textBuffer.toString());
104: }
105: textNodeCount = 0;
106: }
107:
108: }
109:
110: public static void concatenate(Compiler compiler, Node.Nodes page)
111: throws JasperException {
112:
113: TextCatVisitor v = new TextCatVisitor(compiler);
114: page.visit(v);
115:
116: // Cleanup, in case the page ends with a template text
117: v.collectText();
118: }
119: }
|