Class FormatterAbs

java.lang.Object
org.cdlib.mrt.formatter.FormatterAbs
All Implemented Interfaces:
FormatterInf
Direct Known Subclasses:
ANVLFormatter, JSONFormatter, XHTMLFormatter, XMLFormatter

public abstract class FormatterAbs extends Object implements FormatterInf
FormatterAbs is the base formatting class. It uses reflection to determine which methods in a StateInf class should be called for formatting the output.
Author:
dloy
  • Field Details

  • Constructor Details

  • Method Details

    • getXMLFormatter

      public static XMLFormatter getXMLFormatter(LoggerInf logger) throws TException
      get XML formatter
      Parameters:
      logger - process logger
      Returns:
      XML formatter
      Throws:
      TException
    • getXMLFormatter

      public static XMLFormatter getXMLFormatter(String mapperName, LoggerInf logger) throws TException
      get XML formatter
      Parameters:
      mapperName - resource name for mapper properties
      logger - process logger
      Returns:
      XML formatter
      Throws:
      TException
    • getANVLFormatter

      public static ANVLFormatter getANVLFormatter(LoggerInf logger) throws TException
      get ANVL formatter
      Parameters:
      logger - process logger
      Returns:
      ANVL formatter
      Throws:
      TException
    • getXHTMLFormatter

      public static XHTMLFormatter getXHTMLFormatter(LoggerInf logger) throws TException
      get XHTML formatter
      Parameters:
      logger - process logger
      Returns:
      XHTML formatter
      Throws:
      TException
    • getXHTMLFormatter

      public static XHTMLFormatter getXHTMLFormatter(String mapperName, LoggerInf logger) throws TException
      get XHTML formatter
      Parameters:
      mapperName - name of XML mapper file
      logger - process logger
      Returns:
      XHTML formatter
      Throws:
      TException
    • getJSONFormatter

      public static JSONFormatter getJSONFormatter(String mapperName, LoggerInf logger) throws TException
      get JSON formatter
      Parameters:
      logger - process logger
      Returns:
      JSON formatter
      Throws:
      TException
    • getJSONFormatter

      public static JSONFormatter getJSONFormatter(LoggerInf logger) throws TException
      get JSON formatter
      Parameters:
      logger - process logger
      Returns:
      JSON formatter
      Throws:
      TException
    • setFormatterType

      protected void setFormatterType(FormatterInf.Format formatterType)
    • getFormatter

      public static FormatterInf getFormatter(FormatterInf.Format formatType, LoggerInf logger) throws TException
      Get typped formatter
      Parameters:
      formatType - type of formatter being requested
      logger - process logger
      Returns:
      formatter
      Throws:
      TException
    • getFormatterType

      public FormatterInf.Format getFormatterType()
      Type of formatter being used
      Specified by:
      getFormatterType in interface FormatterInf
      Returns:
      type of used formatter
    • printBegin

      protected abstract int printBegin(StateInf state, PrintStream stream) throws TException
      Beginning of formatting
      Parameters:
      stream - print output stream
      Returns:
      starting level (used for indentation - typically 1)
      Throws:
      TException
    • printEnd

      protected abstract void printEnd(PrintStream stream) throws TException
      End of formatting
      Parameters:
      stream - output stream
      Throws:
      TException
    • printStart

      protected abstract void printStart(String name, boolean isFirst, int lvl, PrintStream stream) throws TException
      Beginning element entry. An example would be the element name in XML
      Parameters:
      name - element name
      isFirst - true=is first element of a list, false=subsequent element of list
      lvl - indentation level
      stream - print output stream
      Throws:
      TException
    • printClose

      protected abstract void printClose(String name, int lvl, PrintStream stream) throws TException
      Closing element entry. Example is closeing element in XML
      Parameters:
      name - element name
      lvl - indentation level
      stream - print output stream
      Throws:
      TException
    • print

      protected abstract void print(String name, String value, boolean isFirst, boolean isNumeric, int lvl, PrintStream stream) throws TException
      Formatted entry value
      Parameters:
      name - name of entry
      value - entry value
      isFirst - true=is first element of a list, false=subsequent element of list
      isNumeric - is the element numeric or boolean. Some formats require quotes around value unless it is numeric
      lvl - indentation level
      stream - print output stream
      Throws:
      TException
    • format

      public void format(StateInf state, PrintStream stream) throws TException
      Description copied from interface: FormatterInf
      Create ouput format. Using the StateInf object, create a formatted output to this print stream
      Specified by:
      format in interface FormatterInf
      Parameters:
      state - object to be formatted
      stream - output for formatted data
      Throws:
      TException
    • formatNode

      public void formatNode(Object obj, boolean isFirst, int lvl, PrintStream stream) throws TException
      Throws:
      TException
    • formatNamedNode

      public void formatNamedNode(String name, Object obj, boolean isFirst, int lvl, PrintStream stream) throws TException
      Format a node. This is the formatting of an object, with start and close elements
      Parameters:
      name - class name
      obj - object to be formatted
      isFirst - true=is first element of a list, false=subsequent element of list
      lvl - indentation level
      stream - print output stream
      Throws:
      TException
    • formatObject

      protected void formatObject(Object object, int lvl, PrintStream stream) throws TException
      Use reflection to format this object
      Parameters:
      object - object to be formatted
      lvl - indentation level
      stream - print output stream
      Throws:
      TException
    • formatClass

      protected void formatClass(Class c, Object object, int lvl, PrintStream stream) throws TException
      Format this class. Using reflection extract all methods to determine if they should be called for formatting.
      Parameters:
      c - Class to be formatted
      object - instantiated object of this class
      lvl - indentation level
      stream - print output stream
      Throws:
      TException
    • display

      protected boolean display(Method m, Object obj, boolean isFirst, int lvl, PrintStream stream) throws Exception
      process this method for output using reflection
      Parameters:
      m - method to be processed
      obj - object containing method
      isFirst - is this the first of a list of entries
      lvl - indentation level
      stream - print output stream
      Returns:
      true=output took place, false=no output
      Throws:
      Exception
    • processLinkedHashList

      protected void processLinkedHashList(Method m, Object obj, boolean isFirst, int lvl, PrintStream stream) throws Exception
      process reference type
      Parameters:
      m - method to be processed
      obj - object containing method
      isFirst - is this the first of a list of entries
      lvl - indentation level
      stream - print output stream
      Throws:
      Exception
    • processLinkedHashList

      protected void processLinkedHashList(LinkedHashList<String,Object> map, String name, boolean isFirst, int lvl, PrintStream stream) throws Exception
      Throws:
      Exception
    • processMap

      protected void processMap(Method m, Object obj, boolean isFirst, int lvl, PrintStream stream) throws Exception
      process reference type
      Parameters:
      m - method to be processed
      obj - object containing method
      isFirst - is this the first of a list of entries
      lvl - indentation level
      stream - print output stream
      Throws:
      Exception
    • processMap

      protected void processMap(Map<Object,Object> map, String name, boolean isFirst, int lvl, PrintStream stream) throws Exception
      Throws:
      Exception
    • isNumeric

      protected boolean isNumeric(Class testClass)
      Determine if this class is numeric or boolean. This is required because display formats changed based on numeric/boolean status.
      Parameters:
      testClass - class to be tested
      Returns:
      true=is numeric or boolean, false=is neither numeric nor boolean
    • isNumeric

      protected boolean isNumeric(Object testObject)
      Determine if this class is numeric or boolean. This is required because display formats changed based on numeric/boolean status.
      Parameters:
      testClass - class to be tested
      Returns:
      true=is numeric or boolean, false=is neither numeric nor boolean
    • isNumeric

      protected boolean isNumeric(String test)
      Determine if this class is numeric or boolean. This is required because display formats changed based on numeric/boolean status.
      Parameters:
      testClass - class to be tested
      Returns:
      true=is numeric or boolean, false=is neither numeric nor boolean
    • getMethodName

      protected String getMethodName(Method method) throws TException
      Standard name generator for elements. Remove get at beginning and lower case first letter
      Parameters:
      method - for extracted name
      Returns:
      extracted name
      Throws:
      TException
    • processList

      public void processList(Method mGetList, Object objList, boolean firstNode, int lvl, PrintStream stream) throws TException
      Using reflection, process a list of values. Each entry in the list corresponds to a start/close block
      Parameters:
      mGetList - a getter for a list to be processed
      objList - object containing this getter
      firstNode - first entry in list
      lvl - indentation level
      stream - print output stream
      Throws:
      TException
    • getObjectName

      protected String getObjectName(Object obj)
      From a full object name find the last suffix
      Parameters:
      obj - object for name extraction
      Returns:
      suffix - name
    • isDisplayMethod

      protected boolean isDisplayMethod(Method m) throws TException
      Critical routine to determine if a method should be displayed Must begin with get. Must be public Must not take any arguments Must be return a value that is displayable (primitives, State or List)
      Parameters:
      m - method to be tested
      Returns:
      true=is displayable, false=is not displayable
      Throws:
      TException
    • isDisplayableClass

      protected boolean isDisplayableClass(Class c) throws TException
      Throws:
      TException
    • runMethod

      protected Object runMethod(Method meth, Object object)
      Reflection mechanism to run a method
      Parameters:
      meth - method to be run
      object - object containing the method
      Returns:
      result of running method
    • runStringMethod

      protected String runStringMethod(Method meth, String name, Object object) throws TException
      Runs toString() for this method
      Parameters:
      meth - method to be executed
      object - object containing method
      Returns:
      toString() value
      Throws:
      TException
    • returnsList

      protected boolean returnsList(Method method)
      If the return class is a list this is true
      Parameters:
      m - test the return type of this method
      Returns:
      true=is list, false=not list
    • returnsMap

      protected boolean returnsMap(Method method)
      If the return class is a map this is true
      Parameters:
      m - test the return type of this method
      Returns:
      true=is map, false=not map
    • isEnum

      protected boolean isEnum(Class c)
      /** Is this an enum class?
      Parameters:
      c - class to be tested
      Returns:
      true=enum, false=not enum
    • isStateClass

      protected boolean isStateClass(Class c)
      Is this a StateInf class
      Parameters:
      c - class to be tested
      Returns:
      true=is StateInf class, false=is not a StateInf class
    • isStateStringClass

      protected boolean isStateStringClass(Class c)
      Is this a StateStringInf class
      Parameters:
      c - class to be tested
      Returns:
      true=is StateStringInf class, false=is not a StateStringInf class
    • isLinkedHashList

      protected boolean isLinkedHashList(Class c)
      Is this a StateStringInf class
      Parameters:
      c - class to be tested
      Returns:
      true=is StateStringInf class, false=is not a StateStringInf class
    • isList

      protected boolean isList(Class c)
      Is this a List class
      Parameters:
      c - class to be tested
      Returns:
      true=is List class, false=is not List class
    • isMap

      protected boolean isMap(Class c)
      Is this a Map class
      Parameters:
      c - class to be tested
      Returns:
      true=is Map class, false=is not Map class
    • lowerCaseFirst

      protected String lowerCaseFirst(String in)
      Lower case first letter of string
      Parameters:
      in - String to be processed
      Returns:
      String with first char lower cased
    • interfaceMatches

      protected boolean interfaceMatches(Class c, String match)
      Test if a class has a hierarchical interface of a particular type
      Parameters:
      c - class to be tested
      match - part of an interface string to be tested
      Returns:
      true=class has interface of this type, false=class does not have this interface
    • write

      protected void write(String line, PrintStream stream) throws TException
      Used by formatter class to write to the display output
      Parameters:
      line - data to be written to the stream
      stream - output display stream
      Throws:
      TException
    • writeln

      protected void writeln(String line, PrintStream stream) throws TException
      Used by formatter class to write a line to the display output
      Parameters:
      line - line to be written to the stream
      stream - output display stream
      Throws:
      TException
    • closeOutput

      protected void closeOutput(PrintStream stream)
      Close output stream
      Parameters:
      stream - display stream to be closed
    • log

      protected void log(String msg)
      log output
      Parameters:
      msg - message to log
    • logDebug

      protected void logDebug(String msg)
      log output when debugging is set
      Parameters:
      msg - debug message
    • logException

      protected void logException(Exception ex)
      log exception
      Parameters:
      ex - Exception to log
    • addLvl

      protected void addLvl(int lvl, PrintStream stream) throws TException
      Indent handling for output display stream
      Parameters:
      lvl - levels to indent
      stream - output display stream
      Throws:
      TException