001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.util;
018:
019: import org.apache.avalon.framework.context.Context;
020: import org.apache.avalon.framework.context.ContextException;
021: import org.apache.avalon.framework.context.DefaultContext;
022: import org.apache.avalon.framework.logger.Logger;
023:
024: /**
025: * Helper class to create Stettings and for replacing property references with the value of the
026: * property
027: *
028: * @version $Id: SettingsHelper.java 433495 2006-08-22 04:04:11Z crossley $
029: * @since 2.1.9
030: */
031: public class SettingsHelper {
032:
033: /**
034: * Create the settings object
035: * @param context The current context
036: * @param logger A logger to use to log any errors.
037: * @throws ContextException
038: */
039: public static void createSettings(DefaultContext context,
040: Logger logger) throws ContextException {
041: SimpleSourceResolver resolver = new SimpleSourceResolver();
042: resolver.enableLogging(logger);
043: resolver.contextualize(context);
044: Settings settings = new PropertySettings(resolver, logger);
045: context.put(Settings.PROPERTY_USER_SETTINGS, settings);
046: }
047:
048: /**
049: * Return the Settings object
050: * @param context The context
051: * @return The global Settings
052: */
053: public static Settings getSettings(Context context) {
054: Settings settings = null;
055: try {
056: settings = (Settings) context
057: .get(Settings.PROPERTY_USER_SETTINGS);
058: } catch (Exception e) {
059: // Ignore the exception;
060: }
061: return settings;
062: }
063:
064: /**
065: * Replace all property references in the string with the current value
066: * and return it.
067: */
068: public static String replace(String value, Settings settings,
069: Logger logger) {
070: // quick test for null or no references
071: if (value == null || value.indexOf("${") == -1
072: || settings == null) {
073: return value;
074: }
075: final StringBuffer buffer = new StringBuffer();
076: int prev = 0;
077: int pos;
078:
079: // search for the next instance of $ from the 'prev' position
080: while ((pos = value.indexOf("$", prev)) >= 0) {
081:
082: // if there was any text before this, add it
083: if (pos > prev) {
084: buffer.append(value.substring(prev, pos));
085: }
086:
087: // if we are at the end of the string, end
088: if (pos == (value.length() - 1)) {
089: buffer.append("$");
090: prev = pos + 1;
091: } else if (value.charAt(pos + 1) != '{') {
092: // peek ahead to see if the next char is a property or not
093: // not a property: insert the char as a literal
094: buffer.append(value.substring(pos, pos + 2));
095: prev = pos + 2;
096:
097: } else {
098: // start token found, check for end token
099: int endName = value.indexOf('}', pos);
100: if (endName == -1) {
101: // no end token found, just append the rest
102: buffer.append(value.substring(pos));
103: prev = value.length();
104: } else {
105: final String propertyName = value.substring(
106: pos + 2, endName);
107: String propertyValue = getProperty(propertyName,
108: settings);
109: // compatibility fallback - if the value is null, just readd token
110: if (propertyValue == null) {
111: logger.warn("Property " + propertyName
112: + " not found.");
113: buffer.append("${");
114: buffer.append(propertyName);
115: buffer.append('}');
116: } else {
117: buffer.append(propertyValue);
118: }
119: prev = endName + 1;
120: }
121: }
122: }
123: // no more tokens found
124: // append the rest
125: if (prev < value.length()) {
126: buffer.append(value.substring(prev));
127: }
128: return buffer.toString();
129: }
130:
131: static String getProperty(String name, Settings settings) {
132: String value = null;
133: if (settings != null) {
134: value = settings.getProperty(name);
135: }
136: if (value == null) {
137: value = System.getProperty(name);
138: }
139: return value;
140: }
141: }
|