001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 03/08/2003 / 05:28:03
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.util.bbcode;
044:
045: import java.io.File;
046: import java.io.Serializable;
047: import java.util.Collection;
048: import java.util.LinkedHashMap;
049: import java.util.Map;
050:
051: import javax.xml.parsers.SAXParser;
052: import javax.xml.parsers.SAXParserFactory;
053:
054: import net.jforum.exceptions.ForumException;
055: import net.jforum.util.preferences.ConfigKeys;
056: import net.jforum.util.preferences.SystemGlobals;
057:
058: import org.xml.sax.Attributes;
059: import org.xml.sax.InputSource;
060: import org.xml.sax.SAXException;
061: import org.xml.sax.SAXParseException;
062: import org.xml.sax.helpers.DefaultHandler;
063:
064: /**
065: * @author Rafael Steil
066: * @version $Id: BBCodeHandler.java,v 1.19 2007/07/28 14:17:09 rafaelsteil Exp $
067: */
068: public class BBCodeHandler extends DefaultHandler implements
069: Serializable {
070: private Map bbMap = new LinkedHashMap();
071: private Map alwaysProcessMap = new LinkedHashMap();
072: private String tagName = "";
073: private StringBuffer sb;
074: private BBCode bb;
075:
076: public BBCodeHandler() {
077: }
078:
079: public BBCodeHandler parse() {
080: try {
081: SAXParser parser = SAXParserFactory.newInstance()
082: .newSAXParser();
083: BBCodeHandler bbParser = new BBCodeHandler();
084:
085: String path = SystemGlobals.getValue(ConfigKeys.CONFIG_DIR)
086: + "/bb_config.xml";
087:
088: File fileInput = new File(path);
089:
090: if (fileInput.exists()) {
091: parser.parse(fileInput, bbParser);
092: } else {
093: InputSource input = new InputSource(path);
094: parser.parse(input, bbParser);
095: }
096:
097: return bbParser;
098: } catch (Exception e) {
099: throw new ForumException(e);
100: }
101: }
102:
103: public void addBb(BBCode bb) {
104: if (bb.alwaysProcess()) {
105: this .alwaysProcessMap.put(bb.getTagName(), bb);
106: } else {
107: this .bbMap.put(bb.getTagName(), bb);
108: }
109: }
110:
111: public Collection getBbList() {
112: return this .bbMap.values();
113: }
114:
115: public Collection getAlwaysProcessList() {
116: return this .alwaysProcessMap.values();
117: }
118:
119: public BBCode findByName(String tagName) {
120: return (BBCode) this .bbMap.get(tagName);
121: }
122:
123: public void startElement(String uri, String localName, String tag,
124: Attributes attrs) {
125: if (tag.equals("match")) {
126: this .sb = new StringBuffer();
127: this .bb = new BBCode();
128:
129: String tagName = attrs.getValue("name");
130: if (tagName != null) {
131: this .bb.setTagName(tagName);
132: }
133:
134: // Shall we remove the infamous quotes?
135: String removeQuotes = attrs.getValue("removeQuotes");
136: if (removeQuotes != null && removeQuotes.equals("true")) {
137: this .bb.enableRemoveQuotes();
138: }
139:
140: String 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 uri, String localName, String tag) {
150: if (tag.equals("match")) {
151: this .addBb(this .bb);
152: } else if (this .tagName.equals("replace")) {
153: this .bb.setReplace(this .sb.toString().trim());
154: this .sb.delete(0, this .sb.length());
155: } else if (this .tagName.equals("regex")) {
156: this .bb.setRegex(this .sb.toString().trim());
157: this .sb.delete(0, this .sb.length());
158: }
159:
160: this .tagName = "";
161: }
162:
163: public void characters(char ch[], int start, int length) {
164: if (this .tagName.equals("replace")
165: || this .tagName.equals("regex"))
166: this .sb.append(ch, start, length);
167: }
168:
169: public void error(SAXParseException exception) throws SAXException {
170: throw exception;
171: }
172: }
|