Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StringTemplate |
|
| 1.3333333333333333;1.333 |
1 | /* | |
2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted under the terms of either of the following | |
6 | * Open Source licenses: | |
7 | * | |
8 | * The GNU General Public License, version 2, or any later version, as | |
9 | * published by the Free Software Foundation | |
10 | * (http://www.fsf.org/copyleft/gpl.html); | |
11 | * | |
12 | * or | |
13 | * | |
14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
15 | * | |
16 | * This software is provided "as is", with NO WARRANTY, not even the | |
17 | * implied warranties of fitness to purpose, or merchantability. You | |
18 | * assume all risks and liabilities associated with its use. | |
19 | * | |
20 | * See www.webmacro.org for more information on the WebMacro project. | |
21 | */ | |
22 | ||
23 | ||
24 | package org.webmacro.engine; | |
25 | ||
26 | import org.webmacro.Broker; | |
27 | ||
28 | import java.io.IOException; | |
29 | import java.io.Reader; | |
30 | import java.io.StringReader; | |
31 | ||
32 | /** | |
33 | * StringTemplate objects read their template data from a string. | |
34 | * They do not need an encoding since strings are already converted | |
35 | * to utf by java. | |
36 | * @author Brian Goetz | |
37 | */ | |
38 | ||
39 | public class StringTemplate extends WMTemplate | |
40 | { | |
41 | ||
42 | /** The text associated with this template */ | |
43 | private final String templateText; | |
44 | ||
45 | /** (Optional) name of this template */ | |
46 | private String templateName; | |
47 | ||
48 | /** | |
49 | * Instantiate a template. Encoding information | |
50 | * is not needed, as strings are already converted | |
51 | * to utf in java. | |
52 | */ | |
53 | public StringTemplate (Broker broker, String templateText) | |
54 | { | |
55 | 0 | this(broker, templateText, "unknown"); |
56 | 0 | } |
57 | ||
58 | /** | |
59 | * Instantiate a template. Encoding information | |
60 | * is not needed, as strings are already converted | |
61 | * to utf in java. | |
62 | * @param name name of string template to display in | |
63 | * error messages and logs | |
64 | */ | |
65 | public StringTemplate (Broker broker, String templateText, String name) | |
66 | { | |
67 | 0 | super(broker); |
68 | 0 | this.templateText = templateText; |
69 | 0 | this.templateName = name; |
70 | 0 | } |
71 | ||
72 | public void setName (String name) | |
73 | { | |
74 | 0 | templateName = name; |
75 | 0 | _content.setTemplateName(name); |
76 | 0 | } |
77 | ||
78 | public String getName () | |
79 | { | |
80 | 0 | return templateName; |
81 | } | |
82 | ||
83 | /** | |
84 | * Get the stream the template should be read from. Parse will | |
85 | * call this method in order to locate a stream. | |
86 | */ | |
87 | protected Reader getReader () throws IOException | |
88 | { | |
89 | 0 | return new StringReader(templateText); |
90 | } | |
91 | ||
92 | /** | |
93 | * Return a name for this template. For example, if the template reads | |
94 | * from a file you might want to mention which it is--will be used to | |
95 | * produce error messages describing which template had a problem. | |
96 | */ | |
97 | public String toString () | |
98 | { | |
99 | 0 | StringBuffer b = new StringBuffer(); |
100 | 0 | b.append("StringTemplate(\""); |
101 | 0 | b.append(templateName); |
102 | 0 | if (templateText != null) |
103 | { | |
104 | 0 | b.append("\";\""); |
105 | // be sure to only show first 100 characters, | |
106 | // otherwise it can get somewhat messy... | |
107 | 0 | if (templateText.length() <= 100) |
108 | { | |
109 | 0 | b.append(templateText); |
110 | } | |
111 | else | |
112 | { | |
113 | 0 | b.append(templateText.substring(0, 100)); |
114 | 0 | b.append("..."); |
115 | } | |
116 | } | |
117 | 0 | b.append("\")"); |
118 | 0 | return b.toString(); |
119 | } | |
120 | ||
121 | } |