KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > util > bbcode > BBCodeHandler


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: 03/08/2003 / 05:28:03
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.util.bbcode;
44
45  import java.io.File JavaDoc;
46 import java.io.Serializable JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.LinkedHashMap JavaDoc;
49 import java.util.Map JavaDoc;
50
51 import javax.xml.parsers.SAXParser JavaDoc;
52 import javax.xml.parsers.SAXParserFactory JavaDoc;
53
54 import net.jforum.util.preferences.ConfigKeys;
55 import net.jforum.util.preferences.SystemGlobals;
56
57 import org.xml.sax.Attributes JavaDoc;
58 import org.xml.sax.InputSource JavaDoc;
59 import org.xml.sax.SAXException JavaDoc;
60 import org.xml.sax.SAXParseException JavaDoc;
61 import org.xml.sax.helpers.DefaultHandler JavaDoc;
62
63 /**
64  * @author Rafael Steil
65  * @version $Id: BBCodeHandler.java,v 1.15 2005/07/26 04:01:23 diegopires Exp $
66  */

67 public class BBCodeHandler extends DefaultHandler JavaDoc implements Serializable JavaDoc
68 {
69     private Map JavaDoc bbMap = new LinkedHashMap JavaDoc();
70     private Map JavaDoc alwaysProcessMap = new LinkedHashMap JavaDoc();
71     private String JavaDoc tagName = "";
72     private StringBuffer JavaDoc sb;
73     private BBCode bb;
74     
75     public BBCodeHandler() { }
76     
77     public BBCodeHandler parse() throws Exception JavaDoc
78     {
79         SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
80         BBCodeHandler bbParser = new BBCodeHandler();
81         
82         String JavaDoc path = SystemGlobals.getValue(ConfigKeys.CONFIG_DIR)
83             + "/bb_config.xml";
84         
85         File JavaDoc fileInput = new File JavaDoc(path);
86         
87         if (fileInput.exists()) {
88             parser.parse(fileInput, bbParser);
89         }
90         else {
91             InputSource JavaDoc input = new InputSource JavaDoc(path);
92             parser.parse(input, bbParser);
93         }
94
95         return bbParser;
96     }
97     
98     public void addBb(BBCode bb)
99     {
100         if (bb.alwaysProcess()) {
101             this.alwaysProcessMap.put(bb.getTagName(), bb);
102         }
103         else {
104             this.bbMap.put(bb.getTagName(), bb);
105         }
106     }
107     
108     public Collection JavaDoc getBbList()
109     {
110         return this.bbMap.values();
111     }
112     
113     public Collection JavaDoc getAlwaysProcessList()
114     {
115         return this.alwaysProcessMap.values();
116     }
117     
118     public BBCode findByName(String JavaDoc tagName)
119     {
120         return (BBCode)this.bbMap.get(tagName);
121     }
122     
123     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc tag, Attributes JavaDoc attrs)
124     {
125         if (tag.equals("match")) {
126             this.sb = new StringBuffer JavaDoc();
127             this.bb = new BBCode();
128             
129             String JavaDoc tagName = attrs.getValue("name");
130             if (tagName != null) {
131                 this.bb.setTagName(tagName);
132             }
133             
134             // Shall we remove the infamous quotes?
135
String JavaDoc removeQuotes = attrs.getValue("removeQuotes");
136             if (removeQuotes != null && removeQuotes.equals("true")) {
137                 this.bb.enableRemoveQuotes();
138             }
139             
140             String JavaDoc alwaysProcess = attrs.getValue("alwaysProcess");
141             if (alwaysProcess != null && "true".equals(alwaysProcess)) {
142                 this.bb.enableAlwaysProcess();
143             }
144         }
145     
146         this.tagName = tag;
147     }
148
149     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc tag)
150     {
151         if (tag.equals("match")) {
152             this.addBb(this.bb);
153         }
154         else if (this.tagName.equals("replace")) {
155             this.bb.setReplace(this.sb.toString().trim());
156             this.sb.delete(0, this.sb.length());
157         }
158         else if (this.tagName.equals("regex")) {
159             this.bb.setRegex(this.sb.toString().trim());
160             this.sb.delete(0, this.sb.length());
161         }
162     
163         this.tagName = "";
164     }
165
166     public void characters(char ch[], int start, int length)
167     {
168         if (this.tagName.equals("replace") || this.tagName.equals("regex"))
169             this.sb.append(ch, start, length);
170     }
171
172     public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc
173     {
174         throw exception;
175     }
176 }
Popular Tags