Package net.sf.jpluck.jxl

Source Code of net.sf.jpluck.jxl.URIRewriter

package net.sf.jpluck.jxl;

import net.sf.jpluck.util.el.DefaultVariables;
import net.sf.jpluck.util.el.StaticFunctionMapper;

import org.apache.commons.el.ExpressionEvaluatorImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.ExpressionEvaluator;


public class URIRewriter {
  private ExpressionEvaluator eval = new ExpressionEvaluatorImpl();
  private List rewriteList = new ArrayList();

  public URIRewriter() {
  }

  public void add(Pattern pattern, String expression) {
    rewriteList.add(new Rule(pattern, expression));
  }

  public String rewrite(String uri) {
    try {
      for (Iterator it = rewriteList.iterator(); it.hasNext();) {
        Rule rule = (Rule) it.next();
        if (rule.pattern.matcher(uri).matches()) {
          DefaultVariables dv = new DefaultVariables(uri, null, null);
          return (String) eval.evaluate(rule.expression, String.class, dv,
                          StaticFunctionMapper.getDefault());
        }
      }
      return uri;
    } catch (ELException e) {
      throw new RuntimeException(e);
    }
  }

  private static class Rule {
    private Pattern pattern;
    private String expression;

    Rule(Pattern pattern, String expression) {
      this.pattern = pattern;
      this.expression = expression;
    }
  }
}
TOP

Related Classes of net.sf.jpluck.jxl.URIRewriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.