org.jibx.schema.codegen
Class CodeGen

java.lang.Object
  extended by org.jibx.schema.codegen.CodeGen

public class CodeGen
extends java.lang.Object

Code generator from schema definition.

Author:
Dennis M. Sosnoski

Nested Class Summary
private static class CodeGen.SchemaNameFilter
          File name pattern matcher.
private static class CodeGen.SchemaNameVisitor
          Visitor for checking element namespace usage in schema definitions.
 
Field Summary
private static long BLOCK_NAME_INHERIT_MASK
          Mask for schema elements which block name inheritance downward.
private static QName[] DEFAULT_REPLACEMENTS
          Default type replacements applied.
private  BindingOrganizer m_bindingDirectory
          Directory for constructed bindings.
private  java.util.ArrayList m_definitions
          Definitions to be generated (may be global schema definitions, or reused nested components with classes).
private  SchemasetCustom m_global
          Code generation customizations.
private  PackageOrganizer m_packageDirectory
          Package directory for generated classes.
private  java.io.File m_schemaDir
          Root directory for schemas.
private  java.net.URL m_schemaRoot
          Root URL for schemas.
private  java.io.File m_targetDir
          Target directory for code generation.
private  ValidationContext m_validationContext
          Context for loading and processing schemas.
private static org.apache.log4j.Logger s_logger
          Logger for class.
private static long TYPE_DEFINE_MASK
          Mask for schema elements which define a type.
private static long TYPE_DERIVE_MASK
          Mask for schema elements which derive from a type.
 
Constructor Summary
CodeGen(CodeGenCommandLine parms)
          Constructor.
CodeGen(SchemasetCustom custom, ValidationContext vctx)
          Constructor used by tests.
 
Method Summary
private static void addDefaultSubstitutions(SchemasetCustom custom)
          Add default type substitutions to set currently defined.
 void applyAndNormalize()
          Apply extensions and normalize all schemas.
private  java.lang.String assembleName(java.lang.String prefix, java.lang.String base)
          Assemble a name from a base name and an optional prefix.
private  void assignNames(java.lang.String prefix, GroupItem group, boolean force)
          Set the basic names to be used for a structure of items.
private  java.lang.String checkDirectName(Item item)
          Check if an item has an associated name.
private  void checkInline(DefinitionItem definition, int depth)
          Check if a global definition structure is to be inlined.
private  void compactGroups(GroupItem group)
          Compact group structures.
private  int computeComplexity(GroupItem group, int depth)
          Compute the complexity of a structure.
private  void convertToDefinitions(GroupItem group)
          Convert nested groups which are not inlined to freestanding definitions.
 boolean customizeSchemas()
          Validate and apply customizations to loaded schemas.
private  java.lang.String deriveName(Item item, boolean force)
          Derive the base name for an item.
private  SchemasetCustom findSchemaset(SchemaElement schema, SchemasetCustom custom)
          Find the most specific schemaset owning a schema.
 java.lang.String generate(boolean verbose)
          Generate the data model.
 BindingOrganizer getBindingDirectory()
          Get the binding directory.
 PackageOrganizer getPackageDirectory()
          Get the package directory used for code generation.
private  boolean isSimple(GroupItem group)
          Check if a group consists only of a single non-repeating item, which is not an enumeration.
private  SchemaElement[] load(java.util.ArrayList list)
          Load and validate the root schema list.
static void main(java.lang.String[] args)
          Run the binding generation using command line parameters.
private  boolean processExtensions()
          Process substitutions and deletions defined by extensions.
 void pruneDefinitions()
          Processes the schemas to remove unused global definitions.
private static boolean reportProblems(ValidationContext vctx)
          Report problems using console output.
private  void setName(QName qname, MappingElementBase mapping)
          Set the name and namespace URI for a concrete <mapping> binding component.
 void validateSchemas(SchemaElement[] schemas)
          Validate the schemas.
 void writeBindings(java.lang.String name, java.lang.String pack)
          Write the binding definitions file(s).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

s_logger

private static final org.apache.log4j.Logger s_logger
Logger for class.


DEFAULT_REPLACEMENTS

private static final QName[] DEFAULT_REPLACEMENTS
Default type replacements applied.


TYPE_DERIVE_MASK

private static final long TYPE_DERIVE_MASK
Mask for schema elements which derive from a type.


TYPE_DEFINE_MASK

private static final long TYPE_DEFINE_MASK
Mask for schema elements which define a type.


BLOCK_NAME_INHERIT_MASK

private static final long BLOCK_NAME_INHERIT_MASK
Mask for schema elements which block name inheritance downward.


m_global

private final SchemasetCustom m_global
Code generation customizations.


m_schemaRoot

private final java.net.URL m_schemaRoot
Root URL for schemas.


m_schemaDir

private final java.io.File m_schemaDir
Root directory for schemas.


m_targetDir

private final java.io.File m_targetDir
Target directory for code generation.


m_validationContext

private final ValidationContext m_validationContext
Context for loading and processing schemas.


m_packageDirectory

private PackageOrganizer m_packageDirectory
Package directory for generated classes.


m_definitions

private java.util.ArrayList m_definitions
Definitions to be generated (may be global schema definitions, or reused nested components with classes).


m_bindingDirectory

private BindingOrganizer m_bindingDirectory
Directory for constructed bindings.

Constructor Detail

CodeGen

public CodeGen(CodeGenCommandLine parms)
Constructor.

Parameters:
parms - command line parameters

CodeGen

public CodeGen(SchemasetCustom custom,
               ValidationContext vctx)
Constructor used by tests. This uses supplied schemas and skips writing to the file system.

Parameters:
custom -
vctx -
Method Detail

addDefaultSubstitutions

