Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
Library | Service | Contents | Index

Using XML Java Class Generator, 7 of 8

## How to Run the XML Java Class Generator Samples in sample/

To run the sample program, use the following command from the sample/ directory:

    make target 'demo'

Then follow these three steps to run the sample code:

1. Compile and run `SampleMain` to generate the Java source files.

   Set the CLASSPATH to contain 'classgen.jar', 'xmlparserv2.jar', and current directory.

   - javac SampleMain.java

   - java SampleMain -root WIDL Widl.dtd

   or

   - java SampleMain Widl.xml

2. Compile the Java source files generated by SampleMain:

   - BINDING.java

   - CONDITION.java

   - REGION.java

   - SERVICE.java

   - VARIABLE.java

   - WIDL.java.

3. Run the test application to print the XML Document.

   - javac TestWidl.java

   - java TestWidl

### XML Java CLass Generator, DTD Example 1: DTD Input -- widl.dtd

The following example, `widl.ddt` is the DTD file referenced by the XML file `widl.xml`.

    <!ELEMENT WIDL ( SERVICE | BINDING )* >
    <!ATTLIST WIDL
         NAME       CDATA   #IMPLIED
         VERSION (1.0 | 2.0 | ...) "2.0"
         BASEURL    CDATA   #IMPLIED
         OBJMODEL (wmdom | ...) "wmdom"
    >

    <!ELEMENT SERVICE EMPTY>
    <!ATTLIST SERVICE
         NAME       CDATA   #REQUIRED
         URL        CDATA   #REQUIRED
         METHOD (Get | Post) "Get"
         INPUT      CDATA   #IMPLIED
         OUTPUT     CDATA   #IMPLIED
    >

    <!ELEMENT BINDING ( VARIABLE | CONDITION | REGION )* >
    <!ATTLIST BINDING
         NAME       CDATA   #REQUIRED
         TYPE (Input | Output) "Output"
    >

    <!ELEMENT VARIABLE EMPTY>
    <!ATTLIST VARIABLE
         NAME       CDATA   #REQUIRED
         TYPE (String | String1 | String2) "String"
         USAGE (Function | Header | Internal) "Function"
         VALUE      CDATA   #IMPLIED
         MASK       CDATA   #IMPLIED
         NULLOK    (True | False) #REQUIRED
    >

    <!ELEMENT CONDITION EMPTY>
    <!ATTLIST CONDITION
         TYPE  (Success | Failure | Retry) "Success"
         REF        CDATA   #REQUIRED
         MATCH      CDATA   #REQUIRED
         SERVICE    CDATA   #IMPLIED
    >

    <!ELEMENT REGION EMPTY>
    <!ATTLIST REGION
         NAME       CDATA   #REQUIRED
         START      CDATA   #REQUIRED
         END        CDATA   #REQUIRED
    >

### XML Java CLass Generator, XML Example 1: XML Input -- widl.xml

This XML file inputs SampleMain.java:

    <?xml version="1.0"?>
    <!DOCTYPE WIDL SYSTEM "Widl.dtd">
    <WIDL>
        <SERVICE NAME="sname" URL="surl"/>
        <BINDING NAME="bname"/>
    </WIDL>

### XML Java CLass Generator, Java Example 1: SampleMain.java

`SampleMain.java` generates a Java source file based on the input DTD, `widl.ddt` or `widl.xml`.

    import java.io.*;
    import java.net.*;
    import oracle.xml.parser.v2.*;
    import oracle.xml.classgen.*;
    import org.w3c.dom.Element;

    /**
     * Sample Class to uses XML Class Generator
     */
    public class SampleMain
    {

       public SampleMain()
       {
       }

       public static void main (String args[])
       {
           // validate arguments
          if (args.length < 1)
          {
             System.out.println("Usage: java SampleMain "+
                                "[-root <rootName>] <fileName>");
             System.out.println("fileName\t   Input file, XML document or " +
                                "external DTD file");
             System.out.println("-root <rootName>   Name of the root Element " +
                                "(required if the input file is an external DTD)");
             return ;
          }
          try // to open the XML File/ External DTD File
          {
             // instantiate the parser
             DOMParser   parser = new DOMParser();
             XMLDocument doc    = null;
             DTD         dtd    = null;

             if (args.length == 3)
             {
                parser.parseDTD(fileToURL(args[2]), args[1]);
                dtd = (DTD)parser.getDoctype();
             }
             else
             {
                parser.setValidationMode(true);
                parser.parse(fileToURL(args[0]));
                doc = parser.getDocument();
                dtd = (DTD)doc.getDoctype();
             }

             String doctype_name = null;

             if (args.length == 3)
             {
                doctype_name = args[1];
             }
             else
             {
                /* get the Root Element name from the XMLDocument*/
                doctype_name = doc.getDocumentElement().getTagName();
             }

             // generate the Java files...
             ClassGenerator generator = new ClassGenerator();

             // set generate comments to true
             generator.setGenerateComments(true);
             // set output directory
             generator.setOutputDirectory(".");
             // set validating mode to true
             generator.setValidationMode(true);

             // generate java src
             generator.generate(dtd, doctype_name);

          }
          catch (Exception e)
          {
             System.out.println ("XML Class Generator: Error " + e.toString());
             e.printStackTrace();
          }
       }

       static public URL fileToURL(String sfile)
       {
          File file = new File(sfile);
          String path = file.getAbsolutePath();
          String fSep = System.getProperty("file.separator");
          if (fSep != null && fSep.length() == 1)
             path = path.replace(fSep.charAt(0), '/');
          if (path.length() > 0 && path.charAt(0) != '/')
             path = '/' + path;
          try
          {
             return new URL("file", null, path);
          }
          catch (java.net.MalformedURLException e)
          {
             /* According to the spec this could only happen if the file
                protocol were not recognized. */
             throw new Error("unexpected MalformedURLException");
          }
       }
    }

