Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright SpringSource Inc 2007,2009.
   *
   * 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.
  */
 
 package com.springsource.util.common;
 
 import java.util.UUID;
Utility class for resolving placeholders inside a java.util.Properties instance. These placeholders can refer to other properties in the Properties instance. The place holders may also have a modifier in them
 ${com.springsource:modifier}
 
where everything after the colon is considered the modifier. This class does not interpret these modifiers but rather delegates to a PropertyPlaceholderResolver.PlaceholderValueTransformer for processing.

Concurrent Semantics
Threadsafe.

 
 public final class PropertyPlaceholderResolver {
     
     private static final Pattern PATTERN = Pattern.compile("\\$\\{([^:\\}]*):?([^\\}]*)?\\}");
 
     private static final PlaceholderValueTransformer IDENTITY_TRANSFORMER = new PlaceholderValueTransformer() {
 
         public String transform(String propertyNameString propertyValueString modifier) {
             return propertyValue;
         }
 
     };

    
Resolves all placeholders in the supplied java.util.Properties instance.

Parameters:
input the properties to resolve.
Returns:
the resolved properties.
 
     public Properties resolve(Properties input) {
         return resolve(input);
     }

    
Resolves all placeholders in the supplied java.util.Properties instance and transform any based on their modifiers.

Parameters:
input the properties to resolve.
transformer a transformer for handling property modifiers
Returns:
the resolved properties.
 
     public Properties resolve(Properties inputPlaceholderValueTransformer transformer) {
         Properties result = new Properties();
         Enumeration<?> propertyNames = input.propertyNames();
 
         while (propertyNames.hasMoreElements()) {
 			String propertyName = (StringpropertyNames.nextElement();
 			result.setProperty(propertyNameresolveProperty(propertyNameinputtransformer));
 			
 		}
 
         return result;
     }

    
Resolves all placeholders in the supplied string with values from a java.util.Properties instance.

Parameters:
input the string to resolve
props the properties to use for resolution
Returns:
the resolved string
 
     public String resolve(String inputProperties props) {
         return resolve(inputprops);
     }

    
Resolves all placeholders in the supplied string with values from a java.util.Properties instance and transform any based on their modifiers.

Parameters:
input the string to resolve
props the properties to use for resolution
transformer a transformer for handling property modifiers
Returns:
the resolved string
    public String resolve(String inputProperties propsPlaceholderValueTransformer transformer) {
        String key = UUID.randomUUID().toString();
        props.put(keyinput);
        String value = resolveProperty(keypropstransformer);
        props.remove(key);
        return value;
    }
    private String resolveProperty(String nameProperties propsPlaceholderValueTransformer transformer) {
        Stack<StringvisitState = new Stack<String>();
        return resolve(namepropstransformervisitState);
    }
    private String resolve(String nameProperties propsPlaceholderValueTransformer transformerStack<StringvisitState) {
        visitState.push(name);
        String initialValue = props.getProperty(name);
        if(initialValue == null) {
            throw new RuntimeException("No value found for placeholder '" + name + "'");
        }
        Matcher matcher = .matcher(initialValue);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String propName = matcher.group(1);
            if (visitState.contains(propName)) {
                throw new IllegalArgumentException(formatPropertyCycleMessage(visitState));
            }
            String value = resolve(propNamepropstransformervisitState);
            if (matcher.group(2).length() > 0) {
                value = transformer.transform(propNamevaluematcher.group(2));
            }
            matcher.appendReplacement(sbescapeBackslashes(value));
        }
        matcher.appendTail(sb);
        visitState.pop();
        return sb.toString();
    }
    private static String escapeBackslashes(String string) {
        StringBuffer sb = new StringBuffer(string.length());
        int bsIndex = string.indexOf("\\");
        int pos = 0;
        while (bsIndex != -1) {
            sb.append(string.substring(pos,bsIndex+1));
            sb.append("\\"); // another backslash
            pos = bsIndex+1;
            bsIndex = string.indexOf("\\",pos);
        }
        sb.append(string.substring(posstring.length()));
        return new String(sb);
    }
    
    private String formatPropertyCycleMessage(Stack<StringvisitState) {
        StringBuilder sb = new StringBuilder();
        sb.append("Circular reference in property definitions: ");
        for (String name : visitState) {
            sb.append(name).append(" -> ");
        }
        sb.append(visitState.iterator().next());
        return sb.toString();
    }

    
An interface for property placeholder modifiers. Implementations of this interface are called when a property placeholder modifier is detected on a class.

Concurrent Semantics
Implementations must be threadsafe.

Author(s):
Ben Hale
Since:
2.0.0
    public static interface PlaceholderValueTransformer {

        
Transforms a property from its initial value to some other value

Parameters:
propertyName the name of the property being transformed
propertyValue the original value of the property
modifier the modifer string attached to the placeholder
Returns:
A string that has been modified by this transformer and to be used in place of the original value
        String transform(String propertyNameString propertyValueString modifier);
    }
New to GrepCode? Check out our FAQ X