001
002package gudusoft.gsqlparser.pp.output.html;
003
004import gudusoft.gsqlparser.EDbVendor;
005import gudusoft.gsqlparser.TSourceToken;
006import gudusoft.gsqlparser.pp.output.HighlightingElement;
007import gudusoft.gsqlparser.pp.output.HighlightingElementRender;
008import gudusoft.gsqlparser.pp.output.OutputConfig;
009import gudusoft.gsqlparser.pp.para.GFmtOpt;
010
011import java.awt.Color;
012import java.awt.Font;
013import java.util.HashMap;
014import java.util.Map;
015
016public class HtmlOutputConfig implements OutputConfig
017{
018
019        private String globalFontName = "Courier New";
020        private int globalFontSize = 10;
021
022        private Map<HighlightingElement, HighlightingElementRender> highlightingElementMap = new HashMap<HighlightingElement, HighlightingElementRender>( );
023        private HtmlRenderUtil render;
024        /** Set only after the defaults are in the map: a config set with FormatterFactory.setOutputConfig renders in many threads. */
025        private volatile boolean init = false;
026
027        private void init( )
028        {
029                Font plainFont = new Font( globalFontName, Font.PLAIN, globalFontSize );
030                Font boldFont = new Font( globalFontName, Font.BOLD, globalFontSize );
031                Font italicFont = new Font( globalFontName, Font.ITALIC, globalFontSize );
032
033                Color color_800000 = new Color( 0x80, 0, 0 );
034                Color color_0000FF = new Color( 0, 0, 0xFF );
035                Color color_000000 = new Color( 0, 0, 0 );
036                Color color_C0C0C0 = new Color( 0xC0, 0xC0, 0xC0 );
037                Color color_FF0080 = new Color( 0xFF, 0, 0x80 );
038                Color color_16711935 = new Color( 16711935 );
039                Color color_16711808 = new Color( 16711808 );
040                Color color_FF0000 = new Color( 0xFF, 0, 0 );
041                Color color_008000 = new Color( 0, 0x80, 0 );
042
043                addHighlightingElementRender( HighlightingElement.sfkSpan,
044                                new HtmlHighlightingElementRender( HighlightingElement.sfkSpan,
045                                                Color.WHITE,
046                                                plainFont ),
047                                false );
048                addHighlightingElementRender( HighlightingElement.sfkDefault,
049                                new HtmlHighlightingElementRender( HighlightingElement.sfkDefault,
050                                                Color.BLACK,
051                                                plainFont ),
052                                false );
053                addHighlightingElementRender( HighlightingElement.sfkIdentifer,
054                                new HtmlHighlightingElementRender( HighlightingElement.sfkIdentifer,
055                                                color_800000,
056                                                plainFont ),
057                                false );
058                addHighlightingElementRender( HighlightingElement.sfkStandardkeyword,
059                                new HtmlHighlightingElementRender( HighlightingElement.sfkStandardkeyword,
060                                                color_0000FF,
061                                                plainFont ),
062                                false );
063                addHighlightingElementRender( HighlightingElement.sfkNumber,
064                                new HtmlHighlightingElementRender( HighlightingElement.sfkNumber,
065                                                color_000000,
066                                                plainFont ),
067                                false );
068                addHighlightingElementRender( HighlightingElement.sfkDelimitedIdentifier,
069                                new HtmlHighlightingElementRender( HighlightingElement.sfkDelimitedIdentifier,
070                                                color_800000,
071                                                plainFont ),
072                                false );
073                addHighlightingElementRender( HighlightingElement.sfkSymbol,
074                                new HtmlHighlightingElementRender( HighlightingElement.sfkSymbol,
075                                                color_C0C0C0,
076                                                plainFont ),
077                                false );
078                addHighlightingElementRender( HighlightingElement.sfkFunction,
079                                new HtmlHighlightingElementRender( HighlightingElement.sfkFunction,
080                                                color_FF0080,
081                                                boldFont ),
082                                false );
083                addHighlightingElementRender( HighlightingElement.sfkBuiltInFunction,
084                                new HtmlHighlightingElementRender( HighlightingElement.sfkBuiltInFunction,
085                                                color_16711935,
086                                                italicFont ),
087                                false );
088                addHighlightingElementRender( HighlightingElement.sfkDatatype,
089                                new HtmlHighlightingElementRender( HighlightingElement.sfkDatatype,
090                                                Color.BLACK,
091                                                italicFont ),
092                                false );
093                addHighlightingElementRender( HighlightingElement.sfkParameter,
094                                new HtmlHighlightingElementRender( HighlightingElement.sfkParameter,
095                                                color_800000,
096                                                plainFont ),
097                                false );
098                addHighlightingElementRender( HighlightingElement.sfkbindvar,
099                                new HtmlHighlightingElementRender( HighlightingElement.sfkbindvar,
100                                                color_800000,
101                                                plainFont ),
102                                false );
103                addHighlightingElementRender( HighlightingElement.sfkVendordbkeyword,
104                                new HtmlHighlightingElementRender( HighlightingElement.sfkVendordbkeyword,
105                                                color_0000FF,
106                                                plainFont ),
107                                false );
108                addHighlightingElementRender( HighlightingElement.sfkSQString,
109                                new HtmlHighlightingElementRender( HighlightingElement.sfkSQString,
110                                                color_FF0000,
111                                                plainFont ),
112                                false );
113                addHighlightingElementRender( HighlightingElement.sfkDQString,
114                                new HtmlHighlightingElementRender( HighlightingElement.sfkDQString,
115                                                color_FF0000,
116                                                plainFont ),
117                                false );
118                addHighlightingElementRender( HighlightingElement.sfkComment_dh,
119                                new HtmlHighlightingElementRender( HighlightingElement.sfkComment_dh,
120                                                color_008000,
121                                                italicFont ),
122                                false );
123                addHighlightingElementRender( HighlightingElement.sfkComment_ss,
124                                new HtmlHighlightingElementRender( HighlightingElement.sfkComment_ss,
125                                                color_008000,
126                                                italicFont ),
127                                false );
128                addHighlightingElementRender( HighlightingElement.sfkComment_sign,
129                                new HtmlHighlightingElementRender( HighlightingElement.sfkComment_sign,
130                                                color_008000,
131                                                italicFont ),
132                                false );
133                addHighlightingElementRender( HighlightingElement.sfkMssqlsystemvar,
134                                new HtmlHighlightingElementRender( HighlightingElement.sfkMssqlsystemvar,
135                                                color_16711935,
136                                                italicFont ),
137                                false );
138                addHighlightingElementRender( HighlightingElement.sfksqlvar,
139                                new HtmlHighlightingElementRender( HighlightingElement.sfksqlvar,
140                                                color_16711808,
141                                                plainFont ),
142                                false );
143                addHighlightingElementRender( HighlightingElement.sfkMssqlst1,
144                                new HtmlHighlightingElementRender( HighlightingElement.sfkMssqlst1,
145                                                color_16711808,
146                                                plainFont ),
147                                false );
148                addHighlightingElementRender( HighlightingElement.sfkMssqlst2,
149                                new HtmlHighlightingElementRender( HighlightingElement.sfkMssqlst2,
150                                                color_16711808,
151                                                plainFont ),
152                                false );
153                addHighlightingElementRender( HighlightingElement.sfkMssqlst3,
154                                new HtmlHighlightingElementRender( HighlightingElement.sfkMssqlst3,
155                                                color_16711808,
156                                                plainFont ),
157                                false );
158                addHighlightingElementRender( HighlightingElement.sfkOracleplsqlkeyword,
159                                new HtmlHighlightingElementRender( HighlightingElement.sfkOracleplsqlkeyword,
160                                                color_800000,
161                                                plainFont ),
162                                false );
163                addHighlightingElementRender( HighlightingElement.sfkOraclepackage,
164                                new HtmlHighlightingElementRender( HighlightingElement.sfkOraclepackage,
165                                                color_800000,
166                                                plainFont ),
167                                false );
168                addHighlightingElementRender( HighlightingElement.sfkOraclecommand,
169                                new HtmlHighlightingElementRender( HighlightingElement.sfkOraclecommand,
170                                                color_800000,
171                                                plainFont ),
172                                false );
173                addHighlightingElementRender( HighlightingElement.sfkOracleplsqlmethod,
174                                new HtmlHighlightingElementRender( HighlightingElement.sfkOracleplsqlmethod,
175                                                color_800000,
176                                                plainFont ),
177                                false );
178                addHighlightingElementRender( HighlightingElement.sfkOraclerem,
179                                new HtmlHighlightingElementRender( HighlightingElement.sfkOraclerem,
180                                                color_800000,
181                                                plainFont ),
182                                false );
183                addHighlightingElementRender( HighlightingElement.sfkOraclesqlplus,
184                                new HtmlHighlightingElementRender( HighlightingElement.sfkOraclesqlplus,
185                                                color_800000,
186                                                plainFont ),
187                                false );
188                addHighlightingElementRender( HighlightingElement.sfkSybasesystemobj,
189                                new HtmlHighlightingElementRender( HighlightingElement.sfkSybasesystemobj,
190                                                color_800000,
191                                                plainFont ),
192                                false );
193                addHighlightingElementRender( HighlightingElement.sfkSybasest,
194                                new HtmlHighlightingElementRender( HighlightingElement.sfkSybasest,
195                                                color_800000,
196                                                plainFont ),
197                                false );
198                addHighlightingElementRender( HighlightingElement.sfkSybaseglobalvar,
199                                new HtmlHighlightingElementRender( HighlightingElement.sfkSybaseglobalvar,
200                                                color_800000,
201                                                plainFont ),
202                                false );
203                addHighlightingElementRender( HighlightingElement.sfkopenbracket,
204                                new HtmlHighlightingElementRender( HighlightingElement.sfkopenbracket,
205                                                color_800000,
206                                                plainFont ),
207                                false );
208                addHighlightingElementRender( HighlightingElement.sfkclosebracket,
209                                new HtmlHighlightingElementRender( HighlightingElement.sfkclosebracket,
210                                                color_800000,
211                                                plainFont ),
212                                false );
213                addHighlightingElementRender( HighlightingElement.sfkUserCustomized,
214                                new HtmlHighlightingElementRender( HighlightingElement.sfkUserCustomized,
215                                                color_800000,
216                                                plainFont ),
217                                false );
218        }
219
220        public HtmlOutputConfig( GFmtOpt option, EDbVendor dbVendor )
221        {
222                render = new HtmlRenderUtil( this, option, dbVendor );
223        }
224
225        public void addHighlightingElementRender( HighlightingElement element,
226                        HighlightingElementRender setting )
227        {
228                addHighlightingElementRender( element, setting, true );
229        }
230
231        private void addHighlightingElementRender( HighlightingElement element,
232                        HighlightingElementRender setting, boolean override )
233        {
234                if ( override )
235                {
236                        highlightingElementMap.put( element, setting );
237                }
238                else if ( !highlightingElementMap.containsKey( element ) )
239                {
240                        highlightingElementMap.put( element, setting );
241                }
242        }
243
244        public boolean containsHighlightingElementRender(
245                        HighlightingElement element )
246        {
247                return highlightingElementMap.containsKey( element );
248        }
249
250        public String getGlobalFontName( )
251        {
252                return globalFontName;
253        }
254
255        public int getGlobalFontSize( )
256        {
257                return globalFontSize;
258        }
259
260        public HighlightingElementRender getHighlightingElementRender(
261                        HighlightingElement element )
262        {
263                return highlightingElementMap.get( element );
264        }
265
266        public void removeHighlightingElementRender( HighlightingElement element )
267        {
268                highlightingElementMap.remove( element );
269        }
270
271        public void setGlobalFontName( String globalFontName )
272        {
273                this.globalFontName = globalFontName;
274        }
275
276        public void setGlobalFontSize( int globalFontSize )
277        {
278                this.globalFontSize = globalFontSize;
279        }
280
281        public String renderHighlightingElement( TSourceToken token )
282        {
283                if ( !init )
284                        initOnce( );
285                return render.renderToken( token );
286        }
287
288        /**
289         * Adds the default renders once. The flag was set before the map was filled, so a second thread
290         * rendering with the same config read a missing render and threw a NullPointerException.
291         */
292        private synchronized void initOnce( )
293        {
294                if ( !init )
295                {
296                        init( );
297                        init = true;
298                }
299        }
300}