4 March 2011

How to use mylyn wikitext in java , example

Because it is hard to find simple example of how to use mylyn wikitext in java ,so i provide simple example.

This is basic pattern how to translate from wikitext to html friendly code which you can use where is needed in your marvelous program which will save the world ... or whatever you program want to do.

StringWriter writer = new StringWriter(); //As javadoc said: A character stream that collects its output in a string buffer, which can then be used to construct a string.
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer); //Builds the model for a view
TextileLanguage language = new TextileLanguage(); //Class is explained as "A textile dialect that parses Textile markup."
MarkupParser parser = new MarkupParser(language, builder); //Class is explained as "A markup processor that can process lightweight markup formats such as Textile." but be honest with you i have no f... idea what it means, but is is respond for translate your text-scribble  to wiki-style html
 parser.parse(data); // way to call method that parse data into parse who trasnlate our dirty gibberish into html friendly code

writer.toString(); // gives output in wiki'like style as string  



Below i show how to make working (usually) example.

1) create text file in wiki style like (if you need textile reference,go to this website  textile reference:

h1. THIS IS A EXAMPLE


h2. simple example of wikitext


Here you can find example.
Pastor Cmentarny (TM) (R) (C).


I -love- **smell** of __old__ people in the *p{color:red}red bus *.
because it motivating me to solve math test with ^2^ + b ^2^ = c ^2^

# Women:
## Add useless things to home
## Substract their age
## multiply problems
## divided money betwee
# Man need only:
## Women
## Parties


When i don't know something i searched on "Google":http://google.com.
Instead asking silly question on forum


2)Create project called jmylynexample

3) Add libraries to your  project
wikitext
wikitext.textile

4)Add  main class

package jmylynexample;

/**
*
* @author dsymonowicz
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Example example = new Example();
example.mylyn();
}



}


5) Add example class


/**
* @author pastor cmentarny
*/

package jmylynexample;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.StringWriter;


import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;
import org.eclipse.mylyn.wikitext.core.parser.builder.HtmlDocumentBuilder;
import org.eclipse.mylyn.wikitext.textile.core.TextileLanguage;


class Example {

void mylyn() {


StringWriter writer = new StringWriter(); //A character stream that collects its output in a string buffer, which can then be used to construct a string.
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer); //Builds the model for a view
TextileLanguage language = new TextileLanguage(); //Class is explained as "A textile dialect that parses Textile markup."
MarkupParser parser = new MarkupParser(language, builder); //Class is explained as "A markup processor that can process lightweight markup formats such as Textile." but be honest with you i have no f... idea what it means
String data = loadFile("C:\\mylyn-test.txt"); // we load data (in any way as long as result is as String,in this example is loaded from file)

  parser.parse(data); //  parse data into parser who trasnlate your dirty gibberish into html friendly code


saveFile("C:\\output.html",writer.toString()); //
writer.toString() - gives output in wiki'like style as String (in this example is saved to file)

}

void saveFile(String filename,String content){
try {
FileWriter fstream = new FileWriter(filename);
BufferedWriter out = new BufferedWriter(fstream);
out.write(content);
out.close();
} catch (Exception e) {
System.err.println("Woops! Program is unable to save data to file.Do you have permission to save file?Is it enough space ?\n" +e.getMessage());
}
}

private String loadFile(String filename){
String strLine;
String data = "";
try {
FileInputStream fstream;
fstream = new FileInputStream(filename);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((strLine = br.readLine()) != null) {
data += strLine + "\n";
}
in.close();
} catch (Exception e) {
System.err.println("Woops! Something went wrong. Is file " + filename + " exist or is it accessable?\n" + e.getMessage());
}

return data;
}

}


6) Run and enjoy  amazing view of fantastic result



Sources:
textile reference
mylyn
Mylyn/WikiText