com.legstar.cob2xsd
Class Cob2Xsd

java.lang.Object
  extended by com.legstar.cob2xsd.Cob2Xsd
Direct Known Subclasses:
Cob2XsdIO

public class Cob2Xsd
extends Object

Implements the COBOL Structure to XML Schema translator. This is the API made available to invoke the COBOL to XML Schema translator from your own java code.

There are 6 steps involved:

All options are bundled in Cob2XsdModel instance that is received at construction time.

To invoke the translator, you normally call one of:

methods.

Any error encountered and recovered from is available in getErrorHistory().


Constructor Summary
Cob2Xsd()
          Default constructor.
Cob2Xsd(Cob2XsdModel model)
           
 
Method Summary
protected  void checkCobolSourceFile(File cobolSourceFile)
          make sure the COBOL file is valid.
protected  void checkTarget(File target)
          make sure the target, folder or file, is valid.
 String clean(String cobolSource)
          Remove any non COBOL Structure characters from the source.
protected  org.apache.ws.commons.schema.XmlSchema createXmlSchema(String encoding)
          Create an empty XML Schema.
 List<com.legstar.cobol.model.CobolDataItem> emitModel(org.antlr.runtime.tree.CommonTree ast)
          Generates a model from an Abstract Syntax Tree.
 org.apache.ws.commons.schema.XmlSchema emitXsd(List<com.legstar.cobol.model.CobolDataItem> cobolDataItems)
          Generate an XML Schema using a model of COBOL data items.
 List<String> getErrorHistory()
          list of errors encountered while translating.
 Cob2XsdModel getModel()
          The execution parameters for the COBOL to XML Schema utility.
protected static void getNonUniqueCobolNames(com.legstar.cobol.model.CobolDataItem cobolDataItem, List<String> cobolNames, List<String> nonUniqueCobolNames)
          If data item COBOL name is already used, we add it to the non unique list.
static List<String> getNonUniqueCobolNames(List<com.legstar.cobol.model.CobolDataItem> cobolDataItems)
          Create a list of COBOL names which are not unique.
 org.antlr.runtime.CommonTokenStream lex(String cleanedCobolSource)
          Apply the lexer to produce a token stream from source.
 org.antlr.runtime.tree.CommonTree parse(org.antlr.runtime.CommonTokenStream tokens)
          Apply Parser to produce an abstract syntax tree from a token stream.
 List<com.legstar.cobol.model.CobolDataItem> toModel(String cobolSource)
          Parses a COBOL source into an in-memory model.
 String translate(String cobolSource)
          Execute the translation from COBOL to XML Schema.
 String xsdToString(org.apache.ws.commons.schema.XmlSchema xsd)
          Serialize the XML Schema to a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cob2Xsd

public Cob2Xsd()
Default constructor.


Cob2Xsd

public Cob2Xsd(Cob2XsdModel model)
Parameters:
model - execution parameters for the COBOL to XML Schema utility
Method Detail

translate

public String translate(String cobolSource)
                 throws RecognizerException,
                        XsdGenerationException
Execute the translation from COBOL to XML Schema.

Parameters:
cobolSource - the COBOL source code
Returns:
the XML Schema
Throws:
RecognizerException - if COBOL recognition fails
XsdGenerationException - if XML schema generation process fails

checkCobolSourceFile

protected void checkCobolSourceFile(File cobolSourceFile)
                             throws IOException
make sure the COBOL file is valid.

Parameters:
cobolSourceFile - the COBOL source file
Throws:
IOException - if file cannot be located

checkTarget

protected void checkTarget(File target)
                    throws IOException
make sure the target, folder or file, is valid. We consider that target files will have extensions. Its ok for a target file not to exist but target folders must exist.

Parameters:
target - the target folder or file
Throws:
IOException - if file cannot be located

toModel

public List<com.legstar.cobol.model.CobolDataItem> toModel(String cobolSource)
                                                    throws RecognizerException
Parses a COBOL source into an in-memory model.

