Class DynamicFormatter

java.lang.Object
eu.bandm.tools.tdom.runtime.BaseVisitor
eu.bandm.tools.formatfrontends.absy.Visitor
eu.bandm.tools.formatfrontends.DynamicFormatter
All Implemented Interfaces:
PCDataVisitor

public class DynamicFormatter extends eu.bandm.tools.formatfrontends.absy.Visitor
Interpreter for generating formats from the format description language dynamically.
For a description of this small language see the Format user documentation.
Please note: Beside the constructor there are some public fields and some setter methods for configuring the format generation process.

The argument of the intial call to "format()" can be given a format code explicitly.
For those current field values which are objects themselves, the code tries to find the format code as follows, using reflection:
  1. Find method "public void toFormat()" and execute it.
  2. If this fails: find method "public String getFormatHint()". Execute this, take the resulting string as format code, parse it and interpret the resulting expression.
  3. If this method does not exist: call "defaultFormat(Object)". This method can be overwritten for sub-classes of DynamicFormatter. Its default implementation uses "toString()" and creates a simple Format.Literal.

FIXME for child values : try via reflection
1) "newmodel = .get_()"
2) "newmodel = ."
==> can be used for umod style and for "public" foreign definitions.
FIXME
maintain CACHE structure for both !! --
  • Field Details

    • default_indentation

      public int default_indentation
      used for identation formats without explict number part. Default value is two(2), but can be re-configured anytime.
    • null_representation

      public Format null_representation
      used for representing the java value null. Default value is "Format.literal("null")", but can be re-configured anytime.
    • cacheFormatForObject

      public boolean cacheFormatForObject
      controls caching of generated formats for objects, both putting and getting. Turning on caching makes only sense for immutable objects (i.e. immutable at least during life-time of this DynamicFormatter).. Default value is "false", but can be re-configured anytime.
    • cachePatternForClass

      public boolean cachePatternForClass
      controls caching of retrieved format code for java classes, both putting and getting. Turning on caching makes sense in most cases, when only one format code is constantly used for each class. Default value is "true", but can be re-configured anytime.
    • msg

    • model

      protected Object model
    • format

      protected Format format
    • number

      protected int number
    • literals

      protected Map<String,Format> literals
    • checkedmode

      protected boolean checkedmode
    • NAME_METHOD_getFormatHint

      public static final String NAME_METHOD_getFormatHint
      name of the method a class must (re-)define to define the format code which shall be used for their instances.
      Value is "getFormatHint"
      See Also:
    • NAME_METHOD_toFormat

      public static final String NAME_METHOD_toFormat
      name of the method a class can (re-)define to build a format representation for its instances "manually", by calling the Format factory directly. Value is "toFormat"
      See Also:
    • NO_FOLD

      protected static final int NO_FOLD
      See Also:
    • FOLD_BENEATH

      protected static final int FOLD_BENEATH
      See Also:
    • FOLD_BESIDE

      protected static final int FOLD_BESIDE
      See Also:
    • FOLD_LINE

      protected static final int FOLD_LINE
      See Also:
    • FOLD_BLOCK

      protected static final int FOLD_BLOCK
      See Also:
    • FOLD_APPEND

      protected static final int FOLD_APPEND
      See Also:
    • childinfo

      protected DynamicFormatter.ChildInfo childinfo
    • CHILDINFO_PLAIN

      protected final DynamicFormatter.ChildInfo CHILDINFO_PLAIN
    • emptyList

      protected Format emptyList
    • foundchild

      protected Object foundchild
    • foundchildname

      protected String foundchildname
    • NO_PARAMS

      protected static final Class[] NO_PARAMS
    • NO_ARGS

      protected static final Object[] NO_ARGS
    • NOSUPER

      protected static Format NOSUPER
    • object2format

      protected static final HashMap<Object,Format> object2format
      Format already generated for this particular object.
    • class2formatMethod

      protected static final HashMap<Class,Method> class2formatMethod
      "toFormat" method of class, or constant "null" if no pattern and no toFormat() method exist and default has to be used.
    • class2pattern

      protected static final HashMap<Class,eu.bandm.tools.formatfrontends.absy.Element_pattern> class2pattern
      compiled pattern for class
    • text2pattern

      protected static final HashMap<String,eu.bandm.tools.formatfrontends.absy.Element_pattern> text2pattern
      String value to compiled pattern, for skipping parsing process in case of identical format source tests for different classes.
  • Constructor Details

  • Method Details

    • error

      protected void error(Location<XMLDocumentIdentifier> loc, String s)
    • warning

      protected void warning(Location<XMLDocumentIdentifier> loc, String s)
    • defaultFormat

      protected static Format defaultFormat(Object model)
      used for representing any java object "model" for which no format code could be found. Default value is "Format.literal(String.valueOf(model))", but can be re-configured anytime.
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_alternative element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_beneath element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_beside element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_line element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_block element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_append element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_number element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_indent element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_indent_op element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_literal element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_whitespace element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_clss element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_clssUPCASE element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_tabular element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_listdescr element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_emptylistrepr element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_beneathOp element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_besideOp element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_lineOp element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_blockOp element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_appendOp element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_from element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_too element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_thiz element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_ref element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_swtch element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • casesInt

      protected eu.bandm.tools.formatfrontends.absy.Element_append casesInt(eu.bandm.tools.formatfrontends.absy.Element_cases cases, String childname, int value)
    • casesStringOrChar

      protected eu.bandm.tools.formatfrontends.absy.Element_append casesStringOrChar(eu.bandm.tools.formatfrontends.absy.Element_cases cases, String childname, String value, boolean ischar)
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_supr element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • visit

      public void visit(eu.bandm.tools.formatfrontends.absy.Element_child element)
      Overrides:
      visit in class eu.bandm.tools.formatfrontends.absy.Visitor
    • processChild

      protected Format processChild(Object child)
    • processMap

      protected Format processMap(Map map)
    • processFoldOrList

      protected Format processFoldOrList(Collection list)
    • foldit

      protected Format foldit(Format[] items)
    • processCollectionCode

      protected Format processCollectionCode(Collection list)
    • findToFormatMethod

      protected Method findToFormatMethod(Class<?> cls)
    • invokeToFormatMethod

      protected Format invokeToFormatMethod(Object model, Method method)
    • getFormatHint

      public String getFormatHint(Class<?> cls)
      Side-door entry for interception: retrieve the class specific format description source text.

      This method calls via reflection the classes static method named "getFormatHint()" (which is the value of "static final String NAME_METHOD_getFormatHint") and returns the returned String value.
    • parseHint

      public eu.bandm.tools.formatfrontends.absy.Element_pattern parseHint(String hint)
      Side-door entry for interception: Parses a format front end language string and delivers the AST as a tdom model.
    • deliver

      protected Format deliver(Object model, Format format)
    • format

      public Format format(Object model)
      central method for generating a format for a given object (="model").

      The source text representing the format structure is retrieved by the multi-alternative strategy which is described in the class documentation.
    • format

      public Format format(Object model, String formathint)
      central method for generating a format for a given object (="model").

      The source text representing the format structure of the top-level model is given explicitly. Those for recursively contained objects is retrieved by the multi-alternative strategy which is described in the class documentation.

      Attention: possibly writes into cache "object to format" (without reading it).
    • format

      protected Format format(Object model, eu.bandm.tools.formatfrontends.absy.Element_pattern pattern)
    • toFormat

      public static Format toFormat(Object model, MessageReceiver<? super SimpleMessage> msg)
      convenience method.

      Calls "new DynamicFormatter(msg).format(model);"
    • toFormat

      public static Format toFormat(Object model, String formathint, MessageReceiver<? super SimpleMessage> msg)
      convenience method.

      Calls "new DynamicFormatter(msg).format(model,formathint);"
    • getFormatHint

      public static String getFormatHint(Class cls, MessageReceiver<SimpleMessage> msg)
      convenience method.

      Calls "new DynamicFormatter(msg).getFormatHint(cls);"