[jboss-svn-commits] JBL Code SVN: r26775 - in labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk: src/main/java/org/jboss/labs/clearspace/plugin/nfm and 4 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri May 29 09:17:34 EDT 2009
Author: lkrzyzanek
Date: 2009-05-29 09:17:33 -0400 (Fri, 29 May 2009)
New Revision: 26775
Added:
labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/
labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCode2CSHTMLParseEventHandler.java
labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverter.java
labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/TextConverter.java
labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/test/java/org/jboss/labs/clearspace/plugin/nfm/converter/
labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/test/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverterTest.java
Modified:
labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/pom.xml
labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/DbNukesForumsManager.java
labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/resources/spring.xml
Log:
added conversion from BB to html
Modified: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/pom.xml
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/pom.xml 2009-05-29 11:34:48 UTC (rev 26774)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/pom.xml 2009-05-29 13:17:33 UTC (rev 26775)
@@ -83,6 +83,21 @@
<type>pom</type>
<scope>provided</scope>
</dependency>
+
+ <!--
+ Portal format module (not mavenized) can be downloaded from:
+ http://viewvc.jboss.org/cgi-bin/viewvc.cgi/labs/labs/jbossforums/branches/multipleforums110P26/thirdparty/jbportal/lib/portal-format-lib.jar?view=log&pathrev=19470
+ and installed to local m2 repo by this command:
+ mvn install:install-file -DgroupId=org.jboss.portal -DartifactId=format -Dversion=2.6 -Dpackaging=jar -Dfile=portal-format-lib.jar
+
+ GroupID, ArtifactID, Version is just tip ...
+ -->
+ <dependency>
+ <groupId>org.jboss.portal</groupId>
+ <artifactId>format</artifactId>
+ <version>2.6</version>
+ </dependency>
+
</dependencies>
<pluginRepositories>
Modified: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/DbNukesForumsManager.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/DbNukesForumsManager.java 2009-05-29 11:34:48 UTC (rev 26774)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/DbNukesForumsManager.java 2009-05-29 13:17:33 UTC (rev 26775)
@@ -31,6 +31,7 @@
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
+import org.jboss.labs.clearspace.plugin.nfm.converter.TextConverter;
import org.jboss.labs.clearspace.plugin.nfm.dao.NukesForumsDAO;
import org.jboss.labs.clearspace.plugin.nfm.dao.NukesForumsForumBean;
import org.jboss.labs.clearspace.plugin.nfm.dao.NukesForumsMappingDAO;
@@ -98,6 +99,8 @@
private Date defaultTopicTime;
+ private List<TextConverter> converters;
+
public int createForumMappingFromCategories() {
Set<Map.Entry<Long, Long>> entries = getCategoryMappings().entrySet();
int count = 0;
@@ -186,11 +189,7 @@
rootMessage.setModificationDate(topicPost.getTime());
rootMessage.setSubject(topic.getTitle());
- // TODO need to implement conversion from NF syntax to Wiki syntax:
- // [quote], [quote=""] [url] [code]
-
- org.w3c.dom.Document topicBody = WikiContentHelper
- .wikiToJiveDocument(topicPost.getBody());
+ org.w3c.dom.Document topicBody = createDocument(topicPost.getBody());
rootMessage.setBody(topicBody);
ForumThread thread = forumManager.createThread(community, rootMessage);
@@ -226,8 +225,7 @@
}
ForumMessage replyMessage = forumManager.createMessage(community,
replyUser);
- org.w3c.dom.Document replyBody = WikiContentHelper
- .wikiToJiveDocument(reply.getBody());
+ org.w3c.dom.Document replyBody = createDocument(reply.getBody());
replyMessage.setBody(replyBody);
Date replyDate = reply.getTime();
@@ -273,6 +271,20 @@
return result;
}
+ /**
+ * Create document from string. body of document is converted via converters
+ *
+ * @param text
+ * @return
+ */
+ protected org.w3c.dom.Document createDocument(String textBody) {
+
+ for (TextConverter converter : converters) {
+ textBody = converter.convert(textBody);
+ }
+ return WikiContentHelper.wikiToJiveDocument(textBody);
+ }
+
public int getCategoryCount() {
return getCategoryMappings().size();
}
@@ -461,4 +473,8 @@
return postMappings;
}
+ public void setConverters(List<TextConverter> converters) {
+ this.converters = converters;
+ }
+
}
Added: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCode2CSHTMLParseEventHandler.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCode2CSHTMLParseEventHandler.java (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCode2CSHTMLParseEventHandler.java 2009-05-29 13:17:33 UTC (rev 26775)
@@ -0,0 +1,200 @@
+/*
+ * JBoss.org http://jboss.org/
+ *
+ * Copyright (c) 2009 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Libor Krzyzanek
+ */
+package org.jboss.labs.clearspace.plugin.nfm.converter;
+
+import org.apache.commons.lang.StringEscapeUtils;
+import org.jboss.portal.format.parser.ParseEvent;
+import org.jboss.portal.format.parser.ParseEventHandler;
+import org.jboss.portal.format.parser.TextEvent;
+import org.jboss.portal.format.parser.bbcode.BBCodeParser;
+import org.jboss.portal.format.parser.html.TagParser;
+import org.jboss.portal.format.parser.linebreak.LineBreakParser;
+
+/**
+ * @author <a href="mailto:lkrzyzan at redhat.com">Libor Krzyzanek</a>
+ *
+ */
+public class BBCode2CSHTMLParseEventHandler implements ParseEventHandler {
+
+ /** Code depth. */
+ private int isInPre = 0;
+
+ private BBCodeConverter renderer;
+
+ private final TagParser tagParser = new TagParser();
+
+ private final LineBreakParser lineBreakParser = new LineBreakParser();
+
+ public BBCode2CSHTMLParseEventHandler() {
+ tagParser.setHandler(tagHandler);
+ lineBreakParser.setHandler(lineBreakHandler);
+ }
+
+ private void write(String s) {
+ renderer.writeText(s);
+ }
+
+ private void write(char[] chars, int offset, int length) {
+ renderer.writeChars(chars, offset, length);
+ }
+
+ public void handle(ParseEvent event) {
+ if (event instanceof BBCodeParser.OpenEvent) {
+ BBCodeParser.OpenEvent openEvent = (BBCodeParser.OpenEvent) event;
+ String string = openEvent.getString();
+ switch (openEvent.getType()) {
+ case BBCodeParser.EVENT_BOLD:
+ write("<strong>");
+ break;
+ case BBCodeParser.EVENT_ITALIC:
+ write("<em>");
+ break;
+ case BBCodeParser.EVENT_UNDERLINE:
+ write("<span style=\"text-decoration: underline;\">");
+ break;
+ case BBCodeParser.EVENT_COLOR:
+ write("<span style=\"color: " + string + "\">");
+ break;
+ case BBCodeParser.EVENT_SIZE:
+ write("<span style=\"font-size: " + string
+ + "px; line-height: normal\">");
+ break;
+ case BBCodeParser.EVENT_QUOTE:
+ write("<pre __jive_macro_name=\"quote\" class=\"jive_text_macro jive_macro_quote\">");
+ isInPre++;
+ break;
+ case BBCodeParser.EVENT_CODE:
+ isInPre++;
+ write("<pre __default_attr=\"java\" __jive_macro_name=\"code\" class=\"jive_text_macro jive_macro_code\">");
+ break;
+ case BBCodeParser.EVENT_UNORDERED_LIST:
+ break;
+ case BBCodeParser.EVENT_NUMERICALLY_ORDERED_LIST:
+ break;
+ case BBCodeParser.EVENT_ALPHABETICALLY_ORDERED_LIST:
+ break;
+ case BBCodeParser.EVENT_ITEM:
+ break;
+ case BBCodeParser.EVENT_LINK:
+ write("<a href=\"" + string + "\">" + string);
+ // write(string);
+ break;
+ }
+ } else if (event instanceof BBCodeParser.CloseEvent) {
+ BBCodeParser.CloseEvent closeEvent = (BBCodeParser.CloseEvent) event;
+ switch (closeEvent.getType()) {
+ case BBCodeParser.EVENT_BOLD:
+ write("</strong>");
+ break;
+ case BBCodeParser.EVENT_ITALIC:
+ write("</em>");
+ break;
+ case BBCodeParser.EVENT_UNDERLINE:
+ write("</span>");
+ break;
+ case BBCodeParser.EVENT_COLOR:
+ write("</span>");
+ break;
+ case BBCodeParser.EVENT_SIZE:
+ write("</span>");
+ break;
+ case BBCodeParser.EVENT_QUOTE:
+ write("</pre>");
+ isInPre--;
+ break;
+ case BBCodeParser.EVENT_CODE:
+ write("</pre>");
+ isInPre--;
+ break;
+ case BBCodeParser.EVENT_UNORDERED_LIST:
+ break;
+ case BBCodeParser.EVENT_NUMERICALLY_ORDERED_LIST:
+ break;
+ case BBCodeParser.EVENT_ALPHABETICALLY_ORDERED_LIST:
+ break;
+ case BBCodeParser.EVENT_ITEM:
+ break;
+ case BBCodeParser.EVENT_LINK:
+ write("</a>");
+ break;
+ }
+ } else {
+ TextEvent textEvent = (TextEvent) event;
+ if (isInPre > 0) {
+ lineBreakParser.parse(textEvent.chars(), textEvent.offset(), textEvent
+ .length());
+ } else {
+ tagParser.parse(textEvent.chars(), textEvent.offset(), textEvent
+ .length());
+ }
+ }
+ }
+
+ private ParseEventHandler tagHandler = new ParseEventHandler() {
+ public void handle(ParseEvent event) {
+ if (event instanceof TagParser.OpenEvent) {
+ // TagParser.OpenEvent openEvent = (TagParser.OpenEvent)event;
+ // switch (openEvent.getType())
+ // {
+ // case TagParser.EVENT_TAG:
+ // break;
+ // }
+ } else if (event instanceof TagParser.CloseEvent) {
+ // TagParser.CloseEvent closeEvent = (TagParser.CloseEvent)event;
+ // switch (closeEvent.getType())
+ // {
+ // case TagParser.EVENT_TAG:
+ // break;
+ // }
+ } else {
+ TextEvent textEvent = (TextEvent) event;
+ lineBreakParser.parse(textEvent.chars(), textEvent.offset(), textEvent
+ .length());
+ }
+ }
+ };
+
+ private ParseEventHandler lineBreakHandler = new ParseEventHandler() {
+ public void handle(ParseEvent event) {
+ if (event instanceof TextEvent) {
+ TextEvent textEvent = (TextEvent) event;
+ String text = new String(textEvent.chars(), textEvent.offset(),
+ textEvent.length());
+ if (isInPre > 0) {
+ text = StringEscapeUtils.escapeHtml(text);
+ }
+ write(text);
+ } else {
+ if (isInPre > 0) {
+ write("<br/>");
+ } else {
+ write("</p><p>");
+ }
+ }
+ }
+ };
+
+ public void setRenderer(BBCodeConverter renderer) {
+ this.renderer = renderer;
+ }
+
+}
Property changes on: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCode2CSHTMLParseEventHandler.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverter.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverter.java (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverter.java 2009-05-29 13:17:33 UTC (rev 26775)
@@ -0,0 +1,62 @@
+/*
+ * JBoss.org http://jboss.org/
+ *
+ * Copyright (c) 2009 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Libor Krzyzanek
+ */
+package org.jboss.labs.clearspace.plugin.nfm.converter;
+
+import java.io.StringWriter;
+
+import org.jboss.portal.format.parser.bbcode.BBCodeParser;
+import org.jboss.portal.format.render.AbstractRenderer;
+
+/**
+ * Converter from BB code to Text CS HTML
+ *
+ * @author <a href="mailto:lkrzyzan at redhat.com">Libor Krzyzanek</a>
+ */
+public class BBCodeConverter extends AbstractRenderer implements TextConverter {
+
+ private BBCodeParser codeParser;
+
+ public String convert(String text) {
+ StringWriter sw = new StringWriter();
+ setWriter(sw);
+ render("<p>" + text + "</p>");
+ return sw.toString();
+ }
+
+ @Override
+ public void render(char[] chars, int offset, int length) {
+ codeParser.parse(chars, offset, length);
+ }
+
+ protected void writeText(String s) {
+ super.write(s);
+ }
+
+ protected void writeChars(char[] chars, int offset, int length) {
+ super.write(chars, offset, length);
+ }
+
+ public void setCodeParser(BBCodeParser codeParser) {
+ this.codeParser = codeParser;
+ }
+
+}
Property changes on: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverter.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/TextConverter.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/TextConverter.java (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/TextConverter.java 2009-05-29 13:17:33 UTC (rev 26775)
@@ -0,0 +1,40 @@
+/*
+ * JBoss.org http://jboss.org/
+ *
+ * Copyright (c) 2009 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Libor Krzyzanek
+ */
+package org.jboss.labs.clearspace.plugin.nfm.converter;
+
+/**
+ * Interface of converter
+ *
+ * @author <a href="mailto:lkrzyzan at redhat.com">Libor Krzyzanek</a>
+ */
+public interface TextConverter {
+
+ /**
+ * Convert text
+ *
+ * @param text
+ * text to convert
+ * @return converted Text
+ */
+ public String convert(String text);
+
+}
Property changes on: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/java/org/jboss/labs/clearspace/plugin/nfm/converter/TextConverter.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/resources/spring.xml
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/resources/spring.xml 2009-05-29 11:34:48 UTC (rev 26774)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/main/resources/spring.xml 2009-05-29 13:17:33 UTC (rev 26775)
@@ -26,11 +26,32 @@
<property name="viewCountManager" ref="viewCountManager" />
<property name="questionManager" ref="questionManager" />
<property name="defaultTopicTimeStr" value="2002-01-01" />
-
+
<property name="topicMappings" ref="topicMappings" />
<property name="postMappings" ref="postMappings" />
+
+ <property name="converters">
+ <list>
+ <ref local="bbCodeConverter" />
+ </list>
+ </property>
</bean>
+ <!-- Lazy init is due we not sure that admin put porta-format-lib.jar to classpath -->
+ <bean id="bbCodeConverter"
+ class="org.jboss.labs.clearspace.plugin.nfm.converter.BBCodeConverter">
+ <property name="codeParser" ref="bbCodeParser"/>
+ </bean>
+
+ <bean id="bbCodeParser" class="org.jboss.portal.format.parser.bbcode.BBCodeParser" lazy-init="true">
+ <property name="handler" ref="bbCode2CSHTMLParseEventHandler" />
+ </bean>
+
+ <bean id="bbCode2CSHTMLParseEventHandler"
+ class="org.jboss.labs.clearspace.plugin.nfm.converter.BBCode2CSHTMLParseEventHandler">
+ <property name="renderer" ref="bbCodeConverter" />
+ </bean>
+
<bean id="nukesForumsMappingDAO"
class="org.jboss.labs.clearspace.plugin.nfm.dao.DbNukesForumsMappingDAOImpl">
<property name="dataSource">
Added: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/test/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverterTest.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/test/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverterTest.java (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/test/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverterTest.java 2009-05-29 13:17:33 UTC (rev 26775)
@@ -0,0 +1,38 @@
+package org.jboss.labs.clearspace.plugin.nfm.converter;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.jboss.portal.format.parser.bbcode.BBCodeParser;
+import org.junit.Before;
+import org.junit.Test;
+
+public class BBCodeConverterTest {
+
+ private BBCodeConverter converter;
+
+ @Before
+ public void setupDbNukesForumsManagerTest() {
+ converter = new BBCodeConverter();
+ BBCodeParser codeParser = new BBCodeParser();
+ BBCode2CSHTMLParseEventHandler handler = new BBCode2CSHTMLParseEventHandler();
+ handler.setRenderer(converter);
+ codeParser.setHandler(handler);
+
+ converter.setCodeParser(codeParser);
+ }
+
+ @Test
+ public void testConvert() throws IOException {
+ assertEquals("<strong>bold</strong>", converter.convert("[b]bold[/b]"));
+ assertEquals("<em>italic</em>", converter.convert("[i]italic[/i]"));
+ assertEquals("<span style=\"text-decoration: underline;\">underline</span>", converter.convert("[u]underline[/u]"));
+ assertEquals("<span style=\"color: red\">color</span>", converter.convert("[color=red]color[/color]"));
+ assertEquals("<span style=\"font-size: 7px; line-height: normal\">size7</span>", converter.convert("[size=7]size7[/size]"));
+ assertEquals("<pre __jive_macro_name=\"quote\" class=\"jive_text_macro jive_macro_quote\">quote</pre>", converter.convert("[quote]quote[/quote]"));
+ assertEquals("<pre __default_attr=\"java\" __jive_macro_name=\"code\" class=\"jive_text_macro jive_macro_code\">code</pre>", converter.convert("[code]code[/code]"));
+ assertEquals("<a href=\"www.jboss.org\">www.jboss.org</a>", converter.convert("[url]www.jboss.org[/url]"));
+ }
+
+}
Property changes on: labs/jbosslabs/labs-3.0-build/integration/cs-nfm/trunk/src/test/java/org/jboss/labs/clearspace/plugin/nfm/converter/BBCodeConverterTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
More information about the jboss-svn-commits
mailing list