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.html;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.MarkupContainer;
021import org.apache.wicket.markup.ComponentTag;
022import org.apache.wicket.markup.IMarkupFragment;
023import org.apache.wicket.markup.MarkupStream;
024import org.apache.wicket.markup.resolver.IComponentResolver;
025import org.apache.wicket.util.lang.Args;
026
027/**
028 * For each wicket:head tag a HeaderPartContainer is created and added to the HtmlHeaderContainer
029 * which has been added to the Page.
030 * 
031 * @author Juergen Donnerstag
032 */
033public final class HeaderPartContainer extends WebMarkupContainer implements IComponentResolver
034{
035        private static final long serialVersionUID = 1L;
036
037        /** The panel or bordered page the header part is associated with */
038        private final MarkupContainer container;
039
040        /** <wicket:head scope="...">. A kind of namespace */
041        private final String scope;
042
043        /**
044         * @param id
045         *            The component id
046         * @param container
047         *            The Panel (or bordered page) the header part is associated with
048         * @param markup
049         */
050        public HeaderPartContainer(final String id, final MarkupContainer container,
051                final IMarkupFragment markup)
052        {
053                super(id);
054
055                Args.notNull(container, "container");
056                Args.notNull(markup, "markup");
057
058                setMarkup(markup);
059
060                this.container = container;
061
062                scope = getScopeFromMarkup();
063
064                setRenderBodyOnly(true);
065        }
066
067        /**
068         * 
069         * @return get "wicket:scope" attribute from <wicket:head> tag
070         */
071        private String getScopeFromMarkup()
072        {
073                IMarkupFragment markup = getMarkup();
074                String namespace = markup.getMarkupResourceStream().getWicketNamespace();
075                ComponentTag tag = (ComponentTag)markup.get(0);
076                return tag.getAttributes().getString(namespace + ":scope");
077        }
078
079        /**
080         * Get the scope of the header part
081         * 
082         * @return The scope name
083         */
084        public final String getScope()
085        {
086                return scope;
087        }
088
089        /**
090         * The tag must be resolved against the panel and not against the page
091         */
092        @Override
093        public final Component resolve(final MarkupContainer container,
094                final MarkupStream markupStream, final ComponentTag tag)
095        {
096                // The tag must be resolved against the panel and not against the page
097                return this.container.get(tag.getId());
098        }
099}