org.jibx.binding.model
Class BindingOrganizer

java.lang.Object
  extended by org.jibx.binding.model.BindingOrganizer

public class BindingOrganizer
extends java.lang.Object

Organizer for a set of bindings under construction. This tracks the individual bindings by default namespace or by associated object (to allow multiple bindings using a single namespace), and manages the relationships between the different bindings.

Author:
Dennis M. Sosnoski

Field Summary
private static java.lang.String BASE_PREFIX
          Base prefix used when no prefix set for namespace.
private  boolean m_addConstructors
           
private  boolean m_finished
          Bindings finalized flag.
private  boolean m_forceClasses
           
private  java.util.List m_formats
          List of format definitions in bindings.
private  boolean m_inBinding
           
private  java.util.List m_keyObjects
          List of unique key objects for bindings.
private  boolean m_nonamespaceUsed
          Flag for no-namespace namespace used from namespaced binding.
private  java.util.Set m_nsRootUris
          Set of namespace URIs needing to be defined in root binding.
private  java.util.Map m_objectBindings
          Map from object to binding holder for that namespace.
private  boolean m_outBinding
           
private  java.util.Map m_referenceUriBinding
          Map from namespace URIs referenced in bindings not using that namespace to first referencing binding.
private  boolean m_trackSource
           
private  boolean m_trimWhitespace
           
private  java.util.Map m_uriPrefixDefaults
          Map from namespace URI to default prefix for that namespace.
private  java.util.Map m_uriPrefixFixed
          Map from namespace URI to prefix, for bindings with prefix specified.
private static Logger s_logger
          Logger for class.
 
Constructor Summary
BindingOrganizer(boolean force, boolean track, boolean addcon, boolean in, boolean out, boolean trim)
          Constructor taking flags used with constructed bindings.
 
Method Summary
 BindingHolder addBinding(java.lang.Object obj, java.lang.String uri, java.lang.String prefix, boolean dflt)
          Add a binding to the set in use.
 void addBindingObject(java.lang.Object obj, BindingHolder holder)
          Associate a control object with an existing binding.
 void addDefaultPrefix(java.lang.String uri, java.lang.String pref)
          Set the default prefix for a namespace, if none already set.
 void addFormat(FormatElement format)
          Add a format definition to the binding.
(package private)  void addNamespaceUsage(BindingHolder hold, java.lang.String uri)
          Add usage of namespace for an element or attribute name in binding, assuring that the namespace definition will be in-scope for that binding.
 void addRootUris(java.util.Collection uris)
          Adds a collection of namespace URIs to be referenced at root binding level.
 void addTypeNameReference(BindingHolder hold, java.lang.String uri, java.lang.Object obj)
          Add reference from binding to a type name defined in the same or another binding.
private  java.util.Map buildDefaultPrefixes(UniqueNameSet prefset, java.util.Map nsdfltpref)
          Set the default prefixes to be used for namespaces.
private  void checkModifiable()
          Internal check method to verify that the collection of bindings is still modifiable.
 BindingHolder configureFiles(java.lang.String rootname, java.lang.String trgtpack, java.util.List pregens)
          Configure the names to be used for writing bindings to files.
 void forceBindingPrefix(java.lang.String prefix, BindingHolder holder)
          Force specified prefix to be used for binding.
private  void generatePrefix(java.lang.String uri, UniqueNameSet prefset, java.util.Map uridfltprefs)
          Generate prefix if not already set for namespace.
 BindingHolder getBinding(java.lang.Object obj)
          Get the binding associated with a particular control object and namespace, if defined.
 java.util.List getKeys()
          Get the list of binding key objects.
 BindingHolder getRequiredBinding(java.lang.Object obj)
          Get the binding associated with a particular control object.
private static boolean isAsciiAlpha(char chr)
          Check if a character is an ASCII alpha character.
private static boolean isAsciiAlphaNum(char chr)
          Check if a character is an ASCII alpha or numeric character.
private static boolean isAsciiNum(char chr)
          Check if a character is an ASCII numeric character.
static boolean isEqual(java.lang.Object a, java.lang.Object b)
          General object comparison method.
 java.util.Iterator iterateBindings()
          Iterate the collection of bindings.
 boolean validateBindings(BindingHolder root, java.io.File dir, ValidationContext vctx)
          Validate the constructed bindings.
 void writeBindings(java.io.File dir)
          Write the bindings to supplied destination path.
 
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 Logger s_logger
Logger for class.


BASE_PREFIX

private static final java.lang.String BASE_PREFIX
Base prefix used when no prefix set for namespace.

