1 package org.jahia.services.urlrewriting; 2 3 import java.util.Properties ; 4 import org.apache.regexp.RE; 5 import org.apache.regexp.RESyntaxException; 6 import org.jahia.exceptions.JahiaException; 7 15 16 public class RegexpRewriteRule extends RewriteRule { 17 18 private RE originalFromRewrittenRegexp; 19 private RE rewrittenFromOriginalRegexp; 20 private String originalFromRewrittenSubst; 21 private String rewrittenFromOriginalSubst; 22 23 public RegexpRewriteRule(Properties originalFromRewrittenProps, Properties rewrittenFromOriginalProps) throws JahiaException { 24 super(originalFromRewrittenProps, rewrittenFromOriginalProps); 25 26 try { 27 originalFromRewrittenRegexp = new RE(originalFromRewrittenProps. 28 getProperty("regexp")); 29 originalFromRewrittenSubst = originalFromRewrittenProps. 30 getProperty("subst"); 31 rewrittenFromOriginalRegexp = new RE(rewrittenFromOriginalProps.getProperty("regexp")); 32 rewrittenFromOriginalSubst = rewrittenFromOriginalProps. 33 getProperty("subst"); 34 35 } catch (RESyntaxException rese) { 36 throw new JahiaException("Error while instantiation regpexp", 37 "Error in URL rewriting", 38 JahiaException.DATA_ERROR, 39 JahiaException.ERROR_SEVERITY, 40 rese); 41 } 42 } 43 44 public String getOriginalFromRewritten(String rewrittenURL) { 45 if (originalFromRewrittenRegexp.match(rewrittenURL)) { 46 return originalFromRewrittenRegexp.subst(rewrittenURL, originalFromRewrittenSubst); 47 } else { 48 return null; 49 } 50 } 51 52 public String getRewrittenFromOriginal(String originalURL) { 53 if (rewrittenFromOriginalRegexp.match(originalURL)) { 54 return rewrittenFromOriginalRegexp.subst(originalURL, rewrittenFromOriginalSubst); 55 } else { 56 return null; 57 } 58 } 59 } | Popular Tags |