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.markup.parser.filter;
018
019import java.text.ParseException;
020
021import org.apache.wicket.markup.ComponentTag;
022import org.apache.wicket.markup.MarkupElement;
023import org.apache.wicket.markup.MarkupResourceStream;
024import org.apache.wicket.markup.WicketTag;
025import org.apache.wicket.markup.parser.AbstractMarkupFilter;
026import org.apache.wicket.util.value.IValueMap;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * This markup filter warns if a wicket:container tag has an attribute besides wicket:id. This is
032 * most likely a programmer mistake because the wicket:container tag won't be available in
033 * deployment mode.
034 *
035 * The filter is only active in development mode and does nothing in deployment mode.
036 *
037 * @since 6.0
038 */
039public class WicketContainerTagHandler extends AbstractMarkupFilter
040{
041        private static final Logger log = LoggerFactory.getLogger(WicketContainerTagHandler.class);
042
043        private final boolean usesDevelopmentConfig;
044
045        public WicketContainerTagHandler(boolean usesDevelopmentConfig)
046        {
047                this(null, usesDevelopmentConfig);
048        }
049
050        public WicketContainerTagHandler(MarkupResourceStream resourceStream, boolean usesDevelopmentConfig)
051        {
052                super(resourceStream);
053                this.usesDevelopmentConfig = usesDevelopmentConfig;
054        }
055
056        @Override
057        protected final MarkupElement onComponentTag(ComponentTag tag) throws ParseException
058        {
059                if (usesDevelopmentConfig)
060                {
061                        if (tag instanceof WicketTag)
062                        {
063                                WicketTag wtag = (WicketTag)tag;
064                                if (tag.isOpen() && wtag.isContainerTag())
065                                {
066                                        handleContainerTag(wtag);
067                                }
068                        }
069                }
070
071                return tag;
072        }
073
074        private void handleContainerTag(WicketTag containerTag)
075        {
076                IValueMap attributes = containerTag.getAttributes();
077                for (String attribute : attributes.keySet())
078                {
079                        if (ignoreAttribute(attribute))
080                        {
081                                continue;
082                        }
083
084                        reportAttribute(containerTag, attribute);
085                }
086        }
087
088        private void reportAttribute(WicketTag containerTag, String attribute)
089        {
090                log.warn(
091                        "wicket:container with id '{}' has attribute '{}' in markup, which will be ignored in deployment mode",
092                        containerTag.getId(), attribute);
093        }
094
095        private boolean ignoreAttribute(String attribute)
096        {
097                return attribute.equalsIgnoreCase(getWicketNamespace() + ":id");
098        }
099}