package clipHtml;

import java.io.UnsupportedEncodingException;

/**
 * To replace the jdk HtmlSupport you have to add some code.
 *
 *
 * @author Peter Büttner <info@PeterBuettner.de>
 * @version 27.04.2003 17:57:05
 *
 */
public class PEBHtmlSupport {
	static final String sep = "\r\n";
	static final String header = "Version:1.0"+ sep +
								 "StartHTML:00000000000"+ sep +
								 "EndHTML:00000000000"+ sep +
								 "StartFragment:00000000000"+ sep +
								 "EndFragment:00000000000" + sep ;
/**
 * surrounds the html with this envelope, ready for
 * windows clipboard (jdk support can be made better)
 * <pre>
 * Version:1.0
 * StartHTML:00000000000
 * EndHTML:00000000000
 * StartFragment:00000000000
 * EndFragment:00000000000
 * &lt;!--StartFragment--&gt;
 * ...
 * &lt;!-- EndFragment-- &gt;
 * </pre>
 * We have to return a byte array 'cause in Windows the html needs to be utf-8
 * encoded. And because we have to calculate char-offsets, we encode it here.
 * @param html
 * @return byte[]
 */
static byte[] convertToHTMLFormat(String html) throws UnsupportedEncodingException{
	html = "<!--StartFragment-->" + html + "<!--EndFragment-->\r\n\0";

	byte[] bHtml = html.getBytes("UTF-8");// encode first 'cause it may grow

	int headerLen = header.length();
	int htmlLen = bHtml.length;

	StringBuffer buf = new  StringBuffer(header);
	setValue( buf, "StartHTML", headerLen-1);
	setValue( buf, "EndHTML", headerLen + htmlLen-1);
	setValue( buf, "StartFragment", headerLen-1);
	setValue( buf, "EndFragment", headerLen + htmlLen-1);
	byte[] bHeader = buf.toString().getBytes("UTF-8");// should stay the same (no nonASCII chars in header)

	byte result[] = new byte[headerLen + htmlLen ];
	System.arraycopy(bHeader, 0, result, 0, bHeader.length);
	System.arraycopy(bHtml, 0, result, bHeader.length, bHtml.length);

	return result;
}

/**
 * Replaces name+":00000000000" with name+":xxxxxxxxxxx" where xxx... is the '0' padded value.
 * Value can't be to long, since maxint can be displayed with 11 digits. If value is below zero
 * there is enough place (10 for the digits 1 for sign).<br>
 * If the search is not found nothing is done.
 * @param src
 * @param name
 * @param value
 */
private static void setValue( StringBuffer src, String name, int value){
	String search = name+":00000000000";
	int pos = src.indexOf(search);
	if (pos ==-1) return;// not found, do nothing

	boolean belowZero = value<0;
	if (belowZero) value = -value;

	src.replace(pos+search.length()-(value+"").length(), pos+search.length(), value+"");
	if (belowZero) src.setCharAt(pos+name.length()+1,'-'); // +1 'cause of ':' in "SearchMe:"
}

}
