KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > urlrewriting > RegexpRewriteRule


1 package org.jahia.services.urlrewriting;
2
3 import java.util.Properties JavaDoc;
4 import org.apache.regexp.RE;
5 import org.apache.regexp.RESyntaxException;
6 import org.jahia.exceptions.JahiaException;
7 /**
8  * <p>Title: </p>
9  * <p>Description: </p>
10  * <p>Copyright: Copyright (c) 2002</p>
11  * <p>Company: Jahia Ltd</p>
12  * @author Serge Huber
13  * @version 1.0
14  */

15
16 public class RegexpRewriteRule extends RewriteRule {
17
18     private RE originalFromRewrittenRegexp;
19     private RE rewrittenFromOriginalRegexp;
20     private String JavaDoc originalFromRewrittenSubst;
21     private String JavaDoc rewrittenFromOriginalSubst;
22
23     public RegexpRewriteRule(Properties JavaDoc originalFromRewrittenProps, Properties JavaDoc 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 JavaDoc getOriginalFromRewritten(String JavaDoc rewrittenURL) {
45         if (originalFromRewrittenRegexp.match(rewrittenURL)) {
46             return originalFromRewrittenRegexp.subst(rewrittenURL, originalFromRewrittenSubst);
47         } else {
48             return null;
49         }
50     }
51
52     public String JavaDoc getRewrittenFromOriginal(String JavaDoc originalURL) {
53         if (rewrittenFromOriginalRegexp.match(originalURL)) {
54             return rewrittenFromOriginalRegexp.subst(originalURL, rewrittenFromOriginalSubst);
55         } else {
56             return null;
57         }
58     }
59 }
Popular Tags