Home » Eclipse Projects » Mylyn » API for generation of wiki markup
API for generation of wiki markup [message #920942] |
Sun, 23 September 2012 12:43  |
Eclipse User |
|
|
|
Hi,
Have been reading about Mylyn wiki text for last few hours.
Would like to know if there are any APIs or ANT tasks available for generation of wiki markup for e.g. for Confluence wiki
cheers,
Saurav
[Updated on: Sun, 23 September 2012 13:26] by Moderator
|
|
| | | | | | | | | | | |
Re: API for generation of wiki markup [message #947052 is a reply to message #946213] |
Tue, 16 October 2012 15:22   |
Eclipse User |
|
|
|
Here an example, how you can call a builder (in your case the ConfluenceDocumentBuilder) directly:
import java.io.StringWriter;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.BlockType;
import org.eclipse.mylyn.wikitext.core.parser.TableAttributes;
import org.eclipse.mylyn.wikitext.core.parser.TableCellAttributes;
import org.eclipse.mylyn.wikitext.core.util.ServiceLocator;
public class UseBuilder {
public static void main(String[] args) {
StringWriter out = new StringWriter();
DocumentBuilder builder = ServiceLocator.getInstance().getMarkupLanguage("Confluence").createDocumentBuilder(out);
openTable(builder);
openRow(builder);
addHeaderCell(builder, "File name");
addHeaderCell(builder, "Project name");
addHeaderCell(builder, "Short text");
addHeaderCell(builder, "Description");
closeRow(builder);
openRow(builder);
addCell(builder, "AbstractTestContract.java");
addCell(builder, "com.sap.mts.junit.test");
addCell(builder, "Du noci suhum imi");
addCell(builder, "Lango umidi te ipa, hinne abuni ubo pe. Ika teka imagi gonyo on. Cobi zaga paimoda tu sun.");
closeRow(builder);
openRow(builder);
addCell(builder, "TestWizard.java");
addCell(builder, "com.sap.mts.junit.test");
addCell(builder, "Ume muga cebi kasin du");
addCell(builder, "Kayo kidon jesio xen zn. Ura sane ceika unaua di, dun lindi xolada di, jaben kizinda zin si.");
closeRow(builder);
closeTable(builder);
String markup = out.toString();
System.out.println(markup);
}
private static void openTable(DocumentBuilder builder) {
builder.beginBlock(BlockType.TABLE, new TableAttributes());
}
private static void closeTable(DocumentBuilder builder) {
builder.endBlock();
}
private static void openRow(DocumentBuilder builder) {
builder.beginBlock(BlockType.TABLE_ROW, new TableAttributes());
}
private static void closeRow(DocumentBuilder builder) {
builder.endBlock();
}
private static void addHeaderCell(DocumentBuilder builder, String content) {
builder.beginBlock(BlockType.TABLE_CELL_HEADER, new TableCellAttributes());
builder.characters(content);
builder.endBlock();
}
private static void addCell(DocumentBuilder builder, String content) {
builder.beginBlock(BlockType.TABLE_CELL_NORMAL, new TableCellAttributes());
builder.characters(content);
builder.endBlock();
}
}
"(new ConfluenceLanguage()).createDocumentBuilder(out)" is the correct way to instantiate the ConfluenceDocumentBuilder. It uses only public API.
Now in your case, I have try to parse your XML. The idea is to write a new parser MyCustomParser that will replace the HtmlParser of the first example. I have tried something, now I am publishing it here, but I am not really proud of this code...
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder;
import org.eclipse.mylyn.wikitext.core.parser.TableCellAttributes;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.BlockType;
import org.eclipse.mylyn.wikitext.core.parser.TableAttributes;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class MyCustomParser {
public void parse(InputSource input, DocumentBuilder builder) throws IOException, SAXException, ParserConfigurationException{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(input);
openTable(builder);
openRow(builder);
addHeaderCell(builder, "File name");
addHeaderCell(builder, "Project name");
addHeaderCell(builder, "Short text");
addHeaderCell(builder, "Description");
closeRow(builder);
NodeList rootNodes = doc.getChildNodes();
Node codeReviewNode = rootNodes.item(0);
NodeList fileNodes = codeReviewNode.getChildNodes();
for (int i = 0; i < fileNodes.getLength(); i++) {
Node fileNode = fileNodes.item(i);
Node projectNode = null;
Node shortTextNode = null;
Node descriptionNode = null;
NodeList childNodes = fileNode.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node node = childNodes.item(j);
if("Project".equals(node.getNodeName())) {
projectNode = node;
} else if("ShortText".equals(node.getNodeName())) {
shortTextNode = node;
} else if("Description".equals(node.getNodeName())) {
descriptionNode = node;
} else {
throw new IllegalStateException("unexpected node: "+ node.getNodeName());
}
}
if(projectNode == null || shortTextNode == null || descriptionNode == null) {
throw new IllegalStateException("missing one of the child node.");
}
openRow(builder);
addCell(builder, fileNode.getAttributes().getNamedItem("name").getTextContent());
addCell(builder, projectNode.getAttributes().getNamedItem("name").getTextContent());
addCell(builder, shortTextNode.getAttributes().getNamedItem("name").getTextContent());
addCell(builder, descriptionNode.getAttributes().getNamedItem("name").getTextContent());
closeRow(builder);
}
closeTable(builder);
}
private static void openTable(DocumentBuilder builder) {
builder.beginBlock(BlockType.TABLE, new TableAttributes());
}
private static void closeTable(DocumentBuilder builder) {
builder.endBlock();
}
private static void openRow(DocumentBuilder builder) {
builder.beginBlock(BlockType.TABLE_ROW, new TableAttributes());
}
private static void closeRow(DocumentBuilder builder) {
builder.endBlock();
}
private static void addHeaderCell(DocumentBuilder builder, String content) {
builder.beginBlock(BlockType.TABLE_CELL_HEADER, new TableCellAttributes());
builder.characters(content);
builder.endBlock();
}
private static void addCell(DocumentBuilder builder, String content) {
builder.beginBlock(BlockType.TABLE_CELL_NORMAL, new TableCellAttributes());
builder.characters(content);
builder.endBlock();
}
}
Testing this with the same Main class as in the first example:
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder;
import org.eclipse.mylyn.wikitext.core.util.ServiceLocator;
import org.xml.sax.InputSource;
public class Main {
public static void main(String[] args) {
StringWriter out = new StringWriter();
DocumentBuilder builder = ServiceLocator.getInstance().getMarkupLanguage("Confluence").createDocumentBuilder(out);
MyCustomParser parser = new MyCustomParser();
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
"<CodeReview>" +
"<File name=\"AbstractTestContract.java\">" +
"<Project name=\"com.sap.mts.junit.test\"/>" +
"<ShortText name=\"Du noci suhum imi\"/>" +
"<Description name=\"Lango umidi te ipa, hinne abuni ubo pe. Ika teka imagi gonyo on. Cobi zaga paimoda tu sun.\"/>" +
"</File>" +
"<File name=\"TestWizard.java\">" +
"<Project name=\"com.sap.mts.junit.test\"/>" +
"<ShortText name=\"Ume muga cebi kasin du\"/>" +
"<Description name=\"Kayo kidon jesio xen zn. Ura sane ceika unaua di, dun lindi xolada di, jaben kizinda zin si.\"/>" +
"</File>" +
"</CodeReview>";
InputSource input = new InputSource(new ByteArrayInputStream(content.getBytes(Charset.forName("UTF-8"))));
try {
parser.parse(input, builder);
String markup = out.toString();
System.out.println(markup);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I hope you get the idea...
---
Update: use the correct way to get ConfluenceDocumentBuilder given by David Green
[Updated on: Sun, 02 December 2012 12:00] by Moderator
|
|
|
Re: API for generation of wiki markup [message #987130 is a reply to message #922027] |
Fri, 23 November 2012 12:16  |
Eclipse User |
|
|
|
Jeremie,
Thanks for the amazing support. The correct way to get
ConfluenceDocumentBuilder without using internal API is as follows:
DocumentBuilder builder =
ServiceLocator.getInstance().getMarkupLanguage("Confluence").createDocumentBuilder(writer);
David
On 9/24/2012 10:06 AM, Jeremie Bresson wrote:
> Simple JavaSE example:
>
> import java.io.ByteArrayInputStream;
> import java.io.IOException;
> import java.io.StringWriter;
> import java.nio.charset.Charset;
>
> import
> org.eclipse.mylyn.internal.wikitext.confluence.core.ConfluenceDocumentBuilder;
>
> import org.eclipse.mylyn.wikitext.core.parser.HtmlParser;
> import org.xml.sax.InputSource;
> import org.xml.sax.SAXException;
>
> public class Main {
> public static void main(String[] args) {
> StringWriter out = new StringWriter();
> ConfluenceDocumentBuilder builder = new
> ConfluenceDocumentBuilder(out);
>
> HtmlParser parser = new HtmlParser();
> String content = "<html><body><h1>Hello world</h1>"
> + "<p>this <i>is</i> a <b>good</b> test</p>"
> + "<ul><li>lorem</li><li>ipsum</li></ul></body></html>";
> InputSource input = new InputSource(new ByteArrayInputStream(
> content.getBytes(Charset.forName("UTF-8"))));
> try {
> parser.parse(input, builder);
> String markup = out.toString();
> System.out.println(markup);
> } catch (IOException e) {
> e.printStackTrace();
> } catch (SAXException e) {
> e.printStackTrace();
> }
> }
> }
>
>
> On the classpath you need to have:
> * org.eclipse.mylyn.wikitext.confluence.core_1.8.0.I20120907-2057.jar
> * org.eclipse.mylyn.wikitext.core_1.8.0.I20120907-2057.jar
>
> You also can have http://jsoup.org/ on the classpath. This will allow
> you to have not strict HTML as input.
>
> Very important:
> Notice that ConfluenceDocumentBuilder is an API internal class. It means
> that if you use it, you accept some changes (functions, features...)
> from a release to an other one.
>
> Output:
>
> h1. Hello world
>
> this _is_ a *good* test
>
> * lorem
> * ipsum
>
>
>
>
|
|
|
Goto Forum:
Current Time: Thu Jun 12 12:36:33 EDT 2025
Powered by FUDForum. Page generated in 0.07747 seconds
|