001 002package gudusoft.gsqlparser.pp.output.html; 003 004import gudusoft.gsqlparser.pp.output.HighlightingElement; 005import gudusoft.gsqlparser.pp.output.HighlightingElementRender; 006 007import java.awt.Color; 008import java.awt.Font; 009import java.awt.font.TextAttribute; 010 011public class HtmlHighlightingElementRender implements HighlightingElementRender 012{ 013 014 private Color backgroundColor; 015 016 private HighlightingElement element; 017 018 private String fontName; 019 020 private int fontSize; 021 022 private int fontStyle; 023 024 private Color foregroundColor; 025 026 private boolean strikeOut; 027 028 private boolean underLine; 029 030 public HtmlHighlightingElementRender( HighlightingElement element ) 031 { 032 this.element = element; 033 } 034 035 public HtmlHighlightingElementRender( HighlightingElement element, Font font ) 036 { 037 this.element = element; 038 this.fontSize = font.getSize( ); 039 this.fontStyle = font.getStyle( ); 040 this.fontName = font.getName( ); 041 this.underLine = TextAttribute.UNDERLINE_ON.equals( font.getAttributes( ) 042 .get( TextAttribute.UNDERLINE ) ); 043 this.strikeOut = TextAttribute.STRIKETHROUGH_ON.equals( font.getAttributes( ) 044 .get( TextAttribute.STRIKETHROUGH ) ); 045 this.foregroundColor = (Color) font.getAttributes( ) 046 .get( TextAttribute.FOREGROUND ); 047 this.backgroundColor = (Color) font.getAttributes( ) 048 .get( TextAttribute.BACKGROUND ); 049 } 050 051 public HtmlHighlightingElementRender( HighlightingElement element, 052 Color color, Font font, boolean underLine, boolean strikeOut ) 053 { 054 this.element = element; 055 this.fontSize = font.getSize( ); 056 this.fontStyle = font.getStyle( ); 057 this.fontName = font.getName( ); 058 this.underLine = underLine; 059 this.strikeOut = strikeOut; 060 this.foregroundColor = color; 061 } 062 063 public HtmlHighlightingElementRender( HighlightingElement element, 064 Color color, Font font ) 065 { 066 this.element = element; 067 this.fontSize = font.getSize( ); 068 this.fontStyle = font.getStyle( ); 069 this.fontName = font.getName( ); 070 this.foregroundColor = color; 071 } 072 073 public Color getBackgroundColor( ) 074 { 075 return backgroundColor; 076 } 077 078 public HighlightingElement getElement( ) 079 { 080 return element; 081 } 082 083 public String getFontName( ) 084 { 085 return fontName; 086 } 087 088 public int getFontSize( ) 089 { 090 return fontSize; 091 } 092 093 public int getFontStyle( ) 094 { 095 return fontStyle; 096 } 097 098 public Color getForegroundColor( ) 099 { 100 return foregroundColor; 101 } 102 103 public boolean isStrikeOut( ) 104 { 105 return strikeOut; 106 } 107 108 public boolean isUnderLine( ) 109 { 110 return underLine; 111 } 112 113 public void setBackgroundColor( Color backgroundColor ) 114 { 115 this.backgroundColor = backgroundColor; 116 } 117 118 public void setFontName( String fontName ) 119 { 120 this.fontName = fontName; 121 } 122 123 public void setFontSize( int fontSize ) 124 { 125 this.fontSize = fontSize; 126 } 127 128 public void setFontStyle( int fontStyle ) 129 { 130 this.fontStyle = fontStyle; 131 } 132 133 public void setForegroundColor( Color foregroundColor ) 134 { 135 this.foregroundColor = foregroundColor; 136 } 137 138 public void setStrikeOut( boolean strikeOut ) 139 { 140 this.strikeOut = strikeOut; 141 } 142 143 public void setUnderLine( boolean underLine ) 144 { 145 this.underLine = underLine; 146 } 147 148 public String render( String tokenText ) 149 { 150 StringBuffer buffer = new StringBuffer( ); 151 if ( fontName != null ) 152 buffer.append( "font-family:" ).append( fontName ).append( ";" ); 153 if ( fontSize != 0 ) 154 buffer.append( "font-size:" ).append( fontSize ).append( "pt;" ); 155 if ( fontStyle == Font.ITALIC ) 156 buffer.append( "font-sytle:italic;" ); 157 if ( fontStyle == Font.BOLD ) 158 buffer.append( "font-weight:bold;" ); 159 if ( isUnderLine( ) ) 160 buffer.append( "text-decoration: underline;" ); 161 if ( isStrikeOut( ) ) 162 buffer.append( "text-decoration: line-through;" ); 163 if ( foregroundColor != null ) 164 { 165 buffer.append( "color: " + color2String( foregroundColor ) + ";" ); 166 } 167 if ( backgroundColor != null ) 168 { 169 buffer.append( "background-color: " 170 + color2String( backgroundColor ) 171 + ";" ); 172 } 173 return "<span style=\"" 174 + buffer.toString( ) 175 + "\">" 176 + tokenText 177 + "</span>"; 178 } 179 180 public static String color2String( Color color ) 181 { 182 String R = Integer.toHexString( color.getRed( ) ); 183 R = R.length( ) < 2 ? ( '0' + R ) : R; 184 String B = Integer.toHexString( color.getBlue( ) ); 185 B = B.length( ) < 2 ? ( '0' + B ) : B; 186 String G = Integer.toHexString( color.getGreen( ) ); 187 G = G.length( ) < 2 ? ( '0' + G ) : G; 188 return '#' + R + G + B; 189 } 190}