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: Apr 10, 2002, 11:04:12 PM
022 * $Id: StringExpression.java 578592 2007-09-23 18:51:32Z bommel $
023 */
024
025
026 import org.apache.commons.logging.Log;
027 import org.apache.commons.logging.LogFactory;
028
029 import java.io.Serializable;
030 import java.util.Enumeration;
031 import java.util.Map;
032 import java.util.Vector;
033
034
035 public class StringExpression implements Serializable {
036
037 private static final Log LOG = LogFactory.getLog(StringExpression.class);
038
039 private static final long serialVersionUID = -152894508593519474L;
040
041 private String string;
042
043 public StringExpression(String string) {
044 this.string = string;
045 }
046
047 public String toString() {
048 return string;
049 }
050
051 public String substitute(Map<String, String> variables) {
052 return replaceVariables(string, variables);
053 }
054
055 // implementation copied from ant.ProjectHelper
056 private static String replaceVariables(
057 String stringExpression, Map<String, String> variables) {
058 if (stringExpression == null) {
059 return null;
060 }
061
062 Vector<String> fragments = new Vector<String>();
063 Vector<String> propertyRefs = new Vector<String>();
064 parsePropertyString(stringExpression, fragments, propertyRefs);
065
066 StringBuilder sb = new StringBuilder();
067 Enumeration<String> i = fragments.elements();
068 Enumeration<String> j = propertyRefs.elements();
069 while (i.hasMoreElements()) {
070 String fragment = i.nextElement();
071 if (fragment == null) {
072 String propertyName = j.nextElement();
073 if (!variables.containsKey(propertyName)) {
074 // throw exception ?
075 LOG.error("Property ${" + propertyName + "} has not been set");
076 }
077 fragment = (variables.containsKey(propertyName))
078 ? variables.get(propertyName)
079 : "${" + propertyName + "}";
080 }
081 sb.append(fragment);
082 }
083 return sb.toString();
084 }
085
086 private static void parsePropertyString(
087 String value, Vector<String> fragments, Vector<String> propertyRefs) {
088 int prev = 0;
089 int pos;
090 while ((pos = value.indexOf("$", prev)) >= 0) {
091 if (pos > 0) {
092 fragments.addElement(value.substring(prev, pos));
093 }
094 if (pos == (value.length() - 1)) {
095 fragments.addElement("$");
096 prev = pos + 1;
097 } else if (value.charAt(pos + 1) != '{') {
098 fragments.addElement(value.substring(pos + 1, pos + 2));
099 prev = pos + 2;
100 } else {
101 int endName = value.indexOf('}', pos);
102 if (endName < 0) {
103 throw new IllegalArgumentException("Syntax error in property: " + value);
104 }
105 String propertyName = value.substring(pos + 2, endName);
106 fragments.addElement(null);
107 propertyRefs.addElement(propertyName);
108 prev = endName + 1;
109 }
110 }
111 if (prev < value.length()) {
112 fragments.addElement(value.substring(prev));
113 }
114 }
115
116 }