3 * Please visit http://radeox.org/ for updates and contact.
5 * --LICENSE NOTICE-- This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
6 * General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any
9 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free
13 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --LICENSE NOTICE--
16 package org.plog4u.wiki.macro.code;
18 import java.util.HashMap;
19 import java.util.HashSet;
21 import org.radeox.filter.context.FilterContext;
22 import org.radeox.macro.code.SourceCodeFormatter;
25 * Abstract C++ syntax based code filter
29 abstract public class AbstractCPPBasedCodeFilter implements SourceCodeFormatter {
31 public static void appendChar(StringBuffer result, char currentChar) {
32 switch (currentChar) {
33 case '\"' : // special html escape character
34 result.append(""");
36 case '<' : // special html escape character
37 result.append("<");
39 case '>' : // special html escape character
40 result.append(">");
42 case '&' : // special html escape character
43 result.append("&");
45 case '\'' : // special html escape character
46 result.append("'");
49 result.append(currentChar);
52 public static void createHashMap(HashMap map, String str) {
53 map.put(str, "<b><font color=\"#750055\">"+str+"</font></b>");
56 public AbstractCPPBasedCodeFilter() {
59 private int appendIdentifier(
65 StringBuffer result) {
66 String originalIdent = input.substring(identStart, --currentPosition);
67 String keywordIdent = originalIdent;
68 if (!isKeywordLowerCase()) {
69 keywordIdent = keywordIdent.toLowerCase();
71 String keywordValue = (String) keywords.get(keywordIdent);
72 if (keywordValue!=null) {
73 result.append(keywordValue);
74 // } else if (objectWords != null && objectWords.contains(originalIdent)) {
75 // result.append("<font color=\"#7F9FBF\">");
76 // result.append(originalIdent);
77 // result.append("</font>");
79 result.append(originalIdent);
81 return currentPosition;
84 public String filter(String input, FilterContext context) {
85 char[] source = input.toCharArray();
86 int currentPosition = 0;
88 char currentChar = ' ';
90 HashMap keywordsSet = getKeywordSet();
91 HashSet objectsSet = getObjectSet();
92 StringBuffer result = new StringBuffer(input.length() + input.length() / 4);
93 boolean identFound = false;
94 result.append("<font color=\"#000000\">");
97 currentChar = source[currentPosition++];
98 // if (currentChar >= 'a' && currentChar <= 'z' && isKeywordLowerCase()) {
99 // identStart = currentPosition - 1;
100 // identFound = true;
101 // // start of identifier ?
102 // while (currentChar >= 'a' && currentChar <= 'z') {
103 // currentChar = source[currentPosition++];
105 // currentPosition = appendIdentifier(input, identStart, currentPosition, keywordsSet, objectsSet, result);
106 // identFound = false;
107 // continue; // while loop
109 if ((currentChar >= 'A' && currentChar <= 'Z') || (currentChar == '_') || (currentChar >= 'a' && currentChar <= 'z')) {
110 identStart = currentPosition - 1;
112 // start of identifier ?
113 while ((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z') || currentChar == '_') {
114 currentChar = source[currentPosition++];
116 currentPosition = appendIdentifier(input, identStart, currentPosition, keywordsSet, objectsSet, result);
118 continue; // while loop
119 } else if (currentChar == '\"') { //strings
120 result.append("<font color=\"#2A00FF\">");
121 appendChar(result, currentChar);
122 while (currentPosition < input.length()) {
123 currentChar = source[currentPosition++];
124 appendChar(result, currentChar);
125 if (currentChar == '\"' && source[currentPosition - 2] != '\\') {
129 result.append("</font>");
131 } else if (currentChar == '/' && currentPosition < input.length() && source[currentPosition] == '/') {
133 result.append("<font color=\"#3F7F5F\">");
134 appendChar(result, currentChar);
135 appendChar(result, source[currentPosition++]);
136 while (currentPosition < input.length()) {
137 currentChar = source[currentPosition++];
138 appendChar(result, currentChar);
139 if (currentChar == '\n') {
143 result.append("</font>");
145 } else if (currentChar == '/' && currentPosition < input.length() && source[currentPosition] == '*') {
146 if (currentPosition < (input.length() - 1) && source[currentPosition + 1] == '*') {
148 result.append("<font color=\"#3F5FBF\">");
151 result.append("<font color=\"#3F7F5F\">");
153 appendChar(result, currentChar);
154 appendChar(result, source[currentPosition++]);
155 while (currentPosition < input.length()) {
156 currentChar = source[currentPosition++];
157 appendChar(result, currentChar);
158 if (currentChar == '/' && source[currentPosition - 2] == '*') {
162 result.append("</font>");
164 } else if (currentChar == '<' && isPHPTag() && currentPosition+3 < input.length() && source[currentPosition] == '?'
165 && source[currentPosition+1] == 'p'
166 && source[currentPosition+2] == 'h'
167 && source[currentPosition+3] == 'p') {
170 result.append("<font color=\"#7F0000\"><?php</font>");
172 } else if (currentChar == '?' && isPHPTag() && currentPosition < input.length() && source[currentPosition] == '>') {
174 currentPosition += 4;
175 result.append("<font color=\"#7F0000\">?></font>");
178 appendChar(result, currentChar);
181 } catch (IndexOutOfBoundsException e) {
183 currentPosition = appendIdentifier(input, identStart, currentPosition, keywordsSet, null, result);
186 result.append("</font>");
187 return result.toString();
190 * @return Returns the KEYWORD_SET.
192 abstract public HashMap getKeywordSet();
195 * @return Returns the OBJECT_SET.
197 abstract public HashSet getObjectSet();
199 public int getPriority() {
204 * @return Returns the KEYWORD_MAP.
206 public boolean isKeywordLowerCase() {
213 public boolean isPHPTag() {