%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
net.sf.infrared.web.util.sql.SQLToHtml |
|
|
1 | /* |
|
2 | * Copyright 2005 Tavant Technologies and Contributors |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | * |
|
16 | * |
|
17 | * |
|
18 | * Original Author: chetan.mehrotra (Tavant Technologies) |
|
19 | * Contributor(s): -; |
|
20 | * |
|
21 | */ |
|
22 | ||
23 | package net.sf.infrared.web.util.sql; |
|
24 | ||
25 | import java.io.IOException; |
|
26 | import java.io.StringReader; |
|
27 | ||
28 | import net.sf.infrared.base.util.LoggingFactory; |
|
29 | ||
30 | import org.apache.log4j.Logger; |
|
31 | ||
32 | /** |
|
33 | * Most of the code in this package is taken from <a href="http://ostermiller.org/syntax/">syntax |
|
34 | * highlighting</a> package. The only change that is done is the SQLLexer has been changed |
|
35 | * to allow double quotes for enclosing strings. For this the regular expression |
|
36 | * for string has been tweaked. |
|
37 | * |
|
38 | * Also a new categaroy of quoted strings is added |
|
39 | * |
|
40 | * This package also uses <a href="http://home.comcast.net/~danmeany/sqlformatter.html"> |
|
41 | * SQLFormatter</a> for SQL formatting. |
|
42 | * |
|
43 | * |
|
44 | * @author chetan.mehrotra |
|
45 | * @date Dec 15, 2005 |
|
46 | * @version $Revision: 1.2 $ |
|
47 | */ |
|
48 | 0 | public class SQLToHtml { |
49 | ||
50 | 0 | private static final Logger logger = LoggingFactory.getLogger(SQLToHtml.class.getName()); |
51 | private static final String SPAN_OPEN = "<span class="; |
|
52 | private static final String SPAN_CLOSE = "</span>"; |
|
53 | private static final String GT = ">"; |
|
54 | ||
55 | public static String convertToHtml(String sql){ |
|
56 | 0 | SQLFormatter formatter = new SQLFormatter(); |
57 | 0 | formatter.setText(sql); |
58 | 0 | formatter.format(); |
59 | 0 | sql = formatter.getText(); |
60 | 0 | String formattedString = null; |
61 | try { |
|
62 | 0 | formattedString = parseSql(sql); |
63 | 0 | } catch (IOException e) { |
64 | 0 | formattedString = sql; |
65 | 0 | logger.error("Not able to parse the sql", e); |
66 | 0 | } |
67 | 0 | return formattedString; |
68 | } |
|
69 | ||
70 | public static String parseSql(String sql) throws IOException { |
|
71 | 0 | StringReader reader = new StringReader(sql); |
72 | 0 | Lexer sqlLexer = new SQLLexer(reader); |
73 | ||
74 | 0 | StringBuffer sb = new StringBuffer(); |
75 | 0 | String currentDescription = null; |
76 | Token token; |
|
77 | 0 | sb.append("<pre>"); |
78 | 0 | while((token = sqlLexer.getNextToken()) != null){ |
79 | // optimization implemented here: |
|
80 | // ignored white space can be put in the same span as the stuff |
|
81 | // around it. This saves space because spans don't have to be |
|
82 | // opened and closed. |
|
83 | 0 | if (token.isWhiteSpace() || |
84 | (currentDescription != null && token.getDescription().equals(currentDescription))){ |
|
85 | 0 | writeEscapedHTML(token.getContents(), sb); |
86 | } else { |
|
87 | 0 | if (currentDescription != null) closeSpan(sb); |
88 | 0 | currentDescription = token.getDescription(); |
89 | 0 | openSpan(currentDescription, sb); |
90 | 0 | writeEscapedHTML(token.getContents(), sb); |
91 | } |
|
92 | } |
|
93 | 0 | if (currentDescription != null) closeSpan(sb); |
94 | 0 | sb.append("</pre>"); |
95 | 0 | return sb.toString(); |
96 | } |
|
97 | ||
98 | private static void openSpan(String description,StringBuffer sb){ |
|
99 | 0 | sb.append(SPAN_OPEN).append(description).append(GT); |
100 | 0 | } |
101 | ||
102 | private static void closeSpan(StringBuffer sb){ |
|
103 | 0 | sb.append(SPAN_CLOSE); |
104 | 0 | } |
105 | ||
106 | /** |
|
107 | * Write the string after escaping characters that would hinder |
|
108 | * it from rendering in html. |
|
109 | * |
|
110 | * @param text The string to be escaped and written |
|
111 | * @param out output gets written here |
|
112 | */ |
|
113 | public static void writeEscapedHTML(String text, StringBuffer sb){ |
|
114 | 0 | for (int i=0; i < text.length(); i++){ |
115 | 0 | char ch = text.class="keyword">charAt(i); |
116 | 0 | switch(ch){ |
117 | case '<': { |
|
118 | 0 | sb.append("<"); |
119 | 0 | break; |
120 | } |
|
121 | case '>': { |
|
122 | 0 | sb.append(">"); |
123 | 0 | break; |
124 | } |
|
125 | case '&': { |
|
126 | 0 | sb.append("&"); |
127 | 0 | break; |
128 | } |
|
129 | case '"': { |
|
130 | 0 | sb.append("""); |
131 | 0 | break; |
132 | } |
|
133 | default: { |
|
134 | 0 | sb.append(ch); |
135 | break; |
|
136 | } |
|
137 | } |
|
138 | } |
|
139 | 0 | } |
140 | ||
141 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |