1/*2 * $Id: AbstractModelBody.java 1692328 2015-07-22 21:16:35Z mck $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.autotag.core.runtime;
2223import java.io.IOException;
24import java.io.StringWriter;
25import java.io.Writer;
26import java.util.regex.Pattern;
2728import org.apache.tiles.autotag.core.runtime.util.NullWriter;
2930/**31 * Base class for the abstraction of the body.32 *33 * @version $Rev: 1692328 $ $Date: 2015-07-22 23:16:35 +0200 (Wed, 22 Jul 2015) $34 */35publicabstractclassAbstractModelBodyimplementsModelBody {
3637// precompiled the pattern to avoid compiling on every method call38privatestaticfinal Pattern PATTERN = Pattern.compile("^\\s*|\\s*$");
3940/**41 * The default writer to use.42 */43private Writer defaultWriter;
4445/**46 * Constructor.47 *48 * @param defaultWriter The default writer to use.49 */50publicAbstractModelBody(Writer defaultWriter) {
51this.defaultWriter = defaultWriter;
52 }
5354 @Override
55publicvoid evaluate() throws IOException {
56 evaluate(defaultWriter);
57 }
5859 @Override
60public String evaluateAsString() throws IOException {
61 StringWriter writer = new StringWriter();
62try {
63 evaluate(writer);
64 } finally {
65 writer.close();
66 }
67 String body = writer.toString();
68if (body != null) {
69 body = PATTERN.matcher(body).replaceAll("");
70if (body.length() <= 0) {
71 body = null;
72 }
73 }
74return body;
75 }
7677 @Override
78publicvoid evaluateWithoutWriting() throws IOException {
79NullWriter writer = newNullWriter();
80try {
81 evaluate(writer);
82 } finally {
83 writer.close();
84 }
85 }
8687 }