From a9bc9997caa50d1dd8cdae0cc5bafdcdec85a5c9 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Wed, 19 May 2010 13:54:35 +0000 Subject: [PATCH] Added a new BBCode parser --- src/zutil/parser/BBCodeParser.java | 169 +++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 src/zutil/parser/BBCodeParser.java diff --git a/src/zutil/parser/BBCodeParser.java b/src/zutil/parser/BBCodeParser.java new file mode 100644 index 0000000..950b1f0 --- /dev/null +++ b/src/zutil/parser/BBCodeParser.java @@ -0,0 +1,169 @@ +package zutil.parser; + +import java.util.HashMap; + +/** + * Parses BBCode and replaces them with the corresponding HTML. + * + * @author Ziver + */ +public class BBCodeParser { + /** Contains all the BBCodes and the corresponding HTML **/ + private HashMap bbcodes; + + public static void main(String[] args){ + BBCodeParser parser = new BBCodeParser(); + System.out.println(parser.parse("jshdkj [u]lol [apa]lol[/apa]")); + System.out.println(parser.parse("jshdkj [m]lol[/k] [i]lol[/i]")); + System.out.println(parser.parse("jshdkj [m")); + System.out.println(parser.parse("jshdkj [/m")); + System.out.println(parser.parse("jshdkj m]")); + } + + /** + * Initiates a instance of the parser with the most used BBCodes. + */ + public BBCodeParser(){ + bbcodes = new HashMap(); + addBBCode("b", "%2"); + addBBCode("i", "%2"); + addBBCode("u", "%2"); + addBBCode("url", "%2"); + addBBCode("img", ""); + addBBCode("quote", "
%2
"); + addBBCode("code", "
%2
"); + addBBCode("size", "%2"); + addBBCode("color", "%2"); + addBBCode("ul", "
    %2
"); + addBBCode("li", "
  • %2
  • "); + } + + /** + * Registers a new BBCode to the parser. Only one type of BBCode allowed. + * + * @param bbcode is the BBCode e.g. "b" or "url" + * @param html is the corresponding HTML e.g. "%2" + * where the %x corresponds to BBCode like this: [url=%1]%2[/url] + */ + public void addBBCode(String bbcode, String html){ + bbcodes.put(bbcode, html); + } + + /** + * Removes a BBCode definition from the parser + * + * @param bbcode is the bbcode to remove + */ + public void removeBBCode(String bbcode){ + bbcodes.remove(bbcode); + } + + /** + * Parses the text with BBCode and converts it to HTML + * + * @param text is a String with BBCode + * @return a String where all BBCode has been replaced by HTML + */ + public String parse(String text){ + StringBuilder out = new StringBuilder(); + StringBuilder t = new StringBuilder(text); + + parse(t, out, ""); + + return out.toString(); + } + + private void parse(StringBuilder text, StringBuilder out, String rootBBC){ + StringBuilder bbcode = null; + boolean closeTag = false; + while(text.length() > 0){ + char c = text.charAt(0); + if(c == '['){ + bbcode = new StringBuilder(); + } + else if(c == '/'){ + closeTag = true; + } + else if(bbcode!=null){ + String param = ""; + switch(c){ + case '=': + param = parseParam(text); + case ']': + String bbcode_cache = bbcode.toString(); + if(closeTag){ + if(!rootBBC.equals(bbcode_cache)){ + out.append("[/").append(bbcode).append("]"); + } + return; + } + String value = parseValue(text, bbcode_cache); + if(bbcodes.containsKey( bbcode_cache )){ + String html = bbcodes.get( bbcode_cache ); + html = html.replaceAll("%1", param); + html = html.replaceAll("%2", value); + out.append(html); + } + else{ + out.append('[').append(bbcode); + if(!param.isEmpty()) + out.append('=').append(param); + out.append(']').append(value); + out.append("[/").append(bbcode).append("]"); + } + bbcode = null; + break; + default: + bbcode.append(c); + break; + } + } + else + out.append(c); + if(text.length()>0) + text.deleteCharAt(0); + } + if(bbcode!=null) + if(closeTag)out.append("[/").append(bbcode); + else out.append('[').append(bbcode); + } + + /** + * Parses a parameter from a BBCode block: [url=param] + * + * @param text is the text to parse from + * @return only the parameter string + */ + private String parseParam(StringBuilder text){ + StringBuilder param = new StringBuilder(); + while(text.length() > 0){ + char c = text.charAt(0); + if(c == ']') + break; + else if(c != '=') + param.append(c); + text.deleteCharAt(0); + } + return param.toString(); + } + + /** + * Parses the value in the BBCodes e.g. [url]value[/url] + * + * @param text is the text to parse the value from + */ + private String parseValue(StringBuilder text, String bbcode){ + StringBuilder value = new StringBuilder(); + while(text.length() > 0){ + char c = text.charAt(0); + if(c == '['){ + parse(text, value, bbcode); + break; + } + else if(c != ']') + value.append(c); + text.deleteCharAt(0); + } + return value.toString(); + } +}