### XML Java CLass Generator, Java Example 2: TestWidl.java

TestWidl.java constructs an XML document using the Java source files generated by SampleMain.java.

    import oracle.xml.classgen.*;
    import oracle.xml.parser.v2.*;

    public class TestWidl
    {
       public static void main (String args[])
       {
          try
          {
             WIDL w1 = new WIDL();
             DTD dtd = w1.getDTDNode();
             w1.setNAME("WIDL1");
             w1.setVERSION(WIDL.VERSION_1_0);

             SERVICE s1 = new SERVICE("Service1", "Service_URL");
             s1.setINPUT("File");
             s1.setOUTPUT("File");

             BINDING b1 = new BINDING("Binding1");
             b1.setTYPE(BINDING.TYPE_INPUT);

             BINDING b2 = new BINDING("Binding2");
             b2.setTYPE(BINDING.TYPE_OUTPUT);

             VARIABLE v1 = new VARIABLE("Variable1", VARIABLE.NULLOK_FALSE);
             v1.setTYPE(VARIABLE.TYPE_STRING);
             v1.setUSAGE(VARIABLE.USAGE_INTERNAL);
             v1.setVALUE("value");

             VARIABLE v2 = new VARIABLE("Variable2", VARIABLE.NULLOK_TRUE);
             v2.setTYPE(VARIABLE.TYPE_STRING1);
             v2.setUSAGE(VARIABLE.USAGE_HEADER);

             VARIABLE v3 = new VARIABLE("Variable3", VARIABLE.NULLOK_FALSE);
             v3.setTYPE(VARIABLE.TYPE_STRING2);
             v3.setUSAGE(VARIABLE.USAGE_FUNCTION);
             v3.setMASK("mask");

             CONDITION c1 = new CONDITION("CRef1", "CMatch1");
             c1.setSERVICE("Service1");
             c1.setTYPE(CONDITION.TYPE_SUCCESS);

             CONDITION c2 = new CONDITION("CRef2", "CMatch2");
             c2.setTYPE(CONDITION.TYPE_RETRY);

             CONDITION c3 = new CONDITION("CRef3", "CMatch3");
             c3.setSERVICE("Service3");
             c3.setTYPE(CONDITION.TYPE_FAILURE);

             REGION r1 = new REGION("Region1", "Start", "End");
             b1.addNode(r1);
             b1.addNode(v1);
             b1.addNode(c1);
             b1.addNode(v2);
             b2.addNode(c2);
             b2.addNode(v3);
             w1.addNode(s1);
             w1.addNode(b1);
             w1.addNode(b2);
             w1.validateContent();
             w1.print(System.out);
          }
          catch (Exception e)
          {
             System.out.println(e.toString());
             e.printStackTrace();
          }
       }
    }

### XML Java CLass Generator, XML Example 2: DTD Input -- widl.out

This XML file, widl.out, is constructed and printed by `TestWidl.java.`

    <?xml  version = '1.0' encoding = 'ASCII'?>
    <!DOCTYPE WIDL SYSTEM "file:/oracore/java/xml/ORACORE_MAIN_SOLARIS_990115_
    XMLCLASSGEN/sample/out/WIDL.dtd">
    <WIDL NAME="WIDL1" VERSION="1.0">
       <SERVICE NAME="Service1" URL="Service_URL" INPUT="File" OUTPUT="File"/>
       <BINDING NAME="Binding1" TYPE="Input">
          <REGION NAME="Region1" START="Start" END="End"/>
          <VARIABLE NAME="Variable1" NULLOK="False" TYPE="String" USAGE="Internal"
    VALUE="value"/>
          <CONDITION REF="CRef1" MATCH="CMatch1" SERVICE="Service1" TYPE="Success"/>
          <VARIABLE NAME="Variable2" NULLOK="True" TYPE="String1" USAGE="Header"/>
       </BINDING>
       <BINDING NAME="Binding2" TYPE="Output">
          <CONDITION REF="CRef2" MATCH="CMatch2" TYPE="Retry"/>
          <VARIABLE NAME="Variable3" NULLOK="False" TYPE="String2" USAGE="Function"
    MASK="mask"/>
       </BINDING>
    </WIDL>

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index