무슨 깡으로 블로그에 SyntaxHighlighter 테스트를 하는지 모르겠으나, ANSI 컬러로 SQL colorize하는 소스코드를 붙여본다.
/*
* Copyright (c) 1999-2012, Jang-Ho Hwang, rath@xrath.com.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution. Neither the name of the
* Jang-Ho Hwang nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package rath.adrenaline.tool.colorize;
import rath.adrenaline.pcre.MatchResult;
import rath.adrenaline.pcre.Pattern;
import rath.adrenaline.pcre.Replacer;
import rath.adrenaline.pcre.Substitution;
import rath.adrenaline.util.ANSI;
/**
* Created by IntelliJ IDEA.
* User: rath
* Date: 21/01/2011
* Time: 22:13
*/
public class SqlColorize {
private static Pattern keywordsPattern = Pattern.compile(
"\\b(SELECT|FROM|INSERT|INTO|VALUES|UPDATE|SET|DELETE|VALUES|WHERE|" +
"LIKE|AND|ORDER BY|OR|GROUP BY|HAVING|UNION|LIMIT|USING|JOIN|OUTER|IS|NOT|ASC|DESC)\\b",
Pattern.CASE_INSENSITIVE);
private static Pattern constantPattern = Pattern.compile(
"\\b([0-9]+|NULL|true|false)\\b", Pattern.CASE_INSENSITIVE
);
private static Pattern stringPattern = Pattern.compile("('[^']+')");
private static Pattern functionPattern = Pattern.compile(
"\\b(COUNT|CONCAT|LENGTH|LOWER|UPPER|SUBSTRING|TRIM|" +
"ADDDATE|ADDTIME|CURDATE|NOW|DATE)\\b",
Pattern.CASE_INSENSITIVE);
private static Replacer keywordsReplacer = keywordsPattern.replacer(new Substitution() {
public void append(MatchResult matchResult, StringBuilder buf) {
String keyword = matchResult.group(1);
buf.append(ANSI.CYAN_BOLD);
buf.append(keyword);
buf.append(ANSI.RESET);
}
});
private static Replacer constantReplacer = constantPattern.replacer(new Substitution() {
public void append(MatchResult matchResult, StringBuilder buf) {
String constant = matchResult.group(1);
buf.append(ANSI.GREEN);
buf.append(constant);
buf.append(ANSI.RESET);
}
});
private static Replacer stringReplacer = stringPattern.replacer(new Substitution() {
public void append(MatchResult matchResult, StringBuilder buf) {
String constant = matchResult.group(1);
buf.append(ANSI.MAGENTA);
buf.append(constant);
buf.append(ANSI.RESET);
}
});
private static Replacer functionReplacer = functionPattern.replacer(new Substitution() {
public void append(MatchResult matchResult, StringBuilder buf) {
String constant = matchResult.group(1);
buf.append(ANSI.YELLOW);
buf.append(constant);
buf.append(ANSI.RESET);
}
});
public static String colorize(String sql) {
sql = constantReplacer.replace(sql);
sql = keywordsReplacer.replace(sql);
sql = stringReplacer.replace(sql);
sql = functionReplacer.replace(sql);
return sql;
}
}
워드프레스 LightWord 테마때문에 80 컬럼 안지킨거 딱 걸려서 테마설정 Layout settings => wider로 바꿨다. Java에서는 120컬럼 정도가 어떠할지.
Comments
1 thoughts shared
Continue Reading
Discover more thoughts and insights
MBTI는 맥락에 따라 달라진다
MBTI는 검사 시점에 스스로를 어느 롤로 지정했느냐에 따라 다른 값이 나온다. 질문지에 나오는 타인과 내 관계의 생존주기에 따라 다른 답을 하게 된다. 정규직 회사 동료들이나 오랜 친구들처럼 긴 관계들을 대할
Read more
이상하고 현실적인 처세
회사 생활 및 사회생활을 한지 어느덧 7년차가 되버렸다. 그러다보니 MBTI 의 16개 분류보다는 좀 더 범위가 좁고 특별한 집단 내에서 사람을 분류하는 방법을 터득해 나가게 되었다. IT회사를 상대하면서 느꼈던
Read more
네이트온 lib 개발 중 생각난 것
네이트온의 문자대화 기능과 싸이월드와 연동된 수많은 응용기술들을 생각하다보니 오픈소스로 하는 것은 예나 지금이나 SKT 무서워서 섣불리 못하겠고.. 오픈소스도 어렵고 그보다 리스키한 서비스도 어렵다면, 개인적으로
Read more