Parameters:
cobolSource - the COBOL source
Returns:
a list of root COBOL data items
Throws:
RecognizerException - if COBOL recognition fails

clean

public String clean(String cobolSource)
             throws CleanerException
Remove any non COBOL Structure characters from the source.

Parameters:
cobolSource - the raw source
Returns:
a cleaned up source
Throws:
CleanerException - if source cannot be read

lex

public org.antlr.runtime.CommonTokenStream lex(String cleanedCobolSource)
                                        throws RecognizerException
Apply the lexer to produce a token stream from source.

Parameters:
cleanedCobolSource - the source code (clean outside columns 7 to 72)
Returns:
an antlr token stream
Throws:
RecognizerException - if lexer failed to tokenize COBOL source

parse

public org.antlr.runtime.tree.CommonTree parse(org.antlr.runtime.CommonTokenStream tokens)
                                        throws RecognizerException
Apply Parser to produce an abstract syntax tree from a token stream.

Parameters:
tokens - the stream token produced by lexer
Returns:
an antlr abstract syntax tree
Throws:
RecognizerException - if source contains unsupported statements

emitModel

public List<com.legstar.cobol.model.CobolDataItem> emitModel(org.antlr.runtime.tree.CommonTree ast)
                                                      throws RecognizerException
Generates a model from an Abstract Syntax Tree.

Parameters:
ast - the abstract syntax tree produced by parser
Returns:
a list of root COBOL data items
Throws:
RecognizerException - if tree cannot be walked

emitXsd

public org.apache.ws.commons.schema.XmlSchema emitXsd(List<com.legstar.cobol.model.CobolDataItem> cobolDataItems)
Generate an XML Schema using a model of COBOL data items. The model is a list of root level items. From these, we only process group items (structures) with children.

Parameters:
cobolDataItems - a list of COBOL data items
Returns:
the XML schema

xsdToString

public String xsdToString(org.apache.ws.commons.schema.XmlSchema xsd)
                   throws XsdGenerationException
Serialize the XML Schema to a string.

If we are provided with an XSLT customization file then we transform the XMLSchema.

Parameters:
xsd - the XML Schema before customization
Returns:
a string serialization of the customized XML Schema
Throws:
XsdGenerationException - if customization fails

getNonUniqueCobolNames

public static List<String> getNonUniqueCobolNames(List<com.legstar.cobol.model.CobolDataItem> cobolDataItems)
Create a list of COBOL names which are not unique. This is useful when we build complex type names from the COBOL names. Complex type names need to be unique within a target namespace.

Parameters:
cobolDataItems - a list of root data items
Returns:
a list of COBOL names which are not unique

getNonUniqueCobolNames

protected static void getNonUniqueCobolNames(com.legstar.cobol.model.CobolDataItem cobolDataItem,
                                             List<String> cobolNames,
                                             List<String> nonUniqueCobolNames)
If data item COBOL name is already used, we add it to the non unique list. This recurse to the item children.

We don't add COBOL FILLERs as they are always considered non unique.

Parameters:
cobolDataItem - a COBOL data item
cobolNames - the list of all COBOL names used so far
nonUniqueCobolNames - the list of non unique COBOL names

createXmlSchema

protected org.apache.ws.commons.schema.XmlSchema createXmlSchema(String encoding)
Create an empty XML Schema.

If no targetNamespace, make sure there is no default namespace otherwise our complex types would be considered part of that default namespace (usually XML Schema namespace).

Parameters:
encoding - the character set used to encode this XML Schema
Returns:
a new empty XML schema using the model

getModel

public Cob2XsdModel getModel()
The execution parameters for the COBOL to XML Schema utility.

Returns:
the execution parameters for the COBOL to XML Schema utility

getErrorHistory

public List<String> getErrorHistory()
list of errors encountered while translating.

Most of these errors are warnings which were recovered from but still denote something that user should know about.

Returns:
the list of errors encountered while translating


Copyright © 2015 LegSem. All rights reserved.