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.extensions.markup.html.basic;
018
019import org.apache.wicket.markup.ComponentTag;
020import org.apache.wicket.markup.MarkupStream;
021import org.apache.wicket.markup.html.basic.Label;
022import org.apache.wicket.model.IModel;
023import org.apache.wicket.model.Model;
024
025
026/**
027 * If you have email addresses or web URLs in the data that you are displaying, then you can
028 * automatically display those pieces of data as hyperlinks, you will not have to take any action to
029 * convert that data.
030 * <p>
031 * Email addresses will be wrapped with a &lt;a href="mailto:xxx"&gt;xxx&lt;/a&gt; tag, where "xxx"
032 * is the email address that was detected.
033 * <p>
034 * Web URLs will be wrapped with a &lt;a href="xxx"&gt;xxx&lt;/a&gt; tag, where "xxx" is the URL
035 * that was detected (it can be any valid URL type, http://, https://, ftp://, etc...)
036 * 
037 * @author Juergen Donnerstag
038 */
039public class SmartLinkLabel extends Label
040{
041        private static final long serialVersionUID = 1L;
042
043        /**
044         * @see Label#Label(String, java.io.Serializable)
045         */
046        public SmartLinkLabel(final String name, final String label)
047        {
048                this(name, new Model<>(label));
049        }
050
051        /**
052         * @param name
053         * @param model
054         * @see Label#Label(String, IModel)
055         */
056        public SmartLinkLabel(final String name, final IModel<String> model)
057        {
058                super(name, model);
059        }
060
061        /**
062         * @see Label#Label(String)
063         */
064        public SmartLinkLabel(final String name)
065        {
066                super(name);
067        }
068
069        /**
070         * {@inheritDoc}
071         */
072        @Override
073        public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag)
074        {
075                replaceComponentTagBody(markupStream, openTag,
076                        getSmartLink(getDefaultModelObjectAsString()));
077        }
078
079        /**
080         * 
081         * @return link parser
082         */
083        protected ILinkParser getLinkParser()
084        {
085                return new DefaultLinkParser();
086        }
087
088        /**
089         * Replace all email and URL addresses
090         * 
091         * @param text
092         *            Text to be modified
093         * @return Modified Text
094         */
095        protected final CharSequence getSmartLink(final CharSequence text)
096        {
097                return getLinkParser().parse(text.toString());
098        }
099}