Conversão de caracteres especiais em código Html

1 resposta
G

Olá pessoal,

Estou precisando de uma API ou uma classe que faça a conversão de caracteres especiais em código Html:

& (comercial) por &
"’ (aspas duplas) por ’"’
’<’ (sinal de menor) por ’<’
’>’ (sinal de maior) por ’>’

Se alguem souber de alguma post ae…

Valeu,

1 Resposta

felipesp

Acabei escrevendo a bendita.

É preciso ter um arquivo properties com os caracteres especiais a serem substituidos, ok?

/br/com/seudominio/caracteres.properties

/**

  • Esta classe provê métodos para manipulação e formatação de texto.

  • @author fgomes
    
    */
    
    public class Text {
    
    private static Properties specialChars;
    
    private static final String specialCharsFile = "/br/com/seudominio/caracteres.properties";
    
    private static boolean init = false;
    
    private static void init() throws RelatoriosTextException {
    
    if (!init) {
    
    specialChars = new Properties();
    
    try {
    
    InputStream in = "".getClass().getResourceAsStream(specialCharsFile);
    
    if (in == null) {
    
    throw new RelatoriosTextException("Arquivo properties não encontrado");
    
    }
    
    specialChars.load(in);
    
    in.close();
    
    } catch (FileNotFoundException e) {
    
    throw new RelatoriosTextException(e);
    
    } catch (IOException e) {
    
    throw new RelatoriosTextException(e);
    
    }
    
    init = true;
    
    }
    
    }
    

    /**

    • Substitui todos caracteres especiais por sua representação em html.
    • @param htmlText
    • texto html a ser convertido.
      
    • @return o texto formatado.
    • @throws RelatoriosTextException
      /
      public static String htmlChar(String htmlText) throws RelatoriosTextException {
      init();
      StringBuffer b = new StringBuffer(htmlText);
      String mychar = temCaracterSpecial(b);
      while ((mychar != null)&&(!mychar.equals(""))) {
      substituiCaracter(b, mychar);
      mychar = temCaracterSpecial(b);
      }
      return b.toString();
      }
      /
      *
    • Se o StringBuffer possuir algum caracter a ser substituido, retorna true.
    • @param b sequencia a ser processada.
    • @return o caracter a ser trocado.
      /
      private static String temCaracterSpecial(StringBuffer b) {
      int x = 0;
      char[] c = new char[1];
      for (int i = 0; i < b.length(); i++) {
      b.getChars(i, i+1, c, x);
      if (specialChars.keySet().contains(new String©)) {
      return new String©;
      }
      }
      return null;
      }
      /
      *
    • Troca caracter especial.
    • @param b
    • @param mychar
      */
      private static void substituiCaracter(StringBuffer b, String mychar) {
      int pos = b.indexOf(mychar);
      b.replace(pos, (pos+1), specialChars.getProperty(mychar));
      System.out.println("trocado: " + specialChars.getProperty(mychar));
      }
      }
Criado 23 de dezembro de 2004
Ultima resposta 23 de mai. de 2006
Respostas 1
Participantes 2