Appendix A. Code snippets

Table of Contents

Using generated tranformers
Running Host/Java Transformers
Running Host/XML Transformers
Running Host/JSON Transformers

Using generated tranformers

Running Host/Java Transformers

The COBOL Transformers Generator produces a set of java classes that you can easily use to turn mainframe payloads to java data objects.

This is sample code showing how you would use a generated mainframe to java transformer assuming you just generated a transformer class called com.legstar.test.coxb.lsfileae.DfhcommareaTransformers.

    /**
     * Transform host data and test java data object result.
     * 
     * @param hostBytes a byte array holding the mainframe payload
     * @throws HostTransformException if transforming fails
     */
    public void hostToJavaTransform(final byte[] hostBytes)
            throws HostTransformException {

        DfhcommareaTransformers transformers = new DfhcommareaTransformers();
        Dfhcommarea dfhcommarea = transformers.toJava(hostBytes);
        assertEquals(100, dfhcommarea.getComNumber());
        assertEquals("TOTO", dfhcommarea.getComPersonal().getComName().trim());
        assertEquals("LABAS STREET", dfhcommarea.getComPersonal()
                .getComAddress().trim());
        assertEquals("88993314", dfhcommarea.getComPersonal().getComPhone()
                .trim());
        assertEquals("100458", dfhcommarea.getComDate().trim());
        assertEquals("00100.35", dfhcommarea.getComAmount().trim());
        assertEquals("A VOIR", dfhcommarea.getComComment().trim());
    }

Conversely, you would produce a byte array with mainframe data from a java data object with code similar to this:

    /**
     * Creates a java data object and returns the host data result.
     * 
     * @return a byte array holding the mainframe payload
     * @throws HostTransformException if transforming fails
     */
    public byte[] javaToHostTransform() throws HostTransformException {
        Dfhcommarea dfhcommarea = new Dfhcommarea();
        dfhcommarea.setComNumber(100L);
        ComPersonal comPersonal = new ComPersonal();
        comPersonal.setComName("TOTO");
        comPersonal.setComAddress("LABAS STREET");
        comPersonal.setComPhone("88993314");
        dfhcommarea.setComPersonal(comPersonal);
        dfhcommarea.setComDate("100458");
        dfhcommarea.setComAmount("00100.35");
        dfhcommarea.setComComment("A VOIR");
        DfhcommareaTransformers transformers = new DfhcommareaTransformers();
        return transformers.toHost(dfhcommarea);
    }

Generated transformers use the default IBM01140 US EBCDIC character set for conversions.

Methods toHost and toJava also accept a character set name as a second parameter if you need to use a different one (just make sure your JRE charsets.jar supports your character set).

Running Host/XML Transformers

In addition to Host/Java transformers, you can generate Host/XML transformers by turning the xmlTransformers generation option on.

Using these transformers, this is sample code to turn host data to XML:


    /**
     * Transform host data and test XML result.
     * 
     * @param hostBytes a byte array holding the mainframe payload
     * @throws HostTransformException if transforming fails
     */
    public void hostToXmlTransform(final byte[] hostBytes)
            throws HostTransformException {

        DfhcommareaXmlTransformers transformers =
                new DfhcommareaXmlTransformers();
        StringWriter writer = new StringWriter();
        transformers.toXml(hostBytes, writer);
        assertEquals(
                "<?xml version=\"1.0\" encoding=\"UTF-8\" "
                        + "standalone=\"yes\"?>"
                        + "<Dfhcommarea xmlns="
                        + "\"http://legstar.com/test/coxb/lsfileae\">"
                        + "<ComNumber>100</ComNumber>"
                        + "<ComPersonal>"
                        + "<ComName>TOTO</ComName>"
                        + "<ComAddress>LABAS STREET</ComAddress>"
                        + "<ComPhone>88993314</ComPhone>"
                        + "</ComPersonal>"
                        + "<ComDate>100458</ComDate>"
                        + "<ComAmount>00100.35</ComAmount>"
                        + "<ComComment>A VOIR</ComComment>"
                        + "</Dfhcommarea>", writer.toString());
    }

This is code to turn XML into host data:


    /**
     * Turns an XML into host data.
     * 
     * @return a byte array holding the mainframe payload
     * @throws HostTransformException if transforming fails
     */
    public byte[] xmlToHostTransform() throws HostTransformException {
        StringReader reader = new StringReader(
                "<?xml version=\"1.0\" encoding=\"UTF-8\" "
                        + "standalone=\"yes\"?>"
                        + "<Dfhcommarea xmlns="
                        + "\"http://legstar.com/test/coxb/lsfileae\">"
                        + "<ComNumber>100</ComNumber>"
                        + "<ComPersonal>"
                        + "<ComName>TOTO</ComName>"
                        + "<ComAddress>LABAS STREET</ComAddress>"
                        + "<ComPhone>88993314</ComPhone>"
                        + "</ComPersonal>"
                        + "<ComDate>100458</ComDate>"
                        + "<ComAmount>00100.35</ComAmount>"
                        + "<ComComment>A VOIR</ComComment>"
                        + "</Dfhcommarea>");
        DfhcommareaXmlTransformers transformers =
                new DfhcommareaXmlTransformers();
        return transformers.toHost(new StreamSource(reader));
    }

Running Host/JSON Transformers

In addition to Host/Java transformers, you can generate Host/JSON transformers by turning the jsonTransformers generation option on.

Using these transformers, this is sample code to turn host data to JSON:

    /**
     * Transform host data and test JSON result.
     * 
     * @param hostBytes a byte array holding the mainframe payload
     * @throws HostTransformException if transforming fails
     */
    public void hostToJsonTransform(final byte[] hostBytes)
            throws HostTransformException {

        DfhcommareaJsonTransformers transformers =
                new DfhcommareaJsonTransformers();
        StringWriter writer = new StringWriter();
        transformers.toJson(hostBytes, writer);
        assertEquals("{\"ComNumber\":100,"
                + "\"ComPersonal\":"
                + "{\"ComName\":\"TOTO\","
                + "\"ComAddress\":\"LABAS STREET\","
                + "\"ComPhone\":\"88993314\"},"
                + "\"ComDate\":\"100458\","
                + "\"ComAmount\":\"00100.35\","
                + "\"ComComment\":\"A VOIR\"}",
                writer.toString());
    }

This is code to turn JSON into host data:


    /**
     * Turns JSON into host data.
     * 
     * @return a byte array holding the mainframe payload
     * @throws HostTransformException if transforming fails
     */
    public byte[] jsonToHostTransform() throws HostTransformException {
        StringReader reader = new StringReader(
                "{\"ComNumber\":100,"
                        + "\"ComPersonal\":"
                        + "{\"ComName\":\"TOTO\","
                        + "\"ComAddress\":\"LABAS STREET\","
                        + "\"ComPhone\":\"88993314\"},"
                        + "\"ComDate\":\"100458\","
                        + "\"ComAmount\":\"00100.35\","
                        + "\"ComComment\":\"A VOIR\"}");
        DfhcommareaJsonTransformers transformers =
                new DfhcommareaJsonTransformers();
        return transformers.toHost(reader);
    }