KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > filter > FilterNewline


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.filter;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import org.nemesis.forum.Message;
32 import org.nemesis.forum.MessageFilter;
33 import org.nemesis.forum.util.StringUtils;
34
35 /**
36  * A ForumMessageFilter that converts newline characters into HTML <br> tags.
37  * This filter should only be run after any HTML stripping filters.
38  */

39 public class FilterNewline extends MessageFilter {
40
41     private static final String JavaDoc BR_TAG = "<BR>";
42
43     /**
44      * Property values of the filter.
45      */

46     private Properties JavaDoc props;
47     // AJOUT
48
public Map JavaDoc getFilterProperties(){
49                  return props;
50          }
51     /**
52      * Property descriptions of the filter.
53      */

54     private Properties JavaDoc propDescriptions;
55 // AJOUT
56
public Map JavaDoc getFilterPropertiesDescription(){
57                       return propDescriptions;
58               }
59     /**
60      * Creates a new filter not associated with a message. This is
61      * generally only useful for defining a template filter that other
62      * fitlers will be cloned from.
63      */

64     public FilterNewline() {
65         super();
66         this.props = new Properties JavaDoc();
67         this.propDescriptions = new Properties JavaDoc();
68     }
69
70     /**
71      * Creates a new filter wrapped around the specified message. This
72      * constructor is normally called when cloning a filter template.
73      *
74      * @param message the ForumMessage to wrap the new filter around.
75      * @param properties the property values for the filter.
76      * @param propertyDescriptions the property descriptions for the filter.
77      */

78     public FilterNewline(Message message, Properties JavaDoc props, Properties JavaDoc propDescriptions) {
79         super(message);
80         this.props = new Properties JavaDoc(props);
81         this.propDescriptions = new Properties JavaDoc(propDescriptions);
82     }
83
84     /**
85      * Clones a new filter that will have the same properties and that
86      * will wrap around the specified message.
87      *
88      * @param message the ForumMessage to wrap the new filter around.
89      */

90     public MessageFilter clone(Message message) {
91         return new FilterNewline(message, props, propDescriptions);
92     }
93
94     /**
95      * Returns the name of the filter.
96      */

97     public String JavaDoc getName() {
98         return "Newline Converter";
99     }
100
101     /**
102      * Returns a description of the filter.
103      */

104     public String JavaDoc getDescription() {
105         return "Replaces newline characters with the &lt;br&gt; HTML tag.";
106     }
107
108     /**
109      * Returns the author of the filter.
110      */

111     public String JavaDoc getAuthor() {
112         return "CoolServlets.com";
113     }
114
115     /**
116      * Returns the major version number of the filter.
117      */

118     public int getMajorVersion() {
119         return 1;
120     }
121
122     /**
123      * Returns the minor version number of the filter.
124      */

125     public int getMinorVersion() {
126         return 0;
127     }
128
129     /**
130      * Returns the value of a property of the filter.
131      *
132      * @param name the name of the property.
133      * @returns the value of the property.
134      */

135     public String JavaDoc getFilterProperty(String JavaDoc name) {
136         return props.getProperty(name);
137     }
138
139     /**
140      * Returns the description of a property of the filter.
141      *
142      * @param name the name of the property.
143      * @return the description of the property.
144      */

145     public String JavaDoc getFilterPropertyDescription(String JavaDoc name) {
146         return propDescriptions.getProperty(name);
147     }
148
149     /**
150      * Returns an Enumeration of all the property names.
151      */

152     public Enumeration JavaDoc getFilterPropertyNames() {
153         //No properties so return null.
154
return null;
155     }
156
157     /**
158      * Sets a property of the filter. Each filter has a set number of
159      * properties that are determined by the filter author.
160      *
161      * @param name the name of the property to set.
162      * @param value the new value for the property.
163      *
164      * @throws IllegalArgumentException if the property trying to be set doesn't
165      * exist.
166      */

167     public void setFilterProperty(String JavaDoc name, String JavaDoc value) throws IllegalArgumentException JavaDoc {
168         if (props.getProperty(name) == null) {
169             throw new IllegalArgumentException JavaDoc();
170         }
171         props.put(name, value);
172     }
173
174     /**
175      * <b>Overloaded</b> to return the body of the message with newline
176      * characters converted to &ltbr&gt HTML tags.
177      */

178     public String JavaDoc getBody() {
179         if (message.getBody() == null) {
180             return null;
181         }
182         return convertNewlines(message.getBody());
183     }
184
185     /**
186     * This method takes a string which may contain newline characters
187     * '\n' which it converts to html newline tags.
188     *
189     * @param input The text to be converted.
190     * @return The input string with the newline character '\n' replaced
191     * with <br>.
192     */

193     private String JavaDoc convertNewlines(String JavaDoc input) {
194         String JavaDoc result = StringUtils.replace(input, "\r\n", BR_TAG);
195         return StringUtils.replace(result, "\n", BR_TAG);
196     }
197 }
198
Popular Tags