KickJava   Java API By Example, From Geeks To Geeks.

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


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
34 /**
35  * A ForumMessageFilter that converts URL's to working HTML web links.
36  */

37 public class FilterURLConverter extends MessageFilter {
38
39     /**
40      * Property values of the filter.
41      */

42     private Properties JavaDoc props;
43     // AJOUT
44
public Map JavaDoc getFilterProperties(){
45                  return props;
46          }
47     /**
48      * Property descriptions of the filter.
49      */

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

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

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

87     public MessageFilter clone(Message message) {
88         return new FilterURLConverter(message, props, propDescriptions);
89     }
90
91     /**
92      * Returns the name of the filter.
93      */

94     public String JavaDoc getName() {
95         return "URL Converter";
96     }
97
98     /**
99      * Returns a description of the filter.
100      */

101     public String JavaDoc getDescription() {
102         return "Converts URL's to working web links.";
103     }
104
105     /**
106      * Returns the author of the filter.
107      */

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

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

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

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

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

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

163     public void setFilterProperty(String JavaDoc name, String JavaDoc value) throws IllegalArgumentException JavaDoc {
164         if (props.getProperty(name) == null) {
165             throw new IllegalArgumentException JavaDoc();
166         }
167         props.put(name, value);
168     }
169
170     /**
171      * <b>Overloaded</b> to return the body of message with URLs changed
172      * to working links.
173      */

174     public String JavaDoc getBody() {
175         return convertURL(message.getBody());
176     }
177
178     /**
179      * Creates properties and sets their descriptions.
180      */

181     private void initializeProperties() {
182     }
183
184     /**
185      * This method takes a string which may contain URLs
186      * and replaces them with working links. It does this
187      * by adding the html tags <a href> and </a>.
188      *
189      * @param input the text to be converted.
190      * @returns the input string with the URLs replaced with links.
191      */

192     private String JavaDoc convertURL(String JavaDoc input) {
193         //Check if the string is null or zero length -- if so, return
194
//what was sent in.
195
if (input == null || input.length() == 0) {
196             return input;
197         } else {
198             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
199
200             int i = 0, j = 0, oldend = 0;
201             int len = input.length();
202             char cur;
203
204             while ((i = input.indexOf("http://", oldend)) >= 0) {
205                 j = i + 7;
206                 cur = input.charAt(j);
207                 while (j < len) {
208                     //Is a space?
209
if (cur == ' ')
210                         break;
211                     //Is html?
212
if (cur == '<')
213                         break;
214                     //Is a Win32 newline?
215
if (cur == '\n')
216                         break;
217                     //Is Unix newline?
218
if (cur == '\r' && j < len - 1 && input.charAt(j + 1) == '\n')
219                         break;
220
221                     j++;
222                     if (j < len) {
223                         cur = input.charAt(j);
224                     }
225                 }
226                 buf.append(input.substring(oldend, i));
227                 buf.append("<a href =\"");
228                 buf.append(input.substring(i, j));
229                 buf.append("\">");
230                 buf.append(input.substring(i, j));
231                 buf.append("</a>");
232                 oldend = j;
233             }
234             buf.append(input.substring(j, len));
235             return buf.toString();
236         }
237     }
238 }
239
Popular Tags