Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractModelBody |
|
| 1.5;1.5 |
1 | /* | |
2 | * $Id: AbstractModelBody.java 1692328 2015-07-22 21:16:35Z mck $ | |
3 | * | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | package org.apache.tiles.autotag.core.runtime; | |
22 | ||
23 | import java.io.IOException; | |
24 | import java.io.StringWriter; | |
25 | import java.io.Writer; | |
26 | import java.util.regex.Pattern; | |
27 | ||
28 | import org.apache.tiles.autotag.core.runtime.util.NullWriter; | |
29 | ||
30 | /** | |
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 | */ | |
35 | public abstract class AbstractModelBody implements ModelBody { | |
36 | ||
37 | // precompiled the pattern to avoid compiling on every method call | |
38 | 1 | private static final Pattern PATTERN = Pattern.compile("^\\s*|\\s*$"); |
39 | ||
40 | /** | |
41 | * The default writer to use. | |
42 | */ | |
43 | private Writer defaultWriter; | |
44 | ||
45 | /** | |
46 | * Constructor. | |
47 | * | |
48 | * @param defaultWriter The default writer to use. | |
49 | */ | |
50 | 6 | public AbstractModelBody(Writer defaultWriter) { |
51 | 6 | this.defaultWriter = defaultWriter; |
52 | 6 | } |
53 | ||
54 | @Override | |
55 | public void evaluate() throws IOException { | |
56 | 1 | evaluate(defaultWriter); |
57 | 1 | } |
58 | ||
59 | @Override | |
60 | public String evaluateAsString() throws IOException { | |
61 | 3 | StringWriter writer = new StringWriter(); |
62 | try { | |
63 | 3 | evaluate(writer); |
64 | } finally { | |
65 | 3 | writer.close(); |
66 | 2 | } |
67 | 2 | String body = writer.toString(); |
68 | 2 | if (body != null) { |
69 | 2 | body = PATTERN.matcher(body).replaceAll(""); |
70 | 2 | if (body.length() <= 0) { |
71 | 1 | body = null; |
72 | } | |
73 | } | |
74 | 2 | return body; |
75 | } | |
76 | ||
77 | @Override | |
78 | public void evaluateWithoutWriting() throws IOException { | |
79 | 2 | NullWriter writer = new NullWriter(); |
80 | try { | |
81 | 2 | evaluate(writer); |
82 | } finally { | |
83 | 2 | writer.close(); |
84 | 1 | } |
85 | 1 | } |
86 | ||
87 | } |