Advertisement
Guest User

Untitled

a guest
Jun 12th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. // Some common regular expression definitions.
  2.  
  3. // The 16 colors defined by the HTML Spec (also used by the CSS Spec)
  4. private static final Pattern COLOR_NAME = Pattern
  5. .compile("(?:aqua|black|blue|fuchsia|gray|grey|green|lime|maroon|navy|olive|purple"
  6. + "|red|silver|teal|white|yellow)");
  7.  
  8. // HTML/CSS Spec allows 3 or 6 digit hex to specify color
  9. private static final Pattern COLOR_CODE = Pattern.compile("(?:#(?:[0-9a-fA-F]{3}(?:[0-9a-fA-F]{3})?))");
  10.  
  11. private static final Pattern NUMBER_OR_PERCENT = Pattern.compile("[0-9]+%?");
  12.  
  13. private static final Pattern PARAGRAPH = Pattern.compile("(?:[\\p{L}\\p{N},'\\.\\s\\-_\\(\\)]|&[0-9]{2};)*");
  14.  
  15. private static final Pattern ONSITE_URL = Pattern
  16. .compile("(?:[\\p{L}\\p{N}\\\\\\.\\#@\\$%\\+&;\\-_~,\\?=/!]+|\\#(\\w)+)");
  17.  
  18. private static final Pattern NUMBER = Pattern.compile("[+-]?(?:(?:[0-9]+(?:\\.[0-9]*)?)|\\.[0-9]+)");
  19.  
  20. private static final Pattern NAME = Pattern.compile("[a-zA-Z0-9\\-_\\$]+");
  21.  
  22. private static final Pattern ALIGN = Pattern.compile("(?i)center|left|right|justify|char");
  23.  
  24. private static final Pattern VALIGN = Pattern.compile("(?i)baseline|bottom|middle|top");
  25.  
  26. private static final Predicate<String> COLOR_NAME_OR_COLOR_CODE = new Predicate<String>()
  27. {
  28. @Override
  29. public boolean apply(String s)
  30. {
  31. return COLOR_NAME.matcher(s).matches() || COLOR_CODE.matcher(s).matches();
  32. }
  33. };
  34.  
  35. private static final Pattern ONE_CHAR = Pattern.compile(".?", Pattern.DOTALL);
  36.  
  37. /**
  38. * Allows table elements...
  39. */
  40. public static final PolicyFactory TABLES = new HtmlPolicyBuilder()
  41. .allowAttributes("border", "cellpadding", "cellspacing").matching(NUMBER).onElements("table")
  42. .allowAttributes("bgcolor").matching(COLOR_NAME_OR_COLOR_CODE).onElements("table")
  43. .allowAttributes("background").matching(ONSITE_URL).onElements("table").allowAttributes("align")
  44. .matching(ALIGN).onElements("table").allowAttributes("noresize").matching(Pattern.compile("(?i)noresize"))
  45. .onElements("table").allowAttributes("background").matching(ONSITE_URL).onElements("td", "th", "tr")
  46. .allowAttributes("bgcolor").matching(COLOR_NAME_OR_COLOR_CODE).onElements("td", "th")
  47. .allowAttributes("abbr").matching(PARAGRAPH).onElements("td", "th").allowAttributes("axis", "headers")
  48. .matching(NAME).onElements("td", "th").allowAttributes("scope")
  49. .matching(Pattern.compile("(?i)(?:row|col)(?:group)?")).onElements("td", "th").allowAttributes("nowrap")
  50. .onElements("td", "th").allowAttributes("height", "width").matching(NUMBER_OR_PERCENT)
  51. .onElements("table", "td", "th", "tr", "img").allowAttributes("align").matching(ALIGN)
  52. .onElements("thead", "tbody", "tfoot", "img", "td", "th", "tr", "colgroup", "col")
  53. .allowAttributes("valign").matching(VALIGN)
  54. .onElements("thead", "tbody", "tfoot", "td", "th", "tr", "colgroup", "col").allowAttributes("charoff")
  55. .matching(NUMBER_OR_PERCENT).onElements("td", "th", "tr", "colgroup", "col", "thead", "tbody", "tfoot")
  56. .allowAttributes("char").matching(ONE_CHAR)
  57. .onElements("td", "th", "tr", "colgroup", "col", "thead", "tbody", "tfoot")
  58. .allowAttributes("colspan", "rowspan").matching(NUMBER).onElements("td", "th")
  59. .allowAttributes("span", "width").matching(NUMBER_OR_PERCENT).onElements("colgroup", "col")
  60. .allowElements("table, th, tr, td, thead, tbody, tfoot").toFactory();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement