htmlencode


   public static String htmlEncode(String inHtmlString)
   {
	   String fromCharString="<>&";
	   String[] toStringArray = { "&lt;", "&gt;", "&amp;" };
	   return encode(inHtmlString, fromCharString, toStringArray);
   }

encode


   public static String encode(final String inS
		   	, String fromCharString
		   	, String[] toCharStringArray) 
   {
      // return empty strings as they are
      if (inS == null)
      {
         return inS;
      }
      if (inS.length() == 0)
      {
         return inS;
      }
      StringBuffer thisBuffer = new StringBuffer();
      for(int i=0;i<inS.length();i++)
      {
         char thisChar = inS.charAt(i);
         //it is not an escape character
         String translatedString = 
        	 StringUtils.translateCharacter(thisChar
        			 , fromCharString
        			 , toCharStringArray);
         if (translatedString == null)
         {
        	 //this character is not one of the 
        	 //chars that needs to be translated
        	 thisBuffer.append(thisChar);
        	 continue;
         }
         //needs translation
         thisBuffer.append(translatedString);
      }// end of for
      return thisBuffer.toString();
   }// end of func

tranlsateCharacter


      public static String translateCharacter(char inChar
		   , String fromString
		   , String[] toStringArray)
   {
   	  int i = fromString.indexOf(inChar);
   	  if (i == -1)
   	  {
   	  	return null;
   	  }
   	  //Available in the in string
   	  return toStringArray[i];
   }

test code


      String html1 = new String("<p>Helloworld a &lt; b</p>");
      String eHtml1 = StringUtils.htmlEncode(html1);
      System.out.println(html1 + "===>>" + eHtml1);

Making a part out of it


public class HtmlEncoderPart extends AFactoryPart
{
    protected Object executeRequestForPart(String requestName, Map inArgs)
            throws RequestExecutionException
    {
       try
       {
          String encodingKey= AppObjects.getValue(requestName + ".encodingKey");
          String inEncodeString = (String)inArgs.get(encodingKey);
          if (inEncodeString == null)
          {
        	  throw new RequestExecutionException("Encoding key not found in the args");
          }
          //encoding key value found
          return StringUtils.htmlEncode(inEncodeString);
       }
       catch(ConfigException x)
       {
          throw new RequestExecutionException("Error:config errror",x);
       }
    }//eof-function
}//eof-class

Using it in a pipeline


request.encode_f1.classname=com.ai.parts.HtmlEncoderPart
request.encode_f1.encodingKey=field1
request.encode_f1.resultName=encoded_field1

Once this part is inserted into the execution pipeline there will be two fields available for painting "field1" and "encoded_field1".

Reference

Escape Special Characters from Java Best Practices