[hmath-commits] org.hmath.server/WEB-INF/src/org/hartmath/server/macro/code JavaCodeFilter.java,NONE
Status: Pre-Alpha
Brought to you by:
jsurfer
|
From: Klaus H. <js...@us...> - 2004-04-14 20:06:32
|
Update of /cvsroot/hmath/org.hmath.server/WEB-INF/src/org/hartmath/server/macro/code In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22664/WEB-INF/src/org/hartmath/server/macro/code Added Files: JavaCodeFilter.java PHPCodeFilter.java CHashCodeFilter.java AbstractCPPBasedCodeFilter.java Log Message: misc changes --- NEW FILE: AbstractCPPBasedCodeFilter.java --- /* * This file is part of "HMath Project". * * Copyright (c) 2004 Klaus Hartlage * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser * General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any * later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --LICENSE NOTICE-- */ package org.hartmath.server.macro.code; import java.util.HashMap; import java.util.HashSet; import org.radeox.filter.context.FilterContext; import org.radeox.macro.code.SourceCodeFormatter; /* * Abstract C++ syntax based code filter * */ abstract public class AbstractCPPBasedCodeFilter implements SourceCodeFormatter { public static void appendChar(StringBuffer result, char currentChar) { switch (currentChar) { case '\"' : // special html escape character result.append("""); break; case '<' : // special html escape character result.append("<"); break; case '>' : // special html escape character result.append(">"); break; case '&' : // special html escape character result.append("&"); break; case '\'' : // special html escape character result.append("'"); break; default : result.append(currentChar); } } public static void createHashMap(HashMap map, String str) { map.put(str, "<b><font color=\"#750055\">"+str+"</font></b>"); } public AbstractCPPBasedCodeFilter() { } private int appendIdentifier( String input, int identStart, int currentPosition, HashMap keywords, HashSet objectWords, StringBuffer result) { String originalIdent = input.substring(identStart, --currentPosition); String keywordIdent = originalIdent; if (!isKeywordLowerCase()) { keywordIdent = keywordIdent.toLowerCase(); } String keywordValue = (String) keywords.get(keywordIdent); if (keywordValue!=null) { result.append(keywordValue); // } else if (objectWords != null && objectWords.contains(originalIdent)) { // result.append("<font color=\"#7F9FBF\">"); // result.append(originalIdent); // result.append("</font>"); } else { result.append(originalIdent); } return currentPosition; } public String filter(String input, FilterContext context) { char[] source = input.toCharArray(); int currentPosition = 0; int identStart = 0; char currentChar = ' '; HashMap keywordsSet = getKeywordSet(); HashSet objectsSet = getObjectSet(); StringBuffer result = new StringBuffer(input.length() + input.length() / 4); boolean identFound = false; result.append("<font color=\"#000000\">"); try { while (true) { currentChar = source[currentPosition++]; // if (currentChar >= 'a' && currentChar <= 'z' && isKeywordLowerCase()) { // identStart = currentPosition - 1; // identFound = true; // // start of identifier ? // while (currentChar >= 'a' && currentChar <= 'z') { // currentChar = source[currentPosition++]; // } // currentPosition = appendIdentifier(input, identStart, currentPosition, keywordsSet, objectsSet, result); // identFound = false; // continue; // while loop // } else if ((currentChar >= 'A' && currentChar <= 'Z') || (currentChar == '_') || (currentChar >= 'a' && currentChar <= 'z')) { identStart = currentPosition - 1; identFound = true; // start of identifier ? while ((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z') || currentChar == '_') { currentChar = source[currentPosition++]; } currentPosition = appendIdentifier(input, identStart, currentPosition, keywordsSet, objectsSet, result); identFound = false; continue; // while loop } else if (currentChar == '\"') { //strings result.append("<font color=\"#2A00FF\">"); appendChar(result, currentChar); while (currentPosition < input.length()) { currentChar = source[currentPosition++]; appendChar(result, currentChar); if (currentChar == '\"' && source[currentPosition - 2] != '\\') { break; } } result.append("</font>"); continue; } else if (currentChar == '/' && currentPosition < input.length() && source[currentPosition] == '/') { // line comment result.append("<font color=\"#3F7F5F\">"); appendChar(result, currentChar); appendChar(result, source[currentPosition++]); while (currentPosition < input.length()) { currentChar = source[currentPosition++]; appendChar(result, currentChar); if (currentChar == '\n') { break; } } result.append("</font>"); continue; } else if (currentChar == '/' && currentPosition < input.length() && source[currentPosition] == '*') { if (currentPosition < (input.length() - 1) && source[currentPosition + 1] == '*') { // javadoc style result.append("<font color=\"#3F5FBF\">"); } else { // multiline comment result.append("<font color=\"#3F7F5F\">"); } appendChar(result, currentChar); appendChar(result, source[currentPosition++]); while (currentPosition < input.length()) { currentChar = source[currentPosition++]; appendChar(result, currentChar); if (currentChar == '/' && source[currentPosition - 2] == '*') { break; } } result.append("</font>"); continue; } else if (currentChar == '<' && isPHPTag() && currentPosition+3 < input.length() && source[currentPosition] == '?' && source[currentPosition+1] == 'p' && source[currentPosition+2] == 'h' && source[currentPosition+3] == 'p') { // php start tag currentPosition++; result.append("<font color=\"#7F0000\"><?php</font>"); continue; } else if (currentChar == '?' && isPHPTag() && currentPosition < input.length() && source[currentPosition] == '>') { // php start tag currentPosition += 4; result.append("<font color=\"#7F0000\">?></font>"); continue; } appendChar(result, currentChar); } } catch (IndexOutOfBoundsException e) { if (identFound) { currentPosition = appendIdentifier(input, identStart, currentPosition, keywordsSet, null, result); } } result.append("</font>"); return result.toString(); } /** * @return Returns the KEYWORD_SET. */ abstract public HashMap getKeywordSet(); /** * @return Returns the OBJECT_SET. */ abstract public HashSet getObjectSet(); public int getPriority() { return 0; } /** * @return Returns the KEYWORD_MAP. */ public boolean isKeywordLowerCase() { return true; } /** * */ public boolean isPHPTag() { return false; } } --- NEW FILE: JavaCodeFilter.java --- /* * This file is part of "HMath Project". * * Copyright (c) 2004 Klaus Hartlage * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser * General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any * later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --LICENSE NOTICE-- */ package org.hartmath.server.macro.code; import java.util.HashMap; import java.util.HashSet; import org.radeox.macro.code.SourceCodeFormatter; /** * Java CodeFilter colourizes C# source code * */ public class JavaCodeFilter extends AbstractCPPBasedCodeFilter implements SourceCodeFormatter { private static HashMap KEYWORD_SET = new HashMap(); private static final String[] KEYWORDS = { "class", "abstract", "break", "byvalue", "case", "cast", "catch", "const", "continue", "default", "do", "else", "extends", "false", "final", "finally", "for", "future", "generic", "goto", "if", "implements", "import", "inner", "instanceof", "interface", "native", "new", "null", "operator", "outer", "package", "private", "protected", "public", "rest", "return", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "var", "volatile", "while", "assert", "enum" }; private static final String[] OBJECT_WORDS = { "Boolean", "Byte", "Character", "Class", "ClassLoader", "Cloneable", "Compiler", "Double", "Float", "Integer", "Long", "Math", "Number", "Object", "Process", "Runnable", "Runtime", "SecurityManager", "Short", "String", "StringBuffer", "System", "Thread", "ThreadGroup", "Void", "boolean", "char", "byte", "short", "int", "long", "float", "double", "void"}; private static HashSet OBJECT_SET = new HashSet(); { for (int i = 0; i < KEYWORDS.length; i++) { //KEYWORD_SET.put(KEYWORDS[i], ""+KEYWORDS[i]+""); createHashMap(KEYWORD_SET, KEYWORDS[i]); } for (int i = 0; i < OBJECT_WORDS.length; i++) { OBJECT_SET.add(OBJECT_WORDS[i]); } } public JavaCodeFilter() { } /** * @return Returns the KEYWORD_SET. */ public HashMap getKeywordSet() { return KEYWORD_SET; } public String getName() { return "java"; } /** * @return Returns the OBJECT_SET. */ public HashSet getObjectSet() { return OBJECT_SET; } } --- NEW FILE: PHPCodeFilter.java --- /* * This file is part of "HMath Project". * * Copyright (c) 2004 Klaus Hartlage * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser * General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any * later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --LICENSE NOTICE-- */ package org.hartmath.server.macro.code; import java.util.HashMap; import java.util.HashSet; import org.radeox.macro.code.SourceCodeFormatter; /** * Java CodeFilter colourizes C# source code * */ public class PHPCodeFilter extends AbstractCPPBasedCodeFilter implements SourceCodeFormatter { private static HashMap KEYWORD_SET = new HashMap(); private static final String[] KEYWORDS = { "if", "elseif", "else", "endif", "for", "endfor", "while", "endwhile", "switch", "case", "endswitch", "break", "continue", "return", "include", "include_once", "require", "require_once", "function", "class", "new", "do", "old_function", "default", "global", "static", "foreach", "endforeach", "extends", "empty", "array", "echo", "var", "as", "print", "unset", "exit", "and", "or", "xor", "list", "null", "false", "true", "abstract", "catch", "finally", "try", "private", "protected", "public", "interface", "implements", "instanceof", "super", "throw", "const", "declare", "enddeclare", "eval", "use", "isset", "final" }; { for (int i = 0; i < KEYWORDS.length; i++) { //KEYWORD_SET.put(KEYWORDS[i], ""+KEYWORDS[i]+""); createHashMap(KEYWORD_SET, KEYWORDS[i]); } } public PHPCodeFilter() { } /** * @return Returns the KEYWORD_SET. */ public HashMap getKeywordSet() { return KEYWORD_SET; } public String getName() { return "php"; } /** * @return Returns the OBJECT_SET. */ public HashSet getObjectSet() { return null; } /* * (non-Javadoc) * * @see org.hartmath.server.macro.code.AbstractCPPBasedCodeFilter#isKeywordLowerCase() */ public boolean isKeywordLowerCase() { return false; } /** * */ public boolean isPHPTag() { return true; } } --- NEW FILE: CHashCodeFilter.java --- /* * This file is part of "HMath Project". * * Copyright (c) 2004 Klaus Hartlage * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser * General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any * later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --LICENSE NOTICE-- */ package org.hartmath.server.macro.code; import java.util.HashMap; import java.util.HashSet; import org.radeox.macro.code.SourceCodeFormatter; /** * C# CodeFilter colourizes C# source code * */ public class CHashCodeFilter extends AbstractCPPBasedCodeFilter implements SourceCodeFormatter { private static HashMap KEYWORD_SET = new HashMap(); private static final String[] KEYWORDS = { "class", "abstract", "event", "new", "struct", "as", "explicit", "null", "switch", "base", "extern", "this", "false", "operator", "throw", "break", "finally", "out", "true", "fixed", "override", "try", "case", "float", "params", "typeof", "catch", "for", "private", "foreach", "protected", "checked", "goto", "public", "unchecked", "if", "readonly", "unsafe", "const", "implicit", "ref", "continue", "in", "return", "using", "virtual", "default", "interface", "sealed", "volatile", "delegate", "internal", "do", "is", "sizeof", "while", "lock", "stackalloc", "else", "static", "enum", "namespace", }; private static final String[] OBJECT_WORDS = { "object", "bool", "byte", "float", "uint", "char", "ulong", "ushort", "decimal", "int", "sbyte", "short", "void", "double", "long", "string" }; private static HashSet OBJECT_SET = new HashSet(); { for (int i = 0; i < KEYWORDS.length; i++) { //KEYWORD_SET.put(KEYWORDS[i], ""+KEYWORDS[i]+""); createHashMap(KEYWORD_SET, KEYWORDS[i]); } for (int i = 0; i < OBJECT_WORDS.length; i++) { OBJECT_SET.add(OBJECT_WORDS[i]); } } public CHashCodeFilter() { } /** * @return Returns the KEYWORD_SET. */ public HashMap getKeywordSet() { return KEYWORD_SET; } public String getName() { return "chash"; } /** * @return Returns the OBJECT_SET. */ public HashSet getObjectSet() { return OBJECT_SET; } } |