Description:
A set of templates for reading and writing simple XML items into
data container objects. This example requires the Xerces XML
parser from xml.apache.org. The XML items must be of the form:
<item-name>
<property-name1>value</property-name1>
<property-name2>value</property-name2>
...
</item-name>
See books.xml for an example.
A simple text file, bookscheme.txt defines the meta data for the
system. This is parsed by the class XmlItemTools.
Take a look at the Book*.java files before
looking at the templates so that you can see what the intention
is. Notice how the generated code is easy to understand and
context specific - there is no need for a general solution. Just
change bookscheme.txt to change the fields in the system.
Generate Command:
> jostraca -t templates.txt bookscheme.txt
(you can also use generate.bat or generate.sh)
Generated Files:
Book.java
BookReader.java
BookSaver.java
BookTest.java
BookWriter.java
BookWriter.class
.jostraca/XmlItem_javaWriter.java
.jostraca/XmlItem_javaWriter.class
.jostraca/XmlItemReader_javaWriter.java
.jostraca/XmlItemReader_javaWriter.class
.jostraca/XmlItemSaver_javaWriter.java
.jostraca/XmlItemSaver_javaWriter.class
.jostraca/XmlItemTest_javaWriter.java
.jostraca/XmlItemTest_javaWriter.class
Experiments:
Try adding a new property to bookscheme.txt: Book=Title,Author,Date.
Update the books.xml and add this new property to the existing items.
Create a "work" folder and use the "-w" command line option to put the
"Writer" source and class files into the "work" folder so that they
can be kept out of the way.
Template Contents:
<% @conf
main.JostracaVersion = 0.4
%>
<% init!
XMLItemTools xit = new XMLItemTools();
xit.loadScheme( _getFirstUserArg() );
String itemName = xit.getItemName();
String itemNameSmall = itemName.toLowerCase();
String[] itemFields = xit.getItemFields();
int numFields = itemFields.length;
_setFileNameRoot( itemName );
%>
public class <%=itemName%> {
<% for(int fieldI = 0; fieldI < numFields; fieldI++) { %>
private String i<%=itemFields[fieldI]%>;
<% } %>
<% for(int fieldI = 0; fieldI < numFields; fieldI++) { %>
public String get<%=itemFields[fieldI]%>() {
return i<%=itemFields[fieldI]%>;
}
public void set<%=itemFields[fieldI]%>(String r<%=itemFields[fieldI]%>) {
i<%=itemFields[fieldI]%> = r<%=itemFields[fieldI]%>;
}
<% } %>
}
|
Generated File Contents:
Note: only one generated file is shown
public class Book {
private String iTitle;
private String iAuthor;
public String getTitle() {
return iTitle;
}
public void setTitle(String rTitle) {
iTitle = rTitle;
}
public String getAuthor() {
return iAuthor;
}
public void setAuthor(String rAuthor) {
iAuthor = rAuthor;
}
}
|
|