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 than 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         * Generates the markup ids for the components with
070         * {@link org.apache.wicket.Component#setOutputMarkupId(boolean) #setOutputMarkupId(true)}
071         */
072        private IMarkupIdGenerator markupIdGenerator = new DefaultMarkupIdGenerator();
073
074        /**
075         * Construct
076         */
077        public MarkupSettings()
078        {
079        }
080
081        /**
082         * If true, automatic link resolution is enabled. Disabled by default.
083         *
084         * @see org.apache.wicket.markup.resolver.AutoLinkResolver
085         * @see org.apache.wicket.markup.parser.filter.WicketLinkTagHandler
086         * @return Returns the automaticLinking.
087         */
088        public boolean getAutomaticLinking()
089        {
090                return automaticLinking;
091        }
092
093        /**
094         * @return Returns the compressWhitespace.
095         */
096        public boolean getCompressWhitespace()
097        {
098                return compressWhitespace;
099        }
100
101        /**
102         * @since 1.1
103         * @return Returns default encoding of markup files. If null, the operating system provided
104         *         encoding will be used.
105         */
106        public String getDefaultMarkupEncoding()
107        {
108                return defaultMarkupEncoding;
109        }
110
111        /**
112         * Get the markup factory
113         *
114         * @return A new instance of MarkupFactory.
115         */
116        public MarkupFactory getMarkupFactory()
117        {
118                if (markupFactory == null)
119                {
120                        markupFactory = new MarkupFactory();
121                }
122                return markupFactory;
123        }
124
125        /**
126         * @return Returns the stripComments.
127         */
128        public boolean getStripComments()
129        {
130                return stripComments;
131        }
132
133        /**
134         * Gets whether to remove wicket tags from the output.
135         *
136         * @return whether to remove wicket tags from the output
137         */
138        public boolean getStripWicketTags()
139        {
140                return stripWicketTags;
141        }
142
143        /**
144         * @since 1.3
145         * @return if true, an exception is thrown if the markup file does not contain a xml declaration
146         */
147        public boolean getThrowExceptionOnMissingXmlDeclaration()
148        {
149                return throwExceptionOnMissingXmlDeclaration;
150        }
151
152        /**
153         * Application default for automatic link resolution.
154         *
155         * @param automaticLinking
156         *            The automaticLinking to set.
157         * @see org.apache.wicket.markup.resolver.AutoLinkResolver
158         * @see org.apache.wicket.markup.parser.filter.WicketLinkTagHandler
159         * @return {@code this} object for chaining
160         */
161        public MarkupSettings setAutomaticLinking(boolean automaticLinking)
162        {
163                this.automaticLinking = automaticLinking;
164                return this;
165        }
166
167        /**
168         * Turns on whitespace compression. Multiple occurrences of space/tab characters will be
169         * compressed to a single space. Multiple line breaks newline/carriage-return will also be
170         * compressed to a single newline.
171         * <p>
172         * Compression is currently not HTML aware and so it may be possible for whitespace compression
173         * to break pages. For this reason, whitespace compression is off by default and you should test
174         * your application thoroughly after turning whitespace compression on.
175         * <p>
176         * Spaces are removed from markup at markup load time and there should be no effect on page
177         * rendering speed. In fact, your pages should render faster with whitespace compression
178         * enabled.
179         *
180         * @param compressWhitespace
181         *            The compressWhitespace to set.
182         * @return {@code this} object for chaining
183         */
184        public MarkupSettings setCompressWhitespace(final boolean compressWhitespace)
185        {
186                this.compressWhitespace = compressWhitespace;
187                return this;
188        }
189
190        /**
191         * Set default encoding for markup files. If null, the encoding provided by the operating system
192         * will be used.
193         *
194         * @since 1.1
195         * @param encoding
196         * @return {@code this} object for chaining
197         */
198        public MarkupSettings setDefaultMarkupEncoding(final String encoding)
199        {
200                defaultMarkupEncoding = encoding;
201                return this;
202        }
203
204        /**
205         * Set a new markup factory
206         *
207         * @param factory
208         * @return {@code this} object for chaining
209         */
210        public MarkupSettings setMarkupFactory(final MarkupFactory factory)
211        {
212                Args.notNull(factory, "markup factory");
213                markupFactory = factory;
214                return this;
215        }
216
217        /**
218         * Enables stripping of markup comments denoted in markup by HTML comment tagging.
219         *
220         * @param stripComments
221         *            True to strip markup comments from rendered pages
222         * @return {@code this} object for chaining
223         */
224        public MarkupSettings setStripComments(boolean stripComments)
225        {
226                this.stripComments = stripComments;
227                return this;
228        }
229
230        /**
231         * Sets whether to remove wicket tags from the output.
232         *
233         * @param stripWicketTags
234         *            whether to remove wicket tags from the output
235         * @return {@code this} object for chaining
236         */
237        public MarkupSettings setStripWicketTags(boolean stripWicketTags)
238        {
239                this.stripWicketTags = stripWicketTags;
240                return this;
241        }
242
243        /**
244         * If true, an exception is thrown if the markup file does not contain a xml declaration
245         *
246         * @since 1.3
247         * @param throwException
248         * @return {@code this} object for chaining
249         */
250        public MarkupSettings setThrowExceptionOnMissingXmlDeclaration(boolean throwException)
251        {
252                throwExceptionOnMissingXmlDeclaration = throwException;
253                return this;
254        }
255
256        /**
257         * @return The configured generator for component markup ids
258         */
259        public IMarkupIdGenerator getMarkupIdGenerator()
260        {
261                return markupIdGenerator;
262        }
263
264        /**
265         * Sets a new IMarkupIdGenerator
266         *
267         * @param markupIdGenerator
268         *          The generator of markup ids for the components
269         * @return {@code this} object for chaining
270         */
271        public MarkupSettings setMarkupIdGenerator(IMarkupIdGenerator markupIdGenerator)
272        {
273                this.markupIdGenerator = Args.notNull(markupIdGenerator, "markupIdGenerator");
274                return this;
275        }
276}