org.dhmp.util.xml
Class Schema

java.lang.Object
  extended byorg.dhmp.util.xml.Schema
Direct Known Subclasses:
SQLSchema

public class Schema
extends java.lang.Object

Schema represents the XML Schema document and can be used to validate the syntax and several other constraints for HierarchicalMap. Use SchemaCollection to get the Schema class. For example:

SchemaCollection schemas = new SchemaCollection();
 Schema schema = schemas.get("file:/temp/test.xsd");
The validation for numbers are locale dependent. The locale is obtained from SchemaCollection's locale. Note that Schema always uses the locale assigned for parsing but the ResourceBundle's locale depends on availability of suitable resource. There are thee main methods: validate, replace and transform validate returns true or false depending on the content of HierarchicalMap passed to it. If this content obeys the structure defined by xml schema document passed the result will be true. replace also returns true or false checking against xsd, but it also substitutes the content of HierarchicalMap into corresponding data type defined in xsd. That is, if the xsd data type is long, the corresponding leaf will be converted to java.util.Long and so on. transform tries to fit the HierarchicalMap into valid structure as of xsd definition. the error message will be appended to HierarchicalMap at nearest node. If the element does not satisfies the xsd definition, the error message will be added under "_error" node.

Since:
0.7
Author:
hideyuki
See Also:
SchemaCollection

Field Summary
protected  boolean addErrorMessage
          If true, add error message during map transformation
protected  HierarchicalMap constraints
          The constraints for this XML Schema
protected  java.util.ResourceBundle defaultmessages
          ResourceBundle containing messages used for default message.
static int DO_NOT_REPLACE_VALUE
           
protected  boolean fixedConstraints
          control if constraints can be changed externally
static int FORMAT_VALUE
           
protected  java.util.Locale locale
          locale used for messages
protected  java.util.ResourceBundle messages
          Optional ResourceBundle containing error messages for this XML Schema
static int REPLACE_VALUE
           
static int REPLACEUSINGFORMAT_VALUE
           
protected  java.lang.String resourceLocation
          ResourceBundle location.
protected  java.lang.String schemaLocation
          The URL of the XML Schema for this SchemaCollection
static int TRANSFORM_VALUE
           
 
Method Summary
 boolean addErrorMessage()
          return the current value of addErrorMessage.
 HierarchicalMap getConstraint(java.lang.Object key)
          Returns a HierarchicalMap containing constraint for specific node.
 HierarchicalMap getConstraints()
          Returns a HierarchicalMap that represents the parsed schema.
 java.lang.String getDefaultMessage(java.lang.String key)
          Gets the corresponding default error message from ResourceBundle.
 java.util.Locale getLocale()
          Returns the locale used when parsing number or date values.
 java.lang.String getMessage(java.lang.String key)
          Gets the corresponding schema specific error message from ResourceBundle.
 java.util.Locale getMessageLocale()
          Returns the locale used when retrieving error message from ResourceBundle.
protected  java.lang.Object getNewValue(Validator val, java.lang.String name, java.lang.Object value, HierarchicalMap constraints, Schema schema, HierarchicalMap messages, int replace)
           
 java.lang.String getResourceLocation()
          Returns the location of ResourceBundle containing the error messages for this schema.
 java.lang.String getSchemaLocation()
          Returns the location of schema document containing the valid XML Schema Document.
