/* Copyright 2006 Rene Grothmann, modified by Eric Hakenholz This file is part of C.a.R. software. C.a.R. is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. C.a.R. is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package rene.util.xml; import rene.util.SimpleStringBuffer; public class XmlTranslator { static SimpleStringBuffer H = new SimpleStringBuffer(10000); static String toXml(final String s) { final int m = s.length(); H.clear(); for (int i = 0; i < m; i++) { final char c = s.charAt(i); switch (c) { case '<': toH("<"); break; case '>': toH(">"); break; case '&': toH("&"); break; case '\'': toH("'"); break; case '\"': toH("""); break; default: H.append(c); } } return H.toString(); } static void toH(final String s) { final int m = s.length(); for (int i = 0; i < m; i++) { H.append(s.charAt(i)); } } static String toText(final String s) { final int m = s.length(); H.clear(); for (int i = 0; i < m; i++) { final char c = s.charAt(i); if (c == '&') { if (find(s, i, "<")) { H.append('<'); i += 3; } else if (find(s, i, ">")) { H.append('>'); i += 3; } else if (find(s, i, """)) { H.append('\"'); i += 5; } else if (find(s, i, "'")) { H.append('\''); i += 5; } else if (find(s, i, "&")) { H.append('&'); i += 4; } else H.append(c); } else H.append(c); } return H.toString(); } static boolean find(final String s, final int pos, final String t) { try { for (int i = 0; i < t.length(); i++) { if (s.charAt(pos + i) != t.charAt(i)) return false; } return true; } catch (final Exception e) { return false; } } }