You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2009/05/07 14:33:43 UTC
svn commit: r772635 - in
/labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma:
beans/RichTextString.java beans/richtext/RichTextVisitor.java
conversion/richtext/RichTextConverterToString.java
validation/validators/RichText.java
Author: simoneg
Date: Thu May 7 12:33:42 2009
New Revision: 772635
URL: http://svn.apache.org/viewvc?rev=772635&view=rev
Log:
LABS-285 : javadocs for beans-richtext
Modified:
labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/RichTextString.java
labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/richtext/RichTextVisitor.java
labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/conversion/richtext/RichTextConverterToString.java
labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/validation/validators/RichText.java
Modified: labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/RichTextString.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/RichTextString.java?rev=772635&r1=772634&r2=772635&view=diff
==============================================================================
--- labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/RichTextString.java (original)
+++ labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/RichTextString.java Thu May 7 12:33:42 2009
@@ -3,15 +3,36 @@
import org.apache.magma.validation.CompoundValidator;
import org.apache.magma.validation.validators.RichTextValidator;
+/**
+ * Meta type for rich text, to be used with {@link Consider}.
+ *
+ *
+ * @author Simone Gianni <si...@apache.org>
+ */
public class RichTextString implements CharSequence {
+ /**
+ * Current value of the richtext, HTML format
+ */
private String value = null;
+
+ /**
+ * Settings of the validator, coudl be null if this type is used outside of the context of a property.
+ */
private RichTextValidator validator = null;
+ /**
+ * Inits this instance with the given value.
+ * @param value the HTML value.
+ */
public RichTextString(String value) {
this.setValue(value);
}
+ /**
+ * Fetches the validator from the given property.
+ * @param property The property this instance is used on.
+ */
public void bindProperty(PropertyInfo property) {
CompoundValidator<Object> val = property.getValidator();
if (val != null) {
@@ -19,23 +40,38 @@
}
}
+ /**
+ * @param value The HTML text of this instance.
+ */
public void setValue(String value) {
// TODO perform HTML cleanup here
this.value = value;
}
+ /**
+ * @return The HTML text of this instance.
+ */
public String getValue() {
return this.value;
}
+ /**
+ * Implementation of {@link CharSequence}
+ */
public char charAt(int index) {
return this.value.charAt(index);
}
+ /**
+ * Implementation of {@link CharSequence}
+ */
public int length() {
return this.value.length();
}
+ /**
+ * Implementation of {@link CharSequence}
+ */
public CharSequence subSequence(int start, int end) {
return this.value.subSequence(start, end);
}
Modified: labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/richtext/RichTextVisitor.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/richtext/RichTextVisitor.java?rev=772635&r1=772634&r2=772635&view=diff
==============================================================================
--- labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/richtext/RichTextVisitor.java (original)
+++ labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/beans/richtext/RichTextVisitor.java Thu May 7 12:33:42 2009
@@ -22,17 +22,42 @@
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
+/**
+ * Implements a semi-visitor pattern to parse the HTML element by element and perform validation, pretty formatting and cleanup.
+ *
+ * @author Simone Gianni <si...@apache.org>
+ */
public class RichTextVisitor {
+ /**
+ * Inner class used to apply regular expression replacements for gross cleanup.
+ *
+ * @author Simone Gianni <si...@apache.org>
+ */
static private class ReplacePattern {
+ /**
+ * Precompiled pattern to search for.
+ */
Pattern p = null;
+ /**
+ * Replace value.
+ */
String replace = null;
+ /**
+ * @param pattern The pattern as a string, see {@link Pattern#compile(String)}.
+ * @param replace The replace as a string, see {@link Matcher#replaceAll(String)}.
+ */
public ReplacePattern(String pattern, String replace) {
p = Pattern.compile(pattern);
this.replace = replace;
}
+ /**
+ * Performs the replace of the given input.
+ * @param input The input on which to apply replacements.
+ * @return The replaced string value.
+ */
public CharSequence perform(CharSequence input) {
Matcher m = p.matcher(input);
String ret = m.replaceAll(replace);
@@ -41,6 +66,9 @@
}
+ /**
+ * Gross HTML cleanup by regexp patterns, used when {@link RichTextValidator#isPurgeWord()} is true.
+ */
private static ReplacePattern[] wordClean = {
new ReplacePattern("<o:p>\\s*</o:p>", ""),
new ReplacePattern("<o:p>[\\s\\S]*?</o:p>", " "),
@@ -144,24 +172,55 @@
};
+ /**
+ * The validator we are using for validation/cleanup.
+ */
private RichTextValidator validator = null;
+
+ /**
+ * Error messages found during validation.
+ */
private List<LocalizableString> messages = new ArrayList<LocalizableString>();
+
+ /**
+ * Output of the cleaned up HTML, used only if not null.
+ */
private Writer output = null;
+
+ /**
+ * Computed text length of the HTML, that is length of text element not including formatting informations.
+ */
private long textLength = 0;
+ /**
+ * Inits this instance for validation only.
+ * @param validator The validator containing settings to validate.
+ */
public RichTextVisitor(RichTextValidator validator) {
this.validator = validator;
}
+ /**
+ * Inits this instance for validation and cleanup.
+ * @param validator The validator containing settings to validate.
+ * @param out The writer on which to write cleaned up HTML.
+ */
public RichTextVisitor(RichTextValidator validator, Writer out) {
this.validator = validator;
this.output = out;
}
+ /**
+ * @return Validation messages of errors found during validation.
+ */
public List<LocalizableString> getMessages() {
return this.messages;
}
+ /**
+ * Visits a given input to validate it and eventually clean it up.
+ * @param content The HTML in input.
+ */
public void visit(CharSequence content) {
try {
if (validator.isPurgeWord()) {
@@ -189,6 +248,12 @@
}
}
+ /**
+ * Visits a single HTML node.
+ * @param node The node to check.
+ * @throws IOException
+ * @throws ParserException
+ */
public void visit(Node node) throws IOException, ParserException {
if (node instanceof Text) {
visitStringNode((Text)node);
@@ -251,15 +316,30 @@
}
}
+ /**
+ * Visits a {@link Remark} node.
+ * @param remark The node to visit.
+ * @throws IOException
+ */
public void visitRemarkNode(Remark remark) throws IOException {
if (this.output != null) output.append(remark.toHtml());
}
+ /**
+ * Visits a string node, see {@link Text}.
+ * @param string The node to visit.
+ * @throws IOException
+ */
public void visitStringNode(Text string) throws IOException {
if (this.output != null) output.append(string.toHtml());
this.textLength += string.toPlainTextString().length();
}
+ /**
+ * Checks wether the given tag should be skipped, and if not performs some basic cleanup.
+ * @param tag The HTML tag to check
+ * @return true is the tag has to be skipped cause not permitted.
+ */
public boolean skipTag(Tag tag) {
String tagname = tag.getTagName();
boolean skiptag = false;
Modified: labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/conversion/richtext/RichTextConverterToString.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/conversion/richtext/RichTextConverterToString.java?rev=772635&r1=772634&r2=772635&view=diff
==============================================================================
--- labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/conversion/richtext/RichTextConverterToString.java (original)
+++ labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/conversion/richtext/RichTextConverterToString.java Thu May 7 12:33:42 2009
@@ -12,18 +12,33 @@
import org.apache.magma.validation.CompoundValidator;
import org.apache.magma.validation.validators.RichTextValidator;
-
+/**
+ * Generic converter for {@link RichTextString} to and from string.
+ *
+ * This converter also performs cleanup of HTML value according to the property {@link RichTextValidator} if present,
+ * otherwise performs common basic cleanup.
+ *
+ * Null values map to null values.
+ *
+ * @author Simone Gianni <si...@apache.org>
+ */
public class RichTextConverterToString extends BaseConverter<RichTextString> implements PropertyAwareConverter {
public RichTextConverterToString() {
super(RichTextString.class);
}
+ /**
+ * Always returns a new instance, cause being a {@link PropertyAwareConverter} means this class is not thread safe.
+ */
@Override
public Converter<RichTextString> build(Class<? extends RichTextString> clazz) {
return new RichTextConverterToString();
}
-
+
+ /**
+ * Converts to a {@link RichTextString} performing cleanup.
+ */
public RichTextString from(String value) {
if (value == null) return null;
RichTextValidator validator = null;
@@ -39,7 +54,9 @@
RichTextVisitor visitor = new RichTextVisitor(validator, sw);
visitor.visit(value);
sw.flush();
- return new RichTextString(sw.toString());
+ RichTextString ret = new RichTextString(sw.toString());
+ ret.bindProperty(prop);
+ return ret;
}
public String to(RichTextString value) {
Modified: labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/validation/validators/RichText.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/validation/validators/RichText.java?rev=772635&r1=772634&r2=772635&view=diff
==============================================================================
--- labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/validation/validators/RichText.java (original)
+++ labs/magma/trunk/beans-richtext/src/main/java/org/apache/magma/validation/validators/RichText.java Thu May 7 12:33:42 2009
@@ -5,32 +5,110 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+import org.apache.magma.beans.Consider;
+import org.apache.magma.beans.RichTextString;
import org.apache.magma.validation.ValidationAnnotation;
+/**
+ * Validation for fields marked as {@link RichTextString} using {@link Consider}.
+ *
+ * @author Simone Gianni <si...@apache.org>
+ */
@ValidationAnnotation
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RichText {
+ /**
+ * @return The mime-type of the HTML to use, unused.
+ */
+ // TODO care for cleaning up HTML based on the value of this property.
public String mimeType() default "text/html";
- public boolean leninent() default false;
+ /**
+ * Whether errors found should not be blocking, i.e. settings must be used only for cleanup and
+ * visual appearance of the editor.
+ */
+ public boolean leninent() default false;
+
+ /**
+ * Whether to permit the STRONG styling.
+ */
public boolean permitStrong() default true;
+
+ /**
+ * Whether to permit the EM styling.
+ */
public boolean permitEmphasis() default true;
+
+ /**
+ * Whether to permit superscripts and subscripts.
+ */
public boolean permitScripts() default true;
+
+ /**
+ * Whether to permit the presence of links.
+ */
public boolean permitLinks() default true;
+
+ /**
+ * Whether to permit the presence of images.
+ */
public boolean permitImages() default true;
+
+ /**
+ * Whether to permit the presence on lists, both ordered (OL), unordered (UL) and term list (DL).
+ */
public boolean permitLists() default true;
+
+ /**
+ * Whether to permit the font colors.
+ */
public boolean permitColors() default true;
+
+ /**
+ * Whether to permit the background colors.
+ */
public boolean permitBackgroundColors() default true;
+
+ /**
+ * Whether to permit headings (H1,H2...)
+ */
public boolean permitHeadings() default true;
+
+ /**
+ * Whether to permit tables.
+ */
public boolean permitTables() default true;
+
+ /**
+ * Whether to permit indented paragraphs.
+ */
public boolean permitIndents() default true;
+
+ /**
+ * Whether to permit justified text.
+ */
public boolean permitJustify() default true;
+
+ /**
+ * Whether to permit external objects (like flash movies, YouTube videos).
+ */
public boolean permitObjects() default true;
- public boolean purgeWordHtml() default true;
+ /**
+ * Whether to try or not to purge Microsoft Word HTML reserved tags and CSS stylings.
+ */
+ public boolean purgeWordHtml() default true;
+
+ /**
+ * Raw maximum length permitted, this is the raw length of the HTML string, including tags and all the rest.
+ */
public long rawLength() default -1;
+
+ /**
+ * Test maximum length permitted, this is the length of only the text portion of HTML, excluding tags.
+ */
public long textLength() default -1;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org