- From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
- Date: Thu, 27 Aug 2009 12:13:58 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/util In directory hutz:/tmp/cvs-serv10804/src/org/w3c/unicorn/util Added Files: Tag: dev2 EscapeXMLEntities.java XHTMLize.java Log Message: moved classes to util --- NEW FILE: EscapeXMLEntities.java --- package org.w3c.unicorn.util; import org.apache.commons.lang.StringEscapeUtils; import org.apache.velocity.app.event.ReferenceInsertionEventHandler; public class EscapeXMLEntities implements ReferenceInsertionEventHandler { /** * Escape the XML entities for all inserted references. */ public Object referenceInsert(final String sUnused, final Object oValue) { final String sValue = oValue.toString(); return StringEscapeUtils.escapeXml(sValue); } /** * Escape the provided text. * * @param sValue * @return */ /*public static String escapeText(final String sValue) { final StringBuffer aStringBuffer = new StringBuffer(sValue.length()); final int iLength = sValue.length(); int iPosition = 0; int iNextPosition = EscapeXMLEntities.nextPosition(sValue, iPosition); while (iNextPosition != -1) { aStringBuffer.append(sValue.substring(iPosition, iNextPosition)); aStringBuffer.append(EscapeXMLEntities.escapeChar(sValue .charAt(iNextPosition))); iPosition = iNextPosition + 1; if (iPosition < iLength) { iNextPosition = EscapeXMLEntities.nextPosition(sValue, iPosition); } else { iNextPosition = -1; } } if (iPosition < iLength) { aStringBuffer.append(sValue.substring(iPosition)); } return aStringBuffer.toString(); }*/ /** * Transform the escape char into xml encoding * * @param c * char to transform * @return same character in xml form */ /*private static String escapeChar(final char c) { switch (c) { case '<': return "<"; case '>': return ">"; case '&': return "&"; case '"': return """; } return null; }*/ /** * Returns the position of the next xml tag * * @param s * string to analyze * @param iCurrentPosition * current position in the string * @return position of the next tag */ /*private static int nextPosition(final String s, final int iCurrentPosition) { final int iLT = s.indexOf('<', iCurrentPosition); final int iGT = s.indexOf('>', iCurrentPosition); final int iAMP = s.indexOf('&', iCurrentPosition); final int iQUOT = s.indexOf('"', iCurrentPosition); if ((iLT != -1) && ((iGT == -1) || (iLT < iGT)) && ((iAMP == -1) || (iLT < iAMP)) && ((iQUOT == -1) || (iLT < iQUOT))) { return iLT; } if ((iGT != -1) && ((iAMP == -1) || (iGT < iAMP)) && ((iQUOT == -1) || (iGT < iQUOT))) { return iGT; } if ((iAMP != -1) && ((iQUOT == -1) || (iAMP < iQUOT))) { return iAMP; } return iQUOT; }*/ } --- NEW FILE: XHTMLize.java --- package org.w3c.unicorn.util; import org.apache.commons.lang.StringEscapeUtils; import org.apache.velocity.app.event.ReferenceInsertionEventHandler; import org.w3c.unicorn.response.A; import org.w3c.unicorn.response.Code; import org.w3c.unicorn.response.Img; /** * Escape all XML Entities in the reference insertion. Specifically, the * following conversions are performed: * <DL> * <DT>&</DT> * <DD>&amp;</DD> * <DT><</DT> * <DD>&lt;</DD> * <DT>></DT> * <DD>&gt;</DD> * <DT>"</DT> * <DD>&quot;</DD> * </DL> * * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a> */ public class XHTMLize implements ReferenceInsertionEventHandler { /** * Escape the XML entities for all inserted references. */ public Object referenceInsert(final String sUnused, final Object oValue) { if (oValue == null) { return null; } if (oValue instanceof A) { A link = (A) oValue; return insertA(link); } if (oValue instanceof Img) { Img image = (Img) oValue; return insertImg(image); } if (oValue instanceof Code) { Code code = (Code) oValue; return insertCode(code); } return StringEscapeUtils.escapeHtml(oValue.toString()); } /** * Insert a link * * @param aLink * link to insert * @return return the object containing the link */ private Object insertA(final A aLink) { String sResultat = "<a href=\"" + StringEscapeUtils.escapeHtml(aLink.getHref()) + "\">"; for (final Object oElement : aLink.getContent()) { if (oElement instanceof Img) { sResultat += insertImg((Img) oElement); } else { sResultat += StringEscapeUtils.escapeHtml(oElement.toString()); } } sResultat += "</a>"; return sResultat; } /** * Insert code tag into the tags * * @param aCode * code to insert * @return object with code inserted */ private Object insertCode(final Code aCode) { String sResultat = "<code>"; for (final Object oElement : aCode.getContent()) { if (oElement instanceof A) { sResultat += insertA((A) oElement); } else if (oElement instanceof Img) { sResultat += insertImg((Img) oElement); } else { sResultat += StringEscapeUtils.escapeHtml(oElement.toString()); } } sResultat += "</code>"; return sResultat; } /** * Insert an img tag * * @param img * image path to insert * @return the string containing the image tag */ private String insertImg(final Img aImage) { return "<img src=\"" + StringEscapeUtils.escapeHtml(aImage.getSrc()) + "\" alt=\"" + StringEscapeUtils.escapeHtml(aImage.getAlt()) + "\"/>"; } }
Received on Thursday, 27 August 2009 12:14:09 UTC