private static void addDefaultSubstitutions(SchemasetCustom custom)
Add default type substitutions to set currently defined.

Parameters:
custom -

findSchemaset

private SchemasetCustom findSchemaset(SchemaElement schema,
                                      SchemasetCustom custom)
Find the most specific schemaset owning a schema. If multiple matches are found which are not in line of containment the first match is returned and the conflict is reported as an error.

Parameters:
schema -
custom - schema set customization
Returns:
owning schemaset, null if none

validateSchemas

public void validateSchemas(SchemaElement[] schemas)
Validate the schemas.

Parameters:
schemas - schemas to be validated

load

private SchemaElement[] load(java.util.ArrayList list)
                      throws JiBXException,
                             java.io.IOException
Load and validate the root schema list.

Parameters:
list - resolvers for schemas to be loaded
Returns:
schemas in validation order
Throws:
JiBXException - on unrecoverable error in schemas
java.io.IOException - on error reading schemas

customizeSchemas

public boolean customizeSchemas()
Validate and apply customizations to loaded schemas.

Returns:
true if successful, false if error

processExtensions

private boolean processExtensions()
Process substitutions and deletions defined by extensions. This builds the cross-reference information for the global definition components of the schemas while removing references to deleted components.

Returns:
true if any changes to the schemas, false if not

applyAndNormalize

public void applyAndNormalize()
Apply extensions and normalize all schemas. This may be a multipass process, since applying extensions may create the opportunity for further normalizations and vice versa.


pruneDefinitions

public void pruneDefinitions()
Processes the schemas to remove unused global definitions.


checkDirectName

private java.lang.String checkDirectName(Item item)
Check if an item has an associated name. If the component associated with the item has a name, this just returns that name. The only exception is for inlined global type definitions, which are treated as unnamed.

Parameters:
item -
Returns:
name associated name, or null if none

deriveName

private java.lang.String deriveName(Item item,
                                    boolean force)
Derive the base name for an item. If not forced, the only time a name will be returned is when the item is a reference to a non-type definition. If forced, this will try other alternatives for names including the text "Enumeration" for an enumeration group, the base type name for a type derivation, the schema type name for a value of a schema type, or finally the schema component element name.

Parameters:
item -
force - name forced flag
Returns:
name (null if to use inherited name when force == false)

compactGroups

private void compactGroups(GroupItem group)
Compact group structures. This eliminates redundant groupings, in the form of groups with only one child, which child is a group referencing the same schema component as the parent group, from the data structure representation.

Parameters:
group -

assembleName

private java.lang.String assembleName(java.lang.String prefix,
                                      java.lang.String base)
Assemble a name from a base name and an optional prefix. If the prefix is supplied, this first converts the base name to embedded form, then appends the prefix.

Parameters:
prefix -
base -
Returns:
name

assignNames

private void assignNames(java.lang.String prefix,
                         GroupItem group,
                         boolean force)
Set the basic names to be used for a structure of items. For named components of the schema definition the names used are simply the converted XML local names, for other components more complex rules apply (see deriveName(Item,boolean). This method calls itself recursively to handle nested groups.

Parameters:
prefix - text to be prefixed to names within inlined group (null if none)
group - item group to be assigned
force - force name derivation flag

computeComplexity

private int computeComplexity(GroupItem group,
                              int depth)
Compute the complexity of a structure. In order to find the complexity of a structure all items of the structure must first be checked for inlining, which in turn requires checking their complexity. That makes this method mutually recursive with checkInline(DefinitionItem, int).

Parameters:
group -
depth - nesting depth
Returns:
complexity (0, 1, or 2 for anything more than a single value)

isSimple

private boolean isSimple(GroupItem group)
Check if a group consists only of a single non-repeating item, which is not an enumeration.

Parameters:
group -
Returns:
true if simple group, false if repeated, multiple, or enumeration

convertToDefinitions

private void convertToDefinitions(GroupItem group)
Convert nested groups which are not inlined to freestanding definitions. This calls itself recursively to process nested groups, except those nested within groups converted to definitions.

Parameters:
group -

checkInline

private void checkInline(DefinitionItem definition,
                         int depth)
Check if a global definition structure is to be inlined. This method is mutually recursive with computeComplexity(GroupItem, int). The two methods together determine the inlining status of all items.

Parameters:
definition -
depth - nesting depth

setName

private void setName(QName qname,
                     MappingElementBase mapping)
Set the name and namespace URI for a concrete <mapping> binding component. This is the same logic as used in the StructureClassHolder equivalent.

Parameters:
qname - qualified name to be set (null if none)
mapping -

generate

public java.lang.String generate(boolean verbose)
Generate the data model. This first builds a representation of all the data items from the schema definitions, then determines which items can be inlined and which need separate class representations.

Parameters:
verbose -
Returns:
root package for binding

getBindingDirectory

public BindingOrganizer getBindingDirectory()
Get the binding directory.

Returns:
directory

writeBindings

public void writeBindings(java.lang.String name,
                          java.lang.String pack)
                   throws JiBXException,
                          java.io.IOException
Write the binding definitions file(s). This method can only be used after generate(boolean) is called.

Parameters:
name -
pack -
Throws:
JiBXException
java.io.IOException

reportProblems

private static boolean reportProblems(ValidationContext vctx)
Report problems using console output. This clears the problem list after they've been reported, to avoid multiple reports of the same problems.

Parameters:
vctx -
Returns:
true if one or more errors, false if not

getPackageDirectory

public PackageOrganizer getPackageDirectory()
Get the package directory used for code generation.

Returns:
directory

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Run the binding generation using command line parameters.

Parameters:
args -
Throws:
java.lang.Exception


Project Web Site