001
002package gudusoft.gsqlparser.pp.processor.type.comm;
003
004import gudusoft.gsqlparser.nodes.TParseTreeNode;
005import gudusoft.gsqlparser.pp.para.GFmtOpt;
006
007/**
008 * the base processor
009 * 
010 * @author zhoujun
011 * 
012 */
013public class AbstractProcessor<NodeType extends TParseTreeNode>
014{
015
016        /**
017         * the initial parameters
018         */
019        private Object[] parameters;
020
021        /**
022         * options
023         */
024        private GFmtOpt option;
025
026        /**
027         * the init method
028         * 
029         */
030        public void init( GFmtOpt option, Object... parameters )
031        {
032                this.option = option;
033                this.parameters = parameters;
034        }
035
036        /**
037         * get the first parameter
038         * 
039         * @param <E>
040         * @return
041         */
042        protected <E> E getParameter( Class<E> type )
043        {
044                return this.getParameter( type, 0 );
045        }
046
047        /**
048         * get the parameter object
049         * 
050         * @param <E>
051         * @param pos
052         * @return
053         */
054        protected <E> E getParameter( Class<E> type, int pos )
055        {
056                if ( parameters == null
057                                || pos >= this.parameters.length
058                                || parameters[pos] == null )
059                {
060                        return null;
061                }
062                if ( !type.isAssignableFrom( parameters[pos].getClass( ) ) )
063                {
064                        return null;
065                }
066                return (E) parameters[pos];
067        }
068
069        /**
070         * method template. if you need to add some before actions, override this
071         * method.
072         * 
073         * @param node
074         */
075        public void beforeProcess( NodeType node )
076        {
077
078        }
079
080        /**
081         * method template. if you need to add some before actions, override this
082         * method.
083         * 
084         * @param node
085         */
086        public void afterProcess( NodeType node )
087        {
088
089        }
090
091        public void process( NodeType node )
092        {
093
094        }
095
096        /**
097         * get all the format option
098         * 
099         * @return
100         */
101        public GFmtOpt getOption( )
102        {
103                return option;
104        }
105
106}