protected  void handleElement(HierarchicalMap result, HierarchicalMap root, Validator val, java.lang.String name, java.lang.Object value, HierarchicalMap constraints, Schema schema, boolean addMessage)
           
 boolean hasFixedConstraints()
          Returs if this Schema's constraints is fixed or can be changed.
 boolean hasMessage()
          Returns true if the ResourceBundle containing the error message has successfully loaded.
 void init(HierarchicalMap paramMap)
          Initializes this class using the configuration passed.
 void init(java.lang.String config)
          Initializes this class using the configuration passed.
 boolean isUseDefaultMessage()
          Returs if this Schema is using default message.
 boolean replace(HierarchicalMap hmap)
          Validate the HierarchicalMap against current schema.
 boolean replace(HierarchicalMap hmap, java.lang.StringBuffer messages)
          Validate the HierarchicalMap against current schema and also returns the error message.
 boolean replace(java.lang.Object key, HierarchicalMap hmap)
          Validate the HierarchicalMap against subset of the current schema..
 boolean replace(java.lang.Object key, HierarchicalMap hmap, java.lang.StringBuffer messages)
          Validate the HierarchicalMap against subset of the current schema and also returns the error message.
 void setAddErrorMessage(boolean newvalue)
          If true, error messages are added as "_error" node during transform.
 void setFixedConstraints(boolean fixedConstraints)
          Sets if this Schema's constraints can be changed or fixed.
 void setUseDefaultMessage(boolean useDefaultMessage)
          Sets if default message should be appended when there is no error message defined.
 HierarchicalMap transform(HierarchicalMap original)
           
 HierarchicalMap transform(HierarchicalMap original, HierarchicalMap constraints)
           
 HierarchicalMap transform(HierarchicalMap root, HierarchicalMap original, HierarchicalMap constraints)
          Transform original map into structure define by constraints.
protected  HierarchicalMap transform(HierarchicalMap root, HierarchicalMap original, HierarchicalMap constraints, boolean addMessage)
           
 HierarchicalMap transform(java.lang.String key, HierarchicalMap original)
           
 boolean validate(HierarchicalMap hmap)
          Validate the HierarchicalMap against current schema.
 boolean validate(HierarchicalMap hmap, java.lang.StringBuffer messages)
          Validate the HierarchicalMap against current schema and also returns the error message.
 boolean validate(java.lang.Object key, java.lang.Object value)
          Validate the HierarchicalMap against subset of the current schema.
 boolean validate(java.lang.Object key, java.lang.Object value, java.lang.StringBuffer messages)
          Validate the HierarchicalMap against subset of the current schema and also returns the error message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

schemaLocation

protected java.lang.String schemaLocation
The URL of the XML Schema for this SchemaCollection


locale

protected java.util.Locale locale
locale used for messages


constraints

protected HierarchicalMap constraints
The constraints for this XML Schema


fixedConstraints

protected boolean fixedConstraints
control if constraints can be changed externally


messages

protected java.util.ResourceBundle messages
Optional ResourceBundle containing error messages for this XML Schema


defaultmessages

protected java.util.ResourceBundle defaultmessages
ResourceBundle containing messages used for default message.


addErrorMessage

protected boolean addErrorMessage
If true, add error message during map transformation


resourceLocation

protected java.lang.String resourceLocation
ResourceBundle location. Call getResourceLocation to check the current location and filename expected by schema matches the actual ResourceBundle


DO_NOT_REPLACE_VALUE

public static final int DO_NOT_REPLACE_VALUE
See Also:
Constant Field Values

REPLACE_VALUE

public static final int REPLACE_VALUE
See Also:
Constant Field Values

FORMAT_VALUE

public static final int FORMAT_VALUE
See Also:
Constant Field Values

REPLACEUSINGFORMAT_VALUE

public static final int REPLACEUSINGFORMAT_VALUE
See Also:
Constant Field Values

TRANSFORM_VALUE

public static final int TRANSFORM_VALUE
See Also:
Constant Field Values
Method Detail

init

public void init(java.lang.String config)
Initializes this class using the configuration passed. The configuration must be in XML format like "<Param defaultMessage=\"true\"/>" or "<Param fixed='true\'>". Note that first element must be "Param".

Valid configurations are (bold for default value) :

Parameters:
config - configuration used by this class.

init

public void init(HierarchicalMap paramMap)
Initializes this class using the configuration passed. The configuration must be defined as a leaf or HierarchicalMap containing Strings like "true" or "false". e.g. use map.put("fixed", "true") to set a parameter called "fixed". Valid configurations are (bold for default value) :

