ehsbe CommonsTM
Version 1.1.0rc3

com.ehsbe.commons.action
Class ActionFactory

java.lang.Object
  extended by com.ehsbe.commons.action.ActionFactory

public class ActionFactory
extends Object

Parses in [composite] actions and action lists from an XML stream source.

The correctness of the xml stream is not completely enforced (i.e. not validated via the action DTD), since the developers usually know, what they do - so we rather prefer to get a little bit better performance.

If you are unsure about the correctness of your xml source, you may validate it before you putting it into the production.

The following tags/attributes are subject of translation, if an appropriate resource bundle has been submitted and the appropriate tag/attribute has not been set:

Version:
$Revision: 439 $
Author:
Jens Elkner
See Also:
Translator

Constructor Summary
ActionFactory()
           
 
Method Summary
static HashMap<String,Object> getAttributes(XMLStreamReader reader)
          Get all attributes pairs from the given reader.
static Action getDefaultAction(Locale locale, String cmd)
          Get the default action for the given Locale.
static HashMap<String,Action> getDefaultActions(Locale locale)
          Get a set of default actions.
static void load(StreamSource src, Translator i18n, String resPath, HashMap<String,Action> actionMap, HashMap<String,List<Object>> actionLists)
          Read a action set from the given stream source.
static String parseAction(XMLStreamReader reader, Translator i18n, String resPath, HashMap<String,Action> actionMap)
          Parse an <action ...
static String parseComposite(XMLStreamReader reader, Translator i18n, String resPath, HashMap<String,Action> actionMap)
          Parse a <composite ...
static String parseList(XMLStreamReader reader, Translator i18n, String resPath, HashMap<String,Action> actionMap, HashMap<String,List<Object>> listMap)
          Parse a <list ...
static String translate(String id, String attr, String val, Translator i18n, boolean selected)
          Try to i18n the given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActionFactory

public ActionFactory()
Method Detail

load

public static void load(StreamSource src,
                        Translator i18n,
                        String resPath,
                        HashMap<String,Action> actionMap,
                        HashMap<String,List<Object>> actionLists)
Read a action set from the given stream source. Starts parsing at the first <set ... /> element. All new actions and action lists will replace entries with the same id in the given actionMap and actionList unconditionally.

Parameters:
src - stream to read and parse
i18n - bundle to use for i18n
resPath - base path for resolving relative pathes to icons
actionMap - where to put the new actions
actionLists - where to put the new action lists

parseList

public static String parseList(XMLStreamReader reader,
                               Translator i18n,
                               String resPath,
                               HashMap<String,Action> actionMap,
                               HashMap<String,List<Object>> listMap)
                        throws XMLStreamException
Parse a <list ... /> element. When this method returns, the readers cursor points to the end of the element or at EOF (if maleformed xml).

Parameters:
reader - where to read from. Reader's cursor MUST point to the start of list element.
i18n - translator to use for i18n
resPath - where to lookup images
actionMap - where to lookup referenced actions and to put new actions
listMap - where to lookup referenced action lists and to put new action lists. This maps value are always lists containing the follwoing elements, only: action names, null or a list of the same type.
Returns:
the id of the generated actionList
Throws:
XMLStreamException
NullPointerException - if the reader or the supplied action map or list is null

parseComposite

public static String parseComposite(XMLStreamReader reader,
                                    Translator i18n,
                                    String resPath,
                                    HashMap<String,Action> actionMap)
                             throws XMLStreamException
Parse a <composite ... /> element. When this method returns, the readers cursor points to the end of the element or at EOF (if maleformed xml).

Parameters:
reader - where to read from. Reader's cursor MUST point to the start of composite element.
i18n - translator to use for i18n
resPath - where to lookup images
actionMap - where to lookup referenced actions and to put new actions
Returns:
the id of the generated actionList
Throws:
XMLStreamException
NullPointerException - if the reader or the supplied action ap or list is null

parseAction

public static String parseAction(XMLStreamReader reader,
                                 Translator i18n,
                                 String resPath,
                                 HashMap<String,Action> actionMap)
                          throws XMLStreamException
Parse an <action ... /> element. When this method returns, the readers cursor points to the end of the element or at EOF (if maleformed xml).

Parameters:
reader - where to read from. Reader's cursor MUST point to the start of action element.
i18n - translator to use for i18n
resPath - where to lookup images
actionMap - where to lookup referenced actions and to put new actions
Returns:
the id of the generated actionList
Throws:
XMLStreamException
NullPointerException - if the reader or the supplied action map or list is null

translate

public static final String translate(String id,
                                     String attr,
                                     String val,
                                     Translator i18n,
                                     boolean selected)
Try to i18n the given value.

If the given resource bundle is null, val will be returned. Otherwise:

If val is NOT null: If the lookup in the given bundle returns a valid string, the result of the lookup is returned. Otherwise val will be returned.

If selected is true, lookup id.attr.selected. If it returns a valid String, the result will be returned. Otherwise:

If the lookup for the key id.attr returns a valid String, the result of the lookup will be returned. Otherwise val will be returned.

Parameters:
id - the i18n key prefix to use.
attr - the i18n suffix to use.
val - the value to lookup (highest priority). Might be null.
i18n - resource bundle to use for key lookup. Might be null (i.e. do no lookup).
selected - if true, lookup the i18n key id.attr.selected first (if i18n is not null).
Returns:
null if val is null, otherwise the result of the i18n lookup or the given val itself.

getAttributes

public static HashMap<String,Object> getAttributes(XMLStreamReader reader)
Get all attributes pairs from the given reader.

Parameters:
reader - where to read from. The reader's cursor MUST point to the start of an element.
Returns:
a (possible empty) map, where the keys are attribute names and values the corresponding attribute values.
Throws:
NullPointerException - if the reader is null

getDefaultAction

public static final Action getDefaultAction(Locale locale,
                                            String cmd)
Get the default action for the given Locale. Known actions are: OK, CANCEL, UPDATE, ADD, NEW, DELETE, CLOSE, DEFAULTS, TEST, SAVE, APPLY, RESET, LOGIN, PRINT, SEARCH

Parameters:
locale - locale to use. If null, default locale is used.
cmd - command to use, for looking up the action in question
Returns:
null if not found, a copy of the default action otherwise.

getDefaultActions

public static HashMap<String,Action> getDefaultActions(Locale locale)
Get a set of default actions. Generates always a new Map.

Parameters:
locale - Locale to use for i18n. If null, the default locale will be used.
Returns:
a map with common actions.

ehsbe CommonsTM
Version 1.1.0rc3

Copyright ©2005-2009 EHSBE, Walther-Rathenau-Str. 58, 39104 Magdeburg, Sachsen-Anhalt, Germany. All Rights Reserved.