001 package org.apache.myfaces.tobago.example.demo.jsp;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 /*
021 * Created on: 02.09.2002, 23:37:54
022 * $Id: JspTagConverter.java 578592 2007-09-23 18:51:32Z bommel $
023 */
024
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027 import org.apache.myfaces.tobago.util.XmlUtils;
028 import org.apache.oro.text.regex.MalformedPatternException;
029 import org.apache.oro.text.regex.Pattern;
030
031 import java.util.HashMap;
032 import java.util.Map;
033
034 public class JspTagConverter extends AbstractConverter {
035
036 private static final Log LOG = LogFactory.getLog(JspTagConverter.class);
037
038 private TagConverter tagConverter = new TagConverter();
039 private Map<String, String> tags = new HashMap<String, String>();
040
041 public Pattern initPattern() throws MalformedPatternException {
042 // return compiler.compile("(?s)<%.*?%>");
043 // return compiler.compile("(?s)<%(--)?.*?\\1%>");
044 return getCompiler().compile("(?s)(<%--.*?--%>)|(<%.*?%>)");
045 }
046
047 public String highlightJavaKeyword(String java) {
048 return getUtil().substitute("s/(\\bassert\\b|break\\b|\\bbyte\\b|\\bboolean\\b"
049 + "|\\bcatch\\b|\\bcase\\b|\\bchar\\b|\\bcontinue\\b|\\bdouble\\b"
050 + "|\\bdo\\b|\\belse\\b|\\bextends\\b|\\bfalse\\b|\\bfinal\\b"
051 + "|\\bfloat\\b|\\bfor\\b|\\bfinally\\b|\\bif\\b|\\bimplements\\b"
052 + "|\\bint\\b|\\binterface\\b|\\binstanceof\\b|\\blong\\b|\\blength\\b"
053 + "|\\bnew\\b|\\bnull\\b|\\bprivate\\b|\\bprotected\\b|\\bpublic\\b"
054 + "|\\breturn\\b|\\bswitch\\b|\\bsynchronized\\b|\\bshort\\b"
055 + "|\\bstatic\\b|\\bsuper\\b|\\btry\\b|\\btrue\\b|\\bthis\\b"
056 + "|\\bthrow\\b|\\bthrows\\b|\\bvoid\\b|\\bwhile\\b)"
057 + "/<span class=\"keyword\">$1<\\/span>/g", java);
058 }
059
060 public String convertMatch(String fragment) {
061 String key = "tag" + tags.size();;
062 String tag = XmlUtils.escape(fragment, false);
063 if (fragment.startsWith("<%--")) {
064 tag = "<span class=\"jsp-comment\">" + tag + "</span>";
065 } else if (fragment.startsWith("<%@")) {
066 tag = "<span class=\"jsp-directive\">" + tag + "</span>";
067 } else if (fragment.startsWith("<%!")) {
068 // XXX temporarily hide strings to avoid keyword highlighting in strings
069 tag = highlightStrings(tag);
070 tag = highlightJavaKeyword(tag);
071 tag = "<span class=\"jsp-declaration\">" + tag + "</span>";
072 } else if (fragment.startsWith("<%=")) {
073 tag = highlightStrings(tag);
074 tag = "<span class=\"jsp-scriptlet\">" + tag + "</span>";
075 } else if (fragment.startsWith("<%")) {
076 // XXX temporarily hide strings to avoid keyword highlighting in strings
077 tag = highlightStrings(tag);
078 tag = highlightJavaKeyword(tag);
079 tag = "<span class=\"jsp-tag\">" + tag + "</span>";
080 } else {
081 LOG.error("error: " + fragment);
082 }
083 tags.put(key, tag);
084 return "${" + key + "}";
085 }
086
087 public String convert(String input) {
088 String result; // = StringUtils.replace(input, "$", "$$");
089 result = super.convert(input);
090 result = tagConverter.convert(result);
091 StringExpression stringExpression = new StringExpression(result);
092 return stringExpression.substitute(tags);
093 }
094
095 }