001/* 002 * Copyright 2019 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.plugins.newsletter.testsending; 017 018import java.util.regex.Matcher; 019import java.util.regex.Pattern; 020 021import org.apache.commons.lang3.StringUtils; 022 023import org.ametys.cms.transformation.xslt.ResolveURIComponent; 024 025 026/** 027 * This class parses and resolve links using ResolveLinkComponent 028 */ 029public final class HTMLExpertLinkResolver 030{ 031 private static final Pattern __PARSER = Pattern.compile("\\$\\{type:([^#]+)#url:([^#]+)#download:(true|false)\\}"); 032 033 private HTMLExpertLinkResolver() 034 { 035 // Utility 036 } 037 038 /** 039 * Parse a html string and resolve link found 040 * @param htmlexpert The htmlexpert string 041 * @param idDataHolderContent The id of the content holding the data. Can be null to keep the uri as is 042 * @return The string with links resolved 043 */ 044 public static String parseAndResolve(String htmlexpert, String idDataHolderContent) 045 { 046 String s = htmlexpert; 047 048 Matcher m = __PARSER.matcher(s); 049 while (m.find()) 050 { 051 String type = m.group(1); 052 String uri = m.group(2); 053 boolean download = "true".equals(m.group(3)); 054 055 if ("local".equals(type) && StringUtils.isNotEmpty(idDataHolderContent)) 056 { 057 uri = idDataHolderContent + "@" + StringUtils.substringAfter(uri, "@"); 058 type = NewsletterLocalURIResolver.NEWSLETTER_LOCAL_DATA_TYPE; 059 } 060 061 s = s.substring(0, m.start()) + ResolveURIComponent.resolve(type, uri, download) + s.substring(m.end()); 062 063 m.reset(s); 064 } 065 066 return s; 067 } 068}