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.head;
018
019import org.apache.wicket.Page;
020import org.apache.wicket.model.IModel;
021import org.apache.wicket.model.Model;
022import org.apache.wicket.request.cycle.RequestCycle;
023import org.apache.wicket.request.mapper.parameter.PageParameters;
024
025/**
026 * Header item class for HTML 5 imports.
027 * 
028 * @author Tobias Soloschenko
029 * @author Andrea Del Bene
030 * @since 6.19.0
031 */
032public class HtmlImportHeaderItem extends MetaDataHeaderItem
033{
034
035        private HtmlImportHeaderItem()
036        {
037                super(LINK_TAG);
038        }
039
040        /**
041         * Factory method to create <link> tag.
042         *
043         * @param rel
044         *            the 'rel' attribute of the tag
045         * @param href
046         *            the 'href' attribute of the tag
047         * @param media
048         *            the 'media' attribute of the tag
049         * @return A new {@link MetaDataHeaderItem}
050         */
051        public static MetaDataHeaderItem forLinkTag(String rel, String href, String media)
052        {
053                return forLinkTag(Model.of(rel), Model.of(href), Model.of(media));
054        }
055
056        /**
057         * Factory method to create <link> tag.
058         *
059         * @param rel
060         *            the 'rel' attribute of the tag as String model
061         * @param href
062         *            the 'href' attribute of the tag as String model
063         * @param media
064         *            the 'media' attribute of the tag as String model
065         * @return A new {@link MetaDataHeaderItem}
066         */
067        public static MetaDataHeaderItem forLinkTag(IModel<String> rel, IModel<String> href,
068                IModel<String> media)
069        {
070                MetaDataHeaderItem headerItem = forLinkTag(rel, href);
071                headerItem.addTagAttribute("media", media);
072                return headerItem;
073        }
074        
075        /**
076         * Factory method to create &lt;link&gt; tag.
077         * 
078         * @param href
079         *                       the 'href' attribute of the tag as String
080         * @return A new {@link MetaDataHeaderItem}
081         */
082        public static MetaDataHeaderItem forImportLinkTag(String href)
083        {
084                return forImportLinkTag(Model.of(href), false);
085        }
086        
087        /**
088         * Factory method to create &lt;link&gt; tag.
089         * 
090         * @param href
091         *                       the 'href' attribute of the tag as String model
092         * @return A new {@link MetaDataHeaderItem}
093         */
094        public static MetaDataHeaderItem forImportLinkTag(IModel<String> href)
095        {
096                return forImportLinkTag(href, false);
097        }
098        
099        /**
100         * Factory method to create &lt;link&gt; tag.
101         * 
102         * @param href
103         *                       the 'href' attribute of the tag as String
104         * @param async
105         *                       the 'async' attribute as boolean value
106         * @return A new {@link MetaDataHeaderItem}
107         */
108        public static MetaDataHeaderItem forImportLinkTag(String href, boolean async)
109        {
110                return forImportLinkTag(Model.of(href), async);
111        }
112        
113        /**
114         * Factory method to create &lt;link&gt; tag.
115         * 
116         * @param href
117         *                       the 'href' attribute of the tag as String model
118         * @param async
119         *                       the 'async' attribute as boolean value
120         * @return A new {@link MetaDataHeaderItem}
121         */
122        public static MetaDataHeaderItem forImportLinkTag(IModel<String> href, boolean async)
123        {
124                MetaDataHeaderItem linkTag = forLinkTag(Model.of("import"), href);
125                addAsyncAttribute(linkTag, async);
126                
127                return linkTag;
128        }
129
130        /**
131         * Adds 'async' attribute if boolean parameter is true.
132         * 
133         * @param linkTag
134         *                        the current {@link MetaDataHeaderItem}
135         * @param async
136         *                        tells if we must add the attribute (true) or not (false)
137         */
138        private static void addAsyncAttribute(MetaDataHeaderItem linkTag, boolean async)
139        {
140                if(async)
141                {
142                        linkTag.addTagAttribute("async");
143                }
144        }
145        
146        /**
147         * Factory method to create lt;link&gt; tag for html import.
148         *
149         * @param pageClass
150         *            the page class to generate the import for
151         * @return A new {@link MetaDataHeaderItem}
152         */
153        public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> pageClass)
154        {
155                return forImportLinkTag(pageClass, null);
156        }
157
158        /**
159         * Factory method to create lt;link&gt; tag for html import.
160         *
161         * @param pageClass
162         *            the page class to generate the import for
163         * @param pageParameters
164         *            the page parameters to apply to the import
165         * @return A new {@link MetaDataHeaderItem}
166         */
167        public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> pageClass,
168                PageParameters pageParameters)
169        {
170                return forImportLinkTag(pageClass, pageParameters, false);
171        }
172        
173        /**
174         * Factory method to create lt;link&gt; tag for html import.
175         *
176         * @param pageClass
177         *            the page class to generate the import for
178         * @param pageParameters
179         *            the page parameters to apply to the import
180         * @param async
181         *                        the 'async' attribute as boolean value
182         * @return A new {@link MetaDataHeaderItem}
183         */
184        public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> pageClass,
185                PageParameters pageParameters, boolean async)
186        {
187                return forImportLinkTag(pageClass, pageParameters, (String)null, async);
188        }
189        
190        /**
191         * Factory method to create lt;link&gt; tag for html import.
192         *
193         * @param pageClass
194         *            the page class to generate the import for
195         * @param pageParameters
196         *            the page parameters to apply to the import
197         * @param media
198         *            the 'media' attribute of the tag
199         * @return A new {@link MetaDataHeaderItem}
200         */
201        public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> pageClass,
202                PageParameters pageParameters, String media)
203        {
204                return forImportLinkTag(pageClass, pageParameters, Model.of(media));
205        }
206        
207        /**
208         * Factory method to create lt;link&gt; tag for html import.
209         *
210         * @param pageClass
211         *            the page class to generate the import for
212         * @param pageParameters
213         *            the page parameters to apply to the import
214         * @param media
215         *            the 'media' attribute of the tag
216         * @param async
217         *                        the 'async' attribute as boolean value
218         * @return A new {@link MetaDataHeaderItem}
219         */
220        public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> pageClass,
221                PageParameters pageParameters, String media, boolean async)
222        {
223                return forImportLinkTag(pageClass, pageParameters, Model.of(media), async);
224        }
225
226        /**
227         * Factory method to create lt;link&gt; tag for html import.
228         *
229         * @param pageClass
230         *            the page class to generate the import for
231         * @param pageParameters
232         *            the page parameters to apply to the import
233         * @param media
234         *            the 'media' attribute of the tag as String model
235         * @return A new {@link MetaDataHeaderItem}
236         */
237        public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> pageClass,
238                PageParameters pageParameters, IModel<String> media)
239        {
240                return forImportLinkTag(pageClass, pageParameters, media, false);
241        }
242        
243        /**
244         * Factory method to create lt;link&gt; tag for html import.
245         *
246         * @param pageClass
247         *            the page class to generate the import for
248         * @param pageParameters
249         *            the page parameters to apply to the import
250         * @param media
251         *            the 'media' attribute of the tag as String model
252         * @param async
253         *                        the 'async' attribute as boolean value
254         * @return A new {@link MetaDataHeaderItem}
255         */
256        public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> pageClass,
257                PageParameters pageParameters, IModel<String> media, boolean async)
258        {
259                MetaDataHeaderItem linkTag = forLinkTag(Model.of("import"),
260                        Model.of(RequestCycle.get().urlFor(pageClass, pageParameters).toString()), media);
261                
262                addAsyncAttribute(linkTag, async);
263                
264                return linkTag;
265        }
266}