initial contribution
[phpeclipse.git] / archive / org.plog4u.wiki / src / org / plog4u / wiki / macro / code / AbstractCPPBasedCodeFilter.java
1 /*
2  * 
3  * Please visit http://radeox.org/ for updates and contact.
4  * 
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
7  * later version.
8  * 
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.
11  * 
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--
14  */
15
16 package org.plog4u.wiki.macro.code;
17
18 import java.util.HashMap;
19 import java.util.HashSet;
20
21 import org.radeox.filter.context.FilterContext;
22 import org.radeox.macro.code.SourceCodeFormatter;
23
24 /*
25  * Abstract C++ syntax based code filter
26  *  
27  */
28
29 abstract public class AbstractCPPBasedCodeFilter implements SourceCodeFormatter {
30
31   public static void appendChar(StringBuffer result, char currentChar) {
32     switch (currentChar) {
33       case '\"' : // special html escape character
34         result.append(""");
35         break;
36       case '<' : // special html escape character
37         result.append("&#60;");
38         break;
39       case '>' : // special html escape character
40         result.append("&#62;");
41         break;
42       case '&' : // special html escape character
43         result.append("&#38;");
44         break;
45       case '\'' : // special html escape character
46         result.append("&#39;");
47         break;
48       default :
49         result.append(currentChar);
50     }
51   }
52   public static void createHashMap(HashMap map, String str) {
53     map.put(str, "<b><font color=\"#750055\">"+str+"</font></b>");
54   }
55
56   public AbstractCPPBasedCodeFilter() {
57   }
58
59   private int appendIdentifier(
60     String input,
61     int identStart,
62     int currentPosition,
63     HashMap keywords,
64     HashSet objectWords,
65     StringBuffer result) {
66     String originalIdent = input.substring(identStart, --currentPosition);
67     String keywordIdent = originalIdent;
68     if (!isKeywordLowerCase()) {
69       keywordIdent = keywordIdent.toLowerCase();
70     }
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>");
78     } else {
79       result.append(originalIdent);
80     }
81     return currentPosition;
82   }
83
84   public String filter(String input, FilterContext context) {
85     char[] source = input.toCharArray();
86     int currentPosition = 0;
87     int identStart = 0;
88     char currentChar = ' ';
89
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\">");
95     try {
96       while (true) {
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++];
104         //          }
105         //          currentPosition = appendIdentifier(input, identStart, currentPosition, keywordsSet, objectsSet, result);
106         //          identFound = false;
107         //          continue; // while loop
108         //        } else
109         if ((currentChar >= 'A' && currentChar <= 'Z') || (currentChar == '_') || (currentChar >= 'a' && currentChar <= 'z')) {
110           identStart = currentPosition - 1;
111           identFound = true;
112           // start of identifier ?
113           while ((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z') || currentChar == '_') {
114             currentChar = source[currentPosition++];
115           }
116           currentPosition = appendIdentifier(input, identStart, currentPosition, keywordsSet, objectsSet, result);
117           identFound = false;
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] != '\\') {
126               break;
127             }
128           }
129           result.append("</font>");
130           continue;
131         } else if (currentChar == '/' && currentPosition < input.length() && source[currentPosition] == '/') {
132           // line comment
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') {
140               break;
141             }
142           }
143           result.append("</font>");
144           continue;
145         } else if (currentChar == '/' && currentPosition < input.length() && source[currentPosition] == '*') {
146           if (currentPosition < (input.length() - 1) && source[currentPosition + 1] == '*') {
147             // javadoc style
148             result.append("<font color=\"#3F5FBF\">");
149           } else {
150             // multiline comment
151             result.append("<font color=\"#3F7F5F\">");
152           }
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] == '*') {
159               break;
160             }
161           }
162           result.append("</font>");
163           continue;
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') {
168           // php start tag
169           currentPosition++;
170           result.append("<font color=\"#7F0000\">&#60;?php</font>");
171           continue;
172         } else if (currentChar == '?' && isPHPTag() && currentPosition < input.length() && source[currentPosition] == '>') {
173           // php start tag
174           currentPosition += 4;
175           result.append("<font color=\"#7F0000\">?&#62;</font>");
176           continue;
177         } 
178         appendChar(result, currentChar);
179
180       }
181     } catch (IndexOutOfBoundsException e) {
182       if (identFound) {
183         currentPosition = appendIdentifier(input, identStart, currentPosition, keywordsSet, null, result);
184       }
185     }
186     result.append("</font>");
187     return result.toString();
188   }
189   /**
190    * @return Returns the KEYWORD_SET.
191    */
192   abstract public HashMap getKeywordSet();
193
194   /**
195    * @return Returns the OBJECT_SET.
196    */
197   abstract public HashSet getObjectSet();
198
199   public int getPriority() {
200     return 0;
201   }
202
203   /**
204    * @return Returns the KEYWORD_MAP.
205    */
206   public boolean isKeywordLowerCase() {
207     return true;
208   }
209
210   /**
211    *  
212    */
213   public boolean isPHPTag() {
214     return false;
215   }
216 }