Author: abelevich
Date: 2008-11-04 10:08:16 -0500 (Tue, 04 Nov 2008)
New Revision: 11012
Modified:
trunk/sandbox/ui/editor/src/main/antlr/html-seamtext.g
Log:
remove sanitizer interface and default sanitizer implementation from grammar file use
import instead ...
Modified: trunk/sandbox/ui/editor/src/main/antlr/html-seamtext.g
===================================================================
--- trunk/sandbox/ui/editor/src/main/antlr/html-seamtext.g 2008-11-04 11:34:08 UTC (rev
11011)
+++ trunk/sandbox/ui/editor/src/main/antlr/html-seamtext.g 2008-11-04 15:08:16 UTC (rev
11012)
@@ -3,6 +3,11 @@
package org.richfaces.antlr;
}
+{
+ import org.jboss.seam.text.SeamTextParser.DefaultSanitizer;
+ import org.jboss.seam.text.SeamTextParser.Sanitizer;
+}
+
class HtmlSeamTextParser extends Parser;
options
@@ -12,321 +17,32 @@
}
{
- public class HtmlRecognitionException extends RecognitionException {
- Token openingElement;
- RecognitionException wrappedException;
-
- public HtmlRecognitionException(Token openingElement, RecognitionException
wrappedException) {
- this.openingElement = openingElement;
- this.wrappedException = wrappedException;
- }
-
- public Token getOpeningElement() {
- return openingElement;
- }
-
- public String getMessage() {
- return wrappedException.getMessage();
- }
-
- public Throwable getCause() {
- return wrappedException;
- }
- }
-
- public interface Sanitizer {
+
+ public class HtmlRecognitionException extends RecognitionException {
+ Token openingElement;
+ RecognitionException wrappedException;
- public void validateLinkTagURI(Token element, String uri) throws
SemanticException;
- public void validateHtmlElement(Token element) throws SemanticException;
- public void validateHtmlAttribute(Token element, Token attribute) throws
SemanticException;
- public void validateHtmlAttributeValue(Token element, Token attribute, String
attributeValue) throws SemanticException;
- public void escapeSeamTextToken(Token element);
- public String getInvalidURIMessage(String uri);
- public String getInvalidElementMessage(String elementName);
- public String getInvalidAttributeMessage(String elementName, String
attributeName);
- public String getInvalidAttributeValueMessage(String elementName, String
attributeName, String value);
+ public HtmlRecognitionException(Token openingElement, RecognitionException
wrappedException) {
+ this.openingElement = openingElement;
+ this.wrappedException = wrappedException;
+ }
- }
-
- public static class DefaultSanitizer implements HtmlSeamTextParser.Sanitizer {
-
- protected java.util.Set<String> attributesWhoseValueIsAURI = new
java.util.HashSet(java.util.Arrays.asList(
- "action", "cite", "href", "longdesc",
"src", "xlink:href", "xml:base"
- ));
-
-
- protected java.util.Set<String> uriSchemes = new
java.util.HashSet(java.util.Arrays.asList(
- "afs", "aim", "callto", "ed2k",
"feed", "ftp", "gopher", "http",
"https",
- "irc", "mailto", "news", "nntp",
"rsync", "rtsp", "sftp", "ssh", "tag",
- "tel", "telnet", "urn", "webcal",
"wtai", "xmpp"
- ));
-
- protected java.util.Set<String> acceptableElements = new
java.util.HashSet(java.util.Arrays.asList(
- "a", "abbr", "acronym", "address",
"area", "b", "bdo", "big",
"blockquote",
- "br", "button", "caption", "center",
"cite", "code", "col", "colgroup",
"dd",
- "del", "dfn", "dir", "div",
"dl", "dt", "em", "fieldset", "font",
"form",
- "h1", "h2", "h3", "h4",
"h5", "h6", "hr", "i", "img",
"input", "ins", "kbd",
- "label", "legend", "li", "map",
"menu", "ol", "optgroup", "option",
"p",
- "pre", "q", "s", "samp",
"select", "small", "span", "strike",
"strong",
- "sub", "sup", "table", "tbody",
"td", "textarea", "tfoot", "th",
"thead",
- "tr", "tt", "u", "ul",
"var", "wbr"
- ));
+ public Token getOpeningElement() {
+ return openingElement;
+ }
- protected java.util.Set<String> mathmlElements = new
java.util.HashSet(java.util.Arrays.asList(
- "maction", "math", "merror", "mfrac",
"mi", "mmultiscripts", "mn", "mo",
- "mover", "mpadded", "mphantom",
"mprescripts", "mroot", "mrow", "mspace",
- "msqrt", "mstyle", "msub", "msubsup",
"msup", "mtable", "mtd", "mtext",
- "mtr", "munder", "munderover",
"none"
- ));
+ public String getMessage() {
+ return wrappedException.getMessage();
+ }
- protected java.util.Set<String> svgElements = new
java.util.HashSet(java.util.Arrays.asList(
- "a", "animate", "animateColor",
"animateMotion", "animateTransform",
- "circle", "defs", "desc", "ellipse",
"font-face", "font-face-name",
- "font-face-src", "g", "glyph",
"hkern", "image", "line", "linearGradient",
- "marker", "metadata", "missing-glyph",
"mpath", "path", "polygon",
- "polyline", "radialGradient", "rect",
"set", "stop", "svg", "switch", "text",
- "title", "tspan", "use"
- ));
-
- protected java.util.Set<String> acceptableAttributes = new
java.util.HashSet(java.util.Arrays.asList(
- "abbr", "accept", "accept-charset",
"accesskey", "action", "align", "alt",
- "axis", "border", "cellpadding",
"cellspacing", "char", "charoff", "charset",
- "checked", "cite", "class", "clear",
"color", "cols", "colspan", "compact",
- "coords", "datetime", "dir",
"disabled", "enctype", "for", "frame",
- "headers", "height", "href",
"hreflang", "hspace", "id", "ismap",
"label",
- "lang", "longdesc", "maxlength",
"media", "method", "multiple", "name",
- "nohref", "noshade", "nowrap",
"prompt", "readonly", "rel", "rev",
"rows",
- "rowspan", "rules", "scope",
"selected", "shape", "size", "span",
"src",
- "start", "style", "summary",
"tabindex", "target", "title", "type",
"usemap",
- "valign", "value", "vspace", "width",
"xml:lang"
- ));
-
- protected java.util.Set<String> mathmlAttributes = new
java.util.HashSet(java.util.Arrays.asList(
- "actiontype", "align", "columnalign",
"columnalign", "columnalign",
- "columnlines", "columnspacing", "columnspan",
"depth", "display",
- "displaystyle", "equalcolumns", "equalrows",
"fence", "fontstyle",
- "fontweight", "frame", "height",
"linethickness", "lspace", "mathbackground",
- "mathcolor", "mathvariant", "mathvariant",
"maxsize", "minsize", "other",
- "rowalign", "rowalign", "rowalign",
"rowlines", "rowspacing", "rowspan",
- "rspace", "scriptlevel", "selection",
"separator", "stretchy", "width",
- "width", "xlink:href", "xlink:show",
"xlink:type", "xmlns", "xmlns:xlink"
- ));
-
-
- protected java.util.Set<String> svgAttributes = new
java.util.HashSet(java.util.Arrays.asList(
- "accent-height", "accumulate", "additive",
"alphabetic", "arabic-form",
- "ascent", "attributeName", "attributeType",
"baseProfile", "bbox", "begin",
- "by", "calcMode", "cap-height",
"class", "color", "color-rendering",
- "content", "cx", "cy", "d",
"descent", "display", "dur", "dx", "dy",
"end",
- "fill", "fill-rule", "font-family",
"font-size", "font-stretch",
- "font-style", "font-variant", "font-weight",
"from", "fx", "fy", "g1", "g2",
- "glyph-name", "gradientUnits", "hanging",
"height", "horiz-adv-x",
- "horiz-origin-x", "id", "ideographic",
"k", "keyPoints", "keySplines",
- "keyTimes", "lang", "marker-end",
"marker-mid", "marker-start",
- "markerHeight", "markerUnits", "markerWidth",
"mathematical", "max", "min",
- "name", "offset", "opacity",
"orient", "origin", "overline-position",
- "overline-thickness", "panose-1", "path",
"pathLength", "points",
- "preserveAspectRatio", "r", "refX",
"refY", "repeatCount", "repeatDur",
- "requiredExtensions", "requiredFeatures",
"restart", "rotate", "rx", "ry",
- "slope", "stemh", "stemv",
"stop-color", "stop-opacity",
- "strikethrough-position", "strikethrough-thickness",
"stroke",
- "stroke-dasharray", "stroke-dashoffset",
"stroke-linecap", "stroke-linejoin",
- "stroke-miterlimit", "stroke-opacity",
"stroke-width", "systemLanguage",
- "target", "text-anchor", "to",
"transform", "type", "u1", "u2",
- "underline-position", "underline-thickness",
"unicode", "unicode-range",
- "units-per-em", "values", "version",
"viewBox", "visibility", "width",
- "widths", "x", "x-height", "x1",
"x2", "xlink:actuate", "xlink:arcrole",
- "xlink:href", "xlink:role", "xlink:show",
"xlink:title", "xlink:type",
- "xml:base", "xml:lang", "xml:space",
"xmlns", "xmlns:xlink", "y", "y1",
"y2",
- "zoomAndPan"
- ));
-
- public final java.util.regex.Pattern REGEX_VALID_CSS_VALUE =
java.util.regex.Pattern.compile(
-
"^(#[0-9a-f]{3,6}|rgb\\(\\d{1,3}%?,\\d{1,3}%?,?\\d{1,3}%?\\)?|-?\\d{0,2}\\.?\\d{0,2}(cm|em|ex|in|mm|pc|pt|px|%|,|\\))?)$"
- );
-
- public final java.util.regex.Pattern REGEX_VALID_CSS_STRING1 =
java.util.regex.Pattern.compile(
-
"^([-:,;#%.\\sa-zA-Z0-9!]|\\w-\\w|'[\\s\\w]+'|\"[\\s\\w]+\"|\\([\\d,\\s]+\\))*$"
- );
-
- public final java.util.regex.Pattern REGEX_VALID_CSS_STRING2 =
java.util.regex.Pattern.compile(
- "^(\\s*[-\\w]+\\s*:\\s*[^:;]*(;|$))*$"
- );
-
-
- protected java.util.Set<String> styleProperties = new
java.util.HashSet(java.util.Arrays.asList(
- "azimuth",
- "background", "background-attachment",
"background-color", "background-image",
- "background-position", "background-repeat",
- "border", "border-bottom",
"border-bottom-color", "border-bottom-style",
- "border-bottom-width", "border-collapse",
"border-color", "border-left",
- "border-left-color", "border-left-style",
"border-left-width", "border-right",
- "border-right-color", "border-right-style",
"border-right-width", "border-spacing",
- "border-style", "border-top",
"border-top-color", "border-top-style",
- "border-top-width", "border-width",
- "clear", "color",
- "cursor", "direction", "display",
"elevation", "float", "font",
- "font-family", "font-size", "font-style",
"font-variant", "font-weight",
- "height", "letter-spacing", "line-height",
- "margin", "margin-bottom", "margin-left",
"margin-right", "margin-top",
- "max-height", "max-width", "min-height",
"min-width",
- "overflow",
- "padding", "padding-bottom", "padding-left",
"padding-right", "padding-top",
- "pause", "pause-after", "pause-before",
"pitch",
- "pitch-range", "richness", "speak",
"speak-header", "speak-numeral",
- "speak-punctuation", "speech-rate", "stress",
"text-align",
- "text-decoration", "text-indent",
"unicode-bidi", "vertical-align",
- "voice-family", "volume", "white-space",
"width"
- ));
-
-
- protected java.util.Set<String> stylePropertiesValues = new
java.util.HashSet(java.util.Arrays.asList(
- "aqua", "auto", "baseline", "black",
"block", "blue", "bold", "both",
"bottom", "brown",
- "center", "collapse", "dashed",
"dotted", "fuchsia", "gray", "green",
- "inherit", "italic", "left",
"length", "lime", "maroon", "medium",
"middle", "navy", "none", "normal",
- "nowrap", "olive", "percentage",
"pointer", "purple", "red", "right",
"silver", "solid", "sub", "super",
- "teal", "text-bottom", "text-top",
"top", "transparent", "underline", "white",
"yellow"
- ));
-
- protected java.util.Set<String> svgStyleProperties = new
java.util.HashSet(java.util.Arrays.asList(
- "fill", "fill-opacity", "fill-rule",
"stroke", "stroke-linecap",
- "stroke-linejoin", "stroke-opacity",
"stroke-width"
- ));
-
-
- public void validateLinkTagURI(Token element, String uri) throws SemanticException
{
- if (!validateURI(uri)) {
- throw createSemanticException("Invalid URI", element);
- }
- }
-
- public void validateHtmlElement(Token element) throws SemanticException {
- String elementName = element.getText().toLowerCase();
- if (!acceptableElements.contains(elementName) &&
- !svgElements.contains(elementName) &&
- !mathmlElements.contains(elementName)) {
- throw createSemanticException(getInvalidElementMessage(elementName),
element);
- }
- }
-
- public void validateHtmlAttribute(Token element, Token attribute) throws
SemanticException {
- String elementName = element.getText().toLowerCase();
- String attributeName = attribute.getText().toLowerCase();
- if (!acceptableAttributes.contains(attributeName) &&
- !svgAttributes.contains(attributeName) &&
- !mathmlAttributes.contains(attributeName)) {
- throw createSemanticException(getInvalidAttributeMessage(elementName,
attributeName), element);
- }
-
- }
-
- public void validateHtmlAttributeValue(Token element, Token attribute, String
attributeValue) throws SemanticException{
-
- if (attributeValue == null || attributeValue.length() == 0) return;
-
- String elementName = element.getText().toLowerCase();
- String attributeName = attribute.getText().toLowerCase();
-
- // Check element with attribute that has URI value (href, src, etc.)
- if (attributesWhoseValueIsAURI.contains(attributeName) &&
!validateURI(attributeValue)) {
- throw createSemanticException(getInvalidURIMessage(attributeValue),
element);
- }
-
- // Check attribute value of style (CSS filtering)
- if (attributeName.equals("style")) {
- if (!REGEX_VALID_CSS_STRING1.matcher(attributeValue).matches() ||
- !REGEX_VALID_CSS_STRING2.matcher(attributeValue).matches()) {
- throw createSemanticException(
- getInvalidAttributeValueMessage(elementName, attributeName,
attributeValue),
- element
- );
- }
-
- String[] cssProperties = attributeValue.split(";");
- for (String cssProperty : cssProperties) {
- if (!cssProperty.contains(":")) {
- throw createSemanticException(
- getInvalidAttributeValueMessage(elementName, attributeName,
attributeValue),
- element
- );
- }
- String[] property = cssProperty.split(":");
- String propertyName = property[0].trim();
- String propertyValue = property.length == 2 ? property[1].trim() :
null;
-
- // CSS property name
- if (!styleProperties.contains(propertyName) &&
- !svgStyleProperties.contains(propertyName)) {
- throw createSemanticException(
- getInvalidAttributeValueMessage(elementName, attributeName,
attributeValue),
- element
- );
- }
-
- // CSS property value
- if (propertyValue != null &&
!stylePropertiesValues.contains(propertyValue)) {
- // Not in list, now check the regex
- if (!REGEX_VALID_CSS_VALUE.matcher(propertyValue).matches()) {
- throw createSemanticException(
- getInvalidAttributeValueMessage(elementName,
attributeName, attributeValue),
- element
- );
- }
- }
- }
- }
-
-
- }
-
- public String getInvalidURIMessage(String uri){
- return "invalid URI";
- }
-
- public String getInvalidElementMessage(String elementName){
- return "invalid element '" + elementName + "'";
- }
-
- public String getInvalidAttributeMessage(String elementName, String
attributeName){
- return "invalid attribute '" + attributeName + "' for
element '" + elementName + "'";
- }
-
- public String getInvalidAttributeValueMessage(String elementName, String
attributeName, String value){
- return "invalid value of attribute '" + attributeName +
"' for element '" + elementName + "'";
- }
-
- public void escapeSeamTextToken(Token element) {
- }
-
- protected boolean validateURI(String uri) {
-
- // Relative URI starts with a slash
- if (uri.startsWith("/")) return true;
-
- java.net.URI parsedURI;
- try {
- parsedURI = new java.net.URI(uri);
- } catch (java.net.URISyntaxException ex) {
- return false;
- }
-
- if (!uriSchemes.contains(parsedURI.getScheme())) {
- return false;
- }
- return true;
+ public Throwable getCause() {
+ return wrappedException;
}
-
- public SemanticException createSemanticException(String message, Token element) {
- return new SemanticException(
- message,
- element.getFilename(), element.getLine(), element.getColumn()
- );
- }
-
- }
+ }
+
+
-
private final String SEAMTEXT_MONOSPACE = "|";
private final String SEAMTEXT_TWIDDLE = "~";