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.util.string.AppendingStringBuffer;
020
021/**
022 * This implementation adds link render strategies for email addresses and urls.
023 * 
024 * @see SmartLinkLabel
025 * @see SmartLinkMultiLineLabel
026 * 
027 * @author Gerolf Seitz
028 */
029public class DefaultLinkParser extends LinkParser
030{
031        /** Email address pattern */
032        private static final String emailPattern = "[\\w\\.\\-\\\\+]+@[\\w\\.\\-]+";
033
034        /** URL pattern */
035        private static final String urlPattern = "([a-zA-Z]+://[\\w\\.\\-\\:\\/~]+)[\\w\\.:\\-/?&=%]*";
036
037        /**
038         * Email address render strategy.<br/>
039         * Renders &lt;a href="mailto:{EMAIL}"&gt;{EMAIL}&lt;/a&gt;
040         */
041        public static final ILinkRenderStrategy EMAIL_RENDER_STRATEGY = new ILinkRenderStrategy()
042        {
043                @Override
044                public String buildLink(final String linkTarget)
045                {
046                        return "<a href=\"mailto:" + linkTarget + "\">" + linkTarget + "</a>";
047                }
048        };
049
050        /**
051         * Email address render strategy. Similar to <code>EMAIL_RENDER_STRATEGY</code>, but encrypts
052         * the email address with html entities.
053         */
054        public static final ILinkRenderStrategy ENCRYPTED_EMAIL_RENDER_STRATEGY = new ILinkRenderStrategy()
055        {
056                @Override
057                public String buildLink(final String linkTarget)
058                {
059                        AppendingStringBuffer cryptedEmail = new AppendingStringBuffer(64);
060                        for (int i = 0; i < linkTarget.length(); i++)
061                        {
062                                cryptedEmail.append("&#");
063                                cryptedEmail.append(Integer.toString(linkTarget.charAt(i)));
064                                cryptedEmail.append(";");
065                        }
066
067                        AppendingStringBuffer result = new AppendingStringBuffer(256);
068                        result.append("<a href=\"mailto:");
069                        result.append(cryptedEmail.toString());
070                        result.append("\">");
071                        result.append(cryptedEmail.toString());
072                        result.append("</a>");
073
074                        return result.toString();
075                }
076        };
077
078        /**
079         * Url render strategy.<br/>
080         * Renders &lt;a href="{URL}"&gt;{URL}&lt;/a&gt;
081         */
082        public static final ILinkRenderStrategy URL_RENDER_STRATEGY = new ILinkRenderStrategy()
083        {
084                @Override
085                public String buildLink(final String linkTarget)
086                {
087                        int indexOfQuestion = linkTarget.indexOf('?');
088                        return "<a href=\"" + linkTarget + "\">" +
089                                (indexOfQuestion == -1 ? linkTarget : linkTarget.substring(0, indexOfQuestion)) +
090                                "</a>";
091                }
092        };
093
094        /**
095         * Default constructor.
096         */
097        public DefaultLinkParser()
098        {
099                addLinkRenderStrategy(emailPattern, EMAIL_RENDER_STRATEGY);
100                addLinkRenderStrategy(urlPattern, URL_RENDER_STRATEGY);
101        }
102}