/WebAccess/src/com/ideal/webaccess/StringUtils.java

http://eyes-free.googlecode.com/ · Java · 116 lines · 77 code · 8 blank · 31 comment · 8 complexity · a0775997300c0dc42e1273a0ffaee08f MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The IDEAL Group
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ideal.webaccess;
  17. import java.util.*;
  18. /**
  19. * Class for escaping HTML content. Code from tutorial at:
  20. * http://www.rgagnon.com/javadetails/java-0307.html
  21. */
  22. public class StringUtils {
  23. private StringUtils() {
  24. }
  25. private static HashMap<String, String> htmlEntities;
  26. static {
  27. htmlEntities = new HashMap<String, String>();
  28. htmlEntities.put("&lt;", "<");
  29. htmlEntities.put("&gt;", ">");
  30. htmlEntities.put("&amp;", "&");
  31. htmlEntities.put("&quot;", "\"");
  32. htmlEntities.put("&agrave;", "ŕ");
  33. htmlEntities.put("&Agrave;", "Ŕ");
  34. htmlEntities.put("&acirc;", "â");
  35. htmlEntities.put("&auml;", "ä");
  36. htmlEntities.put("&Auml;", "Ä");
  37. htmlEntities.put("&Acirc;", "Â");
  38. htmlEntities.put("&aring;", "ĺ");
  39. htmlEntities.put("&Aring;", "Ĺ");
  40. htmlEntities.put("&aelig;", "ć");
  41. htmlEntities.put("&AElig;", "Ć");
  42. htmlEntities.put("&ccedil;", "ç");
  43. htmlEntities.put("&Ccedil;", "Ç");
  44. htmlEntities.put("&eacute;", "é");
  45. htmlEntities.put("&Eacute;", "É");
  46. htmlEntities.put("&egrave;", "č");
  47. htmlEntities.put("&Egrave;", "Č");
  48. htmlEntities.put("&ecirc;", "ę");
  49. htmlEntities.put("&Ecirc;", "Ę");
  50. htmlEntities.put("&euml;", "ë");
  51. htmlEntities.put("&Euml;", "Ë");
  52. htmlEntities.put("&iuml;", "ď");
  53. htmlEntities.put("&Iuml;", "Ď");
  54. htmlEntities.put("&ocirc;", "ô");
  55. htmlEntities.put("&Ocirc;", "Ô");
  56. htmlEntities.put("&ouml;", "ö");
  57. htmlEntities.put("&Ouml;", "Ö");
  58. htmlEntities.put("&oslash;", "ř");
  59. htmlEntities.put("&Oslash;", "Ř");
  60. htmlEntities.put("&szlig;", "ß");
  61. htmlEntities.put("&ugrave;", "ů");
  62. htmlEntities.put("&Ugrave;", "Ů");
  63. htmlEntities.put("&ucirc;", "ű");
  64. htmlEntities.put("&Ucirc;", "Ű");
  65. htmlEntities.put("&uuml;", "ü");
  66. htmlEntities.put("&Uuml;", "Ü");
  67. htmlEntities.put("&nbsp;", " ");
  68. htmlEntities.put("&copy;", "\u00a9");
  69. htmlEntities.put("&reg;", "\u00ae");
  70. htmlEntities.put("&euro;", "\u20a0");
  71. }
  72. /*
  73. * Here the original recursive version. It is fine unless you pass a big
  74. * string then a Stack Overflow is possible :-( public static final String
  75. * unescapeHTML(String source, int start){ int i,j; i = source.indexOf("&",
  76. * start); if (i > -1) { j = source.indexOf(";" ,i); if (j > i) { String
  77. * entityToLookFor = source.substring(i , j + 1); String value =
  78. * (String)htmlEntities.get(entityToLookFor); if (value != null) { source =
  79. * new StringBuffer().append(source.substring(0 , i)) .append(value)
  80. * .append(source.substring(j + 1)) .toString(); return unescapeHTML(source,
  81. * i + 1); // recursive call } } } return source; } M. McNeely Jr. has sent
  82. * a version with do...while()loop which is more robust. Thanks to him!
  83. */
  84. public static final String unescapeHTML(String source) {
  85. int i, j;
  86. boolean continueLoop;
  87. int skip = 0;
  88. do {
  89. continueLoop = false;
  90. i = source.indexOf("&", skip);
  91. if (i > -1) {
  92. j = source.indexOf(";", i);
  93. if (j > i) {
  94. String entityToLookFor = source.substring(i, j + 1);
  95. String value = htmlEntities.get(entityToLookFor);
  96. if (value != null) {
  97. source = source.substring(0, i) + value + source.substring(j + 1);
  98. continueLoop = true;
  99. } else if (value == null) {
  100. skip = i + 1;
  101. continueLoop = true;
  102. }
  103. }
  104. }
  105. } while (continueLoop);
  106. return source;
  107. }
  108. }