See Also:
Constant Field Values

m_forceClasses

private final boolean m_forceClasses

m_trackSource

private final boolean m_trackSource

m_addConstructors

private final boolean m_addConstructors

m_inBinding

private final boolean m_inBinding

m_outBinding

private final boolean m_outBinding

m_trimWhitespace

private final boolean m_trimWhitespace

m_uriPrefixFixed

private final java.util.Map m_uriPrefixFixed
Map from namespace URI to prefix, for bindings with prefix specified.


m_uriPrefixDefaults

private final java.util.Map m_uriPrefixDefaults
Map from namespace URI to default prefix for that namespace.


m_keyObjects

private final java.util.List m_keyObjects
List of unique key objects for bindings. Multiple key objects may be supplied for each binding, so this just tracks the one used when initially creating the binding.


m_objectBindings

private final java.util.Map m_objectBindings
Map from object to binding holder for that namespace.


m_referenceUriBinding

private final java.util.Map m_referenceUriBinding
Map from namespace URIs referenced in bindings not using that namespace to first referencing binding.


m_nsRootUris

private final java.util.Set m_nsRootUris
Set of namespace URIs needing to be defined in root binding.


m_formats

private final java.util.List m_formats
List of format definitions in bindings.


m_nonamespaceUsed

private boolean m_nonamespaceUsed
Flag for no-namespace namespace used from namespaced binding.


m_finished

private boolean m_finished
Bindings finalized flag.

Constructor Detail

BindingOrganizer

public BindingOrganizer(boolean force,
                        boolean track,
                        boolean addcon,
                        boolean in,
                        boolean out,
                        boolean trim)
Constructor taking flags used with constructed bindings.

Parameters:
force - force classes flag
track - track source flag
addcon - add constructors flag
in - input binding flag
out - output binding flag
trim - trim whitespace flag
Method Detail

addBinding

public BindingHolder addBinding(java.lang.Object obj,
                                java.lang.String uri,
                                java.lang.String prefix,
                                boolean dflt)
Add a binding to the set in use.

Parameters:
obj - object associated with binding (can be namespace URI, if only one binding per namespace)
uri - namespace URI (null if no namespace)
prefix - namespace prefix (null if not specified, empty string if unprefixed default namespace)
dflt - namespace is default for elements in binding flag
Returns:
created binding holder

getBinding

public BindingHolder getBinding(java.lang.Object obj)
Get the binding associated with a particular control object and namespace, if defined.

Parameters:
obj - object associated with binding
Returns:
binding holder, or null if not yet defined

getRequiredBinding

public BindingHolder getRequiredBinding(java.lang.Object obj)
Get the binding associated with a particular control object.

Parameters:
obj - object associated with binding (can be namespace URI, if only one binding per namespace)
Returns:
binding holder

addBindingObject

public void addBindingObject(java.lang.Object obj,
                             BindingHolder holder)
Associate a control object with an existing binding. The control object is added in addition to any other control objects for that binding, so that the binding will be returned when any of the control objects is used for look up.

Parameters:
obj - object associated with binding (can be namespace URI, if only one binding per namespace)
holder - binding holder

addDefaultPrefix

public void addDefaultPrefix(java.lang.String uri,
                             java.lang.String pref)
Set the default prefix for a namespace, if none already set.

Parameters:
uri - namespace URI
pref - prefix to be used (empty string if to be default namespace)

forceBindingPrefix

public void forceBindingPrefix(java.lang.String prefix,
                               BindingHolder holder)
Force specified prefix to be used for binding.

Parameters:
prefix -
holder -

isEqual

public static boolean isEqual(java.lang.Object a,
                              java.lang.Object b)
General object comparison method. Don't know why Sun hasn't seen fit to include this somewhere, but at least it's easy to write (over and over again).

Parameters:
a - first object to be compared
b - second object to be compared
Returns:
true if both objects are null, or if a.equals(b); false otherwise

checkModifiable

private void checkModifiable()
Internal check method to verify that the collection of bindings is still modifiable.


addNamespaceUsage

void addNamespaceUsage(BindingHolder hold,
                       java.lang.String uri)
Add usage of namespace for an element or attribute name in binding, assuring that the namespace definition will be in-scope for that binding. If a namespace is defined by one binding and referenced by another, or if it's referenced by more than one binding, it will be defined at the root binding level. This method is used as a callback from the binding holder code, in order to track usage of namespaces across all bindings.

Parameters:
hold - binding containing usage
uri - referenced namespace URI (null if no-namespace)

