1 /** 2 * This file Copyright (c) 2003-2011 Magnolia International 3 * Ltd. (http://www.magnolia-cms.com). All rights reserved. 4 * 5 * 6 * This file is dual-licensed under both the Magnolia 7 * Network Agreement and the GNU General Public License. 8 * You may elect to use one or the other of these licenses. 9 * 10 * This file is distributed in the hope that it will be 11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the 12 * implied warranty of MERCHANTABILITY or FITNESS FOR A 13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. 14 * Redistribution, except as permitted by whichever of the GPL 15 * or MNA you select, is prohibited. 16 * 17 * 1. For the GPL license (GPL), you can redistribute and/or 18 * modify this file under the terms of the GNU General 19 * Public License, Version 3, as published by the Free Software 20 * Foundation. You should have received a copy of the GNU 21 * General Public License, Version 3 along with this program; 22 * if not, write to the Free Software Foundation, Inc., 51 23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24 * 25 * 2. For the Magnolia Network Agreement (MNA), this file 26 * and the accompanying materials are made available under the 27 * terms of the MNA which accompanies this distribution, and 28 * is available at http://www.magnolia-cms.com/mna.html 29 * 30 * Any modifications to this file must keep this entire header 31 * intact. 32 * 33 */ 34 package info.magnolia.cms.util; 35 36 import java.util.regex.Pattern; 37 38 import org.apache.commons.lang.StringUtils; 39 40 41 /** 42 * An implementation of URLPattern which matches strings using simple <code>*</code> or <code>?</code> wildcards. 43 * @author Sameer Charles 44 * @author Fabrizio Giustina 45 * TODO rewrite this class using ant-style path comparison and avoiding regexp. See 46 * org.springframework.util.AntPathMatcher in spring 1.2 for a nice implementation. 47 * @version $Revision $ ($Author $) 48 */ 49 public final class SimpleUrlPattern implements UrlPattern { 50 51 /** 52 * Stable serialVersionUID. 53 */ 54 private static final long serialVersionUID = 223L; 55 56 /** 57 * Any char, newline included. 58 */ 59 public static final String URL_CHAR_PATTERN = "."; //$NON-NLS-1$ 60 61 /** 62 * Regexp pattern used for the simple keyword <code>*</code>. Matches 0 or more characters. 63 */ 64 public static final String MULTIPLE_CHAR_PATTERN = URL_CHAR_PATTERN + "*"; //$NON-NLS-1$ 65 66 /** 67 * Regexp pattern used for the simple keyword <code>?</code>. Matches 0 or 1 character. 68 */ 69 public static final String SINGLE_CHAR_PATTERN = URL_CHAR_PATTERN + "?"; //$NON-NLS-1$ 70 71 /** 72 * Regexp pattern used in match(). 73 */ 74 private Pattern pattern; 75 76 /** 77 * Pattern length. Longer patterns have higher priority. 78 */ 79 private int length; 80 81 /** 82 * internal pattern string. 83 */ 84 private String patternString; 85 86 /** 87 * Default constructor used by ContentToBean. 88 */ 89 public SimpleUrlPattern() { 90 } 91 92 /** 93 * Compile a regexp pattern handling <code>*</code> and <code>?</code> chars. 94 * @param string input string 95 * @return a RegExp pattern 96 */ 97 public SimpleUrlPattern(String string) { 98 this.length = StringUtils.removeEnd(string, "*").length(); 99 this.pattern = Pattern.compile(getEncodedString(string), Pattern.DOTALL); 100 this.patternString = string; 101 } 102 103 /** 104 * Replace all "*" with <code>RegexWildcardPattern.MULTIPLE_CHAR_PATTERN</code>. 105 * @param str input string 106 * @return string where all the occurrences of <code>*</code> and <code>?</code> are replaced with a regexp 107 * pattern. 108 */ 109 public static String getEncodedString(String str) { 110 StringBuffer stringBuffer = new StringBuffer(); 111 char[] chars = str.toCharArray(); 112 int i = 0, last = 0; 113 while (i < chars.length) { 114 char c = chars[i]; 115 if (c == '*') { 116 stringBuffer.append('('); 117 stringBuffer.append(chars, last, i - last); 118 stringBuffer.append(')'); 119 stringBuffer.append(MULTIPLE_CHAR_PATTERN); 120 last = i + 1; 121 } 122 else if (c == '?') { 123 stringBuffer.append('('); 124 stringBuffer.append(chars, last, i - last); 125 stringBuffer.append(')'); 126 stringBuffer.append(SINGLE_CHAR_PATTERN); 127 last = i + 1; 128 } 129 i++; 130 } 131 stringBuffer.append(chars, last, i - last); 132 return stringBuffer.toString(); 133 } 134 135 /** 136 * @see info.magnolia.cms.util.UrlPattern#match(java.lang.String) 137 */ 138 @Override 139 public boolean match(String str) { 140 return this.pattern.matcher(str).matches(); 141 } 142 143 /** 144 * @see info.magnolia.cms.util.UrlPattern#getLength() 145 */ 146 @Override 147 public int getLength() { 148 return this.length; 149 } 150 151 /** 152 * @see info.magnolia.cms.util.UrlPattern#getString() 153 */ 154 @Override 155 public String getPatternString() { 156 return patternString; 157 } 158 159 /** 160 * Mainly used by ContentToBean. 161 */ 162 public void setPatternString(String patternString) { 163 this.length = StringUtils.removeEnd(patternString, "*").length(); 164 this.pattern = Pattern.compile(getEncodedString(patternString), Pattern.DOTALL); 165 this.patternString = patternString; 166 } 167 168 @Override 169 public String toString() { 170 // don't use pattern.pattern(), but keep the original string. 171 // The "compiled" pattern will display the ugly patterns like MULTIPLE_CHAR_PATTERN instead of simple * 172 return "SimpleUrlPattern{" + patternString + '}'; 173 } 174 }