001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.transformation.xslt;
017
018import java.util.regex.Matcher;
019import java.util.regex.Pattern;
020
021import org.ametys.core.util.URIUtils;
022
023
024/**
025 * This class parses and resolve links using ResolveLinkComponent
026 */
027public final class HTMLExpertLinkResolver
028{
029    private static final Pattern __PARSER = Pattern.compile("\\$\\{type:([^#]+)#url:([^#]+)#download:(true|false)\\}");
030    
031    private HTMLExpertLinkResolver()
032    {
033        // Utility
034    }
035    
036    /**
037     * Parse a html string and resolve link found
038     * @param htmlexpert The htmlexpert string 
039     * @return The string with links resolved
040     */
041    public static String parseAndResolve(String htmlexpert)
042    {
043        String s = URIUtils.decode(htmlexpert);
044        
045        Matcher m = __PARSER.matcher(s);
046        while (m.find())
047        {
048            String type = m.group(1);
049            String uri = m.group(2);
050            boolean download = "true".equals(m.group(3));
051            
052            s = s.substring(0, m.start()) + ResolveURIComponent.resolve(type, uri, download) + s.substring(m.end());
053            
054            m.reset(s);
055        }
056        
057        return s;
058    }
059}