package org.sakaiproject.util.impl; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.Vector; import java.util.Map.Entry; import org.sakaiproject.component.cover.ServerConfigurationService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class LinkMigrationHelperImpl implements org.sakaiproject.util.api.LinkMigrationHelper{ private final String ESCAPED_SPACE= "%"+"20"; private final String ASSIGNMENT_LINK_SIG = "assignment"; private final String FORUM_LINK_SIG = "forum"; private static final Log LOG = LogFactory.getLog(LinkMigrationHelperImpl.class); public void init(){ } public String bracketAndNullifySelectedLinks(String m) throws Exception { String lbTmp = ServerConfigurationService.getString("LinkMigrationHelper.linksToBracket","assignment,forum"); String[] linksToBracket = lbTmp.split(","); String lnTmp = ServerConfigurationService.getString("LinkMigrationHelper.linksToNullify","sam_pub,/posts/"); String[] linksToNullify = lnTmp.split(","); List existingLinks = findLinks(m); Iterator l = existingLinks.iterator(); while(l.hasNext()){ String nextLink = (String) l.next(); boolean bracketIt = matchLink(nextLink, linksToBracket); boolean nullIt = matchLink(nextLink, linksToNullify); if(bracketIt | nullIt){ String replacementForLink = null; if(bracketIt){ replacementForLink = nextLink; }else{ replacementForLink = findLinkContent(nextLink); } int li = m.indexOf(nextLink); String before = m.substring(0, li); String after = m.substring(li+nextLink.length()); StringBuffer replacementBuffer = new StringBuffer(); replacementBuffer.append(before); replacementBuffer.append(" ["); replacementBuffer.append(replacementForLink); replacementBuffer.append("] "); replacementBuffer.append(after); m=replacementBuffer.toString(); } } return m; } private boolean matchLink(String link, String[] matches){ for(int s = 0; s< matches.length; s++){ String nextKey = matches[s]; if(link.indexOf(nextKey)>0) return true; } return false; } public String migrateAllLinks(Set entrySet, String msgBody){ Iterator<Entry<String, String>> entryItr = entrySet.iterator(); while(entryItr.hasNext()) { Entry<String, String> entry = (Entry<String, String>) entryItr.next(); String fromContextRef = entry.getKey(); String targetContextRef = entry.getValue(); msgBody =migrateOneLink(fromContextRef, targetContextRef, msgBody); } try { msgBody = bracketAndNullifySelectedLinks(msgBody); } catch (Exception e) { // TODO Auto-generated catch block LOG.debug ("Forums LinkMigrationHelper.editLinks failed" + e); } return msgBody; } public String migrateOneLink(String fromContextRef, String targetContextRef, String msgBody){ fromContextRef=fromContextRef.replace(" ",ESCAPED_SPACE); targetContextRef = targetContextRef.replace(" ",ESCAPED_SPACE); // logger.debug("fromContextRef:"+fromContextRef+"="+entry.getValue()); // logger.debug("entry.getValue="+entry.getValue()); if(msgBody.contains(fromContextRef)){ // logger.debug("found a match"); msgBody = msgBody.replace(fromContextRef, targetContextRef); } return msgBody; } private List findLinks(String msgBody) throws Exception { Vector links = new Vector(); int nextLinkAt = 0; nextLinkAt = msgBody.indexOf("<a", nextLinkAt); boolean done = false; if(nextLinkAt<0){ done=true; } while(!done){ int closingTagLocation = msgBody.indexOf("</a>", nextLinkAt); if(closingTagLocation<0){ throw new Exception("unbalanced anchor tag"); }else{ String thisAnchor = msgBody.substring(nextLinkAt, closingTagLocation+4); links.add(thisAnchor); } nextLinkAt = msgBody.indexOf("<a", closingTagLocation+4); if(nextLinkAt<0){ done=true; } } return links; } private String findLinkContent(String link) throws Exception { int contentStart = link.indexOf(">"); int contentEnd = link.indexOf("</a>", contentStart); return link.substring(contentStart+1, contentEnd); } private String findLinkEntire(String link) throws Exception { int contentStart = link.indexOf("<"); int contentEnd = link.indexOf("</a>", contentStart); return link.substring(contentStart+1, contentEnd); } }