1 9 10 package org.jboss.portal.format.render.bbcodehtml; 11 12 import org.jboss.portal.format.parser.ParseEvent; 13 import org.jboss.portal.format.parser.ParseEventHandler; 14 import org.jboss.portal.format.parser.TextEvent; 15 import org.jboss.portal.format.parser.bbcode.BBCodeParser; 16 import org.jboss.portal.format.parser.html.TagParser; 17 import org.jboss.portal.format.parser.linebreak.LineBreakParser; 18 import org.jboss.portal.format.render.AbstractRenderer; 19 20 25 public class ToTextRenderer 26 extends AbstractRenderer 27 { 28 29 private final String PRECEDE_CODE = " | "; 30 31 34 35 private final BBCodeParser codeParser = new BBCodeParser(); 36 37 private final TagParser tagParser = new TagParser(); 38 39 private final LineBreakParser lineBreakParser = new LineBreakParser(); 40 41 42 private int isInCode = 0; 43 44 45 private int indentLevel = 0; 46 47 public ToTextRenderer() 48 { 49 codeParser.setHandler(codeHandler); 50 tagParser.setHandler(tagHandler); 51 lineBreakParser.setHandler(lineBreakHandler); 52 } 53 54 public void render(char[] chars, int offset, int length) 55 { 56 codeParser.parse(chars, offset, length); 57 } 58 59 private ParseEventHandler codeHandler = new ParseEventHandler() 60 { 61 public void handle(ParseEvent event) 62 { 63 if (event instanceof BBCodeParser.OpenEvent) 64 { 65 BBCodeParser.OpenEvent openEvent = (BBCodeParser.OpenEvent) event; 66 String string = openEvent.getString(); 67 switch (openEvent.getType()) 68 { 69 case BBCodeParser.EVENT_BOLD: 70 if (isInCode == 0) 71 write("*"); 72 break; 73 case BBCodeParser.EVENT_ITALIC: 74 break; 75 case BBCodeParser.EVENT_UNDERLINE: 76 break; 77 case BBCodeParser.EVENT_COLOR: 78 break; 79 case BBCodeParser.EVENT_SIZE: 80 break; 81 case BBCodeParser.EVENT_QUOTE: 82 indentLevel++; 83 write(string != null ? string : "anonymous"); 84 write(" wrote : "); 85 break; 86 case BBCodeParser.EVENT_CODE: 87 isInCode++; 88 indentLevel++; 89 int count = indentLevel; 90 while (count-- > 0) 91 { 92 write(PRECEDE_CODE); 93 } 94 break; 95 case BBCodeParser.EVENT_UNORDERED_LIST: 96 indentLevel++; 97 break; 98 case BBCodeParser.EVENT_NUMERICALLY_ORDERED_LIST: 99 indentLevel++; 100 break; 101 case BBCodeParser.EVENT_ALPHABETICALLY_ORDERED_LIST: 102 indentLevel++; 103 break; 104 case BBCodeParser.EVENT_ITEM: 105 write("-> "); 106 break; 107 case BBCodeParser.EVENT_LINK: 108 write("[ " + string); 109 break; 110 } 111 } 112 else if (event instanceof BBCodeParser.CloseEvent) 113 { 114 BBCodeParser.CloseEvent closeEvent = (BBCodeParser.CloseEvent) event; 115 String string = closeEvent.getString(); 116 switch (closeEvent.getType()) 117 { 118 case BBCodeParser.EVENT_BOLD: 119 if (isInCode == 0) 120 write("*"); 121 break; 122 case BBCodeParser.EVENT_ITALIC: 123 break; 124 case BBCodeParser.EVENT_UNDERLINE: 125 break; 126 case BBCodeParser.EVENT_COLOR: 127 break; 128 case BBCodeParser.EVENT_SIZE: 129 break; 130 case BBCodeParser.EVENT_QUOTE: 131 indentLevel--; 132 break; 133 case BBCodeParser.EVENT_CODE: 134 isInCode--; 135 indentLevel--; 136 break; 137 case BBCodeParser.EVENT_UNORDERED_LIST: 138 indentLevel--; 139 break; 140 case BBCodeParser.EVENT_NUMERICALLY_ORDERED_LIST: 141 indentLevel--; 142 break; 143 case BBCodeParser.EVENT_ALPHABETICALLY_ORDERED_LIST: 144 indentLevel--; 145 break; 146 case BBCodeParser.EVENT_ITEM: 147 break; 148 case BBCodeParser.EVENT_LINK: 149 write(" ]"); 150 break; 151 } 152 } 153 else 154 { 155 TextEvent textEvent = (TextEvent) event; 156 if (isInCode > 0) 157 { 158 lineBreakParser.parse(textEvent.chars(), textEvent.offset(), 159 textEvent.length()); 160 } 161 else 162 { 163 tagParser.parse(textEvent.chars(), textEvent.offset(), textEvent 164 .length()); 165 } 166 } 167 } 168 }; 169 170 private ParseEventHandler tagHandler = new ParseEventHandler() 171 { 172 public void handle(ParseEvent event) 173 { 174 if (event instanceof TagParser.OpenEvent) 175 { 176 } 183 else if (event instanceof TagParser.CloseEvent) 184 { 185 } 192 else 193 { 194 TextEvent textEvent = (TextEvent) event; 195 lineBreakParser.parse(textEvent.chars(), textEvent.offset(), 196 textEvent.length()); 197 } 198 } 199 }; 200 201 203 private ParseEventHandler lineBreakHandler = new ParseEventHandler() 204 { 205 public void handle(ParseEvent event) 206 { 207 if (event instanceof TextEvent) 208 { 209 TextEvent textEvent = (TextEvent) event; 210 write(textEvent.chars(), textEvent.offset(), textEvent.length()); 211 212 } 228 else 229 { 230 lineBreak(); 231 } 232 } 233 234 private void lineBreak() 235 { 236 if (indentLevel == 0) 237 { 238 ToTextRenderer.this.write("\n"); 239 } 240 else 241 { 242 ToTextRenderer.this.write("\n"); 243 int count = indentLevel; 244 while (count-- > 0) 245 { 246 ToTextRenderer.this.write(PRECEDE_CODE); 247 } 248 } 249 } 250 }; 251 } 252 | Popular Tags |