All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eva.properties.Replacer Maven / Gradle / Ivy

Go to download

Advanced properties with object factories, references and inheritance.

There is a newer version: 0.3
Show newest version
/*
 * $Id: Replacer.java 9 2007-02-06 18:20:13Z max $
 * 
 * Copyright (c) 2006-2007 Maximilian Antoni. All rights reserved.
 * 
 * This software is licensed as described in the file LICENSE.txt, which you
 * should have received as part of this distribution. The terms are also
 * available at http://www.maxantoni.de/projects/eva-properties/license.txt.
 */
package com.eva.properties;

import java.io.File;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * helper object for replacing string values.
 * 
 * @author Max Antoni
 * @version $Revision: 9 $
 */
class Replacer {
    /**
     * ${something.like.this}
     */
    private static final Pattern PLACEHOLDER = Pattern.compile(
            "\\$\\{[a-zA-Z0-9_\\-\\.]+\\}");

    private Replacer() {
        // prevent instantiation
    }
    
    /**
     * replaces the given string value.
     * 
     * @param inValue the string value.
     * @param inContext the context.
     * @return the object.
     * @throws PropertiesException if the string cannot be replaced
     *             successfully.
     */
    static String replace(String inValue, Context inContext)
            throws PropertiesException {
        Matcher matcher = PLACEHOLDER.matcher(inValue);
        if(!matcher.find()) {
            return inValue;
        }
        StringBuffer buffer = new StringBuffer();
        int pos = 0;
        do {
            int s = matcher.start();
            if(s > pos) {
                buffer.append(inValue.substring(pos, s));
            }
            String group = matcher.group();
            String placeholder = group.substring(2, group.length() - 1);
            Object value = inContext.lookup(placeholder);
            if(value == null) {
                if(Log.INSTANCE.isLoggable(Level.FINE)) {
                    StringBuffer message = new StringBuffer();
                    message.append(group);
                    message.append(" is null");
                    if(inContext.isDebug()) {
                        message.append(" (");
                        inContext.writePath(message);
                        message.append(placeholder);
                        message.append(")");
                    }
                    message.append(".");
                    Log.INSTANCE.log(Level.FINE, message.toString());
                }
                return null;
            }
            if(value instanceof File) {
                buffer.append(((File) value).getAbsolutePath());
            }
            else {
                buffer.append(value);
            }
            pos = matcher.end();
        }
        while(matcher.find());
        if(pos < inValue.length()) {
            buffer.append(inValue.substring(pos));
        }
        return buffer.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy