/* Copyright (c) 2001, 2002, Oracle Corporation.  All rights reserved.  */

/*
   DESCRIPTION
   JSP XML  Utility methods 

   PRIVATE CLASSES
    NONE

   NOTES


   MODIFIED    (MM/DD/YY)
      njunnark  10/09/02 - njunnark_wstags_doctype_update
  
 */

package beans;

import oracle.jsp.util.*;
import java.io.*;
import oracle.xml.parser.v2.XMLNode;

/**
 * JSPUtil :
 * This class provides some utility methods like HTML/XML encoding, 
 * String conversion, XSL transformation etc
 * 
 */
public class JSPUtil 
{

  /**
   * xmlNodeToString :
   * This method converts a XMLNode object to a String.
   * 
   * @param node XMLNode object to convert.
   * @return String representing node object.
   * 
   */
	public static String xmlNodeToString(XMLNode node) throws java.io.IOException 
	{
		java.io.ByteArrayOutputStream ostr = new java.io.ByteArrayOutputStream();
		if(node != null)
			node.print(ostr);
		String strNode = ostr.toString();
		return strNode;
	}

  
	/**
    * Translates a raw string into a string that can be used as an HTML
    * attribute value.  For instance, ampersands and single and double quotes
    * are replaced by the corresponding HTML entities.  Here is an example:
    * <pre>
    * &lt;%@ page import="oracle.JSPUtil"%&gt;
    * &nbsp;       .
    * &nbsp;       .
    * &nbsp;       .
    * &lt;input type="text" name="textbox" size="50"
    * &nbsp;   value="&lt;%= JSPUtil.encodeHTMLAttr(model.getRE()) %&gt;"&gt;
    * </pre>
    *
    * @param  attr This is the attribute value to be encoded.
    * @return A well-formed HTML attribute that an HTML parser will
    *         interpret as the input string (attr).
    **/
	public static String encodeHTMLAttr(String attr)
    {
        if(attr == null)
		{
			return null;
		}
		int attrLen = attr.length();
	
		StringBuffer result = new StringBuffer(6 * attrLen);    // long enough
        // for an attribute composed entirely of double quote characters.

        for (int i = 0; i < attrLen; i++)
        {
            char c = attr.charAt(i);
            switch (c)
            {
            case '<':
                result.append("&lt;");
                break;

            case '>':
                result.append("&gt;");
                break;

            case '&':
				if(!(attr.startsWith("&lt;",i) ||
					 attr.startsWith("&gt;",i) ||
					 attr.startsWith("&amp;",i) ||
					 attr.startsWith("&quot;",i) ||
					 attr.startsWith("&#39;",i) ||
					 attr.startsWith("&#9;",i) ||
					 attr.startsWith("&#10;",i) ||
					 attr.startsWith("&nbsp;",i) ||
					 attr.startsWith("&#13;",i) ))
				{
					result.append("&amp;");
				}
				else
				{
					result.append(c);
				}
                break;

			case '\"':
                result.append("&quot;");
                break;
         
			case '\'':
                result.append("&#39;");
                break;
			
			case '\t':
               result.append("&#9;");
               break;
			
			case '\n':
				result.append("&#10;");
                break;
			
			case '\r':
                result.append("&#13;");
                break;
            
			case ' ':
		       result.append("&nbsp;");
               break;
			

			default:
                result.append(c);
                break;
            }
        }
        return result.toString();
    }

	/**
    * Translates a HTML/XML attribute value string into a raw string .
    * 
    *
    * @param  attr This is the attribute value to be decoded.
    * @return A raw String with all literal quotes,ampersands and single 
    * and double quotes etc .
    **/
    public static String decodeHTMLAttr(String attr)
    {
        
        if(attr == null)
        {
          return null;
        }
        StringBuffer attrBuffer = new StringBuffer(attr);    
        int attrLen = attrBuffer.length();
        
        int idx1 = 0;

        while((idx1 = attr.indexOf("&quot;")) != -1)
        {
          attrBuffer.replace(idx1,idx1+"&quot;".length(),"\"");
          attr = attrBuffer.toString();
        }	
		
        while((idx1 = attr.indexOf("&lt;")) != -1)
        {
          attrBuffer.replace(idx1,idx1+"&lt;".length(),"<");
          attr = attrBuffer.toString();
        }	

        while((idx1 = attr.indexOf("&gt;")) != -1)
        {
          attrBuffer.replace(idx1,idx1+"&gt;".length(),">");
          attr = attrBuffer.toString();
        }	

        while((idx1 = attr.indexOf("&amp;")) != -1)
        {
          attrBuffer.replace(idx1,idx1+"&amp;".length(),"&");
          attr = attrBuffer.toString();
        }	

        while((idx1 = attr.indexOf("&#39;")) != -1)
        {
          attrBuffer.replace(idx1,idx1+"&#39;".length(),"\'");
          attr = attrBuffer.toString();
        }	
       	
        while((idx1 = attr.indexOf("&#9;")) != -1)
        {
          attrBuffer.replace(idx1,idx1+"&#9;".length(),"\t");
          attr = attrBuffer.toString();
        }	
       	
        while((idx1 = attr.indexOf("&#10;")) != -1)
        {
          attrBuffer.replace(idx1,idx1+"&#10;".length(),"\n");
          attr = attrBuffer.toString();
        }	
       	
        while((idx1 = attr.indexOf("&#13;")) != -1)
        {
          attrBuffer.replace(idx1,idx1+"&#13;".length(),"\r");
          attr = attrBuffer.toString();
        }	
       	
        return attr;
    }

  
	public static void main(String[] args) 
	{
		System.out.println("This is beans.JSPUtil Class!");
	}
}
