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
021
022/**
023 * This class parses and resolve links using ResolveLinkComponent
024 */
025public final class HTMLExpertLinkResolver
026{
027    private static final Pattern __PARSER = Pattern.compile("\\$\\{type:([^#]+)#url:([^#]+)#download:(true|false)\\}");
028    
029    private HTMLExpertLinkResolver()
030    {
031        // Utility
032    }
033    
034    /**
035     * Parse a html string and resolve link found
036     * @param htmlexpert The htmlexpert string 
037     * @return The string with links resolved
038     */
039    public static String parseAndResolve(String htmlexpert)
040    {
041        String s = htmlexpert;
042        
043        Matcher m = __PARSER.matcher(s);
044        while (m.find())
045        {
046            String type = m.group(1);
047            String uri = m.group(2);
048            boolean download = "true".equals(m.group(3));
049            
050            s = s.substring(0, m.start()) + ResolveURIComponent.resolve(type, uri, download) + s.substring(m.end());
051            
052            m.reset(s);
053        }
054        
055        return s;
056    }
057}