1 43 package net.jforum.util.bbcode; 44 45 import java.io.File ; 46 import java.io.Serializable ; 47 import java.util.Collection ; 48 import java.util.LinkedHashMap ; 49 import java.util.Map ; 50 51 import javax.xml.parsers.SAXParser ; 52 import javax.xml.parsers.SAXParserFactory ; 53 54 import net.jforum.util.preferences.ConfigKeys; 55 import net.jforum.util.preferences.SystemGlobals; 56 57 import org.xml.sax.Attributes ; 58 import org.xml.sax.InputSource ; 59 import org.xml.sax.SAXException ; 60 import org.xml.sax.SAXParseException ; 61 import org.xml.sax.helpers.DefaultHandler ; 62 63 67 public class BBCodeHandler extends DefaultHandler implements Serializable 68 { 69 private Map bbMap = new LinkedHashMap (); 70 private Map alwaysProcessMap = new LinkedHashMap (); 71 private String tagName = ""; 72 private StringBuffer sb; 73 private BBCode bb; 74 75 public BBCodeHandler() { } 76 77 public BBCodeHandler parse() throws Exception 78 { 79 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 80 BBCodeHandler bbParser = new BBCodeHandler(); 81 82 String path = SystemGlobals.getValue(ConfigKeys.CONFIG_DIR) 83 + "/bb_config.xml"; 84 85 File fileInput = new File (path); 86 87 if (fileInput.exists()) { 88 parser.parse(fileInput, bbParser); 89 } 90 else { 91 InputSource input = new InputSource (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 getBbList() 109 { 110 return this.bbMap.values(); 111 } 112 113 public Collection getAlwaysProcessList() 114 { 115 return this.alwaysProcessMap.values(); 116 } 117 118 public BBCode findByName(String tagName) 119 { 120 return (BBCode)this.bbMap.get(tagName); 121 } 122 123 public void startElement(String uri, String localName, String tag, Attributes attrs) 124 { 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 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 { 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 exception) throws SAXException 173 { 174 throw exception; 175 } 176 } | Popular Tags |