XmlEntity.java

  1. package org.owasp.dependencycheck.xml;

  2. import java.util.Collections;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.annotation.concurrent.ThreadSafe;

  6. /**
  7.  * This is a utility class to convert named XML Entities (such as ø) into
  8.  * its HTML encoded Unicode code point (i.e. ø). This is a slightly
  9.  * modified (class/method rename) from an SO answer:
  10.  * https://stackoverflow.com/questions/7286428/help-the-java-sax-parser-to-understand-bad-xml
  11.  *
  12.  * @author https://stackoverflow.com/users/823393/oldcurmudgeon
  13.  */
  14. @ThreadSafe
  15. public final class XmlEntity {

  16.     /**
  17.      * The map of HTML entities.
  18.      */
  19.     private static final Map<String, Integer> SPECIALS;

  20.     //<editor-fold defaultstate="collapsed" desc="Initialize SPECIALS">
  21.     /*
  22.      * Create a map HTML Named Entities to their numeric equivalent. Derived
  23.      * from Wikipedia
  24.      * http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
  25.      */
  26.     static {
  27.         final Map<String, Integer> map = new HashMap<>();
  28.         map.put("quot", 34);
  29.         map.put("amp", 38);
  30.         map.put("apos", 39);
  31.         map.put("lt", 60);
  32.         map.put("gt", 62);
  33.         map.put("nbsp", 160);
  34.         map.put("iexcl", 161);
  35.         map.put("cent", 162);
  36.         map.put("pound", 163);
  37.         map.put("curren", 164);
  38.         map.put("yen", 165);
  39.         map.put("brvbar", 166);
  40.         map.put("sect", 167);
  41.         map.put("uml", 168);
  42.         map.put("copy", 169);
  43.         map.put("ordf", 170);
  44.         map.put("laquo", 171);
  45.         map.put("not", 172);
  46.         map.put("shy", 173);
  47.         map.put("reg", 174);
  48.         map.put("macr", 175);
  49.         map.put("deg", 176);
  50.         map.put("plusmn", 177);
  51.         map.put("sup2", 178);
  52.         map.put("sup3", 179);
  53.         map.put("acute", 180);
  54.         map.put("micro", 181);
  55.         map.put("para", 182);
  56.         map.put("middot", 183);
  57.         map.put("cedil", 184);
  58.         map.put("sup1", 185);
  59.         map.put("ordm", 186);
  60.         map.put("raquo", 187);
  61.         map.put("frac14", 188);
  62.         map.put("frac12", 189);
  63.         map.put("frac34", 190);
  64.         map.put("iquest", 191);
  65.         map.put("Agrave", 192);
  66.         map.put("Aacute", 193);
  67.         map.put("Acirc", 194);
  68.         map.put("Atilde", 195);
  69.         map.put("Auml", 196);
  70.         map.put("Aring", 197);
  71.         map.put("AElig", 198);
  72.         map.put("Ccedil", 199);
  73.         map.put("Egrave", 200);
  74.         map.put("Eacute", 201);
  75.         map.put("Ecirc", 202);
  76.         map.put("Euml", 203);
  77.         map.put("Igrave", 204);
  78.         map.put("Iacute", 205);
  79.         map.put("Icirc", 206);
  80.         map.put("Iuml", 207);
  81.         map.put("ETH", 208);
  82.         map.put("Ntilde", 209);
  83.         map.put("Ograve", 210);
  84.         map.put("Oacute", 211);
  85.         map.put("Ocirc", 212);
  86.         map.put("Otilde", 213);
  87.         map.put("Ouml", 214);
  88.         map.put("times", 215);
  89.         map.put("Oslash", 216);
  90.         map.put("Ugrave", 217);
  91.         map.put("Uacute", 218);
  92.         map.put("Ucirc", 219);
  93.         map.put("Uuml", 220);
  94.         map.put("Yacute", 221);
  95.         map.put("THORN", 222);
  96.         map.put("szlig", 223);
  97.         map.put("agrave", 224);
  98.         map.put("aacute", 225);
  99.         map.put("acirc", 226);
  100.         map.put("atilde", 227);
  101.         map.put("auml", 228);
  102.         map.put("aring", 229);
  103.         map.put("aelig", 230);
  104.         map.put("ccedil", 231);
  105.         map.put("egrave", 232);
  106.         map.put("eacute", 233);
  107.         map.put("ecirc", 234);
  108.         map.put("euml", 235);
  109.         map.put("igrave", 236);
  110.         map.put("iacute", 237);
  111.         map.put("icirc", 238);
  112.         map.put("iuml", 239);
  113.         map.put("eth", 240);
  114.         map.put("ntilde", 241);
  115.         map.put("ograve", 242);
  116.         map.put("oacute", 243);
  117.         map.put("ocirc", 244);
  118.         map.put("otilde", 245);
  119.         map.put("ouml", 246);
  120.         map.put("divide", 247);
  121.         map.put("oslash", 248);
  122.         map.put("ugrave", 249);
  123.         map.put("uacute", 250);
  124.         map.put("ucirc", 251);
  125.         map.put("uuml", 252);
  126.         map.put("yacute", 253);
  127.         map.put("thorn", 254);
  128.         map.put("yuml", 255);
  129.         map.put("OElig", 338);
  130.         map.put("oelig", 339);
  131.         map.put("Scaron", 352);
  132.         map.put("scaron", 353);
  133.         map.put("Yuml", 376);
  134.         map.put("fnof", 402);
  135.         map.put("circ", 710);
  136.         map.put("tilde", 732);
  137.         map.put("Alpha", 913);
  138.         map.put("Beta", 914);
  139.         map.put("Gamma", 915);
  140.         map.put("Delta", 916);
  141.         map.put("Epsilon", 917);
  142.         map.put("Zeta", 918);
  143.         map.put("Eta", 919);
  144.         map.put("Theta", 920);
  145.         map.put("Iota", 921);
  146.         map.put("Kappa", 922);
  147.         map.put("Lambda", 923);
  148.         map.put("Mu", 924);
  149.         map.put("Nu", 925);
  150.         map.put("Xi", 926);
  151.         map.put("Omicron", 927);
  152.         map.put("Pi", 928);
  153.         map.put("Rho", 929);
  154.         map.put("Sigma", 931);
  155.         map.put("Tau", 932);
  156.         map.put("Upsilon", 933);
  157.         map.put("Phi", 934);
  158.         map.put("Chi", 935);
  159.         map.put("Psi", 936);
  160.         map.put("Omega", 937);
  161.         map.put("alpha", 945);
  162.         map.put("beta", 946);
  163.         map.put("gamma", 947);
  164.         map.put("delta", 948);
  165.         map.put("epsilon", 949);
  166.         map.put("zeta", 950);
  167.         map.put("eta", 951);
  168.         map.put("theta", 952);
  169.         map.put("iota", 953);
  170.         map.put("kappa", 954);
  171.         map.put("lambda", 955);
  172.         map.put("mu", 956);
  173.         map.put("nu", 957);
  174.         map.put("xi", 958);
  175.         map.put("omicron", 959);
  176.         map.put("pi", 960);
  177.         map.put("rho", 961);
  178.         map.put("sigmaf", 962);
  179.         map.put("sigma", 963);
  180.         map.put("tau", 964);
  181.         map.put("upsilon", 965);
  182.         map.put("phi", 966);
  183.         map.put("chi", 967);
  184.         map.put("psi", 968);
  185.         map.put("omega", 969);
  186.         map.put("thetasym", 977);
  187.         map.put("upsih", 978);
  188.         map.put("piv", 982);
  189.         map.put("ensp", 8194);
  190.         map.put("emsp", 8195);
  191.         map.put("thinsp", 8201);
  192.         map.put("zwnj", 8204);
  193.         map.put("zwj", 8205);
  194.         map.put("lrm", 8206);
  195.         map.put("rlm", 8207);
  196.         map.put("ndash", 8211);
  197.         map.put("mdash", 8212);
  198.         map.put("lsquo", 8216);
  199.         map.put("rsquo", 8217);
  200.         map.put("sbquo", 8218);
  201.         map.put("ldquo", 8220);
  202.         map.put("rdquo", 8221);
  203.         map.put("bdquo", 8222);
  204.         map.put("dagger", 8224);
  205.         map.put("Dagger", 8225);
  206.         map.put("bull", 8226);
  207.         map.put("hellip", 8230);
  208.         map.put("permil", 8240);
  209.         map.put("prime", 8242);
  210.         map.put("Prime", 8243);
  211.         map.put("lsaquo", 8249);
  212.         map.put("rsaquo", 8250);
  213.         map.put("oline", 8254);
  214.         map.put("frasl", 8260);
  215.         map.put("euro", 8364);
  216.         map.put("image", 8465);
  217.         map.put("weierp", 8472);
  218.         map.put("real", 8476);
  219.         map.put("trade", 8482);
  220.         map.put("alefsym", 8501);
  221.         map.put("larr", 8592);
  222.         map.put("uarr", 8593);
  223.         map.put("rarr", 8594);
  224.         map.put("darr", 8595);
  225.         map.put("harr", 8596);
  226.         map.put("crarr", 8629);
  227.         map.put("lArr", 8656);
  228.         map.put("uArr", 8657);
  229.         map.put("rArr", 8658);
  230.         map.put("dArr", 8659);
  231.         map.put("hArr", 8660);
  232.         map.put("forall", 8704);
  233.         map.put("part", 8706);
  234.         map.put("exist", 8707);
  235.         map.put("empty", 8709);
  236.         map.put("nabla", 8711);
  237.         map.put("isin", 8712);
  238.         map.put("notin", 8713);
  239.         map.put("ni", 8715);
  240.         map.put("prod", 8719);
  241.         map.put("sum", 8721);
  242.         map.put("minus", 8722);
  243.         map.put("lowast", 8727);
  244.         map.put("radic", 8730);
  245.         map.put("prop", 8733);
  246.         map.put("infin", 8734);
  247.         map.put("ang", 8736);
  248.         map.put("and", 8743);
  249.         map.put("or", 8744);
  250.         map.put("cap", 8745);
  251.         map.put("cup", 8746);
  252.         map.put("int", 8747);
  253.         map.put("there4", 8756);
  254.         map.put("sim", 8764);
  255.         map.put("cong", 8773);
  256.         map.put("asymp", 8776);
  257.         map.put("ne", 8800);
  258.         map.put("equiv", 8801);
  259.         map.put("le", 8804);
  260.         map.put("ge", 8805);
  261.         map.put("sub", 8834);
  262.         map.put("sup", 8835);
  263.         map.put("nsub", 8836);
  264.         map.put("sube", 8838);
  265.         map.put("supe", 8839);
  266.         map.put("oplus", 8853);
  267.         map.put("otimes", 8855);
  268.         map.put("perp", 8869);
  269.         map.put("sdot", 8901);
  270.         map.put("lceil", 8968);
  271.         map.put("rceil", 8969);
  272.         map.put("lfloor", 8970);
  273.         map.put("rfloor", 8971);
  274.         map.put("lang", 10216);
  275.         map.put("rang", 10217);
  276.         map.put("loz", 9674);
  277.         map.put("spades", 9824);
  278.         map.put("clubs", 9827);
  279.         map.put("hearts", 9829);
  280.         map.put("diams", 9830);
  281.         SPECIALS = Collections.unmodifiableMap(map);
  282.     }
  283.     //</editor-fold>

  284.     /**
  285.      * Private constructor for a utility class.
  286.      */
  287.     private XmlEntity() {
  288.     }

  289.     /**
  290.      * Converts a named XML entity into its HTML encoded Unicode code point.
  291.      *
  292.      * @param s the named entity (note, this should not include the leading
  293.      * '&amp;' or trailing ';'
  294.      * @return the HTML encoded Unicode code point representation of the named
  295.      * entity
  296.      */
  297.     public static String fromNamedReference(CharSequence s) {
  298.         if (s == null) {
  299.             return null;
  300.         }
  301.         final Integer code = SPECIALS.get(s.toString());
  302.         if (code != null) {
  303.             return "&#" + code + ";";
  304.         }
  305.         return null;
  306.     }
  307. }