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.settings;
018
019import org.apache.wicket.DefaultMarkupIdGenerator;
020import org.apache.wicket.IMarkupIdGenerator;
021import org.apache.wicket.markup.MarkupFactory;
022import org.apache.wicket.util.lang.Args;
023
024/**
025 * Class for markup related settings.
026 * <p>
027 * <i>compressWhitespace </i> (defaults to false) - Causes pages to render with redundant whitespace
028 * removed. Whitespace stripping is not HTML or JavaScript savvy and can conceivably break pages,
029 * but should provide significant performance improvements.
030 * <p>
031 * <i>stripComments</i> (defaults to false) - Set to true to strip HTML comments during markup
032 * loading
033 *
034 * @author Jonathan Locke
035 * @author Chris Turner
036 * @author Eelco Hillenius
037 * @author Juergen Donnerstag
038 * @author Johan Compagner
039 * @author Igor Vaynberg (ivaynberg)
040 * @author Martijn Dashorst
041 * @author James Carman
042 */
043public class MarkupSettings
044{
045        /** Application default for automatically resolving hrefs */
046        private boolean automaticLinking = false;
047
048        /** True if multiple tabs/spaces should be compressed to a single space */
049        private boolean compressWhitespace = false;
050
051        /** Default markup encoding. If null, the OS default will be used */
052        private String defaultMarkupEncoding;
053
054        /** Factory for creating markup parsers */
055        private MarkupFactory markupFactory;
056
057        /** if true, then throw an exception if the xml declaration is missing from the markup file */
058        private boolean throwExceptionOnMissingXmlDeclaration = false;
059
060        /** Should HTML comments be stripped during rendering? */
061        private boolean stripComments = false;
062
063        /**
064         * If true, wicket tags ( <wicket: ..>) and wicket:id attributes we be removed from output
065         */
066        private boolean stripWicketTags = false;
067
068        /**
069         * If true, wicket auto-labels will always be updated (via AJAX) whenever the associated form component is.
070         * The default is false (for backward compatibility).
071         */
072        private boolean updateAutoLabelsTogetherWithFormComponent = false;
073
074        /**
075         * Generates the markup ids for the components with
076         * {@link org.apache.wicket.Component#setOutputMarkupId(boolean) #setOutputMarkupId(true)}
077         */
078        private IMarkupIdGenerator markupIdGenerator = new DefaultMarkupIdGenerator();
079
080        /**
081         * Construct
082         */
083        public MarkupSettings()
084        {
085        }
086
087        /**
088         * If true, automatic link resolution is enabled. Disabled by default.
089         *
090         * @see org.apache.wicket.markup.resolver.AutoLinkResolver
091         * @see org.apache.wicket.markup.parser.filter.WicketLinkTagHandler
092         * @return Returns the automaticLinking.
093         */
094        public boolean getAutomaticLinking()
095        {
096                return automaticLinking;
097        }
098
099        /**
100         * @return Returns the compressWhitespace.
101         */
102        public boolean getCompressWhitespace()
103        {
104                return compressWhitespace;
105        }
106
107        /**
108         * @since 1.1
109         * @return Returns default encoding of markup files. If null, the operating system provided
110         *         encoding will be used.
111         */
112        public String getDefaultMarkupEncoding()
113        {
114                return defaultMarkupEncoding;
115        }
116
117        /**
118         * Get the markup factory
119         *
120         * @return A new instance of MarkupFactory.
121         */
122        public MarkupFactory getMarkupFactory()
123        {
124                if (markupFactory == null)
125                {
126                        markupFactory = new MarkupFactory();
127                }
128                return markupFactory;
129        }
130
131        /**
132         * @return Returns the stripComments.
133         */
134        public boolean getStripComments()
135        {
136                return stripComments;
137        }
138
139        /**
140         * Gets whether to remove wicket tags from the output.
141         *
142         * @return whether to remove wicket tags from the output
143         */
144        public boolean getStripWicketTags()
145        {
146                return stripWicketTags;
147        }
148
149        /**
150         * @since 1.3
151         * @return if true, an exception is thrown if the markup file does not contain a xml declaration
152         */
153        public boolean getThrowExceptionOnMissingXmlDeclaration()
154        {
155                return throwExceptionOnMissingXmlDeclaration;
156        }
157
158        /**
159         * Application default for automatic link resolution.
160         *
161         * @param automaticLinking
162         *            The automaticLinking to set.
163         * @see org.apache.wicket.markup.resolver.AutoLinkResolver
164         * @see org.apache.wicket.markup.parser.filter.WicketLinkTagHandler
165         * @return {@code this} object for chaining
166         */
167        public MarkupSettings setAutomaticLinking(boolean automaticLinking)
168        {
169                this.automaticLinking = automaticLinking;
170                return this;
171        }
172
173        /**
174         * Turns on whitespace compression. Multiple occurrences of space/tab characters will be
175         * compressed to a single space. Multiple line breaks newline/carriage-return will also be
176         * compressed to a single newline.
177         * <p>
178         * Compression is currently not HTML aware and so it may be possible for whitespace compression
179         * to break pages. For this reason, whitespace compression is off by default and you should test
180         * your application thoroughly after turning whitespace compression on.
181         * <p>
182         * Spaces are removed from markup at markup load time and there should be no effect on page
183         * rendering speed. In fact, your pages should render faster with whitespace compression
184         * enabled.
185         *
186         * @param compressWhitespace
187         *            The compressWhitespace to set.
188         * @return {@code this} object for chaining
189         */
190        public MarkupSettings setCompressWhitespace(final boolean compressWhitespace)
191        {
192                this.compressWhitespace = compressWhitespace;
193                return this;
194        }
195
196        /**
197         * Set default encoding for markup files. If null, the encoding provided by the operating system
198         * will be used.
199         *
200         * @since 1.1
201         * @param encoding
202         * @return {@code this} object for chaining
203         */
204        public MarkupSettings setDefaultMarkupEncoding(final String encoding)
205        {
206                defaultMarkupEncoding = encoding;
207                return this;
208        }
209
210        /**
211         * Set a new markup factory
212         *
213         * @param factory
214         * @return {@code this} object for chaining
215         */
216        public MarkupSettings setMarkupFactory(final MarkupFactory factory)
217        {
218                Args.notNull(factory, "markup factory");
219                markupFactory = factory;
220                return this;
221        }
222
223        /**
224         * Enables stripping of markup comments denoted in markup by HTML comment tagging.
225         *
226         * @param stripComments
227         *            True to strip markup comments from rendered pages
228         * @return {@code this} object for chaining
229         */
230        public MarkupSettings setStripComments(boolean stripComments)
231        {
232                this.stripComments = stripComments;
233                return this;
234        }
235
236        /**
237         * Sets whether to remove wicket tags from the output.
238         *
239         * @param stripWicketTags
240         *            whether to remove wicket tags from the output
241         * @return {@code this} object for chaining
242         */
243        public MarkupSettings setStripWicketTags(boolean stripWicketTags)
244        {
245                this.stripWicketTags = stripWicketTags;
246                return this;
247        }
248
249        /**
250         * If true, an exception is thrown if the markup file does not contain a xml declaration
251         *
252         * @since 1.3
253         * @param throwException
254         * @return {@code this} object for chaining
255         */
256        public MarkupSettings setThrowExceptionOnMissingXmlDeclaration(boolean throwException)
257        {
258                throwExceptionOnMissingXmlDeclaration = throwException;
259                return this;
260        }
261
262        /**
263         * @return The configured generator for component markup ids
264         */
265        public IMarkupIdGenerator getMarkupIdGenerator()
266        {
267                return markupIdGenerator;
268        }
269
270        /**
271         * Sets a new IMarkupIdGenerator
272         *
273         * @param markupIdGenerator
274         *          The generator of markup ids for the components
275         * @return {@code this} object for chaining
276         */
277        public MarkupSettings setMarkupIdGenerator(IMarkupIdGenerator markupIdGenerator)
278        {
279                this.markupIdGenerator = Args.notNull(markupIdGenerator, "markupIdGenerator");
280                return this;
281        }
282
283        /**
284         * @return If true, wicket auto-labels will always be updated (via AJAX) whenever the associated form component is.
285         *             The default is false (for backward compatibility).
286         */
287        public boolean isUpdateAutoLabelsTogetherWithFormComponent()
288        {
289                return updateAutoLabelsTogetherWithFormComponent;
290        }
291
292        /**
293         *
294         * @param updateAutoLabelsTogetherWithFormComponent If true, wicket auto-labels will always be updated (via AJAX)
295         *                                                  whenever the associated form component is.
296         * @return {@code this} object for chaining
297         */
298        public MarkupSettings setUpdateAutoLabelsTogetherWithFormComponent(boolean updateAutoLabelsTogetherWithFormComponent)
299        {
300                this.updateAutoLabelsTogetherWithFormComponent = updateAutoLabelsTogetherWithFormComponent;
301                return this;
302        }
303}