Xvcl And Java
CjOS Project @ cjos.sourceforge.net
Index
Bind Pages
Cjos Library
Cjos Pages
Cjos News Pages
Eric Pages
Introduction To Smart Api
Osgi Pages
Registry Api Pages
Smart Api Pages
Xvcl Pages
CjOS Project
Summary Page
Mailing List
Download
CVS
JOS Technical Edition
Support This Project
Xvcl Manual; Xvcl Getting Started; [ Cjos Audience]

XVCL Manual: Writing with XVCL

Chapter 1: Getting Started


Section 1C - Java Programming Language

Summary

In this section, we reconstruct a simple Java application from the JOS Project.

 Package: org.jos.demo
 Class:
   Demo: Runnable application.
   Main: Main-Class
   Constant: An interface
 CVS Module: eclipse/demo-eclipse
 SPC Location: eclipse/demo-eclipse/module/source

Main class

 ----- Main.java -----
 package org.jos.demo;

public class Main { public static void main( String[] args ) { Demo app = new Demo(); app.run(); }

private Main() { } }

The following x-frame creates Main.java.

 ----- main.xvcl -----
 <?xml version="1.0"?>
 <!DOCTYPE x-frame SYSTEM "default">
 <x-frame name="main" outfile="Main.java">
 package org.jos.demo;

public class Main { public static void main( String[] args ) { Demo app = new Demo(); app.run(); }

private Main() { } } </x-frame>

Brief explanation: XVCL is based upon XML.

 <?xml version="1.0"?>
  • An XVCL is an XML document. This is required by XML.
 <!DOCTYPE x-frame SYSTEM "default">
  • An XML document has a document type. To insist that an x-frame conform to the XVCL document type definition (DTD), this is required.
 <x-frame name="main" outfile="Main.java">
  • An x-frame is the root element of an XVCL document. The name attribute is documentation. The outfile attribute is the name of the target file generated by XVCL Processor.

Main-Class: org.jos.demo.Main

In the manifest file for the jar, we declare org.jos.demo.Main as our main class.

Demo class

This class displays a message from the Constant interface.

 ----- Demo.java -----
 package org.jos.demo.Demo;

public class Demo implements Runnable { public Demo() { } public void run() { println( Constant.PROGRAM_NAME ); println( Constant.MESSAGE ); } public void println( String v ) { System.out.println( v ); } }

The following x-frame creates Demo.java.

 ----- demo.xvcl -----
 <?xml version="1.0"?>
 <!DOCTYPE x-frame SYSTEM "default">
 <x-frame name="demo" outfile="Demo.java">
 package org.jos.demo.Demo;

public class Demo implements Runnable { public Demo() { } public void run() { println( Constant.PROGRAM_NAME ); println( Constant.MESSAGE ); } public void println( String v ) { System.out.println( v ); } } </x-frame>

Constant interface

This interface defines constant values for this package.

 ----- Constant.java -----
 package org.jos.demo.Constant;

public interface Constant { public static final String PROGRAM_NAME = "Demo Program"; public static final String MESSAGE = "Some message."; }

The following x-frame creates Constant.java.

 ----- constant.xvcl -----
 <?xml version="1.0"?>
 <!DOCTYPE x-frame SYSTEM "default">
 <x-frame name="constant" outfile="Constant.java">
 package org.jos.demo.Constant;

public interface Constant { public static final String PROGRAM_NAME = "Demo Program"; public static final String MESSAGE = "Some message."; } </x-frame>

Running demo.jar

We compile the Java source code. We create demo.jar, with the Main-Class: entry in its manifest file. We run demo.jar:

 $> java -jar demo.jar

Result:

 Demo Program
 Some message.

Starting with XVCL

In this manual, we are using Eclipse 3.1 as an interactive development environment (IDE). We are using the XVCL 3.2.1 Plugin for Eclipse 3.1.

  1. Create demo-eclipse project
    1. New | Project | Java Project
    2. Yes, separate source and binary directories.

  2. Create module/source directory
    1. New | Simple | Directory

Procedure: Writing a Java program

The following procedure illustrates how to write a Java program.

The following x-frame generates the org.jos.demo package.

 ----- org-jos-demo.xvcl -----
 <?xml version="1.0"?>
 <x-frame name="org-jos-demo" outdir="org/jos/demo">
 <adapt x-frame="constant"/>
 <adapt x-frame="demo"/>
 <adapt x-frame="main"/>
 </x-frame>

