package org.compiere.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.apache.ecs.MultiPartElement;
import org.apache.ecs.Printable;


/**
 *  Load html-src (text) stored in JAR, e.g. to load a style-sheet
 */
public class StoredHtmlSrc extends MultiPartElement implements Printable {
	
	private static final long serialVersionUID = 50303119083373138L;
	
	/**	Logger					*/
	protected static CLogger 	log = CLogger.getCLogger(StoredHtmlSrc.class.getName());
	
	
	/**
	 *  Load html-src (text) stored in JAR, e.g. to load a style-sheet
	 *  @param elementType e.g. elementType=STYLE
	 *  @param srcLocation package/filename in SRC e.g. org/compiere/util/standard.css
	 *  todo if needed: also write for SinglePartElement and StringElement
	 */
	public StoredHtmlSrc(String elementType, String srcLocation) {
		this.setElementType(elementType);
		
		URL url = getClass().getClassLoader().getResource(srcLocation);
		if (url==null) {
			log.warning("failed to load html-src: " + srcLocation);
			return;
		}			
		InputStreamReader ins;
		BufferedReader bufferedReader = null;
		try {
			ins = new InputStreamReader(url.openStream());
			bufferedReader = new BufferedReader( ins );
			String cssLine;
			String result="";
			while ((cssLine = bufferedReader.readLine()) != null) 
				result+=cssLine;
			this.setTagText(result);
		} catch (IOException e1) {
			log.warning("failed to load html-src: " + srcLocation);
		}
		finally{
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {}
				bufferedReader = null;
			}
		}
	}
}
