The easy Weblog and Wiki Software
[ start | index | login ]
start > Filter Tutorial

Filter Tutorial

Created by funzel. Last edited by Kris, 3 years and 318 days ago. Viewed 4,350 times. #4
[diff] [history] [edit] [rdf]
labels
attachments
(for SnipSnap 0.3.5)

Introduction

SnipSnap uses Macros and Filters to render the text markup to XHTML. Macros are commands and filters usually replace markup with XHTML, for example the BoldFilter replaces __bold__ with <b>bold</b>. You shoud understand the SnipSnap architecture and have read the Macro Tutorial. You should also be familiar with regular expressions.

Filter Interface

The basis for all filters is the Filter interface. All classes that want to act as filters have to implement it.

public interface Filter {
  public String filter(String input, FilterContext context);

public String[] before(); }

The important method is filter(). This method gets a string as an input and a FilterContext object. It takes the input string, applies it's filtering and then returns the filterd string. FilterContext stores some references, e.g. to the rendering engine.

Example Filter

To write your own filter and implement Filter there are some support classes. The most basic is FilterSupport which does not much, but implement the before() method so you do not have to care. Most of the time though you want to use regular expressions (regex) to filter your content. For this there are two classes you can inherit from

  • RegexReplaceFilter
  • RegexTokenFilter
RegexReplaceFilter takes a regex and replaces this with some output like sed or perls s/.../.../g. RegexTokenFilter calls a subroutine for every match. This is explained in Advanced Filter Tutorial.

We want to write a smily replace filter as an example. Suppose we want to replace every Frowny :-( with a smiley :-)

The only thing you have to do is supply the regular expressions:

import org.radeox.filter.regex.RegexReplaceFilter;

public class SmileyFilter extends RegexReplaceFilter { public SmileyFilter() { super(":-\(", ":-)"); } }

The \ is for quoting the ( in the regular expression.

Caching

Radeox supports caching. To tell the engine that the output from your filter is cachable (= produces the same output for the same input which is usually the case with static filters) add the marker interface CacheFilter:

public class SmileyFilter extends RegexReplaceFilter implements CacheFilter {
...

Deployment

To make this filter work you have to create a jar file with

META-INF/services/org.radeox.filter.Filter
example/SmileyFilter.class

META-INF/services/org.radeox.filter.Filter should contain lines with the class names of your filters, for example

example.SmileyFilter

Put this jar into applications/<AppName>/WEB-INF/lib for example if your application is named MyBlog then applications/MyBlog/WEB-INF/lib. Restart the server and the filter should work.

For advanced filters take a look at Advanced Filter Tutorial.

6 comments (by phobia, duelin_markers, chicao, funzel) | post comment

What is SnipSnap?
SnipSnap is a free and easy to install weblog and wiki tool written in Java.

SnipSnap download
Current version: 1.0b3-uttoxeter
Try our >>Web Start Demo!

Resources

5567 Users and 13710 Snips. Installed 5 years and 311 days ago

Logged in Users: (2)
… and 15 Guests.

snipsnap-changed for older changes.

< May 2008 >
SunMonTueWedThuFriSat
123
45678910
11121314151617
18192021222324
25262728293031

snipsnap
Listed on BlogShares
XHTML 1.0 validated
CSS validated
RSS 2.0 validated
RSS Feed

pico-powered

Powered by SnipSnap 1.0b3-uttoxeter
YourKit >>Java Profiler

Fraunhofer FIRST

snipsnap.org | Copyright 2000-2006 Fraunhofer FIRST