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:33:19
022 * $Id: AbstractConverter.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.oro.text.perl.Perl5Util;
028 import org.apache.oro.text.regex.MalformedPatternException;
029 import org.apache.oro.text.regex.MatchResult;
030 import org.apache.oro.text.regex.Pattern;
031 import org.apache.oro.text.regex.PatternMatcherInput;
032 import org.apache.oro.text.regex.Perl5Compiler;
033 import org.apache.oro.text.regex.Perl5Matcher;
034
035 public abstract class AbstractConverter implements Converter {
036
037 private static final Log LOG = LogFactory.getLog(AbstractConverter.class);
038
039 private Pattern pattern;
040
041 private Perl5Matcher matcher = new Perl5Matcher();
042 private Perl5Compiler compiler = new Perl5Compiler();
043 private Perl5Util util = new Perl5Util();
044
045 public abstract Pattern initPattern() throws MalformedPatternException;
046
047 public Perl5Compiler getCompiler() {
048 return compiler;
049 }
050
051 public Perl5Util getUtil() {
052 return util;
053 }
054
055 public Pattern getPattern() {
056 if (pattern == null) {
057 try {
058 pattern = initPattern();
059 } catch (MalformedPatternException e) {
060 LOG.error("", e);
061 }
062 }
063 return pattern;
064 }
065
066 private String getFragment(String data, int start, int end) {
067 return data.substring(start, end);
068 }
069
070 protected String convertMisc(String data, int start, int end) {
071 return convertMisc(getFragment(data, start, end));
072 }
073
074 protected String convertMatch(String data, int start, int end) {
075 return convertMatch(getFragment(data, start, end));
076 }
077
078 public String convertMisc(String fragment) {
079 return fragment;
080 }
081
082 public String convert(String input) {
083 StringBuilder buffer = new StringBuilder();
084 int lastStart = 0;
085 PatternMatcherInput patternMatcherInput = new PatternMatcherInput(input);
086 Pattern pattern = getPattern();
087 if (matcher.contains(patternMatcherInput, pattern)) {
088 do {
089 MatchResult result = matcher.getMatch();
090 int start = result.beginOffset(0);
091 int end = result.endOffset(0);
092 buffer.append(convertMisc(input, lastStart, start));
093 buffer.append(convertMatch(input, start, end));
094 lastStart = end;
095 } while (matcher.contains(patternMatcherInput, pattern));
096 }
097 buffer.append(convertMisc(input, lastStart, input.length()));
098 return buffer.toString();
099 }
100
101 public String highlightStrings(String input) {
102 return util.substitute("s/(\".*?\")/<span class=\"string\">$1<\\/span>/g", input);
103 }
104
105 }