package at.gv.egovernment.moa.id.auth.builder; import at.gv.egovernment.moa.id.BuildException; import at.gv.egovernment.moa.util.StringUtils; /** * Base class for HTML/XML builders providing commonly useful functions. * * @author Paul Ivancsics * @version $Id$ */ public class Builder { /** * Replaces a given number of occurences of a special tag in an XML or HTML template by a value. * @param htmlTemplate template * @param tag special tag * @param value value replacing the tag * @param maxreplacements: -1 to replace each occurence of tag, or limit replacements by a given positive number * @return XML or HTML code, the tag replaced * @throws BuildException when template does not contain the tag */ protected String replaceTag(String template, String tag, String value, int maxreplacements) throws BuildException { String result = template; int index = result.indexOf(tag); if (index < 0) { // Substring not found but should throw new BuildException( "builder.01", new Object[] {"<" + tag.substring(1, tag.length() - 1) + ">"}); } else { // replace each occurence if (maxreplacements == -1) { return StringUtils.replaceAll(template, tag, value); } else { int found = 1; while (index > -1 && (found <= maxreplacements)) { result = result.substring(0, index) + value + result.substring(index + tag.length()); index = result.indexOf(tag); if (index > -1) found += 1; } } } return result; } }