Parameters:
paramMap - HierarchicalMap containing the configuration used by this class.

validate

public boolean validate(HierarchicalMap hmap)
Validate the HierarchicalMap against current schema.

Parameters:
hmap - HierarchicalMap beeing validated.
Returns:
true if it is a valid HierarchicalMap

validate

public boolean validate(HierarchicalMap hmap,
                        java.lang.StringBuffer messages)
Validate the HierarchicalMap against current schema and also returns the error message.

Parameters:
hmap - HierarchicalMap beeing validated.
messages - error message during validation are appended.
Returns:
true if it is a valid HierarchicalMap

validate

public boolean validate(java.lang.Object key,
                        java.lang.Object value)
Validate the HierarchicalMap against subset of the current schema.

Parameters:
key - specify which subset of current schema should be used.
value - Object beeing validated.
Returns:
true if it is a valid HierarchicalMap
Since:
0.7.1

validate

public boolean validate(java.lang.Object key,
                        java.lang.Object value,
                        java.lang.StringBuffer messages)
Validate the HierarchicalMap against subset of the current schema and also returns the error message.

Parameters:
key - specify which subset of current schema should be used.
value - Object beeing validated.
messages - error message during validation are appended.
Returns:
true if it is a valid HierarchicalMap
Since:
0.7.1

replace

public boolean replace(HierarchicalMap hmap)
Validate the HierarchicalMap against current schema. The objects associated to each node are replaced by corresponding java classes according to the data type definiton from schema document.

Parameters:
hmap - HierarchicalMap beeing validated.
Returns:
true if it is a valid HierarchicalMap

replace

public boolean replace(HierarchicalMap hmap,
                       java.lang.StringBuffer messages)
Validate the HierarchicalMap against current schema and also returns the error message. The objects associated to each node are replaced by corresponding java classes according to the data type definiton from schema document.

Parameters:
hmap - HierarchicalMap beeing validated.
messages - error message during validation are appended.
Returns:
true if it is a valid HierarchicalMap

replace

public boolean replace(java.lang.Object key,
                       HierarchicalMap hmap)
Validate the HierarchicalMap against subset of the current schema.. The objects associated to each node are replaced by corresponding java classes according to the data type definiton from schema document.

Parameters:
key - specify which subset of current schema should be used.
hmap - HierarchicalMap beeing validated.
Returns:
true if it is a valid HierarchicalMap

replace

public boolean replace(java.lang.Object key,
                       HierarchicalMap hmap,
                       java.lang.StringBuffer messages)
Validate the HierarchicalMap against subset of the current schema and also returns the error message. The objects associated to each node are replaced by corresponding java classes according to the data type definiton from schema document.

Parameters:
key - specify which subset of current schema should be used.
hmap - HierarchicalMap beeing validated.
messages - error message during validation are appended.
Returns:
true if it is a valid HierarchicalMap

transform

public HierarchicalMap transform(HierarchicalMap original)

transform

public HierarchicalMap transform(java.lang.String key,
                                 HierarchicalMap original)

transform

public HierarchicalMap transform(HierarchicalMap original,
                                 HierarchicalMap constraints)

transform

public HierarchicalMap transform(HierarchicalMap root,
                                 HierarchicalMap original,
                                 HierarchicalMap constraints)
Transform original map into structure define by constraints. It tries to create a map according to the element order described by constraints (xsd - xml schema). Any error will be added under "_error" node at root. It also reports the key of first node under "_focus" at root. For validating propose, must check for the presence of "_error" node. Remember that it will add blank "_error" node, unless proper error message is defined inside xsd file (or set default message using setUseDefaultMessage method).

Parameters:
root - used internally to keep the root node of iteration.
original - map containing the values to be mapped to.
constraints - map containing the schema.
Returns:
new map with transformation applied.

