001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020package org.sonar.colorizer; 021 022import org.sonar.channel.CodeReader; 023 024import java.util.Collections; 025import java.util.HashSet; 026import java.util.Set; 027import java.util.regex.Matcher; 028import java.util.regex.Pattern; 029 030/** 031 * Detect case-sensitive keywords 032 */ 033public class KeywordsTokenizer extends NotThreadSafeTokenizer { 034 035 private final String tagBefore; 036 private final String tagAfter; 037 private boolean caseInsensitive = false; 038 private Matcher matcher; 039 private final StringBuilder tmpBuilder = new StringBuilder(); 040 private static final String DEFAULT_REGEX = "[a-zA-Z_][a-zA-Z0-9_]*+"; 041 042 private Set<String> keywords = new HashSet<String>(); 043 044 public KeywordsTokenizer(String tagBefore, String tagAfter, Set<String> keywords) { 045 this(tagBefore, tagAfter, keywords, DEFAULT_REGEX); 046 } 047 048 public KeywordsTokenizer(String tagBefore, String tagAfter, Set<String> keywords, String regex) { 049 this.tagBefore = tagBefore; 050 this.tagAfter = tagAfter; 051 this.keywords = keywords; 052 this.matcher = Pattern.compile(regex).matcher(""); 053 } 054 055 public KeywordsTokenizer(String tagBefore, String tagAfter, String... keywords) { 056 this.tagBefore = tagBefore; 057 this.tagAfter = tagAfter; 058 Collections.addAll(this.keywords, keywords); 059 this.matcher = Pattern.compile(DEFAULT_REGEX).matcher(""); 060 } 061 062 @Override 063 public boolean consume(CodeReader code, HtmlCodeBuilder codeBuilder) { 064 if (code.popTo(matcher, tmpBuilder) > 0) { 065 if (isKeyword(tmpBuilder.toString())) { 066 codeBuilder.appendWithoutTransforming(tagBefore); 067 codeBuilder.append(tmpBuilder); 068 codeBuilder.appendWithoutTransforming(tagAfter); 069 } else { 070 codeBuilder.append(tmpBuilder); 071 } 072 tmpBuilder.delete(0, tmpBuilder.length()); 073 return true; 074 } 075 return false; 076 } 077 078 private boolean isKeyword(String word) { 079 if ( !caseInsensitive && keywords.contains(word)) { 080 return true; 081 } else if (caseInsensitive && keywords.contains(word.toUpperCase())) { 082 return true; 083 } 084 return false; 085 } 086 087 public void setCaseInsensitive(boolean caseInsensitive) { 088 this.caseInsensitive = caseInsensitive; 089 } 090 091 @Override 092 public KeywordsTokenizer clone() { 093 KeywordsTokenizer clone = new KeywordsTokenizer(tagBefore, tagAfter, keywords, matcher.pattern().pattern()); 094 clone.caseInsensitive = caseInsensitive; 095 return clone; 096 } 097}