addTypeNameReference

public void addTypeNameReference(BindingHolder hold,
                                 java.lang.String uri,
                                 java.lang.Object obj)
Add reference from binding to a type name defined in the same or another binding.

Parameters:
hold - binding containing reference
uri - namespace URI for type name
obj - object associated with referenced binding

addFormat

public void addFormat(FormatElement format)
Add a format definition to the binding.

Parameters:
format -

iterateBindings

public java.util.Iterator iterateBindings()
Iterate the collection of bindings. null values may be present in the collection, due to bindings which have been eliminated.

Returns:
iterator

addRootUris

public void addRootUris(java.util.Collection uris)
Adds a collection of namespace URIs to be referenced at root binding level.

Parameters:
uris -

getKeys

public java.util.List getKeys()
Get the list of binding key objects.

Returns:
keys

isAsciiAlpha

private static boolean isAsciiAlpha(char chr)
Check if a character is an ASCII alpha character.

Parameters:
chr -
Returns:
alpha character flag

isAsciiNum

private static boolean isAsciiNum(char chr)
Check if a character is an ASCII numeric character.

Parameters:
chr -
Returns:
numeric character flag

isAsciiAlphaNum

private static boolean isAsciiAlphaNum(char chr)
Check if a character is an ASCII alpha or numeric character.

Parameters:
chr -
Returns:
alpha or numeric character flag

generatePrefix

private void generatePrefix(java.lang.String uri,
                            UniqueNameSet prefset,
                            java.util.Map uridfltprefs)
Generate prefix if not already set for namespace.

Parameters:
uri -
prefset -
uridfltprefs -

buildDefaultPrefixes

private java.util.Map buildDefaultPrefixes(UniqueNameSet prefset,
                                           java.util.Map nsdfltpref)

Set the default prefixes to be used for namespaces. As part of constructing the map it also checks for conflicts on specified prefixes for namespaces used at the root binding level. This is a complex process, due to the way namespaces work across bindings with abstract mapping references. Since an abstract mapping doesn't define a wrapper element, all namespaces used by the abstract mapping need to be declared on the containing element. In practice, this means that any namespaces in the binding defining the abstract mapping need to be merged into the set of namespaces in the binding making the reference, and these namespaces all must use distinct prefixes. Furthermore, a static analysis of the namespace usage may not be enough to detect conflicts in usage, since even if there are no conflicts in a set of binding definitions conflicts may be created when these bindings are referenced from other bindings (either through direct includes, or as precompiled bindings).

This code checks for any direct conflicts on namespaces with specified prefixes which are being defined in the root binding and throws an exception if such a conflict is found, and creates guaranteed-unique prefixes in cases where no prefixes are provided (either as specified values or as defaults). It does not prevent the user from either specifying prefixes or using default prefixes which may result in conflicts when the bindings are used by other bindings.

Parameters:
prefset - prefixes in use
nsdfltpref - map from namespace URI to default prefix (null value for namespace used as the default allowed)
Returns:
map from namespace URI to non-null and non-empty prefix (used for namespace declarations)

configureFiles

public BindingHolder configureFiles(java.lang.String rootname,
                                    java.lang.String trgtpack,
                                    java.util.List pregens)
Configure the names to be used for writing bindings to files. If only one binding has been defined, it just gets the supplied name. If multiple bindings have been defined, a single root binding is constructed which includes all the other bindings and defines namespaces for those bindings, all <format> definitions are moved to that root binding, and is is given the supplied name while the other bindings are given unique names within the same directory.

Parameters:
rootname - file name for root or singleton binding definition
trgtpack - target package for binding (null if unspecified)
pregens - pregenerated bindings to be included in root binding
Returns:
root or singleton binding holder

validateBindings

public boolean validateBindings(BindingHolder root,
                                java.io.File dir,
                                ValidationContext vctx)
                         throws java.io.IOException
Validate the constructed bindings. The configureFiles(String, String, List) must be called before this method is called, in order to complete the root binding definition.

Parameters:
root - root binding holder (returned by configureFiles(String, String, List))
dir - target directory for writing binding definitions
vctx - validation context to use
Returns:
true if valid, false if error
Throws:
java.io.IOException

writeBindings

public void writeBindings(java.io.File dir)
                   throws JiBXException,
                          java.io.IOException
Write the bindings to supplied destination path. The configureFiles(String, String, List) must be called before this method is called, in order to configure the file names and complete the root binding definition.

Parameters:
dir - target directory for writing binding definitions
Throws:
JiBXException
java.io.IOException


Project Web Site