Wiki to XML using Ant

Folks,

I have been working hard for the past few days, so that I could write an Ant task that could simplify the task of converting WikiCreole Markup to XML format. This conversion was made possible due to the efforts of Martin Junghans and Dirk Riehle. They did research on this subject. As a result, they created an EBNF grammar and an XML interchange format (Creole 1.0).

Brief History

A fried of mine had checkout the sources from SourceForge, this year. Firstly, he used ANTLR to generate the Lexer and Parser files from given grammar file, using a system that had atleast 2 GB RAM.

He re-used the code and made few new classes, just for the sake of running the project and checking the workflow. It worked, sample files having wikicreole markup content were converted to XML format using the XML Schema, defined in Riehle’s and Martin’s second research paper.

CreoleTask

As I want to create support for the same in the NetBeans IDE. The NetBeans IDE makes good use of Ant and Maven, so I wanted to create a task of my own that will take care of the necessary classes that are required to make it possible to convert Wiki to XML. Today, I spent time learning about writing Ant Tasks and this is what I got;

public class CreoleTask extends Task {

 private String gLoc = "creole10.g";
 private String wikipage;

 @Override
 public void execute() throws BuildException {
   ParserHandler ph = new ParserHandler(new Grammar(gLoc));
   if (wikipage == null) {
     throw new BuildException("File not found..");
   }
   wikipage = wikipage.replace('/', '\\');
   log("Importing wikipage " + this.wikipage);
   ph.parseWp(this.wikipage);
 }

 public void setWikipage(String wikipage) {
   this.wikipage = wikipage;
 }
}

Now, what I need is the JAR that contains CreoleTask.class. So, I created a Java Application using NetBeans IDE and opened its build.xml;

<project name="CreoleAnt" default="default" basedir=".">
    <description>Builds, tests, and runs the project CreoleAnt.</description>
    <import file="nbproject/build-impl.xml"/>
</project>

This file contains comments as well;

There exist several targets which are by default empty and which can be used for execution of your tasks. These targets are usually executed before and after some main targets.

I have done this before, i.e. I override an empty target to deploy war file on build. Today, I overriden target -post-jar and added the following;

    <property name="wiki.dir" value="${basedir}/wikipages"/>    

    <target name="-post-jar" depends="-init-project">
        <antcall target="-xml-to-creole"/>
    </target>

    <target name="-xml-to-creole">
        <taskdef name="creole" classname="creole.ant.CreoleTask"
        classpath="${dist.jar}"/>
        <creole wikipage="${wiki.dir}/description"/>
    </target>

As you can see, I added a dependency on target -init-project, that’s because I wanted to reuse the property created in project.properties by NetBeans IDE. In my case, I reused¬† dist.jar property and created new one, wiki.dir. So, I have kept few samples in wikipages directory under the basedir, i.e. project’s root directory. Now, I define a task with the name creole which maps to CreoleTask.

Attribute wikipage maps to setter method of private variable wikipage. Its not a bean class, so its not necessary that the setter method relates to the private members. As shown above, I have passed ${wiki.dir}/description.txt as parameter to the setter method and that’s used by execute method. This method is overriden to run your own code. Its called automatically by Ant, whenever there’s tag present for the defined task.

Its just the beginning, exploration continues!

<property name=”wiki.dir” value=”${basedir}/wikipages”/><target name=”-post-jar” depends=”-init-project”>
<antcall target=”-creole-to-html”/>
</target><target name=”-xml-to-creole”>
<taskdef name=”creole” classname=”creole.ant.CreoleTask”
classpath=”${dist.jar}”/>
<creole wikipage=”${wiki.dir}/description”/>
</target>

WikiCreole, XML and NetBeans IDE

Aug 25, 2008

I had asked a simple question to the NetBeans Community, Are you fed up of using Wiki? Feedback I got was good, if not great. Almost everyone was in favor of the plug in, I wanted to create that would enable editing wikis within NetBeans IDE.

Sep 03, 2008

Just a week later, I thought of sharing some ideas and information, I had by discussing with people on IRC, mailing lists, surfing the web and by reflecting on my experiences working with Multiview editors, not necessarily in NetBeans IDE.

Today,

I just wanted to tell you that, I have researched about the project idea, sporadically for the past 1 year. However, I am once again interested in creating such plugin that would enable you to edit wikis within the NetBeans IDE.

So, stay tuned to this blog for more updates on the project. You may read the above blogs for history of the project, which has not even started yet.

Blog Series (Contd…)
This is the 3rd part of the blog series initiated last year at http://nbguru.wordpress.com/

Multiview Editors and NetBeans

Finally, code has started to run as expected. I am able to see multiview editor for file with extenstion “story” in NetBeans IDE. Well, I am talking about the code I downloaded from Vadiraj’s blog today and executing it almost as expected.

Vadiraj’s Blog

Few years back, Vadiraj Deshpande (Sun Employee) devoted time to make use of Geertjan’s Multiview API Sample, to extend the support for multiview editor supplied with it, by adding a new view using Visual Library.

Last year, I got interested in creating a multiview editor support for new type of files. So, I asked the question on NetBeans Forums, where Vadiraj replied by providing pointers to Geertjan’s blog series and his own blogs.

Multiview Editor Support

Today, I decided to give it a try and headed towards Vadiraj’s blog to get started. Hence, I downloaded the code of Geertjan’s Multiview API Sample and realized from Vadiraj’s blog, that it was supposed to build in NetBeans 5.5 IDE.

Manage NetBeans Platforms

However, as I had recently found out that NetBeans IDE allows you to manage NetBeans Platform for your NetBeans Module Projects. As per the blog, I made changes to the NetBeans Platform Config file of the project and made it possible to actually build the module that’s supposed to work in NetBeans 5.5 IDE against its modules clusters, while using any higher release of NetBeans IDE. This is only possible if you have the installation directories of previous releases.

I didn’t had 5.5, so I made use of 6.0 to build the module and possibly uncover any deprecated usage, if present. The splash screen flashed and soon, after ClassCastException was thrown;

public BookDataLoader() {
   super(BookDataLoader.class.getName());
}

The bold faced text is the culprit, building against NetBeans 6.0 causes this and to solve this issue, do the following change;

public BookDataLoader() {
   super("org.netbeans.modules.bookmultiview.BookDataObject");
}

I have just passed the FQN of the BookDataObject class as String to the super constructor and then, I ran the module in debug mode. It works smoothly now and then, I just drag-n-drop the supplied abc.story file to see the Multiview Editor in action. Indeed, I saw the same view as specified in Geertjan’s blog.

Click to enlarge!

Afterwards, I found out that I actually had NetBeans 5.5.1 IDE. So, I thought of giving it a try and hence, I added it as one of the platforms in 6.5 FCS I was using. I didn’t revert back changes made to the code as mentioned above and build the module. I was amazed to see the same result.

Backward Compatibility

This is really exciting to actually the run the module made using 5.5 release in higher releases of the same. You might also know that NetBeans API’s generally supports backward compatibility, so there should not be any problem if I build the code using NetBeans 6.5/6.7 IDE. So, I did that using 6.5 FCS and to my surprise, it worked as expected.

Although, Output Window opened in 6.5 FCS showed lots of exceptions (caused internally, not by the code) being thrown, warnings, etc. Still, the file got recognized as expected, multiview editor was opened, I was able to switch between the views using toggle buttons on its toolbar and it didn’t threw any exception.

Now, I will explore the code to experiment and learn the concepts, before beginning to create my own multiview editor for new type of files. Thanks, Vadiraj and Geertjan!

Stay tuned..