Package org.cdlib.mrt.formatter
Class FormatterAbs
java.lang.Object
org.cdlib.mrt.formatter.FormatterAbs
- All Implemented Interfaces:
FormatterInf
- Direct Known Subclasses:
ANVLFormatter
,JSONFormatter
,XHTMLFormatter
,XMLFormatter
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
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.cdlib.mrt.formatter.FormatterInf
FormatterInf.Format
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addLvl
(int lvl, PrintStream stream) Indent handling for output display streamprotected void
closeOutput
(PrintStream stream) Close output streamprotected boolean
display
(Method m, Object obj, boolean isFirst, int lvl, PrintStream stream) process this method for output using reflectionvoid
format
(StateInf state, PrintStream stream) Create ouput format.protected void
formatClass
(Class c, Object object, int lvl, PrintStream stream) Format this class.void
formatNamedNode
(String name, Object obj, boolean isFirst, int lvl, PrintStream stream) Format a node.void
formatNode
(Object obj, boolean isFirst, int lvl, PrintStream stream) protected void
formatObject
(Object object, int lvl, PrintStream stream) Use reflection to format this objectstatic ANVLFormatter
getANVLFormatter
(LoggerInf logger) get ANVL formatterstatic FormatterInf
getFormatter
(FormatterInf.Format formatType, LoggerInf logger) Get typped formatterType of formatter being usedstatic JSONFormatter
getJSONFormatter
(String mapperName, LoggerInf logger) get JSON formatterstatic JSONFormatter
getJSONFormatter
(LoggerInf logger) get JSON formatterprotected String
getMethodName
(Method method) Standard name generator for elements.protected String
getObjectName
(Object obj) From a full object name find the last suffixstatic XHTMLFormatter
getXHTMLFormatter
(String mapperName, LoggerInf logger) get XHTML formatterstatic XHTMLFormatter
getXHTMLFormatter
(LoggerInf logger) get XHTML formatterstatic XMLFormatter
getXMLFormatter
(String mapperName, LoggerInf logger) get XML formatterstatic XMLFormatter
getXMLFormatter
(LoggerInf logger) get XML formatterprotected boolean
interfaceMatches
(Class c, String match) Test if a class has a hierarchical interface of a particular typeprotected boolean
protected boolean
Critical routine to determine if a method should be displayed Must begin with get.protected boolean
/** Is this an enum class?protected boolean
Is this a StateStringInf classprotected boolean
Is this a List classprotected boolean
Is this a Map classprotected boolean
Determine if this class is numeric or boolean.protected boolean
Determine if this class is numeric or boolean.protected boolean
Determine if this class is numeric or boolean.protected boolean
Is this a StateInf classprotected boolean
Is this a StateStringInf classprotected void
log outputprotected void
log output when debugging is setprotected void
log exceptionprotected String
lowerCaseFirst
(String in) Lower case first letter of stringprotected abstract void
print
(String name, String value, boolean isFirst, boolean isNumeric, int lvl, PrintStream stream) Formatted entry valueprotected abstract int
printBegin
(StateInf state, PrintStream stream) Beginning of formattingprotected abstract void
printClose
(String name, int lvl, PrintStream stream) Closing element entry.protected abstract void
printEnd
(PrintStream stream) End of formattingprotected abstract void
printStart
(String name, boolean isFirst, int lvl, PrintStream stream) Beginning element entry.protected void
processLinkedHashList
(Method m, Object obj, boolean isFirst, int lvl, PrintStream stream) process reference typeprotected void
processLinkedHashList
(LinkedHashList<String, Object> map, String name, boolean isFirst, int lvl, PrintStream stream) void
processList
(Method mGetList, Object objList, boolean firstNode, int lvl, PrintStream stream) Using reflection, process a list of values.protected void
processMap
(Method m, Object obj, boolean isFirst, int lvl, PrintStream stream) process reference typeprotected void
processMap
(Map<Object, Object> map, String name, boolean isFirst, int lvl, PrintStream stream) protected boolean
returnsList
(Method method) If the return class is a list this is trueprotected boolean
returnsMap
(Method method) If the return class is a map this is trueprotected Object
Reflection mechanism to run a methodprotected String
runStringMethod
(Method meth, String name, Object object) Runs toString() for this methodprotected void
setFormatterType
(FormatterInf.Format formatterType) protected void
write
(String line, PrintStream stream) Used by formatter class to write to the display outputprotected void
writeln
(String line, PrintStream stream) Used by formatter class to write a line to the display output
-
Field Details
-
NAME
- See Also:
-
MESSAGE
- See Also:
-
NL
-
DEBUG
protected static final boolean DEBUG- See Also:
-
logger
-
formatterType
-
-
Constructor Details
-
FormatterAbs
Constructor- Parameters:
logger
- process logger- Throws:
TException
-
-
Method Details
-
getXMLFormatter
get XML formatter- Parameters:
logger
- process logger- Returns:
- XML formatter
- Throws:
TException
-
getXMLFormatter
get XML formatter- Parameters:
mapperName
- resource name for mapper propertieslogger
- process logger- Returns:
- XML formatter
- Throws:
TException
-
getANVLFormatter
get ANVL formatter- Parameters:
logger
- process logger- Returns:
- ANVL formatter
- Throws:
TException
-
getXHTMLFormatter
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 filelogger
- process logger- Returns:
- XHTML formatter
- Throws:
TException
-
getJSONFormatter
get JSON formatter- Parameters:
logger
- process logger- Returns:
- JSON formatter
- Throws:
TException
-
getJSONFormatter
get JSON formatter- Parameters:
logger
- process logger- Returns:
- JSON formatter
- Throws:
TException
-
setFormatterType
-
getFormatter
public static FormatterInf getFormatter(FormatterInf.Format formatType, LoggerInf logger) throws TException Get typped formatter- Parameters:
formatType
- type of formatter being requestedlogger
- process logger- Returns:
- formatter
- Throws:
TException
-
getFormatterType
Type of formatter being used- Specified by:
getFormatterType
in interfaceFormatterInf
- Returns:
- type of used formatter
-
printBegin
Beginning of formatting- Parameters:
stream
- print output stream- Returns:
- starting level (used for indentation - typically 1)
- Throws:
TException
-
printEnd
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 nameisFirst
- true=is first element of a list, false=subsequent element of listlvl
- indentation levelstream
- print output stream- Throws:
TException
-
printClose
Closing element entry. Example is closeing element in XML- Parameters:
name
- element namelvl
- indentation levelstream
- 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 entryvalue
- entry valueisFirst
- true=is first element of a list, false=subsequent element of listisNumeric
- is the element numeric or boolean. Some formats require quotes around value unless it is numericlvl
- indentation levelstream
- print output stream- Throws:
TException
-
format
Description copied from interface:FormatterInf
Create ouput format. Using the StateInf object, create a formatted output to this print stream- Specified by:
format
in interfaceFormatterInf
- Parameters:
state
- object to be formattedstream
- output for formatted data- Throws:
TException
-
formatNode
- 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 nameobj
- object to be formattedisFirst
- true=is first element of a list, false=subsequent element of listlvl
- indentation levelstream
- print output stream- Throws:
TException
-
formatObject
Use reflection to format this object- Parameters:
object
- object to be formattedlvl
- indentation levelstream
- print output stream- Throws:
TException
-
formatClass
Format this class. Using reflection extract all methods to determine if they should be called for formatting.- Parameters:
c
- Class to be formattedobject
- instantiated object of this classlvl
- indentation levelstream
- 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 processedobj
- object containing methodisFirst
- is this the first of a list of entrieslvl
- indentation levelstream
- 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 processedobj
- object containing methodisFirst
- is this the first of a list of entrieslvl
- indentation levelstream
- 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 processedobj
- object containing methodisFirst
- is this the first of a list of entrieslvl
- indentation levelstream
- 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
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
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
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
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 processedobjList
- object containing this getterfirstNode
- first entry in listlvl
- indentation levelstream
- print output stream- Throws:
TException
-
getObjectName
From a full object name find the last suffix- Parameters:
obj
- object for name extraction- Returns:
- suffix - name
-
isDisplayMethod
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
- Throws:
TException
-
runMethod
Reflection mechanism to run a method- Parameters:
meth
- method to be runobject
- object containing the method- Returns:
- result of running method
-
runStringMethod
Runs toString() for this method- Parameters:
meth
- method to be executedobject
- object containing method- Returns:
- toString() value
- Throws:
TException
-
returnsList
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
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
/** Is this an enum class?- Parameters:
c
- class to be tested- Returns:
- true=enum, false=not enum
-
isStateClass
Is this a StateInf class- Parameters:
c
- class to be tested- Returns:
- true=is StateInf class, false=is not a StateInf class
-
isStateStringClass
Is this a StateStringInf class- Parameters:
c
- class to be tested- Returns:
- true=is StateStringInf class, false=is not a StateStringInf class
-
isLinkedHashList
Is this a StateStringInf class- Parameters:
c
- class to be tested- Returns:
- true=is StateStringInf class, false=is not a StateStringInf class
-
isList
Is this a List class- Parameters:
c
- class to be tested- Returns:
- true=is List class, false=is not List class
-
isMap
Is this a Map class- Parameters:
c
- class to be tested- Returns:
- true=is Map class, false=is not Map class
-
lowerCaseFirst
Lower case first letter of string- Parameters:
in
- String to be processed- Returns:
- String with first char lower cased
-
interfaceMatches
Test if a class has a hierarchical interface of a particular type- Parameters:
c
- class to be testedmatch
- part of an interface string to be tested- Returns:
- true=class has interface of this type, false=class does not have this interface
-
write
Used by formatter class to write to the display output- Parameters:
line
- data to be written to the streamstream
- output display stream- Throws:
TException
-
writeln
Used by formatter class to write a line to the display output- Parameters:
line
- line to be written to the streamstream
- output display stream- Throws:
TException
-
closeOutput
Close output stream- Parameters:
stream
- display stream to be closed
-
log
log output- Parameters:
msg
- message to log
-
logDebug
log output when debugging is set- Parameters:
msg
- debug message
-
logException
log exception- Parameters:
ex
- Exception to log
-
addLvl
Indent handling for output display stream- Parameters:
lvl
- levels to indentstream
- output display stream- Throws:
TException
-