Converter to clean up font tags in richtext maintained in Notes Client.


package org.fbbe.xsp.utils;

import javax.faces.application.FacesMessage;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.ibm.xsp.model.domino.wrapped.DominoRichTextItem;



public class RichtextConverter implements Converter {
	private static final String DEFAULTSIZE = "2";
	private static final String DEFAULTFONT = "sans-serif";
	private static final String DEFAULTSTYLE = "";
	private static final String DEFAULTCOLOR = "";
	private static final String DEFAULTCLASS = "";

	public RichtextConverter() {
		// TODO Initialization Code
	}

	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		return (Object) value;
	}

	public String getAsString(FacesContext context, UIComponent component,	Object value) {
		try {
			DominoRichTextItem rti = (DominoRichTextItem) value;
			String html = rti.getHTML();
			Document doc = Jsoup.parseBodyFragment(html);
			Element body = doc.body();
			Elements tags = body.getElementsByTag("font");

			for (Element tag : tags) {
				String tagSize = tag.attr("size");
				String tagFace = tag.attr("face");
				String tagColor = tag.attr("color");
				String tagStyle = tag.attr("style");
				String tagClass = tag.attr("class");

				if (DEFAULTSIZE.equals(tagSize) 
						&& DEFAULTFONT.equals(tagFace)
						&& DEFAULTCOLOR.equals(tagColor)
						&& DEFAULTSTYLE.equals(tagStyle)
						&& DEFAULTCLASS.equals(tagClass)) {
					tag.unwrap();
				} else {
					tag.removeAttr("size");
					tag.removeAttr("face");
					tag.removeAttr("color");
					tag.removeAttr("style");
					tag.removeAttr("class");
					tag.tagName("span");
					String s = "";
					String c = "";
					if (!DEFAULTSTYLE.equals(tagStyle) && !"".equals(tagStyle)) {
						s = tagStyle;
					}
					if (!DEFAULTFONT.equals(tagFace) && !"".equals(tagFace)){
						s += " font-family:" + tagFace + ";";
					}
					if (!DEFAULTCOLOR.equals(tagColor) && !"".equals(tagColor)) {
						s += " color:" + tagColor + ";";
					}
					if (!DEFAULTCLASS.equals(tagClass) && !"".equals(tagClass)) {
						c = tagClass;
					}
					if (!DEFAULTSIZE.equals(tagSize) && !"".equals(tagSize)) {
						c += " fontSize" + tagSize;
					}
					if (!"".equals(s)) {
						tag.attr("style", s.trim());
					}
					if (!"".equals(c)) {
						tag.attr("class", c.trim());
					}
				}
			}
			html = body.html();
			return html;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConverterException(new FacesMessage(e.getMessage()));
		}
	}
}


Add to faces-config.xml
	 <converter>
		<converter-id>RichtextConverter</converter-id>
		<converter-class>org.fbbe.xsp.utils.RichtextConverter</converter-class>
	</converter>

Add to css  replaces font tag size attibute
.fontSize1{font-size:0.75em;}
.fontSize2{font-size:1em;}
.fontSize3{font-size:1.25em;}
.fontSize4{font-size:1.5em;}
.fontSize5{font-size:1.75em;}
.fontSize6{font-size:2em;}
.fontSize7{font-size:2.25em;}

Example usage:
<xp:inputRichText id="content" value="#{document1.body}" readonly="true">
	<xp:this.converter>
		<xp:converter converterId="RichtextConverter">
		</xp:converter>
	</xp:this.converter>
</xp:inputRichText>


All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...