From: Chris L. <chr...@jb...> - 2006-05-29 16:35:31
|
User: claprun Date: 06/05/29 12:34:47 Modified: wsrp/src/main/org/jboss/portal/wsrp WSRPPortletURL.java WSRPTypeFactory.java Log: Extracted URL methods from Tools and WSRPPortletURL to common.util.URLTools (was URL). Revision Changes Path 1.10 +1 -121 jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/WSRPPortletURL.java (In the diff below, changes in quantity of whitespace are not shown.) Index: WSRPPortletURL.java =================================================================== RCS file: /cvsroot/jboss/jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/WSRPPortletURL.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -b -r1.9 -r1.10 --- WSRPPortletURL.java 8 May 2006 16:54:03 -0000 1.9 +++ WSRPPortletURL.java 29 May 2006 16:34:47 -0000 1.10 @@ -31,16 +31,12 @@ import org.jboss.portal.portlet.RenderURL; import org.jboss.portal.portlet.StateString; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * @author <a href="mailto:chr...@jb...">Chris Laprun</a> - * @version $Revision: 1.9 $ + * @version $Revision: 1.10 $ * @since 2.4 (Apr 28, 2006) */ public abstract class WSRPPortletURL implements PortletURL @@ -313,120 +309,4 @@ return params; } - public static URLMatch[] extractURLsFrom(String markup) - { - // todo: will need to re-write without regex after 2.4 - int length; - if (markup != null && (length = markup.length()) != 0) - { - Matcher matcher = LINK.matcher(markup); - int currentIndex = 0; - List links = new ArrayList(); - while (matcher.find(currentIndex) && currentIndex < length) - { - links.add(new URLMatch(matcher.start(2), matcher.end(2), matcher.group(2))); - currentIndex = matcher.end(); - } - - return (URLMatch[])links.toArray(new URLMatch[0]); - } - throw new IllegalArgumentException("Cannot extract URLs from a null or empty markup string!"); - } - - public static int getURLCountIn(String markup) - { - int length; - if (markup != null && (length = markup.length()) != 0) - { - Matcher matcher = LINK.matcher(markup); - int count = 0; - int currentIndex = 0; - while (matcher.find(currentIndex) && currentIndex < length) - { - count++; - currentIndex = matcher.end(); - } - return count; - } - return 0; - } - - public static String replaceURLsBy(String markup, final String[] replacements) - { - if (replacements == null || replacements.length == 0) - { - return markup; - } - - int urlCount = WSRPPortletURL.getURLCountIn(markup); - if (replacements.length != urlCount) - { - throw new IllegalArgumentException("Trying to replace " + urlCount + " URLs by " + replacements.length + " replacement(s)."); - } - - return replaceURLsBy(markup, new URLReplacementGenerator() - { - public String getReplacementFor(int currentIndex, URLMatch currentMatch) - { - return replacements[currentIndex]; - } - }); - } - - public static String replaceURLsBy(String markup, URLReplacementGenerator generator) - { - - WSRPPortletURL.URLMatch[] urls = WSRPPortletURL.extractURLsFrom(markup); - if (urls.length > 0) - { - StringBuffer newMarkup = new StringBuffer(markup.length()); - int currentIndex = 0; - for (int i = 0; i < urls.length; i++) - { - WSRPPortletURL.URLMatch url = urls[i]; - newMarkup.append(markup.substring(currentIndex, url.getStart())).append(generator.getReplacementFor(i, url)); - currentIndex = url.getEnd(); - } - newMarkup.append(markup.substring(currentIndex)); - markup = newMarkup.toString(); - } - return markup; - } - - private static final Pattern LINK = Pattern.compile("(?:href|action)\\s*=\\s*('|\")\\s*([^'\"]*)\\s*('|\")", - Pattern.CASE_INSENSITIVE); - - public static class URLMatch - { - private int start; - private int end; - private String urlAsString; - - private URLMatch(int start, int end, String urlAsString) - { - this.start = start; - this.end = end; - this.urlAsString = urlAsString; - } - - public int getStart() - { - return start; - } - - public int getEnd() - { - return end; - } - - public String getURLAsString() - { - return urlAsString; - } - } - - public abstract static class URLReplacementGenerator - { - public abstract String getReplacementFor(int currentIndex, URLMatch currentMatch); - } } 1.13 +6 -6 jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/WSRPTypeFactory.java (In the diff below, changes in quantity of whitespace are not shown.) Index: WSRPTypeFactory.java =================================================================== RCS file: /cvsroot/jboss/jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/WSRPTypeFactory.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -b -r1.12 -r1.13 --- WSRPTypeFactory.java 27 May 2006 06:44:50 -0000 1.12 +++ WSRPTypeFactory.java 29 May 2006 16:34:47 -0000 1.13 @@ -24,7 +24,7 @@ import org.jboss.portal.Mode; import org.jboss.portal.WindowState; -import org.jboss.portal.common.util.Tools; +import org.jboss.portal.common.util.URLTools; import org.jboss.portal.portlet.ActionURL; import org.jboss.portal.portlet.RenderURL; import org.jboss.portal.portlet.StateString; @@ -57,7 +57,7 @@ * information. * * @author <a href="mailto:chr...@jb...">Chris Laprun</a> - * @version $Revision: 1.12 $ + * @version $Revision: 1.13 $ * @since 2.4 */ public class WSRPTypeFactory @@ -514,10 +514,10 @@ Templates templates = new Templates(); // fix-me: de-escape { and } - templates.setBlockingActionTemplate(Tools.decodeXWWWFormURL(context.encodeURL(ACTION_URL, Boolean.FALSE, null, true))); - templates.setRenderTemplate(Tools.decodeXWWWFormURL(context.encodeURL(RENDER_URL, Boolean.FALSE, null, true))); - templates.setSecureBlockingActionTemplate(Tools.decodeXWWWFormURL(context.encodeURL(ACTION_URL, Boolean.TRUE, null, true))); - templates.setSecureRenderTemplate(Tools.decodeXWWWFormURL(context.encodeURL(RENDER_URL, Boolean.TRUE, null, true))); + templates.setBlockingActionTemplate(URLTools.decodeXWWWFormURL(context.encodeURL(ACTION_URL, Boolean.FALSE, null, true))); + templates.setRenderTemplate(URLTools.decodeXWWWFormURL(context.encodeURL(RENDER_URL, Boolean.FALSE, null, true))); + templates.setSecureBlockingActionTemplate(URLTools.decodeXWWWFormURL(context.encodeURL(ACTION_URL, Boolean.TRUE, null, true))); + templates.setSecureRenderTemplate(URLTools.decodeXWWWFormURL(context.encodeURL(RENDER_URL, Boolean.TRUE, null, true))); //todo: deal with resources return templates; |