/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.internal.renderer;

import org.xwiki.component.annotation.Component;
import org.xwiki.rendering.listener.Link;
import org.xwiki.rendering.renderer.URILabelGenerator;

/**
 * Generate link labels for MAILTO URIs.
 * 
 * @version $Id$
 * @since 2.2RC1
 */
@Component("mailto")
public class MailtoURILabelGenerator implements URILabelGenerator
{
    /**
     * The MAILTO URI prefix (the scheme followed by ":").
     */
    private static final String MAILTO = "mailto:";
    
    /**
     * {@inheritDoc}
     * @see org.xwiki.rendering.renderer.URILabelGenerator#generateLabel(org.xwiki.rendering.listener.Link)
     */
    public String generateLabel(Link link)
    {
        String label = link.getReference().substring(MAILTO.length());
        // Also remove the query string part from the label (we only want the email address).
        int queryStringPosition = label.indexOf("?");
        if (queryStringPosition > -1) {
            label = label.substring(0, queryStringPosition);
        }
        return label;
    }
}

@@ -935,16 +935,60 @@
 derer;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -1640,350 +1640,168 @@
     
-String label = link.getReference().substring(MAILTO.length());%0A        // Also remove the query string part from the label (we only want
+// Remove the scheme prefix and
  the 
-email address).%0A        int 
 query
-S
+ s
 tring
-Position = label.indexOf(%22?%22);%0A        if (queryStringPosition %3E -1) %7B%0A            label = label.substring(0, queryStringPosition);%0A        %7D%0A        return label
+ (if any).%0A        return StringUtils.substringBefore(StringUtils.removeStart(link.getReference(), MAILTO), %22?%22)
 ;%0A  

Improve labels for mailto and attach links