Java tutorial
/* * Copyright 2002-2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Portion of code taken from : * org.springframework.beans.factory.config.PropertyPlaceholderConfigurer */ package com.surveypanel.utils; import java.util.HashSet; import java.util.Set; import org.apache.log4j.Logger; import org.springframework.util.StringUtils; import com.surveypanel.form.Form; import com.surveypanel.form.QuestionImpl; public class PlaceHolderParser { private static Logger logger = Logger.getLogger(PlaceHolderParser.class); public static final String DEFAULT_PLACEHOLDER_PREFIX = "$("; public static final String DEFAULT_PLACEHOLDER_SUFFIX = ")"; public static String parseStringValue(Form form, String strVal) { return parseStringValue(form, strVal, new HashSet<String>()); } protected static String parseStringValue(Form form, String strVal, Set<String> visitedPlaceholders) { StringBuilder buf = new StringBuilder(strVal); int startIndex = strVal.indexOf(DEFAULT_PLACEHOLDER_PREFIX); while (startIndex != -1) { int endIndex = findPlaceholderEndIndex(buf, startIndex); if (endIndex != -1) { String placeholder = buf.substring(startIndex + DEFAULT_PLACEHOLDER_PREFIX.length(), endIndex); if (!visitedPlaceholders.add(placeholder)) { logger.error("Circular placeholder reference '" + placeholder + "' in property definitions"); return placeholder; } // Recursive invocation, parsing placeholders contained in the // placeholder key. placeholder = parseStringValue(form, placeholder, visitedPlaceholders); // Now obtain the value for the fully resolved key... String propVal = resolvePlaceholder(form, placeholder); if (propVal != null) { // Recursive invocation, parsing placeholders contained in // the // previously resolved placeholder value. propVal = parseStringValue(form, propVal, visitedPlaceholders); buf.replace(startIndex, endIndex + DEFAULT_PLACEHOLDER_SUFFIX.length(), propVal); if (logger.isTraceEnabled()) { logger.trace("Resolved placeholder '" + placeholder + "'"); } startIndex = buf.indexOf(DEFAULT_PLACEHOLDER_PREFIX, startIndex + propVal.length()); } else { // Proceed with unprocessed value. startIndex = buf.indexOf(DEFAULT_PLACEHOLDER_PREFIX, endIndex + DEFAULT_PLACEHOLDER_SUFFIX.length()); } visitedPlaceholders.remove(placeholder); } else { startIndex = -1; } } return buf.toString(); } private static int findPlaceholderEndIndex(CharSequence buf, int startIndex) { int index = startIndex + DEFAULT_PLACEHOLDER_PREFIX.length(); int withinNestedPlaceholder = 0; while (index < buf.length()) { if (StringUtils.substringMatch(buf, index, DEFAULT_PLACEHOLDER_SUFFIX)) { if (withinNestedPlaceholder > 0) { withinNestedPlaceholder--; index = index + DEFAULT_PLACEHOLDER_SUFFIX.length(); } else { return index; } } else if (StringUtils.substringMatch(buf, index, DEFAULT_PLACEHOLDER_PREFIX)) { withinNestedPlaceholder++; index = index + DEFAULT_PLACEHOLDER_PREFIX.length(); } else { index++; } } return -1; } protected static String resolvePlaceholder(Form form, String placeholder) { String questionId = placeholder; String[] split = questionId.split("_"); if (split.length == 1) { QuestionImpl question = form.getQuestion(questionId); Object value = form.get(questionId, form); if (question != null && value != null) { return form.getText(question.getName() + "_" + question.getAnswerNameByValue(value.toString())); } } else { return form.getText(questionId); } return ""; } }