Here we give an example on how to generate objective-c header files from java source code. Before continuing, please make sure that you have gone through the "getting started" tutorial in java". In this example we have an annotated class definition in java that we want to move to objective-c. This tutorial will make use of the internaltranslator.cocoa in the java version of s.im.pl serialization to generate code for objective-c header files. It is not necessary that you use this translator from java, developers can write their own source code files and still use the objective-c version of s.im.pl serialization. Since java supports "annotations" (and objective-c doesn't) over class and field definitions it becomes easier to work with java and generate code for header files in objective-c. Since objective-c does not support "annotations" we will also show how you can serialize s.im.pl serialization's internal data structures which will be consumed by your objective-c code.
To view this tutorial you will need to download the entire simplTutorials,
simplTranslators and
ecologylabFundamental projects.
Access to the ecologylab fundamental project source is available
through anonymous SVN access (user: anonymous, password: anonymous).
The source for this tutorial is
located under
/trunk/simplTutorials/src/tutorials/
Below is a set of annotated Java classes similar to the one presented in the Java "getting started" tutorial. We are again using Rss feeds as the case study for this tutorial. Below are three classes decorated with Java annotations. "Rss" is the composite class which holds the version number and channel object. The channel object holds some attributes but more importantly it contains a list of items. The Item object again holds a list of the categorySets. Annotations on each object defines the semantics of how the attribute is going to be translated.
If you are having trouble understanding the annotations please refer to the technical report and the getting started tutorial in java
//Rss.java
|
//Channel.java
|
//Item.java
|
We create the translation scope similar to what was done in the java getting started tutorial. We wrap the three annotated Java classes into a translation scope object and return it to the calling program. Easy!
//RssTranslations.java
|
Now we come to the fun part of this tutorial. We will generate objective-c code right from our Java program which we can then move to our objective-c project and start serializing and deserializing XML files with very few lines of code.
We create a CocoaTranlator object and a TranslationScope object, which is fairly straight forward. We then call the translateToObjC function on our CocoaTranslator object by supplying the destination folder and the list of classes for which we want to generate objective-c header files. In the second part, we also serialize our translation scope object which will be used by our objective-c version of ecoloyglab.xml.
public class TranslatorTutorial
|