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 */
017package org.apache.wicket.util.template;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.nio.charset.Charset;
022import java.time.Instant;
023import java.util.Collections;
024import java.util.Locale;
025import java.util.Map;
026import org.apache.wicket.util.lang.Args;
027import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
028
029
030/**
031 * Provides the ability to 'decorate' the actual template contents before it is contributed to the
032 * header. For example, to embed it inside a JavaScript tag pair.
033 * 
034 * @author Eelco Hillenius
035 * @since 1.2.6
036 */
037public abstract class TextTemplateDecorator extends TextTemplate
038{
039        private static final long serialVersionUID = 1L;
040
041        /**
042         * The decorated <code>TextTemplate</code>.
043         */
044        protected final TextTemplate decorated;
045
046        /**
047         * Constructor.
048         * 
049         * @param textTemplate
050         *            a <code>TextTemplate</code> to decorate
051         */
052        public TextTemplateDecorator(TextTemplate textTemplate)
053        {
054                Args.notNull(textTemplate, "textTemplate");
055
056                decorated = textTemplate;
057        }
058
059        /**
060         * Returns the decorated contents as a <code>String</code>.
061         * 
062         * @return the contents decorated with {@link #getBeforeTemplateContents()} and
063         *         {@link #getAfterTemplateContents()}
064         * @see org.apache.wicket.util.template.TextTemplate#asString()
065         */
066        @Override
067        public String asString()
068        {
069                return asString(Collections.<String, Object> emptyMap());
070        }
071
072        /**
073         * Returns the decorated contents as a <code>String</code>.
074         * 
075         * @return the contents decorated with {@link #getBeforeTemplateContents()} and
076         *         {@link #getAfterTemplateContents()}.
077         * @see org.apache.wicket.util.template.TextTemplate#asString(java.util.Map)
078         */
079        @Override
080        public String asString(Map<String, ?> variables)
081        {
082                StringBuilder b = new StringBuilder();
083                b.append(getBeforeTemplateContents());
084                b.append(decorated.asString(variables));
085                b.append(getAfterTemplateContents());
086                return b.toString();
087        }
088
089        /**
090         * Retrieves the <code>String</code> to put before the actual template contents, for example:
091         * 
092         * <pre>
093         *    &lt;script type=&quot;text/javascript&quot;&gt;
094         * </pre>
095         * 
096         * @return the <code>String</code> to put before the actual template contents
097         */
098        public abstract String getBeforeTemplateContents();
099
100        /**
101         * Retrieves the <code>String</code> to put after the actual template contents, for example:
102         * 
103         * <pre>
104         *    &lt;/script&gt;
105         * </pre>
106         * 
107         * @return the <code>String</code> to put after the actual template contents
108         */
109        public abstract String getAfterTemplateContents();
110
111        @Override
112        public void close() throws IOException
113        {
114                decorated.close();
115        }
116
117        @Override
118        public boolean equals(Object obj)
119        {
120                return decorated.equals(obj);
121        }
122
123        @Override
124        public String getContentType()
125        {
126                return decorated.getContentType();
127        }
128
129        @Override
130        public InputStream getInputStream() throws ResourceStreamNotFoundException
131        {
132                return decorated.getInputStream();
133        }
134
135        @Override
136        public Locale getLocale()
137        {
138                return decorated.getLocale();
139        }
140
141        @Override
142        public int hashCode()
143        {
144                return decorated.hashCode();
145        }
146
147        @Override
148        public Instant lastModifiedTime()
149        {
150                return decorated.lastModifiedTime();
151        }
152
153        @Override
154        public void setCharset(Charset charset)
155        {
156                decorated.setCharset(charset);
157        }
158
159        @Override
160        public void setLastModified(Instant lastModified)
161        {
162                decorated.setLastModified(lastModified);
163        }
164
165        @Override
166        public void setLocale(Locale locale)
167        {
168                decorated.setLocale(locale);
169        }
170
171        @Override
172        public String getString()
173        {
174                return decorated.getString();
175        }
176
177        @Override
178        public String toString()
179        {
180                return decorated.toString();
181        }
182}