Home » Eclipse Projects » Mylyn » API for generation of wiki markup
| | | | | | | | | | | |
Re: API for generation of wiki markup [message #947052 is a reply to message #946213] |
Tue, 16 October 2012 19:22 |
Jeremie Bresson Messages: 124 Registered: November 2010 |
Senior Member |
|
|
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 17:00] Report message to a moderator
|
|
| |
Goto Forum:
Current Time: Wed Apr 24 23:56:30 GMT 2024
Powered by FUDForum. Page generated in 0.04077 seconds
|