The following x-frame generates a product.

 ----- spc.xvcl -----
 <?xml version="1.0"?>
 <!DOCTYPE x-frame SYSTEM "default">
 <x-frame name="spc" outdir="src" outfile="spc.txt">
 <adapt x-frame="org-jos-demo"/>
 </x-frame>

Procedure: Repackaging a Java program

A class is relocateable when the name of a package is a variable. We write

 package <value-of expr="?@package?"/>;

rather than

 package org.jos.demo;

In our package-level frame, org-jos-demo.xvcl, we set the name of the package.

 <set var="package" value="org.jos.demo"/>

In another package-level frame, we can use a different package name and include main.xvcl.

 ----- custom-package.xvcl -----
 <?xml version="1.0"?>
 <!DOCTYPE x-frame SYSTEM "default">
 <x-frame name="custom-package" outdir="custom/package">
 <set var="package" value="custom.package"/>
 <set var="program-name" value="Demo Program"/>
 <set var="message" value="Some message."/>
 <adapt x-frame="constant"/>
 <adapt x-frame="demo"/>
 <adapt x-frame="main"/>
 </x-frame>

Procedure: Renaming a Java class

A class is renameable when the name of a class is a variable. We write

 public class <value-of expr="?@classname?"/> {
 }

instead of

 public class Main {
 }

Constructor

We write

   public <value-of expr="?@classname?">() {
   }

instead of

   public Demo() {
   }

Procedure: Replacing constants

We write

   println( "<value-of expr="?@program-name?"/>" );
   println( "<value-of expr="?@message?"/>" );

instead of

   println( Constant.PROGRAM_NAME );
   println( Constant.MESSAGE );

----- custom-package.xvcl ----- <?xml version="1.0"?> <!DOCTYPE x-frame SYSTEM "default"> <x-frame name="custom-package" outdir="custom/package"> <set var="package" value="custom.package"/> <set var="program-name" value="Demo Program"/> <set var="message" value="Some message."/> <adapt x-frame="constant"/> <adapt x-frame="demo"/> <adapt x-frame="main"/> </x-frame>

Procedure: Declaring an import section

We write

 <break name="import">
 import java.util.Hashtable;
 import java.util.Vector;
 </break>

Procedure: Declaring a private field section

We write

 <break name="private">
   private String name;
   private int weight;
 </break>


Research: Basic Java Components

Introduction

This article describes a Java Class prototype.

SPC

 -- spc.xml
 <x-frame name="spc" language="java" outdir="build/source">
 <insert break="license">
 Copyright (C) 2007 Author All Rights Reserved.
 </insert>
 <adapt x-frame="demo.xml"/>
 </x-frame>

demo.xml

 -- demo.xml
 <x-frame name="demo" outfile="Demo.java"/>
 <set var="classname" value="Demo"/>
 <set var="extends" value="Component"/>
 <set-multi var="implements" value="Clonable,Runnable"/>
 <set-multi var="property-name" value="p1,p2,p3"/>
 <set-multi var="property-type" value="String,int,boolean"/>
 <adapt x-frame="java-class.xml">
 </adapt>
 </x-frame>

Java Class

 -- java-class.xml
 <x-frame name="java-class" language="java">
 <break name="license"/>

<break name="package"/>

<break name="import"/>

<adapt x-frame="?@signature?"/> { <break name="static"/>

<if-def var="property-name"> <while using="property-name,property-type"> /** * Gets <value-of expr="?@property-name?"/> property. */ public <value-of expr="?@property-type?"/> get<value-of expr="?@property-name?"/>() { return <value-of expr="?@property-name?"/> }

/** * Sets <value-of expr="?@property-name?"/> property. */ public void set<value-of expr="?@property-name?"/>( <value-of expr="?@property-type?"/> v) { <value-of expr="?@property-name?"/> = v; }

private <value-of expr="?@property-type?"/> get<value-of expr="?@property-name?"/>; </while> </if-def>

<break name="method"/>

<break name="private"/> } </x-frame>

class signature

 -- signature-class.xml
 <x-frame name="signature-class">
   <if-def var="class-access">
     <value-of expr="class-access"/>
   </if-def>
   class <value-of expr="?@classname?"/>
   <if-def var="extends">
     extends <value-of expr="?@extends?"/>
   </if-def>
   <if-def var="implements">
     implements <value-of "?@implements"/>
   </if-def>
 </x-frame>

interface signature

 -- signature-class.xml
 <x-frame name="interface-class">
   <if-def var="class-access">
     <value-of expr="class-access"/>
   </if-def>
   interface <value-of expr="?@classname?"/>
   <if-def var="extends">
     extends <value-of "?@extends"/>
   </if-def>
 </x-frame>