Package net.sf.jpluck.jxl

Source Code of net.sf.jpluck.jxl.BookmarkProcessor$Bookmark

package net.sf.jpluck.jxl;

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;
import javax.servlet.jsp.el.FunctionMapper;

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

import org.apache.commons.el.ExpressionEvaluatorImpl;


public class BookmarkProcessor {
    private ExpressionEvaluator eval = new ExpressionEvaluatorImpl();
    private List bookmarks = new ArrayList();

    void addBookmark(String regexp, String value) {
        bookmarks.add(new Bookmark(regexp, value));
    }

    public void process(net.sf.jpluck.plucker.Document pluckerDocument, String uri, String title) {
        try {
          if (title == null) {
            title = "";
          }
            for (Iterator it = bookmarks.iterator(); it.hasNext();) {
                Bookmark bookmark = (Bookmark) it.next();
                if (bookmark.pattern.matcher(uri).matches()) {
                  String expression = bookmark.value;
          DefaultVariables variables = new DefaultVariables(uri, null, title);
          FunctionMapper functionMapper = StaticFunctionMapper.getDefault();
                    String name = (String) eval.evaluate(expression, String.class, variables, functionMapper);
                    name = name.trim();
                    if (name.length() > 0) {
            pluckerDocument.addBookmark(name, uri);
                    }
                    return;
                }
            }
        } catch (ELException e) {
            throw new RuntimeException(e);
        }
    }

    private static class Bookmark {
        Pattern pattern;
        String value;

        Bookmark(String regexp, String value) {
            pattern = Pattern.compile(regexp);
            this.value = value;
        }
    }
}
TOP

Related Classes of net.sf.jpluck.jxl.BookmarkProcessor$Bookmark

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.