Here you can find the source of substituteTokens(String pattern, Map tokens)
public static String substituteTokens(String pattern, Map tokens)
//package com.java2s; /*/* w w w.j a v a2 s. c o m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. * */ import java.util.HashMap; import java.util.Map; public class Main { public static final String ORGANISATION_KEY = "organisation"; public static final String ORGANISATION_KEY2 = "organization"; public static final String ORGANISATION_PATH_KEY = "orgPath"; public static String substituteTokens(String pattern, Map tokens) { Map tokensCopy = new HashMap(tokens); if (tokensCopy.containsKey(ORGANISATION_KEY) && !tokensCopy.containsKey(ORGANISATION_KEY2)) { tokensCopy.put(ORGANISATION_KEY2, tokensCopy.get(ORGANISATION_KEY)); } if (tokensCopy.containsKey(ORGANISATION_KEY) && !tokensCopy.containsKey(ORGANISATION_PATH_KEY)) { String org = (String) tokensCopy.get(ORGANISATION_KEY); tokensCopy.put(ORGANISATION_PATH_KEY, org == null ? "" : org.replace('.', '/')); } StringBuffer buffer = new StringBuffer(); char[] chars = pattern.toCharArray(); StringBuffer optionalPart = null; StringBuffer tokenBuffer = null; boolean insideOptionalPart = false; boolean insideToken = false; boolean tokenSeen = false; boolean tokenHadValue = false; for (int i = 0; i < chars.length; i++) { switch (chars[i]) { case '(': if (insideOptionalPart) { throw new IllegalArgumentException( "invalid start of optional part at position " + i + " in pattern " + pattern); } optionalPart = new StringBuffer(); insideOptionalPart = true; tokenSeen = false; tokenHadValue = false; break; case ')': if (!insideOptionalPart || insideToken) { throw new IllegalArgumentException( "invalid end of optional part at position " + i + " in pattern " + pattern); } if (tokenHadValue) { buffer.append(optionalPart.toString()); } else if (!tokenSeen) { buffer.append('(').append(optionalPart.toString()).append(')'); } insideOptionalPart = false; break; case '[': if (insideToken) { throw new IllegalArgumentException( "invalid start of token at position " + i + " in pattern " + pattern); } tokenBuffer = new StringBuffer(); insideToken = true; break; case ']': if (!insideToken) { throw new IllegalArgumentException( "invalid end of token at position " + i + " in pattern " + pattern); } String token = tokenBuffer.toString(); Object tokenValue = tokensCopy.get(token); String value = (tokenValue == null) ? null : tokenValue.toString(); if (insideOptionalPart) { tokenHadValue = (value != null) && (value.length() > 0); optionalPart.append(value); } else { if (value == null) { // the token wasn't set, it's kept as is value = "[" + token + "]"; } buffer.append(value); } insideToken = false; tokenSeen = true; break; default: if (insideToken) { tokenBuffer.append(chars[i]); } else if (insideOptionalPart) { optionalPart.append(chars[i]); } else { buffer.append(chars[i]); } break; } } if (insideToken) { throw new IllegalArgumentException("last token hasn't been closed in pattern " + pattern); } if (insideOptionalPart) { throw new IllegalArgumentException("optional part hasn't been closed in pattern " + pattern); } return buffer.toString(); } }