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.util.lang.Args;
020
021/**
022 * Settings class for various debug settings
023 * <p>
024 * <i>componentUseCheck </i> (defaults to true in development mode) - causes the framework to do a
025 * check after rendering each page to ensure that each component was used in rendering the markup.
026 * If components are found that are not referenced in the markup, an appropriate error will be
027 * displayed
028 *
029 * @author Jonathan Locke
030 * @author Chris Turner
031 * @author Eelco Hillenius
032 * @author Juergen Donnerstag
033 * @author Johan Compagner
034 * @author Igor Vaynberg (ivaynberg)
035 * @author Martijn Dashorst
036 * @author James Carman
037 */
038public class DebugSettings
039{
040        /** ajax debug mode status */
041        private boolean ajaxDebugModeEnabled = false;
042
043        /** True to check that each component on a page is used */
044        private boolean componentUseCheck = true;
045
046        /**
047         * whether wicket should track line precise additions of components for error reporting.
048         */
049        private boolean linePreciseReportingOnAddComponentEnabled = false;
050
051        /**
052         * whether wicket should track line precise instantiations of components for error reporting.
053         */
054        private boolean linePreciseReportingOnNewComponentEnabled = false;
055
056        /**
057         * Whether the container's class name should be printed to response (in a html comment).
058         */
059        private ClassOutputStrategy outputMarkupContainerClassNameStrategy = ClassOutputStrategy.NONE;
060
061        private String componentPathAttributeName = null;
062
063        private boolean developmentUtilitiesEnabled = false;
064
065        /**
066         * @return true if componentUseCheck is enabled
067         */
068        public boolean getComponentUseCheck()
069        {
070                return componentUseCheck;
071        }
072
073        /**
074         * Returns status of ajax debug mode.
075         *
076         * @return true if ajax debug mode is enabled, false otherwise
077         */
078        public boolean isAjaxDebugModeEnabled()
079        {
080                return ajaxDebugModeEnabled;
081        }
082
083        /**
084         * Returns status of line precise error reporting for added components that are not present in
085         * the markup: it points to the line where the component was added to the hierarchy in your Java
086         * classes. This can cause a significant decrease in performance, do not use in customer facing
087         * applications.
088         *
089         * @return true if the line precise error reporting is enabled
090         */
091        public boolean isLinePreciseReportingOnAddComponentEnabled()
092        {
093                return linePreciseReportingOnAddComponentEnabled;
094        }
095
096        /**
097         * Returns status of line precise error reporting for new components that are not present in the
098         * markup: it points to the line where the component was created in your Java classes. This can
099         * cause a significant decrease in performance, do not use in customer facing applications.
100         *
101         * @return true if the line precise error reporting is enabled
102         */
103        public boolean isLinePreciseReportingOnNewComponentEnabled()
104        {
105                return linePreciseReportingOnNewComponentEnabled;
106        }
107
108        /**
109         * Returns whether the output of markup container's should contain the
110         * container's Java class name.
111         *
112         * @return true if the markup container's Java class name should be written to response
113         * @deprecated use {@link #getOutputMarkupContainerClassNameStrategy()} instead
114         */
115        @Deprecated(forRemoval = true)
116        public boolean isOutputMarkupContainerClassName()
117        {
118                return outputMarkupContainerClassNameStrategy != ClassOutputStrategy.NONE;
119        }
120
121        /**
122         * Returns the strategy for outputting the Java class name of a markup container
123         * 
124         * @return the strategy for outputting the Java class name of a markup container
125         */
126        public ClassOutputStrategy getOutputMarkupContainerClassNameStrategy() 
127        {
128                return outputMarkupContainerClassNameStrategy;
129        }
130
131        /**
132         * Enables or disables ajax debug mode.
133         *
134         * @param enable
135         * @return {@code this} object for chaining
136         */
137        public DebugSettings setAjaxDebugModeEnabled(boolean enable)
138        {
139                ajaxDebugModeEnabled = enable;
140                return this;
141        }
142
143        /**
144         * Sets componentUseCheck debug settings
145         *
146         * @param componentUseCheck
147         * @return {@code this} object for chaining
148         */
149        public DebugSettings setComponentUseCheck(final boolean componentUseCheck)
150        {
151                this.componentUseCheck = componentUseCheck;
152                return this;
153        }
154
155        /**
156         * Enables line precise error reporting for added components that are not present in the markup:
157         * it points to the line where the component was added to the hierarchy in your Java classes.
158         * This can cause a significant decrease in performance, do not use in customer facing
159         * applications.
160         *
161         * @param enable
162         * @return {@code this} object for chaining
163         */
164        public DebugSettings setLinePreciseReportingOnAddComponentEnabled(boolean enable)
165        {
166                linePreciseReportingOnAddComponentEnabled = enable;
167                return this;
168        }
169
170        /**
171         * Enables line precise error reporting for new components that are not present in the markup:
172         * it points to the line where the component was created in your Java classes. This can cause a
173         * significant decrease in performance, do not use in customer facing applications.
174         *
175         * @param enable
176         * @return {@code this} object for chaining
177         */
178        public DebugSettings setLinePreciseReportingOnNewComponentEnabled(boolean enable)
179        {
180                linePreciseReportingOnNewComponentEnabled = enable;
181                return this;
182        }
183
184        /**
185         * Enables wrapping output of markup container in html comments that contain markup container's
186         * class name. (Useful for determining which part of page belongs to which markup file).
187         *
188         * @param enable
189         * @return {@code this} object for chaining
190         * @deprecated use {@link #setOutputMarkupContainerClassNameStrategy(ClassOutputStrategy)} instead
191         */
192        @Deprecated(forRemoval = true)
193        public DebugSettings setOutputMarkupContainerClassName(boolean enable)
194        {
195                outputMarkupContainerClassNameStrategy = enable ? ClassOutputStrategy.HTML_COMMENT : ClassOutputStrategy.NONE;
196                return this;
197        }
198
199        /**
200         * Sets the strategy for outputting the Java class name of a markup container in the HTML output.
201         * 
202         * @param strategy
203         * @return {@code this} object for chaining
204         */
205        public DebugSettings setOutputMarkupContainerClassNameStrategy(ClassOutputStrategy strategy) 
206        {
207                outputMarkupContainerClassNameStrategy = Args.notNull(strategy, "strategy");
208                return this;
209        }
210
211        /**
212         * Sets the strategy for outputting the Java class name of a markup container in the HTML output.
213         *
214         * @param strategyName the enum name of the {@link ClassOutputStrategy} to use
215         * @return {@code this} object for chaining
216         */
217        public DebugSettings setOutputMarkupContainerClassNameStrategy(String strategyName)
218        {
219                final ClassOutputStrategy strategy = Enum.valueOf(ClassOutputStrategy.class, strategyName);
220                return setOutputMarkupContainerClassNameStrategy(strategy);
221        }
222
223        /**
224         * If the parameter value is non-empty then Wicket will use it as the name of an attribute of the
225         * component tag to print the {@link org.apache.wicket.Component}'s path.
226         * This can be useful for debugging and automating tests.
227         *
228         * For example: if {@code componentPathAttributeName} is 'data-wicket-path' then Wicket will add
229         * an attribute to the {@link org.apache.wicket.markup.ComponentTag} for each component with name
230         * 'data-wicket-path' and as a value the component's
231         * {@link org.apache.wicket.Component#getPageRelativePath() page relative path}.
232         *
233         * @param componentPathAttributeName
234         *          The name of the attribute for the {@link org.apache.wicket.markup.ComponentTag}.
235         *          If {@code null} or empty then the attribute won't be rendered
236         * @return {@code this} object for chaining
237         */
238        public DebugSettings setComponentPathAttributeName(String componentPathAttributeName)
239        {
240                this.componentPathAttributeName = componentPathAttributeName;
241                return this;
242        }
243
244        /**
245         * @see #setComponentPathAttributeName(String)
246         * @return The name of the attribute for the {@link org.apache.wicket.markup.ComponentTag}.
247         *         If {@code null} or empty then the attribute won't be rendered
248         */
249        public String getComponentPathAttributeName()
250        {
251                return componentPathAttributeName;
252        }
253
254
255        /**
256         * Enables all of the panels and pages, etc, from wicket-devutils package.
257         *
258         * @param enable
259         * @return {@code this} object for chaining
260         */
261        public DebugSettings setDevelopmentUtilitiesEnabled(boolean enable)
262        {
263                developmentUtilitiesEnabled = enable;
264                return this;
265        }
266
267        /**
268         * Are all of the panels and pages, etc, from wicket-devutils package enabled?
269         *
270         * @return true if all of the panels and pages, etc, from wicket-devutils package are enabled
271         */
272        public boolean isDevelopmentUtilitiesEnabled()
273        {
274                return developmentUtilitiesEnabled;
275        }
276        
277        /**
278         * Strategy for outputting the Java class name of a markup container
279         */
280        public enum ClassOutputStrategy 
281        {
282                /**
283                 * Output the container's class name in an HTML comment
284                 */
285                HTML_COMMENT, 
286                /**
287                 * Output the container's class name in a tag attribute
288                 */
289                TAG_ATTRIBUTE, 
290                /**
291                 * Do not output the container's class name
292                 */
293                NONE
294        }
295}