Skip to content

Instantly share code, notes, and snippets.

@thaniaclair
Last active December 17, 2015 05:49
Show Gist options
  • Save thaniaclair/5561102 to your computer and use it in GitHub Desktop.
Save thaniaclair/5561102 to your computer and use it in GitHub Desktop.
Substituidor de tags.
import java.util.HashMap;
import java.util.Map;
import br.com.sebrae.model.LayoutEmail;
import br.com.sebrae.model.LayoutEmailTag;
/**
* Substituidor de tags de {@link LayoutEmail}.
* @author thania.clair
*/
public class LayoutEmailReplacer {
private Map<LayoutEmailTag, String> tagMap;
private String text;
public LayoutEmailReplacer(String text, LayoutEmailTag tag, String value) {
this.text = text;
this.tagMap = new HashMap<LayoutEmailTag, String>();
this.tagMap.put(tag, value);
}
public LayoutEmailReplacer(String text, Map<LayoutEmailTag, String> tagMap) {
this.text = text;
this.tagMap = tagMap;
}
public void addTag(LayoutEmailTag tag, String value) {
if (this.tagMap == null) this.tagMap = new HashMap<LayoutEmailTag, String>();
this.tagMap.put(tag, value);
}
/**
* Gera um texto com todas as tags setadas substituídas por valor.
* @return texto gerado.
*/
public String generatePlainText() {
if (tagMap == null || tagMap.isEmpty()) return text;
for (Map.Entry<LayoutEmailTag, String> entry : tagMap.entrySet()) {
text = text.replace(entry.getKey().getTag(), entry.getValue());
}
return text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Map<LayoutEmailTag, String> getTagMap() {
return tagMap;
}
public void setTagMap(Map<LayoutEmailTag, String> tagMap) {
this.tagMap = tagMap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment