/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.gouv.finances.cp.utils.xml.marshal.escapers;

import fr.gouv.finances.cp.utils.xml.marshal.CharacterEscaper;

/**
 *
 * @author chm
 */
public class DefaultCharacterEscaper implements CharacterEscaper {
    
    public DefaultCharacterEscaper() {
        super();
    }

    public String escapeChar(char ch) {
        switch(ch) {
            case '<': return "&lt;";
            case '>': return "&gt;";
            case '&': return "&amp;";
            case '"': return "&quot;";
        }
        return String.valueOf(ch);
    }
    
    public String escapeString(String s) {
        if(s==null) return null;
        StringBuilder sb = new StringBuilder();
        for(char c:s.toCharArray()) {
            sb.append(escapeChar(c));
        }
        return sb.toString();
    }

}
