Description
Takes a template and replaces the segment keys with the segment values, keys should not have {} around them yet as these will be added around each key in the segments map
License
Educational Community License
Parameter
Parameter | Description |
---|
template | a parse template with {variables} in it |
segments | a map of all possible segment keys and values, unused keys will be ignored |
Exception
Parameter | Description |
---|
IllegalArgumentException | if all template variables cannot be replaced or template is empty/null |
Return
the template with replacement values filled in
Declaration
public static String mergeTemplate(String template,
Map<String, String> segments)
Method Source Code
//package com.java2s;
/**/* www .j a va2 s. c om*/
* $Id$
* $URL$
* TemplateParseUtil.java - entity-broker - Apr 10, 2008 9:57:29 AM - azeckoski
**************************************************************************
* Copyright (c) 2008, 2009 The Sakai Foundation
*
* Licensed under the Educational Community 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.opensource.org/licenses/ECL-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.
*/
import java.util.Map;
import java.util.Map.Entry;
public class Main {
/**
* Takes a template and replaces the segment keys with the segment values,
* keys should not have {} around them yet as these will be added around each key
* in the segments map
*
* @param template a parse template with {variables} in it
* @param segments a map of all possible segment keys and values,
* unused keys will be ignored
* @return the template with replacement values filled in
* @throws IllegalArgumentException if all template variables cannot be replaced or template is empty/null
*/
public static String mergeTemplate(String template,
Map<String, String> segments) {
if (template == null || "".equals(template) || segments == null) {
throw new IllegalArgumentException(
"Cannot operate on null template/segments, template must not be empty");
}
int vars = 0;
char[] chars = template.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '{') {
vars++;
}
}
String reference = template;
int replacements = 0;
for (Entry<String, String> es : segments.entrySet()) {
String keyBraces = "{" + es.getKey() + "}";
if (reference.contains(keyBraces)) {
reference = reference.replace(keyBraces, es.getValue());
replacements++;
}
}
if (replacements != vars) {
throw new IllegalArgumentException(
"Failed merge, could not replace all variables ("
+ vars + ") in the template, only replaced "
+ replacements);
}
return reference;
}
}
Related
- mergeNsPrefixes(final Map prioritaryPrefixes, final Map additionalPrefixes)
- mergeOptions(Map> ssio, Map> allOptions)
- mergePermMap(Map> permMap, Map> subPermMap)
- mergePropertiesToMap(Properties properties, Map map, boolean overwrite)
- mergeResourceBundle(final Map map, final String path)
- mergeValue(Map obj, String key, Object value)
- mergeValueInMap(Map map, String name, String value)