transform

protected HierarchicalMap transform(HierarchicalMap root,
                                    HierarchicalMap original,
                                    HierarchicalMap constraints,
                                    boolean addMessage)

handleElement

protected void handleElement(HierarchicalMap result,
                             HierarchicalMap root,
                             Validator val,
                             java.lang.String name,
                             java.lang.Object value,
                             HierarchicalMap constraints,
                             Schema schema,
                             boolean addMessage)

getNewValue

protected java.lang.Object getNewValue(Validator val,
                                       java.lang.String name,
                                       java.lang.Object value,
                                       HierarchicalMap constraints,
                                       Schema schema,
                                       HierarchicalMap messages,
                                       int replace)

getResourceLocation

public java.lang.String getResourceLocation()
Returns the location of ResourceBundle containing the error messages for this schema.

Returns:
location of resource containg the error messages.
See Also:
SchemaCollection.get(URL, URL)

getSchemaLocation

public java.lang.String getSchemaLocation()
Returns the location of schema document containing the valid XML Schema Document.

Returns:
location of XML schema document.

getConstraints

public HierarchicalMap getConstraints()
Returns a HierarchicalMap that represents the parsed schema.

Returns:
parsed schema.

getConstraint

public HierarchicalMap getConstraint(java.lang.Object key)
Returns a HierarchicalMap containing constraint for specific node.

Parameters:
key - the key for the desired node.
Returns:
corresponding constraint.

setUseDefaultMessage

public void setUseDefaultMessage(boolean useDefaultMessage)
Sets if default message should be appended when there is no error message defined. Error messsages can be defined directly inside XSD file or in ResourceBundle. See tutorial at http://www.dhmp.org for further detail.

Parameters:
useDefaultMessage - true if default message should be used when there is no error message defined.

isUseDefaultMessage

public boolean isUseDefaultMessage()
Returs if this Schema is using default message.

Returns:
true if this Schema is set to use default message.

setFixedConstraints

public void setFixedConstraints(boolean fixedConstraints)
Sets if this Schema's constraints can be changed or fixed. If set to false, constraints can be retrieved using getConstraints() or getConstraint(key) and values like minInclusive, enumeration or pattern can be changed dynamically. Thought the consequence of any inconsistent value is unpredictable. Trying to get the constraint without setting this value to true will result in UnsupportedOperationException.

Parameters:
fixedConstraints - true if constraint is fixed (default).

hasFixedConstraints

public boolean hasFixedConstraints()
Returs if this Schema's constraints is fixed or can be changed.

Returns:
true if this Schema has fixed constraints.

getLocale

public java.util.Locale getLocale()
Returns the locale used when parsing number or date values.

Returns:
current locale used when validating HierarchicalMap.

getMessageLocale

public java.util.Locale getMessageLocale()
Returns the locale used when retrieving error message from ResourceBundle.

Returns:
current locale used for error messages when validating HierarchicalMap.

getMessage

public java.lang.String getMessage(java.lang.String key)
Gets the corresponding schema specific error message from ResourceBundle.

Parameters:
key - the key for the desired message.
Returns:
the error message for the given key.

getDefaultMessage

public java.lang.String getDefaultMessage(java.lang.String key)
Gets the corresponding default error message from ResourceBundle.

Parameters:
key - the key for the desired message.
Returns:
the error message for the given key.

hasMessage

public boolean hasMessage()
Returns true if the ResourceBundle containing the error message has successfully loaded.

Returns:
true if the ResourceBundle could be found and loaded.

setAddErrorMessage

public void setAddErrorMessage(boolean newvalue)
If true, error messages are added as "_error" node during transform. Default is false.

Parameters:
newvalue - boolean value to be set.

addErrorMessage

public boolean addErrorMessage()
return the current value of addErrorMessage. If true, error messages are added as "_error" node during transform.

Returns:
current value of addErrorMessage.